Skip to content

#56 left-associative evaluation of conditional and / or expressions #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,13 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
yield return new TestCaseData("nullVar?[1][3]", onInstanceVariables, true).SetCategory("Instance Property,Null Conditional indexing").Returns(null);
yield return new TestCaseData("simpleArray2?[3]?.Trim()", onInstanceVariables, true).SetCategory("Instance Property,Null Conditional indexing").Returns(null);

yield return new TestCaseData( "false && 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,And Conditional" ).Returns( false );
yield return new TestCaseData( "!string.IsNullOrEmpty(nullVar) && nullVar.StartsWith(\"ABC\")", onInstanceVariables, true ).SetCategory( "Instance Property,And Conditional" ).Returns( false );
yield return new TestCaseData( "string.IsNullOrEmpty(nullVar) || nullVar.StartsWith(\"ABC\")", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional" ).Returns( true );
yield return new TestCaseData( "true || 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional" ).Returns( true );
yield return new TestCaseData( "false && true || true", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional,And Conditional,Precedence check" ).Returns( true );
yield return new TestCaseData( "true || true && false", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional,And Conditional,Precedence check" ).Returns( true );

yield return new TestCaseData("simpleInt.ToString()", onInstanceVariables, true).SetCategory("Instance Method").Returns("42");
yield return new TestCaseData("simpleInt.ToString().Length", onInstanceVariables, true).SetCategory("Instance Method,Instance Property").Returns(2);

Expand Down Expand Up @@ -1141,6 +1148,11 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
yield return new TestCaseData("simpleInt++ - simpleInt", onInstanceVariables, true).SetCategory("Postfix operator, ++").Returns(-1);
yield return new TestCaseData("simpleInt--", onInstanceVariables, true).SetCategory("Postfix operator, --").Returns(42);
yield return new TestCaseData("simpleInt-- - simpleInt", onInstanceVariables, true).SetCategory("Postfix operator, --").Returns(1);

yield return new TestCaseData("false && 1/0>0", onInstanceVariables, true ).SetCategory( "Conditional And, negative left operand (should respect left associativity)" ).Returns( false );
yield return new TestCaseData("true || 1/0>0", onInstanceVariables, true ).SetCategory( "Conditional Or, positive left operand (should respect left associativity)" ).Returns( true );
yield return new TestCaseData("false && (true && 1/0>0)", onInstanceVariables, true ).SetCategory( "Conditional And, negative left operand (should respect left associativity)" ).Returns( false );
yield return new TestCaseData("true || (false || 1/0>0)", onInstanceVariables, true ).SetCategory( "Conditional Or, positive left operand (should respect left associativity)" ).Returns( true );
#endregion

#region Delegates as a variable
Expand Down Expand Up @@ -1426,7 +1438,7 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
evaluator = new ExpressionEvaluator(new Dictionary<string, object>
{
{ "P1var", "P1" },
{ "myObj", new ClassForTest1() },
{ "myObj", new ClassForTest1() }
});

evaluator.PreEvaluateVariable += (sender, e) =>
Expand All @@ -1446,7 +1458,10 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
yield return new TestCaseData(evaluator, "myObj.PropertyThatWillFailed", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
yield return new TestCaseData(evaluator, "myObj.Add3To(5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
yield return new TestCaseData(evaluator, "Abs(-5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");

yield return new TestCaseData(evaluator, "true && 1/0>0",typeof(DivideByZeroException) ).SetCategory( "Conditional And, positive left operand (should lead to exception)" );
yield return new TestCaseData(evaluator, "false || 1/0>0", typeof( DivideByZeroException ) ).SetCategory( "Conditional Or, positive left operand (should lead to exception associativity)" );
yield return new TestCaseData(evaluator, "true && (true && 1/0>0)", typeof( DivideByZeroException ) ).SetCategory( "Conditional And, positive left operand (should lead to exception)" );
yield return new TestCaseData(evaluator, "false || (false || 1/0>0)", typeof( DivideByZeroException ) ).SetCategory( "Conditional Or, positive left operand (should lead to exception associativity)" );
#endregion
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected override void Init()
/// <summary>
/// To evaluate DateTimes objects with #year-month-day syntax (#2019-05-28)
/// </summary>
protected virtual bool EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
protected virtual bool? EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
{
Match match = Regex.Match(expression.Substring(i), @"^\s*#(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})");

Expand All @@ -40,7 +40,7 @@ protected virtual bool EvaluateDateTimeSyntax(string expression, Stack<object> s
/// <summary>
/// To evaluate a string replace with custom ternary indicator
/// </summary>
protected virtual bool EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
protected virtual bool? EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
{
if (expression.Substring(i, 1).Equals("°"))
{
Expand Down
Loading