From 231c9242a4d0fd131d9ee7def68b01e349cee2b3 Mon Sep 17 00:00:00 2001 From: widmogrod Date: Wed, 20 Dec 2017 18:42:55 +0100 Subject: [PATCH] Functiona\reverse tests --- src/Functional/functions.php | 6 ++-- test/Functional/ReverseTest.php | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 test/Functional/ReverseTest.php diff --git a/src/Functional/functions.php b/src/Functional/functions.php index f8aa857..2d3b403 100644 --- a/src/Functional/functions.php +++ b/src/Functional/functions.php @@ -138,6 +138,8 @@ function tee(callable $function = null, $value = null) const reverse = 'Widmogrod\Functional\reverse'; /** + * reverse :: (a -> b -> c -> d) -> (c -> b -> a -> d) + * * Call $function with arguments in reversed order * * @return \Closure @@ -146,8 +148,8 @@ const reverse = 'Widmogrod\Functional\reverse'; */ function reverse(callable $function) { - return function () use ($function) { - return call_user_func_array($function, array_reverse(func_get_args())); + return function (...$args) use ($function) { + return $function(...array_reverse($args)); }; } diff --git a/test/Functional/ReverseTest.php b/test/Functional/ReverseTest.php new file mode 100644 index 0000000..3a0bd28 --- /dev/null +++ b/test/Functional/ReverseTest.php @@ -0,0 +1,73 @@ +assertSame( + $original(...$args), + $function(...$args), + 'Reverse of revert function should be the same function' + ); + $this->assertSame( + $reversed(...array_reverse($args)), + $function(...$args), + 'Reversing arguments to reverse function should result in the same value as the function' + ); + } + + public function provideData() + { + return [ + 'non argument function' => [ + '$function' => function () { + return 1; + }, + '$value' => [], + ], + 'non argument function but with args' => [ + '$function' => function () { + return 1; + }, + '$value' => [1, 2, 3], + ], + 'many args' => [ + '$function' => function ($a, $b, $c, $d) { + return ($a - $c) * pow($b, $d); + }, + '$value' => [ + random_int(-10, 10), + random_int(-10, 10), + random_int(-10, 10), + random_int(-10, 10), + ], + ], + 'variadic args' => [ + '$function' => function (...$args) { + return array_product($args); + }, + '$value' => [ + random_int(-10, 10), + random_int(-10, 10), + random_int(-10, 10), + random_int(-10, 10), + random_int(-10, 10), + random_int(-10, 10), + random_int(-10, 10), + ], + ], + ]; + } +} -- 2.11.0