Skip to content

Commit 113037d

Browse files
authored
Merge pull request #92 from codingseb/dev
Dev
2 parents 46a4443 + 1307602 commit 113037d

File tree

3 files changed

+348
-92
lines changed

3 files changed

+348
-92
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

+53-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Shouldly;
44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Text.RegularExpressions;
78

89
namespace CodingSeb.ExpressionEvaluator.Tests
@@ -985,6 +986,12 @@ public void TypeTesting(string expression, Type type)
985986

986987
#endregion
987988

989+
#region Bugs correction
990+
991+
[TestCase("new DateTime(1985,9,11).ToString(\"dd.MM.yyyy\")", ExpectedResult = "11.09.1985", Category = "Complex expression,Static method,Instance method,Lambda function,Cast")]
992+
993+
#endregion
994+
988995
#endregion
989996
public object DirectExpressionEvaluation(string expression)
990997
{
@@ -1277,9 +1284,9 @@ private void Evaluator_EvaluateVariable(object sender, VariableEvaluationEventAr
12771284
}
12781285
}
12791286

1280-
[TestCase("ClassForTest1.Add(1, 5)", ExpectedResult = 6, Category = "On the fly method")]
1281-
[TestCase("ClassForTest1.Add(1, 5.0)", ExpectedResult = 6, Category = "On the fly method")]
1282-
public object OnTheFlyEvaluation2(string expression)
1287+
[TestCase("ClassForTest1.Add(1, 5)", ExpectedResult = 6)]
1288+
[TestCase("ClassForTest1.Add(1, 5.0)", ExpectedResult = 6)]
1289+
public object OnTheFlyCastEvaluation(string expression)
12831290
{
12841291
ExpressionEvaluator evaluator = new ExpressionEvaluator(new ContextObject1());
12851292

@@ -1303,6 +1310,27 @@ private void Evaluator_EvaluateParameterCast(object sender, ParameterCastEvaluat
13031310
}
13041311
}
13051312

1313+
[TestCase("2[\"Test\"]", ExpectedResult = "Test,Test")]
1314+
[TestCase("3[\"Hello\"]", ExpectedResult = "Hello,Hello,Hello")]
1315+
public object OnTheFlyIndexingEvaluation(string expression)
1316+
{
1317+
ExpressionEvaluator evaluator = new ExpressionEvaluator(new ContextObject1());
1318+
1319+
evaluator.PreEvaluateIndexing += Evaluator_PreEvaluateIndexing;
1320+
1321+
evaluator.Namespaces.Add("CodingSeb.ExpressionEvaluator.Tests");
1322+
1323+
return evaluator.Evaluate(expression);
1324+
}
1325+
1326+
private void Evaluator_PreEvaluateIndexing(object sender, IndexingPreEvaluationEventArg e)
1327+
{
1328+
if(e.This is int intValue && e.EvaluateArg() is string text)
1329+
{
1330+
e.Value = string.Join(",", Enumerable.Repeat(text, intValue));
1331+
}
1332+
}
1333+
13061334
#endregion
13071335

13081336
#endregion
@@ -1492,6 +1520,7 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
14921520
{ "P1var", "P1" },
14931521
{ "myObj", new ClassForTest1() },
14941522
{ "nullVar", null },
1523+
{ "myArray", new int[] {1, 2, 3} },
14951524
});
14961525

14971526
evaluator.PreEvaluateVariable += (sender, e) =>
@@ -1506,11 +1535,18 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
15061535
e.CancelEvaluation = true;
15071536
};
15081537

1538+
evaluator.PreEvaluateIndexing += (sender, e) =>
1539+
{
1540+
if (e.This is int[])
1541+
e.CancelEvaluation = true;
1542+
};
1543+
15091544
yield return new TestCaseData(evaluator, "Pi", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
15101545
yield return new TestCaseData(evaluator, "P1var", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
15111546
yield return new TestCaseData(evaluator, "myObj.PropertyThatWillFailed", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
15121547
yield return new TestCaseData(evaluator, "myObj.Add3To(5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
15131548
yield return new TestCaseData(evaluator, "Abs(-5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
1549+
yield return new TestCaseData(evaluator, "myArray[1]", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFlyCanceledIndexing");
15141550
#endregion
15151551

15161552
#region Bugs corrections
@@ -1529,7 +1565,20 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
15291565
[TestCaseSource(nameof(TestCasesForExceptionThrowingEvaluation))]
15301566
public void ExceptionThrowingEvaluation(ExpressionEvaluator evaluator, string expression, Type exceptionType)
15311567
{
1532-
Assert.Catch(exceptionType, () => evaluator.Evaluate(expression));
1568+
Exception e = null;
1569+
object result = null;
1570+
1571+
try
1572+
{
1573+
result = evaluator.Evaluate(expression);
1574+
}
1575+
catch(Exception exception)
1576+
{
1577+
e = exception;
1578+
}
1579+
1580+
result.ShouldBeNull();
1581+
e.ShouldNotBeNull().ShouldBeOfType(exceptionType);
15331582
}
15341583

15351584
#endregion

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<Product>CodingSeb.ExpressionEvaluator</Product>
66
<Description>A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts</Description>
77
<Copyright>Copyright © Coding Seb 2017</Copyright>
8-
<Version>1.4.23.0</Version>
9-
<AssemblyVersion>1.4.23.0</AssemblyVersion>
10-
<FileVersion>1.4.23.0</FileVersion>
8+
<Version>1.4.24.0</Version>
9+
<AssemblyVersion>1.4.24.0</AssemblyVersion>
10+
<FileVersion>1.4.24.0</FileVersion>
1111
<OutputPath>bin\$(Configuration)\</OutputPath>
1212
<Authors>Coding Seb</Authors>
1313
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
@@ -19,8 +19,9 @@
1919
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2020
<PackageIcon>Icon.png</PackageIcon>
2121
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
22-
<PackageReleaseNotes>* Add the option OptionCanDeclareMultiExpressionsLambdaInSimpleExpressionEvaluate (default is true)
23-
* Allow to declare and call multi expressions lambda in simple expression Evaluate</PackageReleaseNotes>
22+
<PackageReleaseNotes>* Add an event to customize the cast of a method argument
23+
* Add 2 events ExpressionEvaluating and ExpressionEvaluated to hook easily each expression evaluation. Allow to modify expression to evaluate and the corresponding result.
24+
* Correction of a bug in method call selection (dateTime.ToString(format) work again)</PackageReleaseNotes>
2425
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2526
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
2627
</PropertyGroup>

0 commit comments

Comments
 (0)