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

[TASK] Extend MathExpressionNode tests with chained math #398

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,21 @@ public function testEvaluateExpression($expression, array $variables, $expected)
public function getEvaluateExpressionTestValues()
{
return [
['1 gabbagabbahey 1', [], 0],
['1 + 1', [], 2],
['2 - 1', [], 1],
['2 % 4', [], 2],
['2 * 4', [], 8],
['4 / 2', [], 2],
['4 ^ 2', [], 16],
['a + 1', ['a' => 1], 2],
['1 + b', ['b' => 1], 2],
['a + b', ['a' => 1, 'b' => 1], 2],
'Invalid operator returns zero' => ['1 gabbagabbahey 1', [], 0],
'Simple addition' => ['1 + 1', [], 2],
'Chained addition' => ['1 + 1 + 1', [], 3],
'Multiplication before addition' => ['1 + 1 * 2', [], 4],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm, shouldn't this be 3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expressions don't know about operator priority, so 1 + 1 = 2 * 2 = 4 is what it sees. It intentionally also doesn't know about parenthesis grouping in order to KISS.

If you know a reasonable way to implement operator priority (see also source of the expression class) I'd definitely consider adding that as a feature.

'Subtraction' => ['2 - 1', [], 1],
'Modulo' => ['2 % 4', [], 2],
'Multiplication' => ['2 * 4', [], 8],
'Division' => ['4 / 2', [], 2],
'Exponentiation' => ['4 ^ 2', [], 16],
'Simple variable (first) addition' => ['a + 1', ['a' => 1], 2],
'Simple variable (last) addition' => ['1 + b', ['b' => 1], 2],
'Multiple variable addition' => ['a + a + 1', ['a' => 1], 3],
'Variable only addition' => ['a + b', ['a' => 1, 'b' => 1], 2],
'Addition with boolean "true"' => ['a + 1', ['a' => true], 2],
'Addition with boolean "false"' => ['a + 1', ['a' => false], 1],
];
}
}