Skip to content

Commit 3ccbb66

Browse files
committed
Correction of Ternary conditional operator bug #53
1 parent 7b750e2 commit 3ccbb66

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ public void TypeTesting(string expression, Type type)
617617
[TestCase("Abs(-4) > 10 / 2 ? Abs(-3) : (Abs(-4) + 4) / 2", ExpectedResult = 4, Category = "Conditional Operator t ? x : y")]
618618
[TestCase("Abs(-4) < 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 6, Category = "Conditional Operator t ? x : y")]
619619
[TestCase("Abs(-4) > 10 / 2 ? (true ? 6 : 3+2) : (false ? Abs(-18) : 100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
620+
[TestCase("Abs(-4) > 10 / 2?(true ? 6 : 3+2):(false?Abs(-18):100 / 2)", ExpectedResult = 50, Category = "Conditional Operator t ? x : y")]
621+
[TestCase("1==1?true:false", ExpectedResult = true, Category = "Conditional Operator t ? x : y")]
620622
#endregion
621623

622624
#region Math Constants

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,7 @@ protected virtual bool EvaluateTernaryConditionalOperator(string expression, Sta
24562456
{
24572457
stack.Clear();
24582458

2459-
stack.Push(condition ? Evaluate(restOfExpression.Substring(1, j - 1)) : Evaluate(restOfExpression.Substring(j + 1)));
2459+
stack.Push(condition ? Evaluate(restOfExpression.Substring(0, j)) : Evaluate(restOfExpression.Substring(j + 1)));
24602460

24612461
i = expression.Length;
24622462

0 commit comments

Comments
 (0)