Skip to content

Commit e11734b

Browse files
author
Sébastien Geiser
committed
Tests for custom parsing (complex operators)
1 parent c73044c commit e11734b

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,8 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
17401740

17411741
#region inherits ExpressionEvaluator
17421742

1743+
#region Redefine existing operators
1744+
17431745
ExpressionEvaluator xExpressionEvaluator1 = new XExpressionEvaluator1();
17441746

17451747
yield return new TestCaseData(xExpressionEvaluator1
@@ -1832,6 +1834,10 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
18321834
.SetCategory("inherits ExpressionEvaluator")
18331835
.SetCategory("Custom operators");
18341836

1837+
#endregion
1838+
1839+
#region Add your own simple operators
1840+
18351841
ExpressionEvaluator xExpressionEvaluator2 = new XExpressionEvaluator2();
18361842

18371843
yield return new TestCaseData(xExpressionEvaluator2
@@ -1871,6 +1877,54 @@ void Evaluator_PreEvaluateVariable(object sender, VariablePreEvaluationEventArg
18711877

18721878
#endregion
18731879

1880+
#region Add a complex operator or change the parsing process
1881+
1882+
ExpressionEvaluator xExpressionEvaluator3 = new XExpressionEvaluator3();
1883+
1884+
yield return new TestCaseData(xExpressionEvaluator3
1885+
, "\"A sentence where a word must be replaced where it is\" ° \"replaced\" @ \"kept\"", null)
1886+
.Returns("A sentence where a word must be kept where it is")
1887+
.SetCategory("ExpressionEvaluator extend")
1888+
.SetCategory("inherits ExpressionEvaluator")
1889+
.SetCategory("Custom operators")
1890+
.SetCategory("Custom parsing");
1891+
1892+
yield return new TestCaseData(xExpressionEvaluator3
1893+
, "#1985-09-11.Year", null)
1894+
.Returns(1985)
1895+
.SetCategory("ExpressionEvaluator extend")
1896+
.SetCategory("inherits ExpressionEvaluator")
1897+
.SetCategory("Custom syntax")
1898+
.SetCategory("Custom parsing");
1899+
1900+
yield return new TestCaseData(xExpressionEvaluator3
1901+
, "#1985-09-11.Month", null)
1902+
.Returns(9)
1903+
.SetCategory("ExpressionEvaluator extend")
1904+
.SetCategory("inherits ExpressionEvaluator")
1905+
.SetCategory("Custom syntax")
1906+
.SetCategory("Custom parsing");
1907+
1908+
yield return new TestCaseData(xExpressionEvaluator3
1909+
, "#1985-09-11.Day", null)
1910+
.Returns(11)
1911+
.SetCategory("ExpressionEvaluator extend")
1912+
.SetCategory("inherits ExpressionEvaluator")
1913+
.SetCategory("Custom syntax")
1914+
.SetCategory("Custom parsing");
1915+
1916+
yield return new TestCaseData(xExpressionEvaluator3
1917+
, "#1985-09-11.Equals(new DateTime(1985,9,11))", null)
1918+
.Returns(true)
1919+
.SetCategory("ExpressionEvaluator extend")
1920+
.SetCategory("inherits ExpressionEvaluator")
1921+
.SetCategory("Custom syntax")
1922+
.SetCategory("Custom parsing");
1923+
1924+
#endregion
1925+
1926+
#endregion
1927+
18741928
#region bug resolution
18751929

18761930
yield return new TestCaseData(new ExpressionEvaluator()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text.RegularExpressions;
4+
5+
namespace CodingSeb.ExpressionEvaluator.Tests
6+
{
7+
public class XExpressionEvaluator3 : ExpressionEvaluator
8+
{
9+
protected override void Init()
10+
{
11+
ParsingMethods.Insert(0, EvaluateDateTimeSyntax);
12+
ParsingMethods.Add(EvaluateSpecialTernaryOperator);
13+
}
14+
15+
/// <summary>
16+
/// To evaluate DateTimes objects with #year-month-day syntax (#2019-05-28)
17+
/// </summary>
18+
protected virtual bool EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
19+
{
20+
Match match = Regex.Match(expression.Substring(i), @"^\s*#(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})");
21+
22+
if(match.Success)
23+
{
24+
int year = int.Parse(match.Groups["year"].Value);
25+
int month = int.Parse(match.Groups["month"].Value);
26+
int day = int.Parse(match.Groups["day"].Value);
27+
28+
DateTime dateTime = new DateTime(year,month, day);
29+
30+
stack.Push(dateTime);
31+
32+
i += match.Length - 1;
33+
34+
return true;
35+
}
36+
37+
return false;
38+
}
39+
40+
/// <summary>
41+
/// To evaluate a string replace with custom ternary indicator
42+
/// </summary>
43+
protected virtual bool EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
44+
{
45+
if (expression.Substring(i, 1).Equals("°"))
46+
{
47+
string input = (string)ProcessStack(stack);
48+
49+
string restOfExpression = expression.Substring(i + 1);
50+
51+
for (int j = 0; j < restOfExpression.Length; j++)
52+
{
53+
string s2 = restOfExpression.Substring(j, 1);
54+
55+
Match internalStringMatch = stringBeginningRegex.Match(restOfExpression.Substring(j));
56+
57+
if (internalStringMatch.Success)
58+
{
59+
string innerString = internalStringMatch.Value + GetCodeUntilEndOfString(restOfExpression.Substring(j + internalStringMatch.Length), internalStringMatch);
60+
j += innerString.Length - 1;
61+
}
62+
else if (s2.Equals("("))
63+
{
64+
j++;
65+
GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(restOfExpression, ref j, false);
66+
}
67+
else if (s2.Equals("@"))
68+
{
69+
stack.Clear();
70+
71+
stack.Push(input.Replace((string)Evaluate(restOfExpression.Substring(1, j - 1)), (string)Evaluate(restOfExpression.Substring(j + 1))));
72+
73+
i = expression.Length;
74+
75+
return true;
76+
}
77+
}
78+
}
79+
80+
return false;
81+
}
82+
}
83+
}

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,9 +2311,9 @@ protected virtual bool EvaluateTernaryConditionalOperator(string expression, Sta
23112311
{
23122312
bool condition = (bool)ProcessStack(stack);
23132313

2314-
string restOfExpression = expression.Substring(i);
2314+
string restOfExpression = expression.Substring(i + 1);
23152315

2316-
for (int j = 1; j < restOfExpression.Length; j++)
2316+
for (int j = 0; j < restOfExpression.Length; j++)
23172317
{
23182318
string s2 = restOfExpression.Substring(j, 1);
23192319

0 commit comments

Comments
 (0)