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

32 lines
1.1 KiB
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
final class MapWithKeyTest extends TestCase
2019-12-24 01:52:07 +01:00
{
2020-09-01 07:50:23 +02:00
/**
* @dataProvider provideData
*/
public function testMapWithKey(array $expected, iterable $iterable, callable $function): void
{
$result = Iter\map_with_key($iterable, $function);
static::assertSame($expected, Iter\to_array_with_keys($result));
2020-09-01 07:50:23 +02:00
}
public function provideData(): iterable
{
yield [[1, 3, 5], [1, 2, 3], static fn (int $k, int $v): int => $k + $v];
yield [[0, 4, 16], [1, 2, 3], static fn (int $k, int $v): int => $k * (2 ** $v)];
yield [['1', '3', '5'], [1, 2, 3], static fn (int $k, int $v): string => (string) ($k + $v)];
yield [[], [], static fn (int $k, int $v): string => (string) ($k + $v)];
yield [[], Iter\Iterator::create([]), static fn (int $k, int $v): string => (string) ($k + $v)];
yield [['1', '3'], Iter\Iterator::create([1, 2]), static fn (int $k, int $v): string => (string) ($k + $v)];
2020-09-01 07:50:23 +02:00
}
2019-12-24 01:52:07 +01:00
}