|
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 | { |
@@ -133,9 +134,9 @@ public void TypeTesting(string expression, Type type) |
133 | 134 | [TestCase("\"Text()\".Replace(\"(\", \",\")", TestOf = typeof(string), ExpectedResult = "Text,)", Category = "StringWithParenthisOrComaInFunctionsArgs")] |
134 | 135 |
|
135 | 136 | [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")] |
| 137 | + [TestCase("\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ',')).Json", ExpectedResult = "[\"Hello\",\"Test\",\"What\"]", Category = "StringSplit,ArrayOfType")] |
| 138 | + [TestCase("\"Hello,Test,What\".Split(new char[]{','}).Length", ExpectedResult = 3, Category = "StringSplit,Array instanciation")] |
| 139 | + [TestCase("\"Hello,Test,What\".Split(new char[]{','}).Json", ExpectedResult = "[\"Hello\",\"Test\",\"What\"]", Category = "StringSplit,Array instanciation")] |
139 | 140 | #endregion |
140 | 141 |
|
141 | 142 | #region char |
@@ -860,9 +861,15 @@ public object DirectExpressionEvaluation(string expression) |
860 | 861 | { |
861 | 862 | ExpressionEvaluator evaluator = new ExpressionEvaluator(); |
862 | 863 |
|
| 864 | + evaluator.EvaluateVariable += Evaluator_EvaluateVariable; |
| 865 | + |
863 | 866 | evaluator.Namespaces.Add("CodingSeb.ExpressionEvaluator.Tests"); |
864 | 867 |
|
865 | | - return evaluator.Evaluate(expression); |
| 868 | + object result = evaluator.Evaluate(expression); |
| 869 | + |
| 870 | + evaluator.EvaluateVariable -= Evaluator_EvaluateVariable; |
| 871 | + |
| 872 | + return result; |
866 | 873 | } |
867 | 874 |
|
868 | 875 | #endregion |
@@ -1088,6 +1095,10 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr |
1088 | 1095 | { |
1089 | 1096 | e.Value = 8; |
1090 | 1097 | } |
| 1098 | + else if (e.This != null && e.Name.Equals("Json")) |
| 1099 | + { |
| 1100 | + e.Value = JsonConvert.SerializeObject(e.This); |
| 1101 | + } |
1091 | 1102 | } |
1092 | 1103 |
|
1093 | 1104 | #endregion |
|
0 commit comments