Skip to content

Commit 36e1b70

Browse files
committed
Correction of #81 + Tests
1 parent eb128dd commit 36e1b70

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,48 @@ public void ExceptionThrowingEvaluation(ExpressionEvaluator evaluator, string ex
15091509

15101510
#endregion
15111511

1512+
#region Bug corrections
1513+
1514+
/// <summary>
1515+
/// To correct #81 Exception is assigned to variable
1516+
/// With simple variable
1517+
/// </summary>
1518+
[Test]
1519+
[Category("Bug")]
1520+
[Category("#81")]
1521+
public void Evaluate_WithException_ThrowsExceptionAndDoesNotAssignItSimpleVariable()
1522+
{
1523+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
1524+
1525+
evaluator.Variables.Add("exceptionGenerator", new ExceptionGenerator());
1526+
Assert.Throws<ExpressionEvaluatorSyntaxErrorException>(() => evaluator.Evaluate("result = exceptionGenerator.ThrowAnException()"));
1527+
1528+
evaluator.Variables.ContainsKey("result").ShouldBeFalse();
1529+
}
1530+
1531+
/// <summary>
1532+
/// To correct #81 Exception is assigned to variable
1533+
/// With InObject
1534+
/// </summary>
1535+
[Test]
1536+
[Category("Bug")]
1537+
[Category("#81")]
1538+
public void Evaluate_WithException_ThrowsExceptionAndDoesNotAssignItInObject()
1539+
{
1540+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
1541+
1542+
ObjectContainer objectContainer = new ObjectContainer();
1543+
1544+
evaluator.Variables.Add("exceptionGenerator", new ExceptionGenerator());
1545+
evaluator.Variables.Add("objectContainer", objectContainer);
1546+
Assert.Throws<ExpressionEvaluatorSyntaxErrorException>(() => evaluator.Evaluate("objectContainer.AnObjectProperty = exceptionGenerator.ThrowAnException()"));
1547+
1548+
objectContainer.AnObjectProperty.ShouldBeOfType(typeof(int));
1549+
objectContainer.AnObjectProperty.ShouldBe(10);
1550+
}
1551+
1552+
#endregion
1553+
15121554
#region EvaluateWithSpecificEvaluator
15131555

15141556
#region TestCasesEvaluateWithSpecificEvaluator
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace CodingSeb.ExpressionEvaluator.Tests
4+
{
5+
public class ExceptionGenerator
6+
{
7+
public void ThrowAnException()
8+
{
9+
throw new Exception();
10+
}
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CodingSeb.ExpressionEvaluator.Tests
2+
{
3+
public class ObjectContainer
4+
{
5+
public object AnObjectProperty { get; set; } = 10;
6+
}
7+
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,9 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
21982198
varValue = Evaluate(rightExpression);
21992199
}
22002200

2201+
if (varValue is BubbleExceptionContainer exceptionContainer)
2202+
throw exceptionContainer.Exception;
2203+
22012204
stack.Clear();
22022205
stack.Push(varValue);
22032206
}
@@ -2332,6 +2335,9 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
23322335
cusVarValueToPush = Evaluate(rightExpression);
23332336
}
23342337

2338+
if (cusVarValueToPush is BubbleExceptionContainer exceptionContainer)
2339+
throw exceptionContainer.Exception;
2340+
23352341
stack.Clear();
23362342
stack.Push(cusVarValueToPush);
23372343
}
@@ -2730,6 +2736,9 @@ protected virtual bool EvaluateIndexing(string expression, Stack<object> stack,
27302736
valueToPush = Evaluate(rightExpression);
27312737
}
27322738

2739+
if (valueToPush is BubbleExceptionContainer exceptionContainer)
2740+
throw exceptionContainer.Exception;
2741+
27332742
if (left is IDictionary<string, object> dictionaryLeft)
27342743
dictionaryLeft[right] = valueToPush;
27352744
else

0 commit comments

Comments
 (0)