Skip to content

Commit

Permalink
Improved support for null variables (#72)
Browse files Browse the repository at this point in the history
* 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

* Better support for null variables

* Better support for null variables

* Better support for null variables
  • Loading branch information
javiermarinros authored Sep 15, 2020
1 parent 44d72cc commit f8faf3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/NXP/Classes/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public function calculate(array $tokens, array $variables, callable $onVarNotFou
$value = $variables[$variable];
} elseif ($onVarNotFound) {
$value = call_user_func($onVarNotFound, $variable);
}

if (!isset($value)) {
} else {
throw new UnknownVariableException($variable);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NXP/MathExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function getVars() : array
*/
public function getVar(string $variable)
{
if (!isset($this->variables[$variable])) {
if (!array_key_exists($variable, $this->variables)) {
throw new UnknownVariableException("Variable ({$variable}) not set");
}
return $this->variables[$variable];
Expand Down
42 changes: 21 additions & 21 deletions tests/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ public function testZeroDivisionException()
$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'));
$calculator->setVar('one', 1)->setVar('zero', 0);
$this->assertEquals(0.0, $calculator->execute('$one / $zero'));
}

public function testVariableIncorrectExpressionException()
Expand Down Expand Up @@ -337,19 +337,19 @@ public function testVariables()
$this->assertEquals(2.71828182846, $calculator->execute('$e'));
$this->assertEquals(2.71828182846, $calculator->execute('e'));
$calculator->setVars([
'trx_amount' => 100000.01,
'ten' => 10,
'nine' => 9,
'eight' => 8,
'seven' => 7,
'six' => 6,
'five' => 5,
'four' => 4,
'three' => 3,
'two' => 2,
'one' => 1,
'zero' => 0,
]);
'trx_amount' => 100000.01,
'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.01, $calculator->execute('$trx_amount'));
$this->assertEquals(10 - 9, $calculator->execute('$ten - $nine'));
$this->assertEquals(9 - 10, $calculator->execute('$nine - $ten'));
Expand All @@ -374,8 +374,8 @@ public function testEvaluateFunctionParameters()
$calculator->addFunction(
'round',
function ($value, $decimals) {
return round($value, $decimals);
}
return round($value, $decimals);
}
);
$expression = 'round(100 * 1.111111, 2)';
$phpResult = 0;
Expand Down Expand Up @@ -403,9 +403,9 @@ public function testQuotes()
$calculator->addFunction(
'test',
function ($arg) use ($testString) {
$this->assertEquals($testString, $arg);
return 0;
}
$this->assertEquals($testString, $arg);
return 0;
}
);
$calculator->execute('test("' . $testString . '")'); // single quotes
$calculator->execute("test('" . $testString . "')"); // double quotes
Expand Down Expand Up @@ -526,4 +526,4 @@ public function testSetVarsDoesNotAcceptResource()
$this->expectException(MathExecutorException::class);
$calculator->setVar('resource', tmpfile());
}
}
}

0 comments on commit f8faf3f

Please sign in to comment.