dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
2.01k stars 379 forks source link

Added support of collection initializer syntax. #250

Closed holdenmai closed 2 years ago

holdenmai commented 2 years ago

Implementation of collection initializer syntax support for constructor #194 Managed to somehow close the original PR on accident during rebase operation pull 246, but all comments have now been resolved.

You can now write

var target = new Interpreter();
target.Reference(typeof(System.Collections.Generic.List<>));
var intList = target.Eval<System.Collections.Generic.List<int>>("new List<int>(){1, 2, 3, 4, 5}");
Assert.AreEqual(5, intList.Count);
for (int i = 0; i < intList.Count; ++i)
{
    Assert.AreEqual(i + 1, intList[i]);
}
or 
 target.Eval<System.Collections.Generic.List<string>>("new List<string>(){string.Empty}");

as well as implementations supporting multiple parameters in the Add method such as

new MyClassAdder(){{ 1, 2, 3, 4, 5},{\"6\" },7 } and new Dictionary<int, string>(){{1, \"1\"}, {2, \"2\"}, {3, \"3\"}, {4, \"4\"}, {5, \"5\"}}

Reference to method parameters is also supported like below

target.Eval<System.Collections.Generic.List<string>>("new List<string>(){StrProp = string.Empty}", new Parameter("StrProp", "0")) target.Eval<System.Collections.Generic.List<string>>("new List<string>(){StrValue()}", new Parameter("StrValue", new Func<string>(() => "Func")))

holdenmai commented 2 years ago

I have added scenarios within the differing unit tests to cover each of the cases mentioned. Is it preferred that they become standalone cases to better show what negative case they are trying to cover?

davideicardi commented 2 years ago

Thank you @holdenmai

Is it preferred that they become standalone cases to better show what negative case they are trying to cover?

Yes, maybe having separate tests would be better... or try to simplify/explain better the existing ones.

holdenmai commented 2 years ago

I have reorganized the unit tests to better show the different scenarios being covered.

davideicardi commented 2 years ago

LGTM!

@metoule ?

There is just 3 conversations still open that I'm not sure if they are now resolved ...

metoule commented 2 years ago

Sorry for the late review! The code looks good to me, the test cases as well, I still have a few questions, but nothing major.