Skip to content

Commit 59179ab

Browse files
author
Sébastien Geiser
committed
try catch finally OK with tests
1 parent 8204e55 commit 59179ab

File tree

10 files changed

+295
-15
lines changed

10 files changed

+295
-15
lines changed

CodingSeb.ExpressionEvaluator.Tests/CodingSeb.ExpressionEvaluator.Tests.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@
156156
<ItemGroup>
157157
<None Include="Resources\Script0028.txt" />
158158
</ItemGroup>
159+
<ItemGroup>
160+
<None Include="Resources\Script0029.txt" />
161+
</ItemGroup>
162+
<ItemGroup>
163+
<None Include="Resources\Script0030.txt" />
164+
</ItemGroup>
165+
<ItemGroup>
166+
<None Include="Resources\Script0031.txt" />
167+
</ItemGroup>
168+
<ItemGroup>
169+
<None Include="Resources\Script0032.txt" />
170+
</ItemGroup>
159171
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
160172
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
161173
<PropertyGroup>

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,30 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
821821
.SetCategory("Finally")
822822
.SetCategory("Exception")
823823
.Returns("catch : True, finally : True");
824+
825+
yield return new TestCaseData(Resources.Script0030, null, null, null)
826+
.SetCategory("Script")
827+
.SetCategory("Try")
828+
.SetCategory("Catch")
829+
.SetCategory("Exception")
830+
.Returns("catch : True, finally : False");
831+
832+
yield return new TestCaseData(Resources.Script0031, null, null, null)
833+
.SetCategory("Script")
834+
.SetCategory("Try")
835+
.SetCategory("Catch")
836+
.SetCategory("Finally")
837+
.SetCategory("Exception")
838+
.Returns("catch : 1, finally : True");
839+
840+
yield return new TestCaseData(Resources.Script0032, null, null, null)
841+
.SetCategory("Script")
842+
.SetCategory("Try")
843+
.SetCategory("Catch")
844+
.SetCategory("Finally")
845+
.SetCategory("Exception")
846+
.Returns("catch : 2, finally : True");
847+
824848
#endregion
825849

826850
#region block for lambda body
@@ -1162,14 +1186,14 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingScriptEvalu
11621186
OptionOnNoReturnKeywordFoundInScriptAction = OptionOnNoReturnKeywordFoundInScriptAction.ThrowSyntaxException
11631187
};
11641188

1165-
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "1"), typeof(ExpressionEvaluatorSyntaxErrorException), null)
1189+
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "1"), typeof(ExpressionEvaluatorSyntaxErrorException), null,null)
11661190
.SetCategory("Script")
11671191
.SetCategory("return")
11681192
.SetCategory("if")
11691193
.SetCategory("variable assignation")
11701194
.SetCategory("Options")
11711195
.SetCategory("OptionOnNoReturnKeywordFoundInScriptAction = ThrowSyntaxException");
1172-
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "2"), typeof(ExpressionEvaluatorSyntaxErrorException), null)
1196+
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "2"), typeof(ExpressionEvaluatorSyntaxErrorException), null,null)
11731197
.SetCategory("Script")
11741198
.SetCategory("return")
11751199
.SetCategory("if")
@@ -1183,18 +1207,26 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingScriptEvalu
11831207

11841208
#region Throw Exception
11851209

1186-
yield return new TestCaseData(new ExpressionEvaluator(), Resources.Script0025, typeof(Exception), "Exception for test")
1210+
yield return new TestCaseData(new ExpressionEvaluator(), Resources.Script0025, typeof(Exception), "Exception for test",null)
11871211
.SetCategory("Script")
11881212
.SetCategory("Throw")
11891213
.SetCategory("Exception");
11901214

1215+
yield return new TestCaseData(new ExpressionEvaluator(), Resources.Script0029, typeof(DivideByZeroException), null, new Action(() => ClassForTest1.StaticIntProperty.ShouldEqual(20)))
1216+
.SetCategory("Script")
1217+
.SetCategory("Try")
1218+
.SetCategory("Finally")
1219+
.SetCategory("Exception");
1220+
11911221
#endregion
11921222
}
11931223
}
11941224

11951225
[TestCaseSource(nameof(TestCasesForExceptionThrowingScriptEvaluation))]
1196-
public void ExceptionThrowingScriptEvaluation(ExpressionEvaluator evaluator, string script, Type exceptionType, string exceptionMessage)
1226+
public void ExceptionThrowingScriptEvaluation(ExpressionEvaluator evaluator, string script, Type exceptionType, string exceptionMessage, Action ToTestAfter)
11971227
{
1228+
evaluator.Namespaces.Add("CodingSeb.ExpressionEvaluator.Tests");
1229+
11981230
Assert.Catch(exceptionType, () => evaluator.ScriptEvaluate(evaluator.RemoveComments(script)));
11991231

12001232
if (exceptionMessage != null)
@@ -1208,7 +1240,8 @@ public void ExceptionThrowingScriptEvaluation(ExpressionEvaluator evaluator, str
12081240
exception.Message.ShouldEqual(exceptionMessage);
12091241
}
12101242
}
1211-
1243+
1244+
ToTestAfter?.Invoke();
12121245
}
12131246

12141247
#endregion

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

Lines changed: 120 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodingSeb.ExpressionEvaluator.Tests/Resources.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,16 @@
202202
<data name="Script0028" type="System.Resources.ResXFileRef, System.Windows.Forms">
203203
<value>resources\script0028.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
204204
</data>
205+
<data name="Script0029" type="System.Resources.ResXFileRef, System.Windows.Forms">
206+
<value>resources\script0029.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
207+
</data>
208+
<data name="Script0030" type="System.Resources.ResXFileRef, System.Windows.Forms">
209+
<value>resources\script0030.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
210+
</data>
211+
<data name="Script0031" type="System.Resources.ResXFileRef, System.Windows.Forms">
212+
<value>resources\script0031.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
213+
</data>
214+
<data name="Script0032" type="System.Resources.ResXFileRef, System.Windows.Forms">
215+
<value>resources\script0032.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
216+
</data>
205217
</root>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Script0029 */
2+
3+
x = 5;
4+
y = 0;
5+
6+
c = false;
7+
f = false;
8+
9+
try
10+
{
11+
x / y;
12+
}
13+
finally
14+
{
15+
ClassForTest1.StaticIntProperty = 20;
16+
f = true;
17+
}
18+
19+
return $"catch : {c}, finally : {f}";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Script0030 */
2+
3+
x = 5;
4+
y = 0;
5+
6+
c = false;
7+
f = false;
8+
9+
try
10+
{
11+
x / y;
12+
}
13+
catch(exception)
14+
{
15+
c = true;
16+
}
17+
18+
return $"catch : {c}, finally : {f}";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Script0031 */
2+
3+
x = 5;
4+
y = 0;
5+
6+
c = 0;
7+
f = false;
8+
9+
try
10+
{
11+
x / y;
12+
}
13+
catch(DivideByZeroException exception)
14+
{
15+
c = 1;
16+
}
17+
catch(Exception exception)
18+
{
19+
c = 2;
20+
}
21+
finally
22+
{
23+
f = true;
24+
}
25+
26+
return $"catch : {c}, finally : {f}";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Script0032 */
2+
3+
c = 0;
4+
f = false;
5+
6+
try
7+
{
8+
throw new Exception("My exception");
9+
}
10+
catch(DivideByZeroException exception)
11+
{
12+
c = 1;
13+
}
14+
catch(Exception exception)
15+
{
16+
c = 2;
17+
}
18+
finally
19+
{
20+
f = true;
21+
}
22+
23+
return $"catch : {c}, finally : {f}";

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -835,16 +835,35 @@ void ExecuteTryList()
835835
}
836836
catch(Exception exception)
837837
{
838-
if(tryStatementsList[1][0].ToString().Equals("catch"))
838+
bool atLeasOneCatch = false;
839+
840+
foreach (List<string> catchStatement in tryStatementsList.Skip(1).TakeWhile(e => e[0].Equals("catch")))
839841
{
840-
if(tryStatementsList[1][1] != null)
842+
if (catchStatement[1] != null)
841843
{
842-
string[] exceptionVariable = tryStatementsList[1][1].ToString().Trim().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
844+
string[] exceptionVariable = catchStatement[1].ToString().Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
845+
846+
string exceptionName = exceptionVariable[0];
847+
848+
if (exceptionVariable.Length >= 2)
849+
{
850+
if (!((ClassOrTypeName)Evaluate(exceptionVariable[0])).Type.IsAssignableFrom(exception.GetType()))
851+
continue;
843852

844-
Variables[exceptionVariable[0]] = exception;
853+
exceptionName = exceptionVariable[1];
854+
}
855+
856+
Variables[exceptionName] = exception;
845857
}
846858

847-
lastResult = ScriptEvaluate(tryStatementsList[1][2], ref isReturn, ref isBreak, ref isContinue);
859+
lastResult = ScriptEvaluate(catchStatement[2], ref isReturn, ref isBreak, ref isContinue);
860+
atLeasOneCatch = true;
861+
break;
862+
}
863+
864+
if(!atLeasOneCatch)
865+
{
866+
throw exception;
848867
}
849868
}
850869
finally

0 commit comments

Comments
 (0)