Skip to content

Commit 875bae2

Browse files
committed
Correction of #127 new instance when OptionInlineNamespacesEvaluationActive is false
1 parent 21ef067 commit 875bae2

File tree

2 files changed

+96
-20
lines changed

2 files changed

+96
-20
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,53 @@ public void ExceptionThrowingEvaluation(ExpressionEvaluator evaluator, string ex
15911591

15921592
#region Bug corrections
15931593

1594+
/// <summary>
1595+
/// To correct #127 Evaluating "new DateTime(2022,1,20)" does not work
1596+
/// unless OptionInlineNamespacesEvaluationActive is turned on
1597+
/// </summary>
1598+
[Test]
1599+
[Category("Bug")]
1600+
[Category("#127")]
1601+
public void Evaluate_NewDateTime_When_OptionInlineNamespacesEvaluationActive_is_off()
1602+
{
1603+
ExpressionEvaluator evaluator = new ExpressionEvaluator()
1604+
{
1605+
OptionInlineNamespacesEvaluationActive = false,
1606+
};
1607+
1608+
DateTime? dateTime = evaluator.Evaluate<DateTime>("new DateTime(2022,1,20)");
1609+
1610+
dateTime.HasValue.ShouldBeTrue();
1611+
1612+
dateTime.Value.Year.ShouldBe(2022);
1613+
dateTime.Value.Month.ShouldBe(1);
1614+
dateTime.Value.Day.ShouldBe(20);
1615+
}
1616+
1617+
/// <summary>
1618+
/// To correct #127 Evaluating "new DateTime(2022,1,20)" does not work
1619+
/// unless OptionInlineNamespacesEvaluationActive is turned on
1620+
/// </summary>
1621+
[Test]
1622+
[Category("Bug")]
1623+
[Category("#127")]
1624+
public void Evaluate_NewDateTime_When_OptionInlineNamespacesEvaluationActive_is_on()
1625+
{
1626+
ExpressionEvaluator evaluator = new ExpressionEvaluator()
1627+
{
1628+
OptionInlineNamespacesEvaluationActive = true,
1629+
};
1630+
1631+
DateTime? dateTime = evaluator.Evaluate<DateTime>("new DateTime(2022,1,20)");
1632+
1633+
dateTime.HasValue.ShouldBeTrue();
1634+
1635+
dateTime.Value.Year.ShouldBe(2022);
1636+
dateTime.Value.Month.ShouldBe(1);
1637+
dateTime.Value.Day.ShouldBe(20);
1638+
}
1639+
1640+
15941641
/// <summary>
15951642
/// To correct #81 Exception is assigned to variable
15961643
/// With simple variable

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,32 +2509,61 @@ protected virtual Type EvaluateType(string expression,ref int i, string currentN
25092509
Type staticType = GetTypeByFriendlyName(typeName, genericsTypes);
25102510

25112511
// For inline namespace parsing
2512-
if (staticType == null && OptionInlineNamespacesEvaluationActive)
2512+
if (staticType == null)
25132513
{
2514-
int subIndex = 0;
2515-
Match namespaceMatch = varOrFunctionRegEx.Match(expression.Substring(i + subIndex));
2516-
2517-
while (staticType == null
2518-
&& namespaceMatch.Success
2519-
&& !namespaceMatch.Groups["sign"].Success
2520-
&& !namespaceMatch.Groups["assignationOperator"].Success
2521-
&& !namespaceMatch.Groups["postfixOperator"].Success
2522-
&& !namespaceMatch.Groups["isfunction"].Success
2523-
&& i + subIndex < expression.Length
2524-
&& !typeName.EndsWith("?"))
2514+
if (OptionInlineNamespacesEvaluationActive)
25252515
{
2526-
subIndex += namespaceMatch.Length;
2527-
typeName += $"{namespaceMatch.Groups["inObject"].Value}{namespaceMatch.Groups["name"].Value}{((i + subIndex < expression.Length && expression.Substring(i + subIndex)[0] == '?') ? "?" : "") }";
2516+
int subIndex = 0;
2517+
Match namespaceMatch = varOrFunctionRegEx.Match(expression.Substring(i + subIndex));
2518+
2519+
while (staticType == null
2520+
&& namespaceMatch.Success
2521+
&& !namespaceMatch.Groups["sign"].Success
2522+
&& !namespaceMatch.Groups["assignationOperator"].Success
2523+
&& !namespaceMatch.Groups["postfixOperator"].Success
2524+
&& !namespaceMatch.Groups["isfunction"].Success
2525+
&& i + subIndex < expression.Length
2526+
&& !typeName.EndsWith("?"))
2527+
{
2528+
subIndex += namespaceMatch.Length;
2529+
typeName += $"{namespaceMatch.Groups["inObject"].Value}{namespaceMatch.Groups["name"].Value}{((i + subIndex < expression.Length && expression.Substring(i + subIndex)[0] == '?') ? "?" : "") }";
25282530

2529-
staticType = GetTypeByFriendlyName(typeName, namespaceMatch.Groups["isgeneric"].Value);
2531+
staticType = GetTypeByFriendlyName(typeName, namespaceMatch.Groups["isgeneric"].Value);
25302532

2531-
if (staticType != null)
2532-
{
2533-
i += subIndex;
2534-
break;
2533+
if (staticType != null)
2534+
{
2535+
i += subIndex;
2536+
break;
2537+
}
2538+
2539+
namespaceMatch = varOrFunctionRegEx.Match(expression.Substring(i + subIndex));
25352540
}
2541+
}
2542+
else
2543+
{
2544+
int subIndex = 0;
2545+
Match typeMatch = varOrFunctionRegEx.Match(expression.Substring(i + subIndex));
2546+
2547+
if (staticType == null
2548+
&& typeMatch.Success
2549+
&& !typeMatch.Groups["sign"].Success
2550+
&& !typeMatch.Groups["assignationOperator"].Success
2551+
&& !typeMatch.Groups["postfixOperator"].Success
2552+
&& !typeMatch.Groups["isfunction"].Success
2553+
&& !typeMatch.Groups["inObject"].Success
2554+
&& i + subIndex < expression.Length
2555+
&& !typeName.EndsWith("?"))
2556+
{
2557+
subIndex += typeMatch.Length;
2558+
typeName += $"{typeMatch.Groups["name"].Value}{((i + subIndex < expression.Length && expression.Substring(i + subIndex)[0] == '?') ? "?" : "") }";
25362559

2537-
namespaceMatch = varOrFunctionRegEx.Match(expression.Substring(i + subIndex));
2560+
staticType = GetTypeByFriendlyName(typeName, typeMatch.Groups["isgeneric"].Value);
2561+
2562+
if (staticType != null)
2563+
{
2564+
i += subIndex;
2565+
}
2566+
}
25382567
}
25392568
}
25402569

0 commit comments

Comments
 (0)