33using System . Collections . Generic ;
44using System . Text . RegularExpressions ;
55using Shouldly ;
6+ using Newtonsoft . Json ;
67
78namespace CodingSeb . ExpressionEvaluator . Tests
89{
@@ -103,12 +104,21 @@ public void TypeTesting(string expression, Type type)
103104 [ TestCase ( "\" Hello World\" " , TestOf = typeof ( string ) , ExpectedResult = "Hello World" , Category = "SimpleString" ) ]
104105 [ TestCase ( "\" Hello\" + \" World\" " , TestOf = typeof ( string ) , ExpectedResult = "HelloWorld" , Category = "SimpleString" ) ]
105106
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" ) ]
106112 [ TestCase ( "\" " + @"\\\n" + "\" " , TestOf = typeof ( string ) , ExpectedResult = "\\ \n " , Category = "StringEscape" ) ]
107113 [ TestCase ( "@\" " + @"\\n" + "\" " , TestOf = typeof ( string ) , ExpectedResult = @"\\n" , Category = "StringEscape" ) ]
108114
109115 [ 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" ) ]
110120 [ 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" ) ]
112122 [ TestCase ( "$\" Test { 5+5 + \" Test{\" } Test\" " , TestOf = typeof ( string ) , ExpectedResult = "Test 10 Test{ Test" , Category = "StringInterpolation" ) ]
113123 [ TestCase ( "$\" Test { 5+5 + \" Test{{ }\" } Test\" " , TestOf = typeof ( string ) , ExpectedResult = "Test 10 Test{{ } Test" , Category = "StringInterpolation" ) ]
114124
@@ -133,9 +143,9 @@ public void TypeTesting(string expression, Type type)
133143 [ TestCase ( "\" Text()\" .Replace(\" (\" , \" ,\" )" , TestOf = typeof ( string ) , ExpectedResult = "Text,)" , Category = "StringWithParenthisOrComaInFunctionsArgs" ) ]
134144
135145 [ 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 " ) ]
139149 #endregion
140150
141151 #region char
@@ -162,6 +172,7 @@ public void TypeTesting(string expression, Type type)
162172 [ TestCase ( @"'\r'" , TestOf = typeof ( char ) , ExpectedResult = '\r ' , Category = "char" ) ]
163173 [ TestCase ( @"'\t'" , TestOf = typeof ( char ) , ExpectedResult = '\t ' , Category = "char" ) ]
164174 [ TestCase ( @"'\v'" , TestOf = typeof ( char ) , ExpectedResult = '\v ' , Category = "char" ) ]
175+ [ TestCase ( "'\" '" , TestOf = typeof ( char ) , ExpectedResult = '"' , Category = "char" ) ]
165176 [ TestCase ( "\" hello\" + ' ' + '!'" , ExpectedResult = "hello !" , Category = "char" ) ]
166177 [ TestCase ( "(int)'a'" , ExpectedResult = 97 , Category = "char" ) ]
167178 [ TestCase ( "'a'.CompareTo('b')" , ExpectedResult = - 1 , Category = "char" ) ]
@@ -860,9 +871,15 @@ public object DirectExpressionEvaluation(string expression)
860871 {
861872 ExpressionEvaluator evaluator = new ExpressionEvaluator ( ) ;
862873
874+ evaluator . EvaluateVariable += Evaluator_EvaluateVariable ;
875+
863876 evaluator . Namespaces . Add ( "CodingSeb.ExpressionEvaluator.Tests" ) ;
864877
865- return evaluator . Evaluate ( expression ) ;
878+ object result = evaluator . Evaluate ( expression ) ;
879+
880+ evaluator . EvaluateVariable -= Evaluator_EvaluateVariable ;
881+
882+ return result ;
866883 }
867884
868885 #endregion
@@ -1088,6 +1105,10 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
10881105 {
10891106 e . Value = 8 ;
10901107 }
1108+ else if ( e . This != null && e . Name . Equals ( "Json" ) )
1109+ {
1110+ e . Value = JsonConvert . SerializeObject ( e . This ) ;
1111+ }
10911112 }
10921113
10931114 #endregion
0 commit comments