2019-12-24 01:52:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\Iter;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-08-08 06:35:34 +02:00
|
|
|
use Psl\Collection;
|
|
|
|
use Psl\Iter;
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
final class FlattenTest extends TestCase
|
2019-12-24 01:52:07 +01:00
|
|
|
{
|
2020-08-08 06:35:34 +02:00
|
|
|
public function testFlatten(): void
|
|
|
|
{
|
|
|
|
$result = Iter\flatten([
|
|
|
|
[1, 2, 3],
|
|
|
|
new Collection\Map(['a' => 'b', 'c' => 'd', 'e' => 'f']),
|
2020-10-15 10:18:03 +02:00
|
|
|
(static fn () => yield 'hey' => 'hello')()
|
2020-08-08 06:35:34 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
static::assertSame([
|
|
|
|
0 => 1,
|
|
|
|
1 => 2,
|
|
|
|
2 => 3,
|
|
|
|
'a' => 'b',
|
|
|
|
'c' => 'd',
|
|
|
|
'e' => 'f',
|
|
|
|
'hey' => 'hello'
|
|
|
|
], Iter\to_array_with_keys($result));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFlattenSameKeys(): void
|
|
|
|
{
|
|
|
|
$result = Iter\flatten(new Collection\Vector([
|
|
|
|
new Collection\MutableMap(['a' => 'b']),
|
|
|
|
['a' => 'foo']
|
|
|
|
]));
|
|
|
|
|
|
|
|
static::assertSame('a', Iter\first_key($result));
|
|
|
|
static::assertSame('b', Iter\first($result));
|
|
|
|
|
|
|
|
static::assertSame('a', Iter\last_key($result));
|
|
|
|
static::assertSame('foo', Iter\last($result));
|
|
|
|
}
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|