3
3
using System . Collections . Generic ;
4
4
using System . Text . RegularExpressions ;
5
5
using Shouldly ;
6
+ using Newtonsoft . Json ;
6
7
7
8
namespace CodingSeb . ExpressionEvaluator . Tests
8
9
{
@@ -103,12 +104,21 @@ public void TypeTesting(string expression, Type type)
103
104
[ TestCase ( "\" Hello World\" " , TestOf = typeof ( string ) , ExpectedResult = "Hello World" , Category = "SimpleString" ) ]
104
105
[ TestCase ( "\" Hello\" + \" World\" " , TestOf = typeof ( string ) , ExpectedResult = "HelloWorld" , Category = "SimpleString" ) ]
105
106
107
+ [ TestCase ( "\" \\ \" \" " , TestOf = typeof ( string ) , ExpectedResult = "\" " , Category = "StringEscape" ) ]
108
+ [ TestCase ( "\" \\ n\" " , TestOf = typeof ( string ) , ExpectedResult = "\n " , Category = "StringEscape" ) ]
109
+ [ TestCase ( "\" \\ r\" " , TestOf = typeof ( string ) , ExpectedResult = "\r " , Category = "StringEscape" ) ]
110
+ [ TestCase ( "\" \\ t\" " , TestOf = typeof ( string ) , ExpectedResult = "\t " , Category = "StringEscape" ) ]
111
+ [ TestCase ( "\" " + @"\\" + "\" " , TestOf = typeof ( string ) , ExpectedResult = @"\" , Category = "StringEscape" ) ]
106
112
[ TestCase ( "\" " + @"\\\n" + "\" " , TestOf = typeof ( string ) , ExpectedResult = "\\ \n " , Category = "StringEscape" ) ]
107
113
[ TestCase ( "@\" " + @"\\n" + "\" " , TestOf = typeof ( string ) , ExpectedResult = @"\\n" , Category = "StringEscape" ) ]
108
114
109
115
[ TestCase ( "$\" Hello {1 + 2}\" " , TestOf = typeof ( string ) , ExpectedResult = "Hello 3" , Category = "StringInterpolation" ) ]
116
+ [ TestCase ( "$\" {'\" '}\" " , TestOf = typeof ( string ) , ExpectedResult = "\" " , Category = "StringInterpolation" ) ]
117
+ [ TestCase ( "$\" { '\" ' }\" " , TestOf = typeof ( string ) , ExpectedResult = "\" " , Category = "StringInterpolation" ) ]
118
+ [ TestCase ( "$\" {{\" " , TestOf = typeof ( string ) , ExpectedResult = "{" , Category = "StringInterpolation" ) ]
119
+ [ TestCase ( "$\" { \" {\" }\" " , TestOf = typeof ( string ) , ExpectedResult = "{" , Category = "StringInterpolation" ) ]
110
120
[ TestCase ( "$\" Test { 5+5 } Test\" " , TestOf = typeof ( string ) , ExpectedResult = "Test 10 Test" , Category = "StringInterpolation" ) ]
111
- [ TestCase ( "$\" Test { 5+5 + \" Test\" } Test\" " , TestOf = typeof ( string ) , ExpectedResult = "Test 10 Test Test" , Category = "StringInterpolation" ) ]
121
+ [ TestCase ( "$\" Test { 5+5 + \" Test\" } Test\" " , TestOf = typeof ( string ) , ExpectedResult = "Test 10 Test Test" , Category = "StringInterpolation" ) ]
112
122
[ TestCase ( "$\" Test { 5+5 + \" Test{\" } Test\" " , TestOf = typeof ( string ) , ExpectedResult = "Test 10 Test{ Test" , Category = "StringInterpolation" ) ]
113
123
[ TestCase ( "$\" Test { 5+5 + \" Test{{ }\" } Test\" " , TestOf = typeof ( string ) , ExpectedResult = "Test 10 Test{{ } Test" , Category = "StringInterpolation" ) ]
114
124
@@ -133,9 +143,9 @@ public void TypeTesting(string expression, Type type)
133
143
[ TestCase ( "\" Text()\" .Replace(\" (\" , \" ,\" )" , TestOf = typeof ( string ) , ExpectedResult = "Text,)" , Category = "StringWithParenthisOrComaInFunctionsArgs" ) ]
134
144
135
145
[ TestCase ( "\" Hello,Test,What\" .Split(ArrayOfType(typeof(char), ',')).Length" , ExpectedResult = 3 , Category = "StringSplit,ArrayOfType" ) ]
136
- [ TestCase ( "\" Hello,Test,What\" .Split(ArrayOfType(typeof(char), ','))[0] " , ExpectedResult = "Hello" , Category = "StringSplit,ArrayOfType" ) ]
137
- [ TestCase ( "\" Hello,Test,What\" .Split(ArrayOfType(typeof(char), ','))[1] " , ExpectedResult = "Test" , Category = "StringSplit,ArrayOfType " ) ]
138
- [ TestCase ( "\" Hello,Test,What\" .Split(ArrayOfType(typeof(char), ','))[2] " , ExpectedResult = "What" , Category = "StringSplit,ArrayOfType " ) ]
146
+ [ TestCase ( "\" Hello,Test,What\" .Split(ArrayOfType(typeof(char), ',')).Json " , ExpectedResult = "[ \" Hello\" , \" Test \" , \" What \" ] " , Category = "StringSplit,ArrayOfType" ) ]
147
+ [ TestCase ( "\" Hello,Test,What\" .Split(new char[]{ ','}).Length " , ExpectedResult = 3 , Category = "StringSplit,Array instanciation " ) ]
148
+ [ TestCase ( "\" Hello,Test,What\" .Split(new char[]{ ','}).Json " , ExpectedResult = "[ \" Hello \" , \" Test \" , \" What\" ]" , Category = "StringSplit,Array instanciation " ) ]
139
149
#endregion
140
150
141
151
#region char
@@ -162,6 +172,7 @@ public void TypeTesting(string expression, Type type)
162
172
[ TestCase ( @"'\r'" , TestOf = typeof ( char ) , ExpectedResult = '\r ' , Category = "char" ) ]
163
173
[ TestCase ( @"'\t'" , TestOf = typeof ( char ) , ExpectedResult = '\t ' , Category = "char" ) ]
164
174
[ TestCase ( @"'\v'" , TestOf = typeof ( char ) , ExpectedResult = '\v ' , Category = "char" ) ]
175
+ [ TestCase ( "'\" '" , TestOf = typeof ( char ) , ExpectedResult = '"' , Category = "char" ) ]
165
176
[ TestCase ( "\" hello\" + ' ' + '!'" , ExpectedResult = "hello !" , Category = "char" ) ]
166
177
[ TestCase ( "(int)'a'" , ExpectedResult = 97 , Category = "char" ) ]
167
178
[ TestCase ( "'a'.CompareTo('b')" , ExpectedResult = - 1 , Category = "char" ) ]
@@ -860,9 +871,15 @@ public object DirectExpressionEvaluation(string expression)
860
871
{
861
872
ExpressionEvaluator evaluator = new ExpressionEvaluator ( ) ;
862
873
874
+ evaluator . EvaluateVariable += Evaluator_EvaluateVariable ;
875
+
863
876
evaluator . Namespaces . Add ( "CodingSeb.ExpressionEvaluator.Tests" ) ;
864
877
865
- return evaluator . Evaluate ( expression ) ;
878
+ object result = evaluator . Evaluate ( expression ) ;
879
+
880
+ evaluator . EvaluateVariable -= Evaluator_EvaluateVariable ;
881
+
882
+ return result ;
866
883
}
867
884
868
885
#endregion
@@ -1088,6 +1105,10 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
1088
1105
{
1089
1106
e . Value = 8 ;
1090
1107
}
1108
+ else if ( e . This != null && e . Name . Equals ( "Json" ) )
1109
+ {
1110
+ e . Value = JsonConvert . SerializeObject ( e . This ) ;
1111
+ }
1091
1112
}
1092
1113
1093
1114
#endregion
0 commit comments