forked from dynamicexpresso/DynamicExpresso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructorTest.cs
419 lines (365 loc) · 13.7 KB
/
ConstructorTest.cs
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections;
using DynamicExpresso.Exceptions;
using NUnit.Framework;
using System.Linq;
namespace DynamicExpresso.UnitTest
{
[TestFixture]
public class ConstructorTest
{
[Test]
public void New_Of_Base_Type()
{
var target = new Interpreter();
Assert.AreEqual(new DateTime(2015, 1, 24), target.Eval("new DateTime(2015, 1, 24)"));
Assert.AreEqual(new string('a', 10), target.Eval("new string('a', 10)"));
}
[Test]
public void New_Of_Custom_Type()
{
var target = new Interpreter();
target.Reference(typeof(Uri));
Assert.AreEqual(new Uri("http://www.google.com"), target.Eval("new Uri(\"http://www.google.com\")"));
}
[Test]
public void New_And_Member_Access()
{
var target = new Interpreter();
Assert.AreEqual(new DateTime(2015, 1, 24).Month, target.Eval("new DateTime(2015, 1, 24).Month"));
Assert.AreEqual(new DateTime(2015, 1, 24).Month + 34, target.Eval("new DateTime( 2015, 1, 24).Month + 34"));
}
[Test]
public void Constructor_invocation_without_new_is_not_supported()
{
var target = new Interpreter();
Assert.Throws<ParseException>(() => target.Parse("DateTime(2010, 5, 23)"));
}
[Test]
public void Unknown_New_Type_Is_Not_Supported()
{
var target = new Interpreter();
Assert.Throws<UnknownIdentifierException>(() => target.Parse("new unkkeyword()"));
}
[Test]
public void Empty_object_initializer()
{
var target = new Interpreter();
target.Reference(typeof(MyClass));
Assert.AreEqual(new MyClass() { }, target.Eval("new MyClass() {}"));
Assert.AreEqual(new MyClass("test") { }, target.Eval("new MyClass(\"test\") {}"));
Assert.AreEqual(new MyClass { }, target.Eval("new MyClass{}"));
}
[Test]
public void Object_initializer()
{
var target = new Interpreter();
target.Reference(typeof(MyClass));
// each member initializer can end with a comma, even if there's nothing afterwards
Assert.AreEqual(new MyClass { StrProp = "test", }, target.Eval("new MyClass { StrProp = \"test\", }"));
Assert.AreEqual(new MyClass { StrProp = "test" }, target.Eval("new MyClass { StrProp = \"test\" }"));
Assert.AreEqual(new MyClass("test") { IntField = 5, }, target.Eval("new MyClass(\"test\") { IntField = 5, }"));
Assert.AreEqual(new MyClass("test") { IntField = 5 }, target.Eval("new MyClass(\"test\") { IntField = 5 }"));
Assert.AreEqual(new MyClass() { StrProp = "test", IntField = 5, }, target.Eval("new MyClass() { StrProp = \"test\", IntField = 5, }"));
Assert.AreEqual(new MyClass() { StrProp = "test", IntField = 5 }, target.Eval("new MyClass() { StrProp = \"test\", IntField = 5 }"));
}
[Test]
public void Constructor_invocation_generics()
{
var target = new Interpreter();
target.Reference(typeof(Tuple<>));
target.Reference(typeof(Tuple<,>));
target.Reference(typeof(Tuple<,,>));
Assert.AreEqual(1, target.Eval("new Tuple<int>(1).Item1"));
Assert.AreEqual("My str item", target.Eval("new Tuple<int, string>(5, \"My str item\").Item2"));
Assert.AreEqual(3, target.Eval("new Tuple<int, int, int>(1, 2, 3).Item3"));
}
[Test]
public void Constructor_invocation_named_generics_with_arity()
{
var target = new Interpreter();
target.Reference(typeof(Tuple<>), "Toto`1");
target.Reference(typeof(Tuple<,>), "Toto`2");
target.Reference(typeof(Tuple<,,>), "Toto`3");
Assert.AreEqual(1, target.Eval("new Toto<int>(1).Item1"));
Assert.AreEqual("My str item", target.Eval("new Toto<int, string>(5, \"My str item\").Item2"));
Assert.AreEqual(3, target.Eval("new Toto<int, int, int>(1, 2, 3).Item3"));
}
[Test]
public void Constructor_invocation_named_generics()
{
var target = new Interpreter();
target.Reference(typeof(Tuple<,>), "Tuple");
Assert.AreEqual("My str item", target.Eval("new Tuple<int, string>(5, \"My str item\").Item2"));
target.Reference(typeof(Tuple<>), "Tuple1");
target.Reference(typeof(Tuple<,,>), "Tuple3");
Assert.AreEqual(1, target.Eval("new Tuple1<int>(1).Item1"));
Assert.AreEqual(3, target.Eval("new Tuple3<int, int, int>(1, 2, 3).Item3"));
}
[Test]
public void Object_initializer_syntax_error()
{
var target = new Interpreter();
target.Reference(typeof(MyClass));
Assert.Throws<ParseException>(() => target.Parse("new MyClass() { StrProp }"));
Assert.Throws<ParseException>(() => target.Parse("new MyClass() { StrProp = }"));
Assert.Throws<ArgumentException>(() => target.Parse("new MyClass() { StrProp = 5 }")); // type mismatch
Assert.Throws<ArgumentException>(() => target.Parse("new MyClass() { ReadOnlyProp = 5 }")); // read only prop
Assert.Throws<ParseException>(() => target.Parse("new MyClass() { UnkProp = 5 }")); // unknown property
Assert.Throws<ParseException>(() => target.Parse("new MyClass() { StrProp ")); // no close bracket
Assert.Throws<ParseException>(() => target.Parse("new MyClass() StrProp }")); // no open bracket
Assert.Throws<ParseException>(() => target.Parse("new MyClass() {{IntField = 5}}")); // multiple bracket
Assert.Throws<ParseException>(() => target.Parse("new MyClass() {5}")); // collection initializer not supported
}
[Test]
public void Ctor_params_array()
{
var target = new Interpreter();
target.Reference(typeof(MyClass));
Assert.AreEqual(new MyClass(6, 5, 4, 3).MyArr, target.Eval("new MyClass(6, 5, 4, 3).MyArr"));
}
[Test]
public void Array_constructor()
{
var target = new Interpreter();
var arr = target.Eval<int[]>("new int[] { 1, 2 }");
Assert.AreEqual(2, arr.Length);
Assert.AreEqual(1, arr[0]);
Assert.AreEqual(2, arr[1]);
}
[Test]
public void Array_constructor_type_mismatch()
{
// Exception: an expression of type 'System.Char' cannot be used to initialize an array of type 'System.Int32'
var target = new Interpreter();
Assert.Throws<ParseException>(() => target.Eval<int[]>("new int[] { 1, 'a' }"));
}
[Test]
public void Jagged_array_constructor()
{
var target = new Interpreter();
var arr = target.Eval<int[][]>("new int[][] { new int[] { 1, 2, }, new int[] { 3, 4, }, }");
Assert.AreEqual(2, arr.Length);
Assert.AreEqual(1, arr[0][0]);
Assert.AreEqual(2, arr[0][1]);
Assert.AreEqual(3, arr[1][0]);
Assert.AreEqual(4, arr[1][1]);
}
[Test]
public void Array_multi_dimension_constructor()
{
// creating a multidimensional array is not supported
var target = new Interpreter();
Assert.Throws<ParseException>(() => target.Parse("new int[,] { { 1 }, { 2 } }"));
}
[Test]
public void Ctor_NewDictionaryWithItems()
{
var target = new Interpreter();
target.Reference(typeof(System.Collections.Generic.Dictionary<,>));
var l = target.Eval<System.Collections.Generic.Dictionary<int, string>>("new Dictionary<int, string>(){{1, \"1\"}, {2, \"2\"}, {3, \"3\"}, {4, \"4\"}, {5, \"5\"}}");
Assert.AreEqual(5, l.Count);
for (int i = 0; i < l.Count; ++i)
{
Assert.AreEqual(i + 1 + "", l[i + 1]);
}
}
[Test]
public void Ctor_NewMyClassWithItems()
{
var target = new Interpreter();
target.Reference(typeof(MyClassAdder));
Assert.AreEqual(new MyClassAdder() { { 1, 2, 3, 4, 5 }, { "6" }, 7 }, target.Eval<MyClassAdder>("new MyClassAdder(){{ 1, 2, 3, 4, 5},{\"6\" },7 }.Add(true)"));
}
[Test]
public void Ctor_NewMyClass_ExpectedValues()
{
var target = new Interpreter();
target.Reference(typeof(MyClassAdder));
target.Reference(typeof(MyClass));
var strProp = new Parameter("StrProp", typeof(string).MakeByRefType(), "0");
var intProp = new Parameter("IntField", typeof(int).MakeByRefType(), int.MaxValue);
var args = new object[]
{
strProp.Value,
intProp.Value
};
Assert.AreEqual(
new MyClassAdder() { { 1, 2, 3, 4, 5 }, "6", 7 },
target.Parse("new MyClassAdder(){{ 1, 2, 3, 4, 5},{StrProp = \"6\" },7}", strProp, intProp).Invoke(args));
Assert.AreEqual(
new MyClassAdder() { { 1, 2, 3, 4, 5 }, string.Empty, 7 },
target.Eval<MyClassAdder>("new MyClassAdder(){{ 1, 2, 3, 4, 5},string.Empty, 7}"));
var IntField = int.MaxValue;
Assert.AreEqual(
new MyClassAdder() { { IntField = 5 }, { 1, 2, 3, 4, IntField }, "6" },
target.Parse("new MyClassAdder(){ { IntField = 5 }, { 1, 2, 3, 4, 5},{StrProp = \"6\" }, IntField}", strProp, intProp).Invoke(args));
}
[Test]
public void Ctor_NewMyClass_CanStillUseMemberSyntax()
{
var target = new Interpreter();
target.Reference(typeof(MyClassAdder));
target.Reference(typeof(MyClass));
Assert.AreEqual(
new MyClassAdder() { StrProp = string.Empty, MyArr = new[] { 1, 2, 3, 4, 5 }, IntField = int.MinValue },
target.Eval<MyClassAdder>("new MyClassAdder() {StrProp = string.Empty, MyArr = new int[] {1, 2, 3, 4, 5}, IntField = int.MinValue }"));
}
[Test]
public void Ctor_InvalidInitializerMemberDeclarator()
{
var target = new Interpreter();
target.Reference(typeof(MyClassAdder));
target.Reference(typeof(MyClass));
Assert.Throws<ParseException>(() => target.Eval<MyClassAdder>("new MyClassAdder(){{ 1, 2, 3, 4, 5},{StrProp = \"6\" },7 }"));
//Start with collection, then do member init
Assert.Throws<ParseException>(() => target.Eval<MyClassAdder>("new MyClassAdder(){{ 1, 2, 3, 4, 5},StrProp = \"6\" ,7 }"));
//Member init first, then attempt a collection init.
Assert.Throws<ParseException>(() => target.Eval<MyClassAdder>("new MyClassAdder(){StrProp = \"6\" ,{ 1, 2, 3, 4, 5},7 }"));
}
[Test]
public void Ctor_CannotUseCollectionInitDoesNotImplementIEnumerable()
{
var target = new Interpreter();
target.Reference(typeof(MyClassAdder));
target.Reference(typeof(MyClass));
Assert.Throws<ParseException>(() => target.Eval<MyClass>("new MyClass(){ 1, 2, 3, 4, 5}"));
}
[Test]
public void Ctor_NewListWithItems()
{
Ctor_NewListGeneric<string>("\"1\"", "\"2\"", "\"3\"", "{string.Empty}", "string.Empty", "int.MaxValue.ToString()", "{int.MinValue.ToString()}");
Ctor_NewListGeneric<int>("1", "2", "3", "int.MinValue", "int.MaxValue", "{int.MinValue}", "{int.MaxValue}");
Ctor_NewListGeneric<object>("string.Empty", "int.MinValue");
}
[Test]
public void Ctor_NewListCantFindAddMethod()
{
var target = new Interpreter();
target.Reference(typeof(System.Collections.Generic.List<>));
try
{
target.Eval<System.Collections.Generic.List<int>>("new List<int>(){string.Empty}");
}
catch (ParseException ex)
{
if (ex.Message.Contains("The best overloaded Add "))
{
Assert.IsTrue(ex.Message.Contains("Add"));
}
else
{
throw;
}
}
Assert.Throws<ParseException>(() => target.Eval<System.Collections.Generic.List<string>>("new List<string>(){int.MaxValue}"));
}
public void Ctor_NewListGeneric<TObject>(params string[] items)
{
var target = new Interpreter();
target.Reference(typeof(System.Collections.Generic.List<>));
target.Reference(typeof(TObject));
//Create a random list of values to test.
var actual = new System.Collections.Generic.List<TObject>();
foreach (var v in items)
{
actual.Add(target.Eval<TObject>(v.Trim('}', '{')));
}
for (var min = 0; min < actual.Count; ++min)
{
for (var count = Math.Min(min, 1); count <= actual.Count - min; ++count)
{
var evalText = $"new List<{typeof(TObject).Name}>(){{{string.Join(",", items.Skip(min).Take(count))}}}";
System.Collections.Generic.List<TObject> eval = null;
Assert.DoesNotThrow(() => eval = target.Eval<System.Collections.Generic.List<TObject>>(evalText), evalText);
Assert.AreEqual(count, eval.Count);
for (var i = 0; i < count; ++i)
{
Assert.AreEqual(actual[i + min], eval[i]);
}
}
}
}
[Test]
public void Ctor_NewListWithString()
{
var target = new Interpreter();
target.Reference(typeof(System.Collections.Generic.List<>));
var list = target.Eval<System.Collections.Generic.List<string>>("new List<string>(){string.Empty}");
Assert.AreEqual(1, list.Count);
for (int i = 0; i < list.Count; ++i)
{
Assert.AreSame(string.Empty, list[i]);
}
list = target.Eval<System.Collections.Generic.List<string>>("new List<string>(){StrProp = string.Empty}", new Parameter("StrProp", "0"));
Assert.AreSame(string.Empty, list[0]);
list = target.Eval<System.Collections.Generic.List<string>>("new List<string>(){{StrProp = string.Empty}}", new Parameter("StrProp", "0"));
Assert.AreSame(string.Empty, list[0]);
list = target.Eval<System.Collections.Generic.List<string>>("new List<string>(){StrValue()}", new Parameter("StrValue", new Func<string>(() => "Func")));
Assert.AreEqual("Func", list[0]);
}
private class MyClass
{
public int IntField;
public string StrProp { get; set; }
public int ReadOnlyProp { get; }
public int[] MyArr { get; set; }
public MyClass()
{
}
public MyClass(string strValue)
{
StrProp = strValue;
}
public MyClass(params int[] intValues)
{
MyArr = intValues;
}
public override bool Equals(object obj)
{
return Equals(obj as MyClass);
}
public bool Equals(MyClass p)
{
if (p is null) return false;
if (ReferenceEquals(this, p)) return true;
return IntField == p.IntField && StrProp == p.StrProp && ReadOnlyProp == p.ReadOnlyProp;
}
// remove compilation warning
public override int GetHashCode()
{
return 0;
}
}
private class MyClassAdder : MyClass, System.Collections.IEnumerable
{
public MyClassAdder Add(string s)
{
StrProp = s;
return this;
}
public MyClassAdder Add(int intValue)
{
IntField = intValue;
return this;
}
public MyClassAdder Add(params int[] intValues)
{
MyArr = intValues;
return this;
}
public MyClassAdder Add(bool returnMe)
{
if (returnMe)
{
return this;
}
return null;
}
IEnumerator IEnumerable.GetEnumerator()
{
yield break;
}
}
}
}