Skip to content

Commit 629f893

Browse files
committed
#105 OK + Correction same error in String interpolation
1 parent 4aa26e1 commit 629f893

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,20 +1662,38 @@ public void Evaluate_ExtensionMethodOnContext()
16621662
}
16631663

16641664
/// <summary>
1665-
/// To correct #105 Exception should be thrown and not set in the stack
1665+
/// To correct #105 Exception were not thrown when concat with string
16661666
/// </summary>
16671667
[Test]
16681668
[Category("Bug")]
16691669
[Category("#105")]
1670-
public void Evaluate_Exceptions_NotThrown()
1670+
public void Evaluate_ExceptionsNotThrownWhenConcatWithString()
16711671
{
16721672
var evaluator = new ExpressionEvaluator
16731673
{
16741674
Context = new { Person = new Person2 { Name = null, Number = 1.11m } }
16751675
};
16761676

1677-
Should.Throw<Exception>(() => Console.WriteLine(evaluator.Evaluate("\"Test one \" + Person.Name.Trim()")));
1678-
Should.Throw<Exception>(() => Console.WriteLine(evaluator.Evaluate("\"Test two \" + Person.AnotherName.Trim()")));
1677+
Should.Throw<NullReferenceException>(() => Console.WriteLine(evaluator.Evaluate("Person.Name.Trim() + \"Test one \"")));
1678+
Should.Throw<NullReferenceException>(() => Console.WriteLine(evaluator.Evaluate("\"Test one \" + Person.Name.Trim()")));
1679+
Should.Throw<ExpressionEvaluatorSyntaxErrorException>(() => Console.WriteLine(evaluator.Evaluate("\"Test two \" + Person.AnotherName.Trim()")));
1680+
}
1681+
1682+
/// <summary>
1683+
/// #105 Exception were not thrown when in string interpolation
1684+
/// </summary>
1685+
[Test]
1686+
[Category("Bug")]
1687+
[Category("#105")]
1688+
public void Evaluate_ExceptionsNotThrownWhenInStringInterpolation()
1689+
{
1690+
var evaluator = new ExpressionEvaluator
1691+
{
1692+
Context = new { Person = new Person2 { Name = null, Number = 1.11m } }
1693+
};
1694+
1695+
Should.Throw<NullReferenceException>(() => Console.WriteLine(evaluator.Evaluate("$\"Test one {Person.Name.Trim()}\"")));
1696+
Should.Throw<ExpressionEvaluatorSyntaxErrorException>(() => Console.WriteLine(evaluator.Evaluate("$\"Test two {Person.AnotherName.Trim()}\"")));
16791697
}
16801698

16811699
#endregion

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,15 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
20922092
{
20932093
throw;
20942094
}
2095+
catch (NullReferenceException nullException)
2096+
{
2097+
stack.Push(new BubbleExceptionContainer()
2098+
{
2099+
Exception = nullException
2100+
});
2101+
2102+
return true;
2103+
}
20952104
catch (Exception ex)
20962105
{
20972106
//Transport the exception in stack.
@@ -2979,7 +2988,13 @@ protected virtual bool EvaluateString(string expression, Stack<object> stack, re
29792988
string beVerb = bracketCount == 1 ? "is" : "are";
29802989
throw new Exception($"{bracketCount} '}}' character {beVerb} missing in expression : [{expression}]");
29812990
}
2982-
resultString.Append(Evaluate(innerExp.ToString()));
2991+
2992+
object obj = Evaluate(innerExp.ToString());
2993+
2994+
if (obj is BubbleExceptionContainer bubbleExceptionContainer)
2995+
throw bubbleExceptionContainer.Exception;
2996+
2997+
resultString.Append(obj);
29832998
}
29842999
}
29853000
else if (expression.Substring(i, expression.Length - i)[0] == '}')
@@ -3119,14 +3134,24 @@ void EvaluateFirstPreviousUnaryOp(int j)
31193134
}
31203135
else
31213136
{
3137+
var left = (dynamic)list[i + 1];
3138+
var right = (dynamic)list[i - 1];
3139+
31223140
try
31233141
{
3124-
list[i] = operatorEvalutationsDict[eOp]((dynamic)list[i + 1], (dynamic)list[i - 1]);
3142+
list[i] = operatorEvalutationsDict[eOp](left, right);
3143+
3144+
if (left is BubbleExceptionContainer && right is string)
3145+
{
3146+
list[i] = left; //Bubble up the causing error
3147+
}
3148+
else if (right is BubbleExceptionContainer && left is string)
3149+
{
3150+
list[i] = right; //Bubble up the causing error
3151+
}
31253152
}
31263153
catch (Exception ex)
31273154
{
3128-
var left = (dynamic)list[i + 1];
3129-
var right = (dynamic)list[i - 1];
31303155
if (left is BubbleExceptionContainer)
31313156
{
31323157
list[i] = left; //Bubble up the causing error

0 commit comments

Comments
 (0)