Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix NullReferenceException when predicate doesn't return a value #459

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Source/Parser/Functions/IterableJoiningFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public override bool Evaluate(InterpreterScope scope, out ExpressionBase result)
if (!predicate.Evaluate(iteratorScope, out result))
return false;

if (result == null)
{
result = new ErrorExpression("predicate did not return a value", predicate);
return false;
}

expression = Combine(expression, result);
if (expression.Type == ExpressionType.Error)
{
Expand Down
14 changes: 14 additions & 0 deletions Tests/Parser/Functions/AllOfFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,19 @@ public void TestDictionary()
Assert.That(Evaluate("all_of({1:\"One\",2:\"Two\",3:\"Three\"}, a => byte(0x1234) != a)"),
Is.EqualTo("byte(0x001234) != 1 && byte(0x001234) != 2 && byte(0x001234) != 3"));
}

[Test]
public void TestMissingReturn()
{
Assert.That(Evaluate("all_of([1, 2, 3], (a) { if (a == 2) return a })"),
Is.EqualTo("predicate did not return a value"));
}

[Test]
public void TestErrorInPredicate()
{
Assert.That(Evaluate("all_of([1, 2, 3], (a) { return b })"),
Is.EqualTo("Unknown variable: b"));
}
}
}
14 changes: 14 additions & 0 deletions Tests/Parser/Functions/AnyOfFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,19 @@ public void TestPredicateWithExtraParameters()
Assert.That(Evaluate("any_of([1], (a, b) => byte(0x1234) == a)"),
Is.EqualTo("predicate function must accept a single parameter"));
}

[Test]
public void TestMissingReturn()
{
Assert.That(Evaluate("any_of([1, 2, 3], (a) { if (a == 2) return a })"),
Is.EqualTo("predicate did not return a value"));
}

[Test]
public void TestErrorInPredicate()
{
Assert.That(Evaluate("any_of([1, 2, 3], (a) { return b })"),
Is.EqualTo("Unknown variable: b"));
}
}
}
14 changes: 14 additions & 0 deletions Tests/Parser/Functions/ArrayMapFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,19 @@ public void TestPredicateWithExtraParameters()
Assert.That(Evaluate("array_map([1], (a, b) => byte(a))"),
Is.EqualTo("predicate function must accept a single parameter"));
}

[Test]
public void TestMissingReturn()
{
Assert.That(Evaluate("array_map([1, 2, 3], (a) { if (a == 2) return a })"),
Is.EqualTo("predicate did not return a value"));
}

[Test]
public void TestErrorInPredicate()
{
Assert.That(Evaluate("array_map([1, 2, 3], (a) { return b })"),
Is.EqualTo("Unknown variable: b"));
}
}
}
14 changes: 14 additions & 0 deletions Tests/Parser/Functions/NoneOfFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,19 @@ public void TestDictionary()
Assert.That(Evaluate("none_of({1:\"One\",2:\"Two\",3:\"Three\"}, a => byte(0x1234) == a)"),
Is.EqualTo("byte(0x001234) != 1 && byte(0x001234) != 2 && byte(0x001234) != 3"));
}

[Test]
public void TestMissingReturn()
{
Assert.That(Evaluate("none_of([1, 2, 3], (a) { if (a == 2) return a })"),
Is.EqualTo("predicate did not return a value"));
}

[Test]
public void TestErrorInPredicate()
{
Assert.That(Evaluate("none_of([1, 2, 3], (a) { return b })"),
Is.EqualTo("Unknown variable: b"));
}
}
}
14 changes: 14 additions & 0 deletions Tests/Parser/Functions/SumOfFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,19 @@ public void TestDictionary()
Assert.That(Evaluate("sum_of({1:\"One\",2:\"Two\",3:\"Three\"}, a => byte(a)) == 9"),
Is.EqualTo("(byte(0x000001) + byte(0x000002) + byte(0x000003)) == 9"));
}

[Test]
public void TestMissingReturn()
{
Assert.That(Evaluate("sum_of([1, 2, 3], (a) { if (a == 2) return a })"),
Is.EqualTo("predicate did not return a value"));
}

[Test]
public void TestErrorInPredicate()
{
Assert.That(Evaluate("sum_of([1, 2, 3], (a) { return b })"),
Is.EqualTo("Unknown variable: b"));
}
}
}
14 changes: 14 additions & 0 deletions Tests/Parser/Functions/TallyOfFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,19 @@ public void TestDictionary()
Assert.That(Evaluate("tally_of({1:\"One\",2:\"Two\",3:\"Three\"}, 11, a => byte(a) == 9)"),
Is.EqualTo("tally(11, byte(0x000001) == 9, byte(0x000002) == 9, byte(0x000003) == 9)"));
}

[Test]
public void TestMissingReturn()
{
Assert.That(Evaluate("tally_of([1, 2, 3], 4, (a) { if (a == 2) return a })"),
Is.EqualTo("predicate did not return a value"));
}

[Test]
public void TestErrorInPredicate()
{
Assert.That(Evaluate("tally_of([1, 2, 3], 4, (a) { return b })"),
Is.EqualTo("Unknown variable: b"));
}
}
}
Loading