Skip to content

Commit e25f9dc

Browse files
committed
Correction for #115
1 parent 7f564dc commit e25f9dc

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,40 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
587587
.SetCategory("??=")
588588
.Returns("First Null-coalescing assignation");
589589

590+
#endregion
591+
592+
#region Implicit cast when assign
593+
594+
ExpressionEvaluator evaluator0012 = new ExpressionEvaluator();
595+
596+
yield return new TestCaseData("ushort x = 2;",
597+
evaluator0012,
598+
null,
599+
new Action(() => evaluator0012.Variables["x"].ShouldBe(2)), null)
600+
.SetCategory("Script")
601+
.SetCategory("Variable assignation")
602+
.SetCategory("=")
603+
.Returns(2);
604+
605+
yield return new TestCaseData("double y = 3;",
606+
evaluator0012,
607+
null,
608+
new Action(() => evaluator0012.Variables["y"].ShouldBe(3)), null)
609+
.SetCategory("Script")
610+
.SetCategory("Variable assignation")
611+
.SetCategory("=")
612+
.Returns(3);
613+
614+
//yield return new TestCaseData("float z = 3.2;",
615+
// evaluator0012,
616+
// null,
617+
// new Action(() => ((float)evaluator0012.Variables["z"]).ShouldBe(3.2f, 0.001)), null)
618+
//.SetCategory("Script")
619+
//.SetCategory("Variable assignation")
620+
//.SetCategory("=")
621+
//.Returns();
622+
623+
590624
#endregion
591625

592626
#region Array content assignation

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3314,7 +3314,14 @@ protected virtual void AssignVariable(string varName, object value)
33143314
}
33153315
else
33163316
{
3317-
throw new InvalidCastException($"A object of type {typeToAssign} can not be cast implicitely in {stronglyTypedVariable.Type}");
3317+
try
3318+
{
3319+
Variables[varName] = Convert.ChangeType(value, stronglyTypedVariable.Type);
3320+
}
3321+
catch
3322+
{
3323+
throw new InvalidCastException($"A object of type {typeToAssign} can not be cast implicitely in {stronglyTypedVariable.Type}");
3324+
}
33183325
}
33193326
}
33203327
else

0 commit comments

Comments
 (0)