From 5d6b4a5dfdea6d4e2814391afcfcd9cf4259c046 Mon Sep 17 00:00:00 2001 From: Fatih Kızmaz Date: Tue, 17 May 2022 00:57:37 +0300 Subject: Full support for arrays => min, max and avg funcs accept array argument. Also array function is defined which return arguments as array. Square bracket arrays are also supported. (#108) valid expression -> "max([1,2,3])" valid expression -> "max(array(1,2,3))" valid expression -> "max($ages_arr)" valid expression -> "max(ages_arr())" --- tests/MathTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'tests') diff --git a/tests/MathTest.php b/tests/MathTest.php index c39eec7..382d67d 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -305,6 +305,22 @@ class MathTest extends TestCase $this->assertEquals(100, $calculator->execute('10 ^ 2')); } + public function testArrays() : void + { + $calculator = new MathExecutor(); + $this->assertEquals([1, 5, 2], $calculator->execute('array(1, 5, 2)')); + $this->assertEquals([1, 5, 2], $calculator->execute('[1, 5, 2]')); + $this->assertEquals(\max([1, 5, 2]), $calculator->execute('max([1, 5, 2])')); + $this->assertEquals(\max([1, 5, 2]), $calculator->execute('max(array(1, 5, 2))')); + $calculator->addFunction('arr_with_max_elements', static function($arg1, ...$args) { + $args = \is_array($arg1) ? $arg1 : [$arg1, ...$args]; + \usort($args, static fn($arr1, $arr2) => \count($arr2) <=> \count($arr1)); + + return $args[0]; + }); + $this->assertEquals([3, 3, 3], $calculator->execute('arr_with_max_elements([[1],array(2,2),[3,3,3]])')); + } + public function testFunctionParameterOrder() : void { $calculator = new MathExecutor(); -- cgit v1.2.3