diff options
author | Bruce Wells <brucekwells@gmail.com> | 2020-07-27 19:25:59 +0300 |
---|---|---|
committer | Bruce Wells <brucekwells@gmail.com> | 2020-09-16 04:14:44 +0300 |
commit | 8a2cae984f77ed55195bed071b12a165fa532561 (patch) | |
tree | f91391945ab9eea944a785209d6db1698e1529cc | |
parent | 92d1a4524b5b2c7b4fc778e99815a49d227fc5ef (diff) |
Better setVar error message (#70)
Additional unit tests
Readme update
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | src/NXP/MathExecutor.php | 3 | ||||
-rw-r--r-- | tests/MathTest.php | 57 |
3 files changed, 59 insertions, 7 deletions
@@ -1,4 +1,4 @@ -# MathExecutor [![Tests](https://github.com/neonxp/MathExecutor/workflows/Tests/badge.svg)](https://github.com/neonxp/MathExecutor/actions?query=workflow%3ATests) [![Latest Packagist release](https://img.shields.io/packagist/v/nxp/math-executor.svg)](https://packagist.org/packages/nxp/math-executor) +# MathExecutor [![Tests](https://github.com/neonxp/MathExecutor/workflows/Tests/badge.svg)](https://github.com/neonxp/MathExecutor/actions?query=workflow%3ATests) # A simple and extensible math expressions calculator @@ -115,6 +115,8 @@ You can think of the **if** function as prototyped like: function if($condition, $returnIfTrue, $returnIfFalse) ``` ## Variables: +Variables can be prefixed with the dollar sign ($) for PHP compatibility, but is not required. + Default variables: ``` @@ -127,7 +129,7 @@ You can add your own variables to executor: ```php $executor->setVar('var1', 0.15)->setVar('var2', 0.22); -echo $executor->execute("$var1 + $var2"); +echo $executor->execute("$var1 + var2"); ``` You can dynamically define variables at run time. If a variable has a high computation cost, but might not be used, then you can define an undefined variable handler. It will only get called when the variable is used, rather than having to always set it initially. diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index d23ba80..434246a 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -432,7 +432,8 @@ class MathExecutor public function setVar(string $variable, $value) : self { if (!is_scalar($value)) { - throw new MathExecutorException("Variable ({$variable}) value must be a scalar type ({gettype($value)})"); + $type = gettype($value); + throw new MathExecutorException("Variable ({$variable}) type ({$type}) is not scalar"); } $this->variables[$variable] = $value; diff --git a/tests/MathTest.php b/tests/MathTest.php index 2669ccd..8f5665b 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -15,6 +15,7 @@ use Exception; use NXP\Exception\DivisionByZeroException; use NXP\Exception\IncorrectExpressionException; use NXP\Exception\IncorrectNumberOfFunctionParametersException; +use NXP\Exception\MathExecutorException; use NXP\Exception\UnknownFunctionException; use NXP\Exception\UnknownVariableException; use NXP\MathExecutor; @@ -332,9 +333,11 @@ class MathTest extends TestCase { $calculator = new MathExecutor(); $this->assertEquals(3.14159265359, $calculator->execute('$pi')); + $this->assertEquals(3.14159265359, $calculator->execute('pi')); $this->assertEquals(2.71828182846, $calculator->execute('$e')); + $this->assertEquals(2.71828182846, $calculator->execute('e')); $calculator->setVars([ - 'trx_amount' => 100000, + 'trx_amount' => 100000.01, 'ten' => 10, 'nine' => 9, 'eight' => 8, @@ -347,7 +350,7 @@ class MathTest extends TestCase 'one' => 1, 'zero' => 0, ]); - $this->assertEquals(100000, $calculator->execute('$trx_amount')); + $this->assertEquals(100000.01, $calculator->execute('$trx_amount')); $this->assertEquals(10 - 9, $calculator->execute('$ten - $nine')); $this->assertEquals(9 - 10, $calculator->execute('$nine - $ten')); $this->assertEquals(10 + 9, $calculator->execute('$ten + $nine')); @@ -355,8 +358,14 @@ class MathTest extends TestCase $this->assertEquals(10 / 9, $calculator->execute('$ten / $nine')); $this->assertEquals(10 / (9 / 5), $calculator->execute('$ten / ($nine / $five)')); - $this->expectException(UnknownVariableException::class); - $this->assertEquals(0.0, $calculator->execute('$unsetVariable')); + // test variables without leading $ + $this->assertEquals(100000.01, $calculator->execute('trx_amount')); + $this->assertEquals(10 - 9, $calculator->execute('ten - nine')); + $this->assertEquals(9 - 10, $calculator->execute('nine - ten')); + $this->assertEquals(10 + 9, $calculator->execute('ten + nine')); + $this->assertEquals(10 * 9, $calculator->execute('ten * nine')); + $this->assertEquals(10 / 9, $calculator->execute('ten / nine')); + $this->assertEquals(10 / (9 / 5), $calculator->execute('ten / (nine / five)')); } public function testEvaluateFunctionParameters() @@ -477,4 +486,44 @@ class MathTest extends TestCase $calculator = new MathExecutor(); $this->assertGreaterThan(1, count($calculator->getVars())); } + + public function testUndefinedVarThrowsExecption() + { + $calculator = new MathExecutor(); + $this->assertGreaterThan(1, count($calculator->getVars())); + $this->expectException(UnknownVariableException::class); + $calculator->execute('5 * undefined'); + } + + public function testSetVarsAcceptsAllScalars() + { + $calculator = new MathExecutor(); + $calculator->setVar('boolTrue', true); + $calculator->setVar('boolFalse', false); + $calculator->setVar('int', 1); + $calculator->setVar('float', 1.1); + $calculator->setVar('string', 'string'); + $this->assertEquals(7, count($calculator->getVars())); + } + + public function testSetVarsDoesNoAcceptObject() + { + $calculator = new MathExecutor(); + $this->expectException(MathExecutorException::class); + $calculator->setVar('object', $this); + } + + public function testSetVarsDoesNotAcceptNull() + { + $calculator = new MathExecutor(); + $this->expectException(MathExecutorException::class); + $calculator->setVar('null', null); + } + + public function testSetVarsDoesNotAcceptResource() + { + $calculator = new MathExecutor(); + $this->expectException(MathExecutorException::class); + $calculator->setVar('resource', tmpfile()); + } } |