aboutsummaryrefslogtreecommitdiff
path: root/tests/MathTest.php
diff options
context:
space:
mode:
authorBruce Wells <brucekwells@gmail.com>2020-06-04 18:43:16 +0300
committerGitHub <noreply@github.com>2020-06-04 18:43:16 +0300
commitaa8ffe19f2eb6f194b3e6ee1aac4c2706659e6b9 (patch)
tree55ef6ebce4cdcb53bfe7cc142ef40f90756d9b6c /tests/MathTest.php
parentea898d7a7b85aeb677f81f13784232c99b61808a (diff)
Variable fixes (#67)V2.0.3
* Reproduce if throws UnknownOperatorException * Fix variable detection * Adding IncorrectNumberOfFunctionParametersException * Removing tabs * Better exception message text
Diffstat (limited to 'tests/MathTest.php')
-rw-r--r--tests/MathTest.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php
index 6108bd8..ee9c6bd 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -14,7 +14,9 @@ namespace NXP\Tests;
use Exception;
use NXP\Exception\DivisionByZeroException;
use NXP\Exception\IncorrectExpressionException;
+use NXP\Exception\IncorrectNumberOfFunctionParametersException;
use NXP\Exception\UnknownFunctionException;
+use NXP\Exception\UnknownVariableException;
use NXP\MathExecutor;
use PHPUnit\Framework\TestCase;
@@ -234,6 +236,18 @@ class MathTest extends TestCase
$calculator = new MathExecutor();
$this->expectException(DivisionByZeroException::class);
$calculator->execute('10 / 0');
+ $calculator->setVar('one', 1)->setVar('zero', 0);
+ $this->assertEquals(0.0, $calculator->execute('$one / $zero'));
+ }
+
+ public function testVariableIncorrectExpressionException()
+ {
+ $calculator = new MathExecutor();
+ $calculator->setVar('four', 4);
+ $this->assertEquals(4, $calculator->execute('$four'));
+ $this->expectException(IncorrectExpressionException::class);
+ $this->assertEquals(0.0, $calculator->execute('$'));
+ $this->assertEquals(0.0, $calculator->execute('$ + $four'));
}
public function testExponentiation()
@@ -289,6 +303,60 @@ class MathTest extends TestCase
$this->assertEquals(cos(2), $calculator->execute(
'if(cos(2), cos(2), 0)'
));
+ $trx_amount = 100000;
+ $calculator->setVar('trx_amount', $trx_amount);
+ $this->assertEquals($trx_amount, $calculator->execute('$trx_amount'));
+ $this->assertEquals($trx_amount * 0.03, $calculator->execute(
+ 'if($trx_amount < 40000, $trx_amount * 0.06, $trx_amount * 0.03)'
+ ));
+ $this->assertEquals($trx_amount * 0.03, $calculator->execute(
+ 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
+ ));
+ $trx_amount = 39000;
+ $calculator->setVar('trx_amount', $trx_amount);
+ $this->assertEquals($trx_amount * 0.06, $calculator->execute(
+ 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
+ ));
+ $trx_amount = 59000;
+ $calculator->setVar('trx_amount', $trx_amount);
+ $this->assertEquals($trx_amount * 0.05, $calculator->execute(
+ 'if($trx_amount < 40000, $trx_amount * 0.06, if($trx_amount < 60000, $trx_amount * 0.05, $trx_amount * 0.03))'
+ ));
+ $this->expectException(IncorrectNumberOfFunctionParametersException::class);
+ $this->assertEquals(0.0, $calculator->execute(
+ 'if($trx_amount < 40000, $trx_amount * 0.06)'
+ ));
+ }
+
+ public function testVariables()
+ {
+ $calculator = new MathExecutor();
+ $this->assertEquals(3.14159265359, $calculator->execute('$pi'));
+ $this->assertEquals(2.71828182846, $calculator->execute('$e'));
+ $calculator->setVars([
+ 'trx_amount' => 100000,
+ 'ten' => 10,
+ 'nine' => 9,
+ 'eight' => 8,
+ 'seven' => 7,
+ 'six' => 6,
+ 'five' => 5,
+ 'four' => 4,
+ 'three' => 3,
+ 'two' => 2,
+ 'one' => 1,
+ 'zero' => 0,
+ ]);
+ $this->assertEquals(100000, $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)'));
+
+ $this->expectException(UnknownVariableException::class);
+ $this->assertEquals(0.0, $calculator->execute('$unsetVariable'));
}
public function testEvaluateFunctionParameters()