Skip to content

Commit 148f392

Browse files
committed
Test String Split the CSharp way
1 parent 769eca6 commit 148f392

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Text.RegularExpressions;
55
using Shouldly;
6+
using Newtonsoft.Json;
67

78
namespace CodingSeb.ExpressionEvaluator.Tests
89
{
@@ -133,9 +134,9 @@ public void TypeTesting(string expression, Type type)
133134
[TestCase("\"Text()\".Replace(\"(\", \",\")", TestOf = typeof(string), ExpectedResult = "Text,)", Category = "StringWithParenthisOrComaInFunctionsArgs")]
134135

135136
[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")]
139140
#endregion
140141

141142
#region char
@@ -860,9 +861,15 @@ public object DirectExpressionEvaluation(string expression)
860861
{
861862
ExpressionEvaluator evaluator = new ExpressionEvaluator();
862863

864+
evaluator.EvaluateVariable += Evaluator_EvaluateVariable;
865+
863866
evaluator.Namespaces.Add("CodingSeb.ExpressionEvaluator.Tests");
864867

865-
return evaluator.Evaluate(expression);
868+
object result = evaluator.Evaluate(expression);
869+
870+
evaluator.EvaluateVariable -= Evaluator_EvaluateVariable;
871+
872+
return result;
866873
}
867874

868875
#endregion
@@ -1088,6 +1095,10 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
10881095
{
10891096
e.Value = 8;
10901097
}
1098+
else if (e.This != null && e.Name.Equals("Json"))
1099+
{
1100+
e.Value = JsonConvert.SerializeObject(e.This);
1101+
}
10911102
}
10921103

10931104
#endregion

0 commit comments

Comments
 (0)