aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorFatih Kızmaz <barka_21@hotmail.com>2022-05-13 15:55:52 +0300
committerGitHub <noreply@github.com>2022-05-13 15:55:52 +0300
commite21d59c9def9f25bc6a7c3fbd41e2e126d68b1df (patch)
tree379ebf22c76d48aa5897bb9db487c60cae542533 /tests
parentda506a7ce05c6141e3c32f72c351eace38577f89 (diff)
Support unlimited args for min, max default funcs. (#106)
* Support unlimited args for min, max default funcs. Default functions max and min were requiring 2 arguments strictly. Now they supoort unlimited args, same as php's min, max funcs. * Improved functions: support unlimited parameters (see min, max funcs), optional parameters (see round func), parameters with types (see round func, throws IncorrectFunctionParameterException on unmatched type, union types and intersection types not supported because of min php level! there is a todo for this, to support them later @see CustomFunction@execute) Also added unittests for improvements. * Run php-cs-fixer fix
Diffstat (limited to 'tests')
-rw-r--r--tests/MathTest.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/MathTest.php b/tests/MathTest.php
index f82452b..e616721 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -14,6 +14,7 @@ namespace NXP\Tests;
use Exception;
use NXP\Exception\DivisionByZeroException;
use NXP\Exception\IncorrectExpressionException;
+use NXP\Exception\IncorrectFunctionParameterException;
use NXP\Exception\IncorrectNumberOfFunctionParametersException;
use NXP\Exception\MathExecutorException;
use NXP\Exception\UnknownFunctionException;
@@ -243,6 +244,7 @@ class MathTest extends TestCase
['-(4*-2)-5'],
['-(-4*2) - 5'],
['-4*-5'],
+ ['max(1,2,4.9,3)']
];
}
@@ -323,6 +325,45 @@ class MathTest extends TestCase
$this->assertEquals(\round(100 / 30), $calculator->execute('round(100/30)'));
}
+ public function testFunctionUnlimitedParameters() : void
+ {
+ $calculator = new MathExecutor();
+ $calculator->addFunction('max', static function($arg1, $arg2, ...$args) {
+ return \max($arg1, $arg2, ...$args);
+ });
+ $this->assertEquals(\max(4, 6, 8.1, 2, 7), $calculator->execute('max(4,6,8.1,2,7)'));
+ }
+
+ public function testFunctionOptionalParameters() : void
+ {
+ $calculator = new MathExecutor();
+ $calculator->addFunction('round', static function($num, $precision = 0) {
+ return \round($num, $precision);
+ });
+ $this->assertEquals(\round(11.176), $calculator->execute('round(11.176)'));
+ $this->assertEquals(\round(11.176, 2), $calculator->execute('round(11.176,2)'));
+ }
+
+ public function testFunctionParameterTypes() : void
+ {
+ $calculator = new MathExecutor();
+ $this->expectException(IncorrectFunctionParameterException::class);
+ $calculator->addFunction('myfunc', static function(string $name, int $age) {
+ return $name . $age;
+ });
+ $calculator->execute('myfunc(22, "John Doe")');
+ }
+
+ public function testFunctionIncorrectNumberOfParameters() : void
+ {
+ $calculator = new MathExecutor();
+ $this->expectException(IncorrectNumberOfFunctionParametersException::class);
+ $calculator->addFunction('myfunc', static function($arg1, $arg2) {
+ return $arg1 + $arg2;
+ });
+ $calculator->execute('myfunc(1)');
+ }
+
public function testFunctionIf() : void
{
$calculator = new MathExecutor();