endtoend-test-psl/tests/Psl/Iter/MapTest.php

32 lines
927 B
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
2020-09-01 07:50:23 +02:00
use Psl\Iter;
2019-12-24 01:52:07 +01:00
class MapTest extends TestCase
{
2020-09-01 07:50:23 +02:00
/**
* @dataProvider provideData
*/
public function testMap(array $expected, iterable $iterable, callable $function): void
{
$result = Iter\map($iterable, $function);
self::assertSame($expected, Iter\to_array_with_keys($result));
}
public function provideData(): iterable
{
2020-09-01 07:56:15 +02:00
yield [[1, 2, 3], [1, 2, 3], fn (int $v): int => $v];
yield [[2, 4, 6], [1, 2, 3], fn (int $v): int => $v * 2];
yield [['1', '2', '3'], [1, 2, 3], fn (int $v): string => (string) $v];
yield [[], [], fn (int $k): string => (string) $v];
yield [[], Iter\Iterator::create([]), fn (int $v): string => (string) $v];
yield [['1', '2'], Iter\Iterator::create([1, 2]), fn (int $v): string => (string) $v];
2020-09-01 07:50:23 +02:00
}
2019-12-24 01:52:07 +01:00
}