aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md6
-rw-r--r--src/NXP/MathExecutor.php3
-rw-r--r--tests/MathTest.php57
3 files changed, 59 insertions, 7 deletions
diff --git a/README.md b/README.md
index 69651d5..f88e5e3 100644
--- a/README.md
+++ b/README.md
@@ -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());
+ }
}