Skip to content

Commit a082db9

Browse files
committed
Dicionary initializer object format OK
1 parent aa3a753 commit a082db9

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,27 @@ public void TypeTesting(string expression, Type type)
504504
[TestCase("new List<string>{ \"Hello\", \"Test\" }.Count", ExpectedResult = 2, Category = "Create instance with new Keyword, Collection Initializer")]
505505
[TestCase("new List<string>{ \"Hello\", \"Test\" }[0]", ExpectedResult = "Hello", Category = "Create instance with new Keyword, Collection Initializer")]
506506
[TestCase("new List<string>{ \"Hello\", \"Test\" }[1]", ExpectedResult = "Test", Category = "Create instance with new Keyword, Collection Initializer")]
507+
[TestCase("new ClassForTest1(){ IntProperty = 100, StringProperty = \"A Text\" }.GetType()", ExpectedResult = typeof(ClassForTest1), Category = "Create instance with new Keyword, Object Initializer")]
508+
[TestCase("new ClassForTest1(){ IntProperty = 100, StringProperty = \"A Text\" }.IntProperty", ExpectedResult = 100, Category = "Create instance with new Keyword, Object Initializer")]
509+
[TestCase("new ClassForTest1(){ IntProperty = 100, StringProperty = \"A Text\" }.StringProperty", ExpectedResult = "A Text", Category = "Create instance with new Keyword, Object Initializer")]
510+
[TestCase("new ClassForTest1{ IntProperty = 100, StringProperty = \"A Text\" }.GetType()", ExpectedResult = typeof(ClassForTest1), Category = "Create instance with new Keyword, Object Initializer")]
511+
[TestCase("new ClassForTest1{ IntProperty = 100, StringProperty = \"A Text\" }.IntProperty", ExpectedResult = 100, Category = "Create instance with new Keyword, Object Initializer")]
512+
[TestCase("new ClassForTest1{ IntProperty = 100, StringProperty = \"A Text\" }.StringProperty", ExpectedResult = "A Text", Category = "Create instance with new Keyword, Object Initializer")]
513+
[TestCase("new ClassForTest2(10){ Value2 = 100 }.GetType()", ExpectedResult = typeof(ClassForTest2), Category = "Create instance with new Keyword, Object Initializer")]
514+
[TestCase("new ClassForTest2(10){ Value2 = 100 }.Value1", ExpectedResult = 10, Category = "Create instance with new Keyword, Object Initializer")]
515+
[TestCase("new ClassForTest2(10){ Value2 = 100 }.Value2", ExpectedResult = 100, Category = "Create instance with new Keyword, Object Initializer")]
516+
[TestCase("new Dictionary<int, string>(){ [7] = \"seven\", [7+2] = \"nine\" }.GetType()", ExpectedResult = typeof(Dictionary<int, string>), Category = "Create instance with new Keyword, Object Initializer")]
517+
[TestCase("new Dictionary<int, string>(){ [7] = \"seven\", [7+2] = \"nine\" }[7]", ExpectedResult = "seven", Category = "Create instance with new Keyword, Object Initializer")]
518+
[TestCase("new Dictionary<int, string>(){ [7] = \"seven\", [7+2] = \"nine\" }[9]", ExpectedResult = "nine", Category = "Create instance with new Keyword, Object Initializer")]
519+
[TestCase("new Dictionary<int, string>{ [7] = \"seven\", [7+2] = \"nine\" }.GetType()", ExpectedResult = typeof(Dictionary<int, string>), Category = "Create instance with new Keyword, Object Initializer")]
520+
[TestCase("new Dictionary<int, string>{ [7] = \"seven\", [7+2] = \"nine\" }[7]", ExpectedResult = "seven", Category = "Create instance with new Keyword, Object Initializer")]
521+
[TestCase("new Dictionary<int, string>{ [7] = \"seven\", [7+2] = \"nine\" }[9]", ExpectedResult = "nine", Category = "Create instance with new Keyword, Object Initializer")]
522+
[TestCase("new Dictionary<string, int>(){ [\"seven\"] = 7, [\"nine\"] = 9 }.GetType()", ExpectedResult = typeof(Dictionary<string, int>), Category = "Create instance with new Keyword, Object Initializer")]
523+
[TestCase("new Dictionary<string, int>(){ [\"seven\"] = 7, [\"nine\"] = 9 }[\"seven\"]", ExpectedResult = 7, Category = "Create instance with new Keyword, Object Initializer")]
524+
[TestCase("new Dictionary<string, int>(){ [\"seven\"] = 7, [\"nine\"] = 9 }[\"nine\"]", ExpectedResult = 9, Category = "Create instance with new Keyword, Object Initializer")]
525+
[TestCase("new Dictionary<string, int>{ [\"seven\"] = 7, [\"nine\"] = 9 }.GetType()", ExpectedResult = typeof(Dictionary<string, int>), Category = "Create instance with new Keyword, Object Initializer")]
526+
[TestCase("new Dictionary<string, int>{ [\"seven\"] = 7, [\"nine\"] = 9 }[\"seven\"]", ExpectedResult = 7, Category = "Create instance with new Keyword, Object Initializer")]
527+
[TestCase("new Dictionary<string, int>{ [\"seven\"] = 7, [\"nine\"] = 9 }[\"nine\"]", ExpectedResult = 9, Category = "Create instance with new Keyword, Object Initializer")]
507528

508529
#endregion
509530

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/ClassForTest1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class ClassForTest1
88

99
public int IntProperty { get; set; } = 25;
1010

11-
11+
public string StringProperty { get; set; } = string.Empty;
1212

1313
public static int StaticIntProperty { get; set; } = 67;
1414

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/ClassForTest2.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ public ClassForTest2(int value1)
88
}
99

1010
public int Value1 { get; private set; }
11+
12+
public int Value2 { get; set; }
1113
}
1214
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ExpressionEvaluator
5454
// Depending on OptionInlineNamespacesEvaluationActive. Initialized in constructor
5555
private string InstanceCreationWithNewKeywordRegexPattern { get { return $@"^new\s+(?<name>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern}0-9{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }]*)\s*(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?\s*((?<isfunction>[(])|(?<isArray>\[)|(?<isInit>[{{]))?"; } }
5656
private Regex instanceCreationWithNewKeywordRegex = null;
57-
private string CastRegexPattern { get { return $@"^\(\s*(?<typeName>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern }0-9\{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }[\]<>]*[?]?)\s*\)"; } }
57+
private string CastRegexPattern { get { return $@"^\(\s*(?<typeName>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern }0-9{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }\[\]<>]*[?]?)\s*\)"; } }
5858
private Regex castRegex = null;
5959

6060
private static readonly string primaryTypesRegexPattern = @"(?<=^|[^" + diactiticsKeywordsRegexPattern + @"])(?<primaryType>object|string|bool[?]?|byte[?]?|char[?]?|decimal[?]?|double[?]?|short[?]?|int[?]?|long[?]?|sbyte[?]?|float[?]?|ushort[?]?|uint[?]?|void)(?=[^a-zA-Z_]|$)";
@@ -1444,12 +1444,28 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
14441444

14451445
List<string> initArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, true, ",", "{", "}");
14461446

1447-
if (typeof(IEnumerable).IsAssignableFrom(type))
1447+
if (typeof(IEnumerable).IsAssignableFrom(type) && !typeof(IDictionary).IsAssignableFrom(type))
14481448
{
14491449
MethodInfo methodInfo = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
14501450

14511451
initArgs.ForEach(subExpr => methodInfo.Invoke(element, new object[] { Evaluate(subExpr) }));
14521452
}
1453+
else
1454+
{
1455+
ExpressionEvaluator initEvaluator = new ExpressionEvaluator(new Dictionary<string, object>() { { "this", element } });
1456+
1457+
initArgs.ForEach(subExpr =>
1458+
{
1459+
if (subExpr.Contains("="))
1460+
{
1461+
string trimmedSubExpr = subExpr.TrimStart();
1462+
1463+
initEvaluator.Evaluate($"this{(trimmedSubExpr.StartsWith("[") ? string.Empty : ".")}{trimmedSubExpr}");
1464+
}
1465+
else
1466+
throw new ExpressionEvaluatorSyntaxErrorException($"A '=' char is missing in [{subExpr}]. It is in a object initializer. It must contains one.");
1467+
});
1468+
}
14531469
}
14541470
else
14551471
i--;
@@ -1462,12 +1478,29 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
14621478

14631479
object element = Activator.CreateInstance(type, new object[0]);
14641480

1465-
if (typeof(IEnumerable).IsAssignableFrom(type))
1481+
if (typeof(IEnumerable).IsAssignableFrom(type) && !typeof(IDictionary).IsAssignableFrom(type))
14661482
{
14671483
MethodInfo methodInfo = type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
14681484

14691485
initArgs.ForEach(subExpr => methodInfo.Invoke(element, new object[] { Evaluate(subExpr) }));
14701486
}
1487+
else
1488+
{
1489+
ExpressionEvaluator initEvaluator = new ExpressionEvaluator(new Dictionary<string, object>() { { "this", element } });
1490+
1491+
initArgs.ForEach(subExpr =>
1492+
{
1493+
if (subExpr.Contains("="))
1494+
{
1495+
string trimmedSubExpr = subExpr.TrimStart();
1496+
1497+
initEvaluator.Evaluate($"this{(trimmedSubExpr.StartsWith("[") ? string.Empty : ".")}{trimmedSubExpr}");
1498+
}
1499+
else
1500+
throw new ExpressionEvaluatorSyntaxErrorException($"A '=' char is missing in [{subExpr}]. It is in a object initializer. It must contains one.");
1501+
});
1502+
}
1503+
14711504

14721505
stack.Push(element);
14731506
}
@@ -1862,7 +1895,7 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
18621895
else
18631896
{
18641897
string typeName = $"{varFuncName}{((i < expr.Length && expr.Substring(i)[0] == '?') ? "?" : "") }";
1865-
Type staticType = GetTypeByFriendlyName(typeName);
1898+
Type staticType = GetTypeByFriendlyName(typeName, genericsTypes);
18661899

18671900
if(staticType == null && OptionInlineNamespacesEvaluationActive)
18681901
{
@@ -1979,7 +2012,7 @@ private bool EvaluateTwoCharsOperators(string expr, Stack<object> stack, ref int
19792012
{
19802013
if (i < expr.Length - 1)
19812014
{
1982-
String op = expr.Substring(i, 2);
2015+
string op = expr.Substring(i, 2);
19832016
if (operatorsDictionary.ContainsKey(op))
19842017
{
19852018
stack.Push(operatorsDictionary[op]);

0 commit comments

Comments
 (0)