aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJavier Marín <javier@marinros.com>2020-07-26 05:27:26 +0300
committerGitHub <noreply@github.com>2020-07-26 05:27:26 +0300
commitc1e07f254a4e952868be8240080661628aef8e69 (patch)
treeb7ff2fa4c478a9ccc2fd0d17c64d54732f3391fb /tests
parentaa8ffe19f2eb6f194b3e6ee1aac4c2706659e6b9 (diff)
Handler for not found variables (#68)
* Added handler to define not found variables Added support for string variables Fixed strings and ints comparison error * Check if variables have scalar types (int, float, string and bool) Better $onVarNotFound logic
Diffstat (limited to 'tests')
-rw-r--r--tests/MathTest.php31
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php
index ee9c6bd..d76a57c 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -409,6 +409,37 @@ class MathTest extends TestCase
$this->assertEquals(1, $calculator->execute('(-4 + 5)'));
}
+ public function testStringComparison()
+ {
+ $calculator = new MathExecutor();
+ $this->assertEquals(true, $calculator->execute('"a" == \'a\''));
+ $this->assertEquals(true, $calculator->execute('"hello world" == "hello world"'));
+ $this->assertEquals(false, $calculator->execute('"hello world" == "hola mundo"'));
+ $this->assertEquals(true, $calculator->execute('"hello world" != "hola mundo"'));
+ }
+
+ public function testVarStringComparison()
+ {
+ $calculator = new MathExecutor();
+ $calculator->setVar('var', 0);
+ $this->assertEquals($calculator->execute('0 == "a"'), $calculator->execute('var == "a"'));
+ }
+
+ public function testOnVarNotFound()
+ {
+ $calculator = new MathExecutor();
+ $calculator->setVarNotFoundHandler(
+ function ($varName) {
+ if ($varName == 'undefined') {
+ return 3;
+ } else {
+ return null;
+ }
+ }
+ );
+ $this->assertEquals(15, $calculator->execute('5 * undefined'));
+ }
+
public function testMinusZero()
{
$calculator = new MathExecutor();