aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorFatih Kızmaz <barka_21@hotmail.com>2022-05-17 00:57:37 +0300
committerGitHub <noreply@github.com>2022-05-17 00:57:37 +0300
commit5d6b4a5dfdea6d4e2814391afcfcd9cf4259c046 (patch)
tree7b793b462a6d59748eb0a8faff18ad81ee4dae4e /tests
parent2874b1134189634853c3afaaf46c045f66d51ab9 (diff)
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())"
Diffstat (limited to 'tests')
-rw-r--r--tests/MathTest.php16
1 files changed, 16 insertions, 0 deletions
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();