alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using DynamicExpresso.Exceptions;
using NUnit.Framework;
 
namespace DynamicExpresso.UnitTest
{
    [TestFixture]
    public class VisitorsTest
    {
        [Test]
        public void By_default_reflection_is_not_permitted()
        {
            var target = new Interpreter();
 
            Assert.Throws<ReflectionNotAllowedException>(() => target.Parse("typeof(double).GetMethods()"));
            Assert.Throws<ReflectionNotAllowedException>(() => target.Parse("typeof(double).Assembly"));
 
            Assert.Throws<ReflectionNotAllowedException>(() => target.Parse("x.GetType().GetMethods()", new Parameter("x", typeof(X))));
            Assert.Throws<ReflectionNotAllowedException>(() => target.Parse("x.GetType().Assembly", new Parameter("x", typeof(X))));
        }
 
        [Test]
        public void By_default_reflection_to_get_name_is_permitted()
        {
            var target = new Interpreter();
 
            Assert.That(target.Eval("typeof(double).Name"), Is.EqualTo("Double"));
            Assert.That(target.Eval("x.GetType().Name", new Parameter("x", typeof(X), new X())), Is.EqualTo("X"));
        }
 
        [Test]
        public void Reflection_can_be_enabled()
        {
            var target = new Interpreter()
                .EnableReflection();
 
            Assert.That(target.Eval("typeof(double).GetMethods()"), Is.EqualTo(typeof(double).GetMethods()));
            Assert.That(target.Eval("typeof(double).Assembly"), Is.EqualTo(typeof(double).Assembly));
 
            var x = new X();
            Assert.That(target.Eval("x.GetType().GetMethods()", new Parameter("x", x)), Is.EqualTo(x.GetType().GetMethods()));
            Assert.That(target.Eval("x.GetType().Assembly", new Parameter("x", x)), Is.EqualTo(x.GetType().Assembly));
        }
 
        public class X { }
    }
}