sklose / NCalc2

expression evaluator for .NET with built-in compiler
MIT License
166 stars 58 forks source link

Can't compare null parameter to a constant #45

Closed altso closed 4 years ago

altso commented 4 years ago

The following test fails with an exception below. As a user, I would expect it to pass. I can create a PR with a fix if you agree it's a bug.

[Fact]
public void ShouldCompareNullableNullToNonNullable()
{
    var e = new Expression("[x] = 5", EvaluateOptions.AllowNullParameter);

    e.Parameters["x"] = null;
    Assert.False((bool)e.Evaluate());
}
System.InvalidCastException
Null object cannot be converted to a value type.
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at NCalc.Domain.EvaluationVisitor.CompareUsingMostPreciseType(Object a, Object b) in \src\NCalc\Domain\EvaluationVisitor.cs:line 65
   at NCalc.Domain.EvaluationVisitor.Visit(BinaryExpression expression) in \src\NCalc\Domain\EvaluationVisitor.cs:line 160
   at NCalc.Domain.BinaryExpression.Accept(LogicalExpressionVisitor visitor) in \src\NCalc\Domain\BinaryExpression.cs:line 20
   at NCalc.Expression.Evaluate() in \src\NCalc\Expression.cs:line 323
   at NCalc.Tests.Fixtures.ShouldCompareNullableNullToNonNullable() in \test\NCalc.Tests\Fixtures.cs:line 96