Skip to content

Commit e2601a0

Browse files
authored
Merge pull request #61 from codingseb/dev
Dev #56
2 parents b9bc766 + fc7a8b6 commit e2601a0

File tree

8 files changed

+233
-149
lines changed

8 files changed

+233
-149
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,11 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
14811481
.SetCategory("OptionScriptNeedSemicolonAtTheEndOfLastExpression")
14821482
.Returns("0,1,2,3,4,");
14831483

1484+
yield return new TestCaseData(Resources.Script0070, new ExpressionEvaluator { OptionScriptNeedSemicolonAtTheEndOfLastExpression = false }, null, null, null)
1485+
.SetCategory("Script")
1486+
.SetCategory("new Exception must not throw the exception")
1487+
.Returns(3);
1488+
14841489
#endregion
14851490
}
14861491
}

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

+36-23
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,10 @@ public void TypeTesting(string expression, Type type)
800800
[TestCase("ListOfType(typeof(int), 1,2,3 )[0]", ExpectedResult = 1, Category = "Standard Functions,ListOfType Function,Indexing")]
801801
[TestCase("ListOfType(typeof(int), 1,2,3 )[1]", ExpectedResult = 2, Category = "Standard Functions,ListOfType Function,Indexing")]
802802
[TestCase("ListOfType(typeof(int), 1,2,3 )[2]", ExpectedResult = 3, Category = "Standard Functions,ListOfType Function,Indexing")]
803-
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" ).GetType()", ExpectedResult = typeof(List<string>), Category = "Standard Functions,ListOfType Function,Instance Property")]
804-
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" ).Count", ExpectedResult = 2, Category = "Standard Functions,ListOfType Function,Instance Property")]
805-
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" )[0]", ExpectedResult = "hello", Category = "Standard Functions,ListOfType Function,Indexing")]
806-
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\" )[1]", ExpectedResult = "Test", Category = "Standard Functions,ListOfType Function,Indexing")]
803+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\").GetType()", ExpectedResult = typeof(List<string>), Category = "Standard Functions,ListOfType Function,Instance Property")]
804+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\").Count", ExpectedResult = 2, Category = "Standard Functions,ListOfType Function,Instance Property")]
805+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\")[0]", ExpectedResult = "hello", Category = "Standard Functions,ListOfType Function,Indexing")]
806+
[TestCase("ListOfType(typeof(string), \"hello\",\"Test\")[1]", ExpectedResult = "Test", Category = "Standard Functions,ListOfType Function,Indexing")]
807807
#endregion
808808

809809
#region Log Function
@@ -1111,23 +1111,16 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
11111111
yield return new TestCaseData("nullVar?[1][3]", onInstanceVariables, true).SetCategory("Instance Property,Null Conditional indexing").Returns(null);
11121112
yield return new TestCaseData("simpleArray2?[3]?.Trim()", onInstanceVariables, true).SetCategory("Instance Property,Null Conditional indexing").Returns(null);
11131113

1114-
yield return new TestCaseData( "false && 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,And Conditional" ).Returns( false );
1115-
yield return new TestCaseData( "!string.IsNullOrEmpty(nullVar) && nullVar.StartsWith(\"ABC\")", onInstanceVariables, true ).SetCategory( "Instance Property,And Conditional" ).Returns( false );
1116-
yield return new TestCaseData( "string.IsNullOrEmpty(nullVar) || nullVar.StartsWith(\"ABC\")", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional" ).Returns( true );
1117-
yield return new TestCaseData( "true || 1/0 == 0", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional" ).Returns( true );
1118-
yield return new TestCaseData( "false && true || true", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional,And Conditional,Precedence check" ).Returns( true );
1119-
yield return new TestCaseData( "true || true && false", onInstanceVariables, true ).SetCategory( "Instance Property,Or Conditional,And Conditional,Precedence check" ).Returns( true );
1120-
1121-
yield return new TestCaseData("simpleInt.ToString()", onInstanceVariables, true).SetCategory("Instance Method").Returns("42");
1122-
yield return new TestCaseData("simpleInt.ToString().Length", onInstanceVariables, true).SetCategory("Instance Method,Instance Property").Returns(2);
1123-
11241114
yield return new TestCaseData("customObject.IntProperty", onInstanceVariables, true).SetCategory("Instance Property").Returns(25);
11251115
yield return new TestCaseData("customObject?.IntProperty", onInstanceVariables, true).SetCategory("Instance Property").Returns(25);
11261116
yield return new TestCaseData("customObject.intField", onInstanceVariables, true).SetCategory("Instance Field").Returns(12);
11271117
yield return new TestCaseData("customObject?.intField", onInstanceVariables, true).SetCategory("Instance Field").Returns(12);
11281118
yield return new TestCaseData("customObject.Add3To(9)", onInstanceVariables, true).SetCategory("Instance Method").Returns(12);
11291119
yield return new TestCaseData("customObject?.Add3To(5)", onInstanceVariables, true).SetCategory("Instance Method").Returns(8);
11301120

1121+
yield return new TestCaseData("simpleInt.ToString()", onInstanceVariables, true).SetCategory("Instance Method").Returns("42");
1122+
yield return new TestCaseData("simpleInt.ToString().Length", onInstanceVariables, true).SetCategory("Instance Method,Instance Property").Returns(2);
1123+
11311124
yield return new TestCaseData("ClassForTest1.StaticIntProperty", onInstanceVariables, true).SetCategory("Static Property").Returns(67);
11321125
yield return new TestCaseData("ClassForTest1.StaticStringMethod(\"Bob\")", onInstanceVariables, true).SetCategory("Static Method").Returns("Hello Bob");
11331126

@@ -1149,10 +1142,24 @@ public static IEnumerable<TestCaseData> TestCasesForWithCustomVariablesExpressio
11491142
yield return new TestCaseData("simpleInt--", onInstanceVariables, true).SetCategory("Postfix operator, --").Returns(42);
11501143
yield return new TestCaseData("simpleInt-- - simpleInt", onInstanceVariables, true).SetCategory("Postfix operator, --").Returns(1);
11511144

1152-
yield return new TestCaseData("false && 1/0>0", onInstanceVariables, true ).SetCategory( "Conditional And, negative left operand (should respect left associativity)" ).Returns( false );
1153-
yield return new TestCaseData("true || 1/0>0", onInstanceVariables, true ).SetCategory( "Conditional Or, positive left operand (should respect left associativity)" ).Returns( true );
1154-
yield return new TestCaseData("false && (true && 1/0>0)", onInstanceVariables, true ).SetCategory( "Conditional And, negative left operand (should respect left associativity)" ).Returns( false );
1155-
yield return new TestCaseData("true || (false || 1/0>0)", onInstanceVariables, true ).SetCategory( "Conditional Or, positive left operand (should respect left associativity)" ).Returns( true );
1145+
yield return new TestCaseData("false && 1/0>0", onInstanceVariables, true).SetCategory("Conditional And, negative left operand (should respect left associativity)").Returns(false);
1146+
yield return new TestCaseData("true || 1/0>0", onInstanceVariables, true).SetCategory("Conditional Or, positive left operand (should respect left associativity)").Returns(true);
1147+
yield return new TestCaseData("false && (true && 1/0>0)", onInstanceVariables, true).SetCategory("Conditional And, negative left operand (should respect left associativity)").Returns(false);
1148+
yield return new TestCaseData("true || (false || 1/0>0)", onInstanceVariables, true).SetCategory("Conditional Or, positive left operand (should respect left associativity)").Returns(true);
1149+
yield return new TestCaseData("false && 1/0 == 0", onInstanceVariables, true).SetCategory("Instance Property,And Conditional").Returns(false);
1150+
yield return new TestCaseData("!string.IsNullOrEmpty(nullVar) && nullVar.StartsWith(\"ABC\")", onInstanceVariables, true).SetCategory("Instance Property,And Conditional").Returns(false);
1151+
yield return new TestCaseData("string.IsNullOrEmpty(nullVar) || nullVar.StartsWith(\"ABC\")", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional").Returns(true);
1152+
yield return new TestCaseData("!string.IsNullOrEmpty(nullVar) && nullVar.StartsWith(\"ABC\") == false", onInstanceVariables, true).SetCategory("Instance Property,And Conditional").Returns(false);
1153+
yield return new TestCaseData("string.IsNullOrEmpty(nullVar) || nullVar.StartsWith(\"ABC\") == false", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional").Returns(true);
1154+
yield return new TestCaseData("!string.IsNullOrEmpty(nullVar) && nullVar.Length < 2", onInstanceVariables, true).SetCategory("Instance Property,And Conditional").Returns(false);
1155+
yield return new TestCaseData("string.IsNullOrEmpty(nullVar) || nullVar.Length < 2", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional").Returns(true);
1156+
yield return new TestCaseData("true || 1/0 == 0", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional").Returns(true);
1157+
yield return new TestCaseData("false && true || true", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional,And Conditional,Precedence check").Returns(true);
1158+
yield return new TestCaseData("true || true && false", onInstanceVariables, true).SetCategory("Instance Property,Or Conditional,And Conditional,Precedence check").Returns(true);
1159+
yield return new TestCaseData("false && nullVar.What ? nullVar.Text : \"Hello\"", onInstanceVariables, true).SetCategory("Instance Property,Ternary operator, And Conditional").Returns("Hello");
1160+
yield return new TestCaseData("false && (false && nullVar.What ? nullVar.boolValue : true) ? nullVar.Text : \"Hello\"", onInstanceVariables, true).SetCategory("Instance Property,Ternary operator, And Conditional").Returns("Hello");
1161+
1162+
11561163
#endregion
11571164

11581165
#region Delegates as a variable
@@ -1438,7 +1445,8 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
14381445
evaluator = new ExpressionEvaluator(new Dictionary<string, object>
14391446
{
14401447
{ "P1var", "P1" },
1441-
{ "myObj", new ClassForTest1() }
1448+
{ "myObj", new ClassForTest1() },
1449+
{ "nullVar", null },
14421450
});
14431451

14441452
evaluator.PreEvaluateVariable += (sender, e) =>
@@ -1458,10 +1466,15 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingEvaluation
14581466
yield return new TestCaseData(evaluator, "myObj.PropertyThatWillFailed", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Var");
14591467
yield return new TestCaseData(evaluator, "myObj.Add3To(5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
14601468
yield return new TestCaseData(evaluator, "Abs(-5)", typeof(ExpressionEvaluatorSyntaxErrorException)).SetCategory("OnTheFly canceled Func");
1461-
yield return new TestCaseData(evaluator, "true && 1/0>0",typeof(DivideByZeroException) ).SetCategory( "Conditional And, positive left operand (should lead to exception)" );
1462-
yield return new TestCaseData(evaluator, "false || 1/0>0", typeof( DivideByZeroException ) ).SetCategory( "Conditional Or, positive left operand (should lead to exception associativity)" );
1463-
yield return new TestCaseData(evaluator, "true && (true && 1/0>0)", typeof( DivideByZeroException ) ).SetCategory( "Conditional And, positive left operand (should lead to exception)" );
1464-
yield return new TestCaseData(evaluator, "false || (false || 1/0>0)", typeof( DivideByZeroException ) ).SetCategory( "Conditional Or, positive left operand (should lead to exception associativity)" );
1469+
#endregion
1470+
1471+
#region Bugs corrections
1472+
1473+
yield return new TestCaseData(evaluator, "true && 1/0>0", typeof(DivideByZeroException)).SetCategory("Conditional And, positive left operand (should lead to exception)");
1474+
yield return new TestCaseData(evaluator, "false || 1/0>0", typeof(DivideByZeroException)).SetCategory("Conditional Or, positive left operand (should lead to exception associativity)");
1475+
yield return new TestCaseData(evaluator, "true && (true && 1/0>0)", typeof(DivideByZeroException)).SetCategory("Conditional And, positive left operand (should lead to exception)");
1476+
yield return new TestCaseData(evaluator, "false || (false || 1/0>0)", typeof(DivideByZeroException)).SetCategory("Conditional Or, positive left operand (should lead to exception associativity)");
1477+
14651478
#endregion
14661479
}
14671480
}

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodingSeb.ExpressionEvaluator.Tests/Resources.resx

+3
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,7 @@
325325
<data name="Script0069" type="System.Resources.ResXFileRef, System.Windows.Forms">
326326
<value>resources\script0069.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
327327
</data>
328+
<data name="Script0070" type="System.Resources.ResXFileRef, System.Windows.Forms">
329+
<value>resources\script0070.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
330+
</data>
328331
</root>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Script0070 */
2+
var e = new Exception("Must not be thrown");
3+
4+
return 1+2;

CodingSeb.ExpressionEvaluator.Tests/TestsUtils/XExpressionEvaluator3.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected override void Init()
1515
/// <summary>
1616
/// To evaluate DateTimes objects with #year-month-day syntax (#2019-05-28)
1717
/// </summary>
18-
protected virtual bool? EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
18+
protected virtual bool EvaluateDateTimeSyntax(string expression, Stack<object> stack, ref int i)
1919
{
2020
Match match = Regex.Match(expression.Substring(i), @"^\s*#(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})");
2121

@@ -40,7 +40,7 @@ protected override void Init()
4040
/// <summary>
4141
/// To evaluate a string replace with custom ternary indicator
4242
/// </summary>
43-
protected virtual bool? EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
43+
protected virtual bool EvaluateSpecialTernaryOperator(string expression, Stack<object> stack, ref int i)
4444
{
4545
if (expression.Substring(i, 1).Equals("°"))
4646
{

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

+6-4
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.12.0</Version>
9-
<AssemblyVersion>1.4.12.0</AssemblyVersion>
10-
<FileVersion>1.4.12.0</FileVersion>
8+
<Version>1.4.13.0</Version>
9+
<AssemblyVersion>1.4.13.0</AssemblyVersion>
10+
<FileVersion>1.4.13.0</FileVersion>
1111
<OutputPath>bin\$(Configuration)\</OutputPath>
1212
<Authors>Coding Seb</Authors>
1313
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
@@ -18,7 +18,9 @@
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1919
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
21-
<PackageReleaseNotes>* Correction of a bug on &amp;&amp; and || operators when the right part throw an exception when only the left part should be evaluated</PackageReleaseNotes>
21+
<PackageReleaseNotes>* More complete Corrections of the bug on &amp;&amp; and || operators when the right part throw an exception when only the left part should be evaluated
22+
* Correction of a bug when creating an exception without throwing (introduced in last release)
23+
* Some refactoring</PackageReleaseNotes>
2224
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2325
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
2426
</PropertyGroup>

0 commit comments

Comments
 (0)