endtoend-test-psl/tests/Psl/Dict/PartitionWithKeyTest.php
2021-02-16 00:23:01 +01:00

68 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Psl\Tests\Dict;
use PHPUnit\Framework\TestCase;
use Psl\Dict;
use Psl\Str;
final class PartitionWithKeyTest extends TestCase
{
/**
* @dataProvider provideData
*/
public function testPartition(array $expected, array $array, callable $predicate): void
{
static::assertSame($expected, Dict\partition_with_key($array, $predicate));
}
public function provideData(): array
{
return [
[
[[1 => 'bar', 2 => 'baz'], [0 => 'foo', 3 => 'qux']],
['foo', 'bar', 'baz', 'qux'],
static fn (int $k, string $str) => Str\starts_with($str, 'b'),
],
[
[[0 => 'foo', 3 => 'qux'], [1 => 'bar', 2 => 'baz']],
['foo', 'bar', 'baz', 'qux'],
static fn (int $k, string $str) => !Str\starts_with($str, 'b'),
],
[
[[], []],
[],
static fn ($_k, $_v) => false,
],
[
[[], ['foo', 'bar', 'baz', 'qux']],
['foo', 'bar', 'baz', 'qux'],
static fn (int $k, string $str) => false,
],
[
[['foo', 'bar', 'baz', 'qux'], []],
['foo', 'bar', 'baz', 'qux'],
static fn (int $k, string $str) => true,
],
[
[[1 => 'bar', 2 => 'baz', 3 => 'qux'], ['foo']],
['foo', 'bar', 'baz', 'qux'],
static fn (int $k, string $str) => (bool) $k,
],
[
[['foo'], [1 => 'bar', 2 => 'baz', 3 => 'qux']],
['foo', 'bar', 'baz', 'qux'],
static fn (int $k, string $str) => !((bool) $k),
],
];
}
}