-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionEvaluatorTEst.cs
112 lines (101 loc) · 3.96 KB
/
ExpressionEvaluatorTEst.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using NUnit.Framework;
using System.Collections.Generic;
namespace ExpressionEvaluator.Test
{
[TestFixture]
public class ExpressionEvaluatorTest
{
[Test]
public void Evaluate_NullExpression()
{
Assert.IsNull(ExpressionEvaluator.Evaluate(null));
}
[TestCase("false", false)]
[TestCase("true", true)]
[TestCase("True", true)]
[TestCase("not true", false)]
[TestCase("false OR true", true)]
[TestCase("false AND true", false)]
[TestCase("1 < 2", true)]
[TestCase("5 = 5", true)]
[TestCase("5 <> 5", false)]
public void Evaluate_Bool(string expression, bool expectedResult)
{
Assert.AreEqual(expectedResult, ExpressionEvaluator.Evaluate(expression));
}
[TestCase("(1 + 2) * 2", 6)]
public void Evaluate_Numeric(string expression, int expectedResult)
{
Assert.AreEqual(expectedResult, ExpressionEvaluator.Evaluate(expression));
}
[TestCase("(1 + 2) * 2", 6)]
[TestCase("7 / 2", 4)]
public void Evaluate_Int(string expression, int expectedResult)
{
Assert.AreEqual(expectedResult, ExpressionEvaluator.Evaluate<int>(expression));
}
[TestCase("7 / 2", 3.5)]
public void Evaluate_Double(string expression, double expectedResult)
{
Assert.AreEqual(expectedResult, ExpressionEvaluator.Evaluate<double>(expression));
}
[TestCase("1 && 0", true, " true and false ")]
[TestCase("1 && 0", false, "true and false")]
[TestCase("1 && 0 || 1", true, " true and false or true ")]
[TestCase("1 && 0 || 1", false, "true and false or true")]
[TestCase("0 && !1", true, " false and not true ")]
[TestCase("0 && !1", false, "false and not true")]
[TestCase("(!0 && 1) || 1", true, "( not false and true ) or true ")]
[TestCase("(!0 && 1) || 1", false, "( not false and true ) or true")]
[TestCase("(!0 != 1) || 1", true, "( not false <> true ) or true ")]
[TestCase("!0", false, "not false")]
[TestCase(" !0", false, "not false")]
[TestCase("!0 ", false, "not false")]
[TestCase("(!0)", false, "( not false )")]
public void ReplaceOperators(string expression, bool allowExtraSpaces, string expectedResult)
{
ExpressionEvaluator.SetOperatorMap(new Dictionary<string, string>
{
{"and", "&&"},
{"or", "||"},
{"<>", "!=" },
{"not", "!" },
{"true", "1" },
{"false", "0" }
});
Assert.AreEqual(expectedResult,
ExpressionEvaluator.ReplaceOperators(expression, allowExtraSpaces));
}
[TestCase("1 && 0", false)]
[TestCase("1 && 0 || 1", true)]
[TestCase("0 && !1", false)]
[TestCase("(!0 && 1) || 1", true)]
public void SetOperatorMap_Evaluate_Bool(string expression, bool expectedResult)
{
ExpressionEvaluator.SetOperatorMap(new Dictionary<string, string>
{
{"and", "&&"},
{"or", "||"},
{"not", "!" },
{"<>", "!=" },
{"true", "1" },
{"false", "0" }
});
Assert.AreEqual(expectedResult,
ExpressionEvaluator.Evaluate<bool>(expression));
}
[TestCase("(1 plus 2) times 2", 6)]
[TestCase("7 divideBy 2", 4)]
public void SetOperatorMap_Evaluate_Int(string expression, int expectedResult)
{
ExpressionEvaluator.SetOperatorMap(new Dictionary<string, string>
{
{"+", "plus"},
{"*", "times"},
{"/", "divideBy"},
});
Assert.AreEqual(expectedResult,
ExpressionEvaluator.Evaluate<int>(expression));
}
}
}