Skip to content

Commit 8204e55

Browse files
committed
First simple try catch finally test OK
1 parent e5f7af5 commit 8204e55

File tree

6 files changed

+129
-3
lines changed

6 files changed

+129
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@
153153
<ItemGroup>
154154
<None Include="Resources\Script0027.txt" />
155155
</ItemGroup>
156+
<ItemGroup>
157+
<None Include="Resources\Script0028.txt" />
158+
</ItemGroup>
156159
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
157160
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
158161
<PropertyGroup>

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,17 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
812812

813813
#endregion
814814

815+
#region try, catch, finally
816+
817+
yield return new TestCaseData(Resources.Script0028, null, null, null)
818+
.SetCategory("Script")
819+
.SetCategory("Try")
820+
.SetCategory("Catch")
821+
.SetCategory("Finally")
822+
.SetCategory("Exception")
823+
.Returns("catch : True, finally : True");
824+
#endregion
825+
815826
#region block for lambda body
816827

817828
yield return new TestCaseData(Resources.Script0006, null, null, null)
@@ -1170,7 +1181,7 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingScriptEvalu
11701181

11711182
#endregion
11721183

1173-
#region Throw Exception and try finally
1184+
#region Throw Exception
11741185

11751186
yield return new TestCaseData(new ExpressionEvaluator(), Resources.Script0025, typeof(Exception), "Exception for test")
11761187
.SetCategory("Script")

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

Lines changed: 33 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,7 @@
199199
<data name="Script0027" type="System.Resources.ResXFileRef, System.Windows.Forms">
200200
<value>resources\script0027.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
201201
</data>
202+
<data name="Script0028" type="System.Resources.ResXFileRef, System.Windows.Forms">
203+
<value>resources\script0028.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
204+
</data>
202205
</root>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Script0028 */
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+
finally
18+
{
19+
f = true;
20+
}
21+
22+
return $"catch : {c}, finally : {f}";

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,38 @@ void ExecuteTryList()
824824
{
825825
if(tryStatementsList.Count > 0)
826826
{
827+
if(tryStatementsList.Count == 1)
828+
{
829+
throw new ExpressionEvaluatorSyntaxErrorException("a try statement need at least one catch or one finally statement.");
830+
}
831+
832+
try
833+
{
834+
lastResult = ScriptEvaluate(tryStatementsList[0][0], ref isReturn, ref isBreak, ref isContinue);
835+
}
836+
catch(Exception exception)
837+
{
838+
if(tryStatementsList[1][0].ToString().Equals("catch"))
839+
{
840+
if(tryStatementsList[1][1] != null)
841+
{
842+
string[] exceptionVariable = tryStatementsList[1][1].ToString().Trim().Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
843+
844+
Variables[exceptionVariable[0]] = exception;
845+
}
846+
847+
lastResult = ScriptEvaluate(tryStatementsList[1][2], ref isReturn, ref isBreak, ref isContinue);
848+
}
849+
}
850+
finally
851+
{
852+
if(tryStatementsList.Last()[0].Equals("finally"))
853+
{
854+
lastResult = ScriptEvaluate(tryStatementsList.Last()[1], ref isReturn, ref isBreak, ref isContinue);
855+
}
856+
}
827857

858+
tryStatementsList.Clear();
828859
}
829860
}
830861

@@ -916,6 +947,30 @@ void ExecuteBlocksStacks()
916947
ifBlockEvaluatedState = IfBlockEvaluatedState.NoBlockEvaluated;
917948
}
918949
}
950+
else if (keyword.Equals("catch"))
951+
{
952+
if (tryBlockEvaluatedState == TryBlockEvaluatedState.NoBlockEvaluated)
953+
{
954+
throw new ExpressionEvaluatorSyntaxErrorException("No corresponding [try] for [catch] statement.");
955+
}
956+
else
957+
{
958+
tryStatementsList.Add(new List<string>() { "catch", keywordAttributes.Count > 0 ? keywordAttributes[0] : null, subScript });
959+
tryBlockEvaluatedState = TryBlockEvaluatedState.Catch;
960+
}
961+
}
962+
else if (keyword.Equals("finally"))
963+
{
964+
if (tryBlockEvaluatedState == TryBlockEvaluatedState.NoBlockEvaluated)
965+
{
966+
throw new ExpressionEvaluatorSyntaxErrorException("No corresponding [try] for [finally] statement.");
967+
}
968+
else
969+
{
970+
tryStatementsList.Add(new List<string>() { "finally", subScript });
971+
tryBlockEvaluatedState = TryBlockEvaluatedState.NoBlockEvaluated;
972+
}
973+
}
919974
else
920975
{
921976
ExecuteBlocksStacks();
@@ -1071,6 +1126,7 @@ void forAction(int index)
10711126
}
10721127

10731128
ifBlockEvaluatedState = IfBlockEvaluatedState.NoBlockEvaluated;
1129+
tryBlockEvaluatedState = TryBlockEvaluatedState.NoBlockEvaluated;
10741130

10751131
i++;
10761132
}

0 commit comments

Comments
 (0)