Skip to content

Commit

Permalink
Release prep (#69)
Browse files Browse the repository at this point in the history
* String comparison unit tests

* getVars and getFunctions sanity checks

* Add dynamic variable documentation
  • Loading branch information
phpfui authored Jul 27, 2020
1 parent c1e07f2 commit b38893d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Conditional If logic
* Support for user defined operators
* Support for user defined functions
* Dynamic variable resolution (delayed computation)
* Unlimited variable name lengths
* String support, as function parameters or as evaluated as a number by PHP
* Exceptions on divide by zero, or treat as zero
Expand Down Expand Up @@ -128,6 +129,21 @@ $executor->setVar('var1', 0.15)->setVar('var2', 0.22);

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.

```php
$calculator = new MathExecutor();
$calculator->setVarNotFoundHandler(
function ($varName) {
if ($varName == 'trans') {
return transmogrify();
}
return null;
}
);
```

## Division By Zero Support:
Division by zero throws a `\NXP\Exception\DivisionByZeroException` by default
```php
Expand Down
39 changes: 35 additions & 4 deletions tests/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,21 @@ public function testStringComparison()
$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"'));
$this->assertEquals(true, $calculator->execute('"a" < "b"'));
$this->assertEquals(false, $calculator->execute('"a" > "b"'));
$this->assertEquals(true, $calculator->execute('"a" <= "b"'));
$this->assertEquals(false, $calculator->execute('"a" >= "b"'));
$this->assertEquals(true, $calculator->execute('"A" != "a"'));
}

public function testVarStringComparison()
{
$calculator = new MathExecutor();
$calculator->setVar('var', 0);
$this->assertEquals($calculator->execute('0 == "a"'), $calculator->execute('var == "a"'));
$calculator->setVar('var', 97);
$this->assertEquals(false, $calculator->execute('97 == "a"'));
$this->assertEquals(false, $calculator->execute('$var == "a"'));
$calculator->setVar('var', 'a');
$this->assertEquals(true, $calculator->execute('$var == "a"'));
}

public function testOnVarNotFound()
Expand All @@ -432,9 +440,8 @@ public function testOnVarNotFound()
function ($varName) {
if ($varName == 'undefined') {
return 3;
} else {
return null;
}
return null;
}
);
$this->assertEquals(15, $calculator->execute('5 * undefined'));
Expand All @@ -446,4 +453,28 @@ public function testMinusZero()
$this->assertEquals(1, $calculator->execute('1 - 0'));
$this->assertEquals(1, $calculator->execute('1-0'));
}

public function testGetFunctionsReturnsArray()
{
$calculator = new MathExecutor();
$this->assertIsArray($calculator->getFunctions());
}

public function testGetFunctionsReturnsFunctions()
{
$calculator = new MathExecutor();
$this->assertGreaterThan(40, count($calculator->getFunctions()));
}

public function testGetVarsReturnsArray()
{
$calculator = new MathExecutor();
$this->assertIsArray($calculator->getVars());
}

public function testGetVarsReturnsCount()
{
$calculator = new MathExecutor();
$this->assertGreaterThan(1, count($calculator->getVars()));
}
}

0 comments on commit b38893d

Please sign in to comment.