Left bracket and minus fix (#55)

* Left bracket and minus fix (#54)

* Add test for left bracket and minus fix #55
This commit is contained in:
Clément Lafont 2020-01-13 18:09:18 +01:00 committed by Bruce Wells
parent ff550b4180
commit 707e22029d
2 changed files with 13 additions and 2 deletions

View file

@ -53,8 +53,12 @@ class TokenMinus extends AbstractOperator
$op2 = array_pop($stack); $op2 = array_pop($stack);
$op1 = array_pop($stack); $op1 = array_pop($stack);
if ($op1 === null || $op2 === null) { if ($op2 === null) {
throw new IncorrectExpressionException("Subtraction requires two operators"); throw new IncorrectExpressionException("Subtraction requires right operator");
}
if (!$op1) {
$op1 = new TokenNumber(0);
} }
$result = $op1->getValue() - $op2->getValue(); $result = $op1->getValue() - $op2->getValue();

View file

@ -297,4 +297,11 @@ class MathTest extends \PHPUnit\Framework\TestCase
$calculator->execute('test("' . $testString . '")'); // single quotes $calculator->execute('test("' . $testString . '")'); // single quotes
$calculator->execute("test('" . $testString . "')"); // double quotes $calculator->execute("test('" . $testString . "')"); // double quotes
} }
public function testBeginWithBracketAndMinus()
{
$calculator = new MathExecutor();
$this->assertEquals(-4, $calculator->execute('(-4)'));
$this->assertEquals(1, $calculator->execute('(-4 + 5)'));
}
} }