|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @package Functional-php |
| 5 | + * @author Lars Strojny <lstrojny@php.net> |
| 6 | + * @copyright 2011-2021 Lars Strojny |
| 7 | + * @license https://opensource.org/licenses/MIT MIT |
| 8 | + * @link https://github.com/lstrojny/functional-php |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Functional\Tests; |
| 12 | + |
| 13 | +use function Functional\juxt; |
| 14 | + |
| 15 | +class JuxtTest extends AbstractTestCase |
| 16 | +{ |
| 17 | + public function test(): void |
| 18 | + { |
| 19 | + $juxted = juxt('strtoupper', 'strtolower', 'strrev'); |
| 20 | + |
| 21 | + self::assertSame(['HELLO', 'hello', 'olleH'], $juxted('Hello')); |
| 22 | + } |
| 23 | + |
| 24 | + public function testWithMultipleArguments(): void |
| 25 | + { |
| 26 | + $minMax = juxt('min', 'max'); |
| 27 | + |
| 28 | + self::assertSame([1, 5], $minMax(3, 1, 5, 2)); |
| 29 | + } |
| 30 | + |
| 31 | + public function testWithArrayFunctions(): void |
| 32 | + { |
| 33 | + $stats = juxt('array_sum', 'count'); |
| 34 | + |
| 35 | + self::assertSame([15, 5], $stats([1, 2, 3, 4, 5])); |
| 36 | + } |
| 37 | + |
| 38 | + public function testWithClosures(): void |
| 39 | + { |
| 40 | + $double = static function ($x) { |
| 41 | + return $x * 2; |
| 42 | + }; |
| 43 | + $square = static function ($x) { |
| 44 | + return $x * $x; |
| 45 | + }; |
| 46 | + $negate = static function ($x) { |
| 47 | + return -$x; |
| 48 | + }; |
| 49 | + |
| 50 | + $juxted = juxt($double, $square, $negate); |
| 51 | + |
| 52 | + self::assertSame([10, 25, -5], $juxted(5)); |
| 53 | + } |
| 54 | + |
| 55 | + public function testWithNoFunctions(): void |
| 56 | + { |
| 57 | + $juxted = juxt(); |
| 58 | + |
| 59 | + self::assertSame([], $juxted('anything')); |
| 60 | + } |
| 61 | + |
| 62 | + public function testWithSingleFunction(): void |
| 63 | + { |
| 64 | + $juxted = juxt('strlen'); |
| 65 | + |
| 66 | + self::assertSame([5], $juxted('hello')); |
| 67 | + } |
| 68 | +} |
0 commit comments