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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using DynamicExpresso.Exceptions;
 
namespace DynamicExpresso.UnitTest
{
    [TestFixture]
    public class TypesTest
    {
        [Test]
        public void Default_Types()
        {
            var target = new Interpreter();
 
            var predefinedTypes = new Dictionary<string, Type>{
                    {"Object", typeof(object)},
                    {"object", typeof(object)},
                    {"Boolean", typeof(bool)},
                    {"bool", typeof(bool)},
                    {"Char", typeof(char)},
                    {"char", typeof(char)},
                    {"String", typeof(string)},
                    {"string", typeof(string)},
                    {"SByte", typeof(sbyte)},
                    {"Byte", typeof(byte)},
                    {"byte", typeof(byte)},
                    {"Int16", typeof(short)},
                    {"UInt16", typeof(ushort)},
                    {"Int32", typeof(int)},
                    {"int", typeof(int)},
                    {"UInt32", typeof(uint)},
                    {"Int64", typeof(long)},
                    {"long", typeof(long)},
                    {"UInt64", typeof(ulong)},
                    {"Single", typeof(float)},
                    {"Double", typeof(double)},
                    {"double", typeof(double)},
                    {"Decimal", typeof(decimal)},
                    {"decimal", typeof(decimal)},
                    {"DateTime", typeof(DateTime)},
                    {"TimeSpan", typeof(TimeSpan)},
                    {"Guid", typeof(Guid)},
                    {"Math", typeof(Math)},
                    {"Convert", typeof(Convert)},
                };
 
            foreach (var t in predefinedTypes)
                Assert.That(target.Eval(string.Format("typeof({0})", t.Key)), Is.EqualTo(t.Value));
        }
 
        [Test]
        public void Load_interpreter_without_any_configuration_doesn_t_recognize_types()
        {
            var target = new Interpreter(InterpreterOptions.None);
 
            Assert.Throws<UnknownIdentifierException>(() => target.Eval("typeof(string)"));
        }
 
        [Test]
        public void Custom_Types()
        {
            var target = new Interpreter()
                                            .Reference(typeof(Uri));
 
            Assert.That(target.Eval("typeof(Uri)"), Is.EqualTo(typeof(Uri)));
            Assert.That(target.Eval("new Uri(\"http://test\")"), Is.EqualTo(new Uri("http://test")));
        }
 
        [Test]
        public void Reference_the_same_type_multiple_times_doesn_t_have_effect()
        {
            var target = new Interpreter()
                                            .Reference(typeof(string))
                                            .Reference(typeof(string))
                                            .Reference(typeof(Uri))
                                            .Reference(typeof(Uri))
                                            .Reference(typeof(Uri));
 
            Assert.That(target.Eval("typeof(Uri)"), Is.EqualTo(typeof(Uri)));
            Assert.That(target.Eval("new Uri(\"http://test\")"), Is.EqualTo(new Uri("http://test")));
        }
 
        [Test]
        public void References_can_be_case_insensitive()
        {
            var target = new Interpreter(InterpreterOptions.DefaultCaseInsensitive)
                                            .Reference(typeof(string))
                                            .Reference(typeof(Uri));
 
            Assert.That(target.Eval("typeof(Uri)"), Is.EqualTo(typeof(Uri)));
            Assert.That(target.Eval("typeof(uri)"), Is.EqualTo(typeof(Uri)));
            Assert.That(target.Eval("typeof(URI)"), Is.EqualTo(typeof(Uri)));
            Assert.That(target.Eval("STRING.Empty"), Is.EqualTo(string.Empty));
        }
 
        [Test]
        public void Custom_Type_Constructor()
        {
            var target = new Interpreter()
                                            .Reference(typeof(MyDataContract));
 
            Assert.That(target.Eval("new MyDataContract(\"davide\").Name"), Is.EqualTo(new MyDataContract("davide").Name));
            Assert.That(target.Eval("new MyDataContract(44 , 88).Name"), Is.EqualTo(new MyDataContract(44, 88).Name));
        }
 
        [Test]
        public void Custom_Type_Alias()
        {
            var target = new Interpreter()
                                            .Reference(typeof(MyDataContract), "DC");
 
            Assert.That(target.Parse("new DC(\"davide\")").ReturnType, Is.EqualTo(typeof(MyDataContract)));
        }
 
        [Test]
        public void Custom_Enum()
        {
            var target = new Interpreter()
                                            .Reference(typeof(MyCustomEnum));
 
            Assert.That(target.Eval("MyCustomEnum.Value1"), Is.EqualTo(MyCustomEnum.Value1));
            Assert.That(target.Eval("MyCustomEnum.Value2"), Is.EqualTo(MyCustomEnum.Value2));
        }
 
        [Test]
        public void Enum_are_case_sensitive_by_default()
        {
            var target = new Interpreter()
                                            .Reference(typeof(EnumCaseSensitive));
 
            Assert.That(target.Eval("EnumCaseSensitive.Test"), Is.EqualTo(EnumCaseSensitive.Test));
            Assert.That(target.Eval("EnumCaseSensitive.TEST"), Is.EqualTo(EnumCaseSensitive.TEST));
        }
 
        [Test]
        public void Getting_the_list_of_used_types()
        {
            var target = new Interpreter();
 
            var lambda = target.Parse("Math.Max(a, typeof(string).Name.Length)", new Parameter("a", 1));
 
            Assert.That(lambda.Types.Count(), Is.EqualTo(2));
            Assert.That(lambda.Types.ElementAt(0).Name, Is.EqualTo("Math"));
            Assert.That(lambda.Types.ElementAt(1).Name, Is.EqualTo("string"));
        }
 
        public enum EnumCaseSensitive
        {
            Test = 1,
            // ReSharper disable once InconsistentNaming
            TEST = 2
        }
 
        public enum MyCustomEnum
        {
            Value1,
            Value2
        }
 
        private class MyDataContract
        {
            public MyDataContract(string name)
            {
                Name = name;
            }
 
            public MyDataContract(int x, int y)
            {
                Name = string.Format("{0} - {1}", x, y);
            }
 
            public string Name { get; set; }
        }
 
        public class MyTestClass<T>
        {
            private T _val;
            public MyTestClass(T val)
            {
                _val = val;
            }
 
            public override string ToString()
            {
                if (_val == null)
                    return "null";
 
                return _val.ToString();
            }
        }
    }
}