Skip to content

Commit cecaa08

Browse files
committed
More tests for var and func evaluations priorities
1 parent 7e84788 commit cecaa08

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,15 +1402,29 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
14021402

14031403
#region On the fly pre events cancel
14041404

1405-
evaluator = new ExpressionEvaluator();
1405+
evaluator = new ExpressionEvaluator(new Dictionary<string, object>
1406+
{
1407+
{ "P1var", "P1" },
1408+
{ "myObj", new ClassForTest1() },
1409+
});
14061410

14071411
evaluator.PreEvaluateVariable += (sender, e) =>
14081412
{
14091413
if (e.Name.StartsWith("P"))
14101414
e.CancelEvaluation = true;
14111415
};
14121416

1417+
evaluator.PreEvaluateFunction += (sender, e) =>
1418+
{
1419+
if (e.Name.StartsWith("A"))
1420+
e.CancelEvaluation = true;
1421+
};
1422+
14131423
yield return new TestCaseData(evaluator, "Pi", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
1424+
yield return new TestCaseData(evaluator, "P1var", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
1425+
yield return new TestCaseData(evaluator, "myObj.PropertyThatWillFailed", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
1426+
yield return new TestCaseData(evaluator, "myObj.Add3To(5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
1427+
yield return new TestCaseData(evaluator, "Abs(-5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
14141428

14151429
#endregion
14161430
}
@@ -1563,14 +1577,20 @@ public static IEnumerable<TestCaseData> TestCasesEvaluateWithSpecificEvaluator
15631577

15641578
#endregion
15651579

1566-
#region Onthefly events (Pre events and/or Generics)
1580+
#region Onthefly events (Pre events custom vars and standard events priorities and/or Generics)
15671581

1568-
ExpressionEvaluator evaluatorOnTheFlyGenericTypes = new ExpressionEvaluator();
1582+
ExpressionEvaluator evaluatorOnTheFlyGenericTypes = new ExpressionEvaluator(new Dictionary<string, object>
1583+
{
1584+
{ "myvar1", 10 },
1585+
{ "myvar2", 3 },
1586+
});
15691587

15701588
void VariableEval(object sender, VariableEvaluationEventArg e)
15711589
{
1572-
if(e.Name.Equals("GetSpecifiedGenericTypesVar"))
1590+
if (e.Name.Equals("GetSpecifiedGenericTypesVar"))
15731591
e.Value = e.EvaluateGenericTypes();
1592+
else if (e.Name.Equals("myvar2"))
1593+
e.Value = 50;
15741594
}
15751595

15761596
void FunctionEval(object sender, FunctionEvaluationEventArg e)
@@ -1647,6 +1667,16 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
16471667
.SetCategory("On the fly var")
16481668
.SetCategory("GenericTypes");
16491669

1670+
yield return new TestCaseData(evaluatorOnTheFlyGenericTypes
1671+
, "myvar1")
1672+
.Returns(5)
1673+
.SetCategory("var evaluation priority");
1674+
1675+
yield return new TestCaseData(evaluatorOnTheFlyGenericTypes
1676+
, "myvar2")
1677+
.Returns(3)
1678+
.SetCategory("var evaluation priority");
1679+
16501680
#endregion
16511681
}
16521682
}

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/ClassForTest1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class ClassForTest1
1212

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

15+
public int PropertyThatWillFailed { get; set; } = 1;
16+
1517
public int Add3To(int value)
1618
{
1719
return value + 3;

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,6 @@ private bool EvaluateVarOrFunc(string expr, string restOfExpression, Stack<objec
21202120
stack.Push(varValueToPush);
21212121
}
21222122
else if ((Variables.TryGetValue(varFuncName, out object cusVarValueToPush) || varFuncMatch.Groups["assignationOperator"].Success)
2123-
&& !varFuncMatch.Groups["inObject"].Success
21242123
&& (cusVarValueToPush == null || !TypesToBlock.Contains(cusVarValueToPush.GetType())))
21252124
{
21262125
stack.Push(cusVarValueToPush);

0 commit comments

Comments
 (0)