2020-09-28 15:57:00 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\Fun;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psl\Fun;
|
|
|
|
use Psl\Str;
|
|
|
|
|
|
|
|
final class PipeTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testItCombinesMultipleFunctionToExecutesInOrder(): void
|
|
|
|
{
|
|
|
|
$x = Fun\pipe(
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn (string $x): string => $x . ' world',
|
|
|
|
static fn (string $y): string => $y . '?',
|
|
|
|
static fn (string $z): string => $z . '!',
|
2020-09-28 15:57:00 +02:00
|
|
|
);
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame('Hello world?!', $x('Hello'));
|
2020-09-28 15:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCombinesMultipleFunctionsThatDealWithDifferentTypes(): void
|
|
|
|
{
|
|
|
|
$x = Fun\pipe(
|
2020-10-15 10:18:03 +02:00
|
|
|
static fn (string $x): int => Str\length($x),
|
|
|
|
static fn (int $y): string => $y . '!'
|
2020-09-28 15:57:00 +02:00
|
|
|
);
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame('5!', $x('Hello'));
|
2020-09-28 15:57:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testItCanCreateAnEmptyCombination(): void
|
|
|
|
{
|
|
|
|
$x = Fun\pipe();
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame('Hello', $x('Hello'));
|
2020-09-28 15:57:00 +02:00
|
|
|
}
|
|
|
|
}
|