Skip to content

Commit f8bda99

Browse files
committed
Corrections Properties bugs linked to BubbleExceptions and refactoring
1 parent 72267c6 commit f8bda99

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,8 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
11141114
yield return new TestCaseData( "false && 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,And Conditional" ).Returns( false );
11151115
yield return new TestCaseData("!string.IsNullOrEmpty(nullVar) && nullVar.StartsWith(\"ABC\")", onInstanceVariables, true).SetCategory("Instance Property,And Conditional").Returns(false);
11161116
yield return new TestCaseData("string.IsNullOrEmpty(nullVar) || nullVar.StartsWith(\"ABC\")", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional").Returns(true);
1117+
yield return new TestCaseData("!string.IsNullOrEmpty(nullVar) && nullVar.StartsWith(\"ABC\") == false", onInstanceVariables, true).SetCategory("Instance Property,And Conditional").Returns(false);
1118+
yield return new TestCaseData("string.IsNullOrEmpty(nullVar) || nullVar.StartsWith(\"ABC\") == false", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional").Returns(true);
11171119
yield return new TestCaseData("!string.IsNullOrEmpty(nullVar) && nullVar.Length < 2", onInstanceVariables, true).SetCategory("Instance Property,And Conditional").Returns(false);
11181120
yield return new TestCaseData("string.IsNullOrEmpty(nullVar) || nullVar.Length < 2", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional").Returns(true);
11191121
yield return new TestCaseData( "true || 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional" ).Returns( true );

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 override void Init()
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
{

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ protected enum TryBlockEvaluatedState
215215
protected static object IndexingOperatorFunc(dynamic left, dynamic right)
216216
{
217217
if (left is NullConditionalNullValue)
218+
{
218219
return left;
220+
}
219221
else if (left is BubbleExceptionContainer)
220222
{
221223
return left;
@@ -1528,20 +1530,7 @@ public object Evaluate(string expression)
15281530

15291531
for (int i = 0; i < expression.Length; i++)
15301532
{
1531-
if (!ParsingMethods.Any(parsingMethod =>
1532-
{
1533-
bool? pRes = parsingMethod(expression, stack, ref i);
1534-
//Possibility to implement an option to toggle left associativity
1535-
//If "null" is returned, an error occured while parsing
1536-
if (pRes.HasValue)
1537-
{
1538-
return pRes.Value; //normal case
1539-
}
1540-
else
1541-
{
1542-
return true; //Go on with parsing without throwing an exception. We want to reach the stack processing.
1543-
}
1544-
}))
1533+
if (!ParsingMethods.Any(parsingMethod => parsingMethod(expression, stack, ref i)))
15451534
{
15461535
string s = expression.Substring(i, 1);
15471536

@@ -1573,7 +1562,7 @@ public object Evaluate(string expression)
15731562

15741563
#region Sub parts evaluate methods (protected virtual)
15751564

1576-
protected virtual bool? EvaluateCast(string expression, Stack<object> stack, ref int i)
1565+
protected virtual bool EvaluateCast(string expression, Stack<object> stack, ref int i)
15771566
{
15781567
Match castMatch = Regex.Match(expression.Substring(i), CastRegexPattern, optionCaseSensitiveEvaluationActive ? RegexOptions.None : RegexOptions.IgnoreCase);
15791568

@@ -1595,7 +1584,7 @@ public object Evaluate(string expression)
15951584
return false;
15961585
}
15971586

1598-
protected virtual bool? EvaluateNumber(string expression, Stack<object> stack, ref int i)
1587+
protected virtual bool EvaluateNumber(string expression, Stack<object> stack, ref int i)
15991588
{
16001589
string restOfExpression = expression.Substring(i);
16011590
Match numberMatch = Regex.Match(restOfExpression, numberRegexPattern, RegexOptions.IgnoreCase);
@@ -1658,7 +1647,7 @@ public object Evaluate(string expression)
16581647
}
16591648
}
16601649

1661-
protected virtual bool? EvaluateInstanceCreationWithNewKeyword(string expression, Stack<object> stack, ref int i)
1650+
protected virtual bool EvaluateInstanceCreationWithNewKeyword(string expression, Stack<object> stack, ref int i)
16621651
{
16631652
if (!OptionNewKeywordEvaluationActive)
16641653
return false;
@@ -1828,7 +1817,7 @@ void Init(object element, List<string> initArgs)
18281817
}
18291818
}
18301819

1831-
protected virtual bool? EvaluateVarOrFunc(string expression, Stack<object> stack, ref int i)
1820+
protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack, ref int i)
18321821
{
18331822
Match varFuncMatch = varOrFunctionRegEx.Match(expression.Substring(i));
18341823

@@ -1889,7 +1878,7 @@ void Init(object element, List<string> initArgs)
18891878
else if (obj is BubbleExceptionContainer)
18901879
{
18911880
stack.Push(obj);
1892-
return null;
1881+
return true;
18931882
}
18941883
else
18951884
{
@@ -1997,7 +1986,7 @@ void Init(object element, List<string> initArgs)
19971986
{
19981987
Exception = new ExpressionEvaluatorSyntaxErrorException($"The call of the method \"{varFuncName}\" on type [{objType}] generate this error : {ex.InnerException?.Message ?? ex.Message}", ex)
19991988
});
2000-
return null; //Signals an error to the parsing method array call
1989+
return true; //Signals an error to the parsing method array call
20011990
}
20021991
}
20031992
else
@@ -2081,7 +2070,7 @@ void Init(object element, List<string> initArgs)
20812070
else if (obj is BubbleExceptionContainer)
20822071
{
20832072
stack.Push(obj);
2084-
return null;
2073+
return true;
20852074
}
20862075
else
20872076
{
@@ -2234,7 +2223,13 @@ void Init(object element, List<string> initArgs)
22342223
}
22352224
catch (Exception ex)
22362225
{
2237-
throw new ExpressionEvaluatorSyntaxErrorException($"[{objType}] object has no public Property or Member named \"{varFuncName}\".", ex);
2226+
//Transport the exception in stack.
2227+
stack.Push(new BubbleExceptionContainer()
2228+
{
2229+
Exception = new ExpressionEvaluatorSyntaxErrorException($"[{objType}] object has no public Property or Member named \"{varFuncName}\".", ex)
2230+
});
2231+
i--;
2232+
return true; //Signals an error to the parsing method array call
22382233
}
22392234
}
22402235
else
@@ -2441,7 +2436,7 @@ void Init(object element, List<string> initArgs)
24412436
}
24422437
}
24432438

2444-
protected virtual bool? EvaluateChar(string expression, Stack<object> stack, ref int i)
2439+
protected virtual bool EvaluateChar(string expression, Stack<object> stack, ref int i)
24452440
{
24462441
if (!OptionCharEvaluationActive)
24472442
return false;
@@ -2492,7 +2487,7 @@ void Init(object element, List<string> initArgs)
24922487
}
24932488
}
24942489

2495-
protected virtual bool? EvaluateOperators(string expression, Stack<object> stack, ref int i)
2490+
protected virtual bool EvaluateOperators(string expression, Stack<object> stack, ref int i)
24962491
{
24972492
string regexPattern = "^(" + string.Join("|", operatorsDictionary
24982493
.Keys
@@ -2512,7 +2507,7 @@ void Init(object element, List<string> initArgs)
25122507
return false;
25132508
}
25142509

2515-
protected virtual bool? EvaluateTernaryConditionalOperator(string expression, Stack<object> stack, ref int i)
2510+
protected virtual bool EvaluateTernaryConditionalOperator(string expression, Stack<object> stack, ref int i)
25162511
{
25172512
if (expression.Substring(i, 1).Equals("?"))
25182513
{
@@ -2552,7 +2547,7 @@ void Init(object element, List<string> initArgs)
25522547
return false;
25532548
}
25542549

2555-
protected virtual bool? EvaluateParenthis(string expression, Stack<object> stack, ref int i)
2550+
protected virtual bool EvaluateParenthis(string expression, Stack<object> stack, ref int i)
25562551
{
25572552
string s = expression.Substring(i, 1);
25582553

@@ -2603,7 +2598,7 @@ protected virtual void CorrectStackWithUnaryPlusOrMinusBeforeParenthisIfNecessar
26032598
}
26042599
}
26052600

2606-
protected virtual bool? EvaluateIndexing(string expression, Stack<object> stack, ref int i)
2601+
protected virtual bool EvaluateIndexing(string expression, Stack<object> stack, ref int i)
26072602
{
26082603
if (!OptionIndexingActive)
26092604
return false;
@@ -2731,7 +2726,7 @@ protected virtual void CorrectStackWithUnaryPlusOrMinusBeforeParenthisIfNecessar
27312726
return false;
27322727
}
27332728

2734-
protected virtual bool? EvaluateString(string expression, Stack<object> stack, ref int i)
2729+
protected virtual bool EvaluateString(string expression, Stack<object> stack, ref int i)
27352730
{
27362731
if (!OptionStringEvaluationActive)
27372732
return false;
@@ -3013,7 +3008,7 @@ public string RemoveComments(string scriptWithComments)
30133008

30143009
#region Utils methods for parsing and interpretation
30153010

3016-
protected delegate bool? ParsingMethodDelegate(string expression, Stack<object> stack, ref int i);
3011+
protected delegate bool ParsingMethodDelegate(string expression, Stack<object> stack, ref int i);
30173012

30183013
protected delegate dynamic InternalDelegate(params dynamic[] args);
30193014

0 commit comments

Comments
 (0)