Skip to content

Commit 9c39a0e

Browse files
committed
throw keyword
1 parent 01c7c51 commit 9c39a0e

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
<ItemGroup>
144144
<None Include="Resources\Script0024.txt" />
145145
</ItemGroup>
146+
<ItemGroup>
147+
<None Include="Resources\Script0025.txt" />
148+
</ItemGroup>
146149
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
147150
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
148151
<PropertyGroup>

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,14 +1135,14 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingScriptEvalu
11351135
OptionOnNoReturnKeywordFoundInScriptAction = OptionOnNoReturnKeywordFoundInScriptAction.ThrowSyntaxException
11361136
};
11371137

1138-
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "1"), typeof(ExpressionEvaluatorSyntaxErrorException))
1138+
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "1"), typeof(ExpressionEvaluatorSyntaxErrorException), null)
11391139
.SetCategory("Script")
11401140
.SetCategory("return")
11411141
.SetCategory("if")
11421142
.SetCategory("variable assignation")
11431143
.SetCategory("Options")
11441144
.SetCategory("OptionOnNoReturnKeywordFoundInScriptAction = ThrowSyntaxException");
1145-
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "2"), typeof(ExpressionEvaluatorSyntaxErrorException))
1145+
yield return new TestCaseData(evaluator, Resources.Script0008.Replace("[valx]", "2"), typeof(ExpressionEvaluatorSyntaxErrorException), null)
11461146
.SetCategory("Script")
11471147
.SetCategory("return")
11481148
.SetCategory("if")
@@ -1153,13 +1153,35 @@ public static IEnumerable<TestCaseData> TestCasesForExceptionThrowingScriptEvalu
11531153
#endregion
11541154

11551155
#endregion
1156+
1157+
#region Throw Exception and try finally
1158+
1159+
yield return new TestCaseData(new ExpressionEvaluator(), Resources.Script0025, typeof(Exception), "Exception for test")
1160+
.SetCategory("Script")
1161+
.SetCategory("Throw")
1162+
.SetCategory("Exception");
1163+
1164+
#endregion
11561165
}
11571166
}
11581167

11591168
[TestCaseSource(nameof(TestCasesForExceptionThrowingScriptEvaluation))]
1160-
public void ExceptionThrowingScriptEvaluation(ExpressionEvaluator evaluator, string script, Type exceptionType)
1169+
public void ExceptionThrowingScriptEvaluation(ExpressionEvaluator evaluator, string script, Type exceptionType, string exceptionMessage)
11611170
{
1162-
Assert.Catch(exceptionType, () => evaluator.ScriptEvaluate(script));
1171+
Assert.Catch(exceptionType, () => evaluator.ScriptEvaluate(evaluator.RemoveComments(script)));
1172+
1173+
if (exceptionMessage != null)
1174+
{
1175+
try
1176+
{
1177+
evaluator.ScriptEvaluate(evaluator.RemoveComments(script));
1178+
}
1179+
catch(Exception exception)
1180+
{
1181+
exception.Message.ShouldEqual(exceptionMessage);
1182+
}
1183+
}
1184+
11631185
}
11641186

11651187
#endregion

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

Lines changed: 11 additions & 0 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
@@ -190,4 +190,7 @@
190190
<data name="Script0024" type="System.Resources.ResXFileRef, System.Windows.Forms">
191191
<value>resources\script0024.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
192192
</data>
193+
<data name="Script0025" type="System.Resources.ResXFileRef, System.Windows.Forms">
194+
<value>resources\script0025.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
195+
</data>
193196
</root>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* Script0025 */
2+
3+
throw new Exception("Exception for test");

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ object ManageJumpStatementsOrExpressionEval(string expression)
749749
return lastResult;
750750
}
751751

752+
if(expressionToTest.StartsWith("throw "))
753+
{
754+
throw Evaluate(expressionToTest.Remove(0, 6)) as Exception;
755+
}
756+
752757
expression = returnKeywordRegex.Replace(expression, match =>
753758
{
754759
if (OptionCaseSensitiveEvaluationActive && !match.Value.StartsWith("return"))

0 commit comments

Comments
 (0)