Skip to content

Commit 6decc7a

Browse files
authored
Merge pull request #59 from Karbot/master
#56 left-associative evaluation of conditional and / or expressions
2 parents 74c1bf6 + 7c81346 commit 6decc7a

File tree

3 files changed

+162
-46
lines changed

3 files changed

+162
-46
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,13 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
11111111
yield return new TestCaseData("nullVar?[1][3]", onInstanceVariables, true).SetCategory("Instance Property,Null Conditional indexing").Returns(null);
11121112
yield return new TestCaseData("simpleArray2?[3]?.Trim()", onInstanceVariables, true).SetCategory("Instance Property,Null Conditional indexing").Returns(null);
11131113

1114+
yield return new TestCaseData( "false && 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,And Conditional" ).Returns( false );
1115+
yield return new TestCaseData( "!string.IsNullOrEmpty(nullVar) && nullVar.StartsWith(\"ABC\")", onInstanceVariables, true ).SetCategory( "Instance Property,And Conditional" ).Returns( false );
1116+
yield return new TestCaseData( "string.IsNullOrEmpty(nullVar) || nullVar.StartsWith(\"ABC\")", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional" ).Returns( true );
1117+
yield return new TestCaseData( "true || 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional" ).Returns( true );
1118+
yield return new TestCaseData( "false && true || true", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional,And Conditional,Precedence check" ).Returns( true );
1119+
yield return new TestCaseData( "true || true && false", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional,And Conditional,Precedence check" ).Returns( true );
1120+
11141121
yield return new TestCaseData("simpleInt.ToString()", onInstanceVariables, true).SetCategory("Instance Method").Returns("42");
11151122
yield return new TestCaseData("simpleInt.ToString().Length", onInstanceVariables, true).SetCategory("Instance Method,Instance Property").Returns(2);
11161123

@@ -1141,6 +1148,11 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
11411148
yield return new TestCaseData("simpleInt++ - simpleInt", onInstanceVariables, true).SetCategory("Postfix operator, ++").Returns(-1);
11421149
yield return new TestCaseData("simpleInt--", onInstanceVariables, true).SetCategory("Postfix operator, --").Returns(42);
11431150
yield return new TestCaseData("simpleInt-- - simpleInt", onInstanceVariables, true).SetCategory("Postfix operator, --").Returns(1);
1151+
1152+
yield return new TestCaseData("false && 1/0>0", onInstanceVariables, true ).SetCategory( "Conditional And, negative left operand (should respect left associativity)" ).Returns( false );
1153+
yield return new TestCaseData("true || 1/0>0", onInstanceVariables, true ).SetCategory( "Conditional Or, positive left operand (should respect left associativity)" ).Returns( true );
1154+
yield return new TestCaseData("false && (true && 1/0>0)", onInstanceVariables, true ).SetCategory( "Conditional And, negative left operand (should respect left associativity)" ).Returns( false );
1155+
yield return new TestCaseData("true || (false || 1/0>0)", onInstanceVariables, true ).SetCategory( "Conditional Or, positive left operand (should respect left associativity)" ).Returns( true );
11441156
#endregion
11451157

11461158
#region Delegates as a variable
@@ -1426,7 +1438,7 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
14261438
evaluator = new ExpressionEvaluator(new Dictionary<string, object>
14271439
{
14281440
{ "P1var", "P1" },
1429-
{ "myObj", new ClassForTest1() },
1441+
{ "myObj", new ClassForTest1() }
14301442
});
14311443

14321444
evaluator.PreEvaluateVariable += (sender, e) =>
@@ -1446,7 +1458,10 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
14461458
yield return new TestCaseData(evaluator, "myObj.PropertyThatWillFailed", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
14471459
yield return new TestCaseData(evaluator, "myObj.Add3To(5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
14481460
yield return new TestCaseData(evaluator, "Abs(-5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
1449-
1461+
yield return new TestCaseData(evaluator, "true && 1/0>0",typeof(DivideByZeroException) ).SetCategory( "Conditional And, positive left operand (should lead to exception)" );
1462+
yield return new TestCaseData(evaluator, "false || 1/0>0", typeof( DivideByZeroException ) ).SetCategory( "Conditional Or, positive left operand (should lead to exception associativity)" );
1463+
yield return new TestCaseData(evaluator, "true && (true && 1/0>0)", typeof( DivideByZeroException ) ).SetCategory( "Conditional And, positive left operand (should lead to exception)" );
1464+
yield return new TestCaseData(evaluator, "false || (false || 1/0>0)", typeof( DivideByZeroException ) ).SetCategory( "Conditional Or, positive left operand (should lead to exception associativity)" );
14501465
#endregion
14511466
}
14521467
}

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/XExpressionEvaluator3.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected override void Init()
1515
/// <summary>
1616
/// To evaluate DateTimes objects with #year-month-day syntax (#2019-05-28)
1717
/// </summary>
18-
protected virtual bool EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
18+
protected virtual bool? EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
1919
{
2020
Match match = Regex.Match(expression.Substring(i), @"^\s*#(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})");
2121

@@ -40,7 +40,7 @@ protected virtual bool EvaluateDateTimeSyntax(string expression, Stack<object> s
4040
/// <summary>
4141
/// To evaluate a string replace with custom ternary indicator
4242
/// </summary>
43-
protected virtual bool EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
43+
protected virtual bool? EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
4444
{
4545
if (expression.Substring(i, 1).Equals("°"))
4646
{

0 commit comments

Comments
 (0)