diff options
author | Bruce Wells <phpfui@users.noreply.github.com> | 2018-10-25 18:54:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-25 18:54:54 +0300 |
commit | 43f0ff3f28d198fbb4e36346fc5f36fa91cf3e18 (patch) | |
tree | 28a0abef9e0e48df01660985339e93c69a2cd746 /tests/MathTest.php | |
parent | 4a672cfd94c07e1821227f27fd1edd2217685136 (diff) |
Support for better invalid expression detection and divide by zero (#30)
* Additional validation for bad expressions (*+ for example)
* Removing DivisionByZeroException testing for now
Added more unit tests.
Diffstat (limited to 'tests/MathTest.php')
-rw-r--r-- | tests/MathTest.php | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php index db6e486..c8b8a03 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -11,7 +11,15 @@ namespace NXP\Tests; -use \NXP\MathExecutor; +use NXP\MathExecutor; +use NXP\Exception\DivisionByZeroException; +use NXP\Exception\IncorrectBracketsException; +use NXP\Exception\IncorrectExpressionException; +use NXP\Exception\MathExecutorException; +use NXP\Exception\UnknownFunctionException; +use NXP\Exception\UnknownOperatorException; +use NXP\Exception\UnknownTokenException; +use NXP\Exception\UnknownVariableException; class MathTest extends \PHPUnit_Framework_TestCase { @@ -27,10 +35,28 @@ class MathTest extends \PHPUnit_Framework_TestCase $this->assertEquals($calculator->execute($expression), $phpResult); } + public function testUnknownFunctionException() + { + $calculator = new MathExecutor(); + $this->expectException(UnknownFunctionException::class); + $calculator->execute('1 * fred("wilma") + 3'); + } + + public function testIncorrectExpressionException() + { + $calculator = new MathExecutor(); + $this->expectException(IncorrectExpressionException::class); + $calculator->execute('1 * + '); + } + public function testZeroDivision() { $calculator = new MathExecutor(); $this->assertEquals($calculator->execute('1 / 0'), 0); + + // future version with allow for optional exceptions on divide by zero + // $this->expectException(DivisionByZeroException::class); + // $calculator->execute('1 / 0'); } public function testExponentiation() @@ -63,15 +89,25 @@ class MathTest extends \PHPUnit_Framework_TestCase ['(5 + 3) * -1'], - ['2+2*2'], + ['2- 2*2'], + ['2-(2*2)'], + ['(2- 2)*2'], + ['2 + 2*2'], + ['2+ 2*2'], ['(2+2)*2'], - ['(2+2)*-2'], + ['(2 + 2)*-2'], ['(2+-2)*2'], ['sin(10) * cos(50) / min(10, 20/2)'], ['100500 * 3.5E5'], ['100500 * 3.5E-5'], + + ['-1 + -2'], + ['-1+-2'], + ['-1- -2'], + ['-1/-2'], + ['-1*-2'], ]; } |