mirror of
https://github.com/danog/endtoend-test-psl.git
synced 2025-01-22 05:11:47 +01:00
[Iter] add more tests (#41)
This commit is contained in:
parent
788871c9e3
commit
9b9ddbd290
@ -10,7 +10,7 @@ use Generator;
|
||||
* Returns a generator formed by merging the iterable elements of the
|
||||
* given iterable.
|
||||
*
|
||||
* @psalm-template Tk of array-key
|
||||
* @psalm-template Tk
|
||||
* @psalm-template Tv
|
||||
*
|
||||
* @psalm-param iterable<iterable<Tk, Tv>> $iterables
|
||||
|
@ -22,17 +22,18 @@ use Psl\Arr;
|
||||
* Iter\first_key([])
|
||||
* => Null
|
||||
*
|
||||
* @psalm-template Tk of array-key
|
||||
* @psalm-template Tk
|
||||
* @psalm-template Tv
|
||||
*
|
||||
* @psalm-param iterable<Tk, Tv> $iterable
|
||||
*
|
||||
* @psalm-return null|Tk
|
||||
*
|
||||
* @see Arr\first_key()
|
||||
*/
|
||||
function first_key(iterable $iterable)
|
||||
{
|
||||
/** @psalm-var null|Tk */
|
||||
return Arr\first_key(to_array_with_keys($iterable));
|
||||
foreach ($iterable as $k => $_) {
|
||||
return $k;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ use Psl\Gen;
|
||||
* Returns an iterator formed by merging the iterable elements of the
|
||||
* given iterable.
|
||||
*
|
||||
* @psalm-template Tk of array-key
|
||||
* @psalm-template Tk
|
||||
* @psalm-template Tv
|
||||
*
|
||||
* @psalm-param iterable<iterable<Tk, Tv>> $iterables
|
||||
|
@ -4,22 +4,22 @@ declare(strict_types=1);
|
||||
|
||||
namespace Psl\Iter;
|
||||
|
||||
use Psl\Arr;
|
||||
|
||||
/**
|
||||
* Get the last value of an iterable, if the iterable is empty, returns null.
|
||||
*
|
||||
* @psalm-template Tk of array-key
|
||||
* @psalm-template Tv
|
||||
*
|
||||
* @psalm-param array<Tk, Tv> $iterable
|
||||
* @psalm-param iterable<Tk, Tv> $iterable
|
||||
*
|
||||
* @psalm-return null|Tv
|
||||
*
|
||||
* @see Arr\last()
|
||||
*/
|
||||
function last(iterable $iterable)
|
||||
{
|
||||
/** @psalm-var null|Tv */
|
||||
return Arr\last(to_array_with_keys($iterable));
|
||||
$last = null;
|
||||
foreach ($iterable as $v) {
|
||||
$last = $v;
|
||||
}
|
||||
|
||||
return $last;
|
||||
}
|
||||
|
@ -4,22 +4,22 @@ declare(strict_types=1);
|
||||
|
||||
namespace Psl\Iter;
|
||||
|
||||
use Psl\Arr;
|
||||
|
||||
/**
|
||||
* Get the last key of an iterable, if the iterable is empty, null will be returned.
|
||||
*
|
||||
* @psalm-template Tk of array-key
|
||||
* @psalm-template Tk
|
||||
* @psalm-template Tv
|
||||
*
|
||||
* @psalm-param array<Tk, Tv> $iterable
|
||||
* @psalm-param iterable<Tk, Tv> $iterable
|
||||
*
|
||||
* @psalm-return null|Tk
|
||||
*
|
||||
* @see Arr\last_key()
|
||||
*/
|
||||
function last_key(iterable $iterable)
|
||||
{
|
||||
/** @psalm-var null|Tk */
|
||||
return Arr\last_key(to_array_with_keys($iterable));
|
||||
$last = null;
|
||||
foreach ($iterable as $k => $_) {
|
||||
$last = $k;
|
||||
}
|
||||
|
||||
return $last;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Collection;
|
||||
use Psl\Iter;
|
||||
|
||||
class FilterNullsTest extends TestCase
|
||||
@ -16,5 +17,18 @@ class FilterNullsTest extends TestCase
|
||||
self::assertCount(1, Iter\filter_nulls([null, false]));
|
||||
self::assertCount(1, Iter\filter_nulls([null, 'null']));
|
||||
self::assertCount(1, Iter\filter_nulls(['null']));
|
||||
self::assertCount(1, Iter\filter_nulls(new Iter\Iterator(['null'])));
|
||||
self::assertCount(0, Iter\filter_nulls(new Iter\Iterator([null])));
|
||||
self::assertCount(0, Iter\filter_nulls(new Iter\Iterator([null, null])));
|
||||
self::assertCount(3, Iter\filter_nulls(new Iter\Iterator([null, false, '', 0])));
|
||||
self::assertCount(3, Iter\filter_nulls(new Collection\Vector([null, false, '', 0])));
|
||||
self::assertCount(3, Iter\filter_nulls(new Collection\Map([null, false, '', 0])));
|
||||
self::assertCount(3, Iter\filter_nulls((static function (): iterable {
|
||||
yield null;
|
||||
yield false;
|
||||
yield '';
|
||||
yield 0;
|
||||
yield null;
|
||||
})()));
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,48 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Iter;
|
||||
|
||||
class FilterTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testFilter(array $expected, iterable $iterable, ?callable $predicate = null): void
|
||||
{
|
||||
$result = Iter\filter($iterable, $predicate);
|
||||
|
||||
self::assertSame($expected, Iter\to_array_with_keys($result));
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [[], []];
|
||||
yield [['a', 'b'], ['a', 'b']];
|
||||
yield [[], ['a', 'b'], fn () => false];
|
||||
yield [['a', 'b'], ['a', 'b'], fn (string $_): bool => true];
|
||||
yield [['a'], ['a', 'b'], fn (string $v): bool => 'b' !== $v];
|
||||
|
||||
yield [['a', 'b'], new Iter\Iterator(['a', 'b'])];
|
||||
yield [[], new Iter\Iterator(['a', 'b']), fn () => false];
|
||||
yield [['a', 'b'], new Iter\Iterator(['a', 'b']), fn (string $_): bool => true];
|
||||
yield [['a'], new Iter\Iterator(['a', 'b']), fn (string $v): bool => 'b' !== $v];
|
||||
|
||||
yield [['a', 'b'], (static function () {
|
||||
yield 'a';
|
||||
yield 'b';
|
||||
})()];
|
||||
yield [[], (static function () {
|
||||
yield 'a';
|
||||
yield 'b';
|
||||
})(), fn () => false];
|
||||
yield [['a', 'b'], (static function () {
|
||||
yield 'a';
|
||||
yield 'b';
|
||||
})(), fn (string $_): bool => true];
|
||||
yield [['a'], (static function () {
|
||||
yield 'a';
|
||||
yield 'b';
|
||||
})(), fn (string $v): bool => 'b' !== $v];
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,46 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Collection;
|
||||
use Psl\Iter;
|
||||
|
||||
class FilterWithKeyTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testFilterWithKey(array $expected, iterable $iterable, ?callable $predicate = null): void
|
||||
{
|
||||
$result = Iter\filter_with_key($iterable, $predicate);
|
||||
|
||||
self::assertSame($expected, Iter\to_array_with_keys($result));
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [[], []];
|
||||
yield [['a', 'b'], ['a', 'b']];
|
||||
yield [[], ['a', 'b'], fn (int $_k, string $_v) => false];
|
||||
yield [['a', 'b'], ['a', 'b'], fn (int $_k, string $_v): bool => true];
|
||||
yield [['a'], ['a', 'b'], fn (int $k, string $v): bool => 'b' !== $v];
|
||||
yield [[], ['a', 'b'], fn (int $k, string $v): bool => 'b' !== $v && 0 !== $k];
|
||||
yield [['a'], ['a', 'b'], fn (int $k, string $v): bool => 'b' !== $v && 1 !== $k];
|
||||
yield [[], ['a', 'b'], fn (int $k, string $v): bool => 'a' !== $v && 1 !== $k];
|
||||
yield [[1 => 'b'], ['a', 'b'], fn (int $k, string $v): bool => 'a' !== $v && 0 !== $k];
|
||||
|
||||
yield [[], new \SplFixedArray(0)];
|
||||
yield [['a', 'b'], new Collection\Vector(['a', 'b'])];
|
||||
yield [[], new Collection\Map(['a', 'b']), fn (int $_k, string $_v) => false];
|
||||
yield [['a', 'b'], new Iter\Iterator(['a', 'b']), fn (int $_k, string $_v): bool => true];
|
||||
yield [['a'], new Collection\MutableMap(['a', 'b']), fn (int $k, string $v): bool => 'b' !== $v];
|
||||
yield [[], new Collection\MutableVector(['a', 'b']), fn (int $k, string $v): bool => 'b' !== $v && 0 !== $k];
|
||||
yield [['a'], new Iter\Iterator(['a', 'b']), fn (int $k, string $v): bool => 'b' !== $v && 1 !== $k];
|
||||
yield [[], new \ArrayIterator(['a', 'b']), fn (int $k, string $v): bool => 'a' !== $v && 1 !== $k];
|
||||
yield [[1 => 'b'], new \ArrayObject(['a', 'b']), fn (int $k, string $v): bool => 'a' !== $v && 0 !== $k];
|
||||
|
||||
$doublyLinkedList = new \SplDoublyLinkedList();
|
||||
$doublyLinkedList->add(0, 'a');
|
||||
$doublyLinkedList->add(1, 'b');
|
||||
yield [['a', 'b'], $doublyLinkedList];
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,36 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Collection;
|
||||
use Psl\Iter;
|
||||
|
||||
class FirstKeyTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testFirstKey($expected, iterable $iterable): void
|
||||
{
|
||||
$result = Iter\first_key($iterable);
|
||||
|
||||
self::assertSame($expected, $result);
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [null, []];
|
||||
yield [null, new \SplDoublyLinkedList()];
|
||||
yield ['a', ['a' => 'b']];
|
||||
yield [0, ['a', 'b']];
|
||||
yield [0, new Collection\Vector(['a', 'b'])];
|
||||
yield [0, new Collection\Vector(['a' => 'b'])];
|
||||
yield ['a', new Collection\Map(['a' => 'b'])];
|
||||
yield [null, (static function () {
|
||||
yield null => null;
|
||||
})()];
|
||||
yield [null, (static function () {
|
||||
return;
|
||||
yield;
|
||||
})()];
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,36 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Collection;
|
||||
use Psl\Iter;
|
||||
|
||||
class FirstTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testFirstKey($expected, iterable $iterable): void
|
||||
{
|
||||
$result = Iter\first($iterable);
|
||||
|
||||
self::assertSame($expected, $result);
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [null, []];
|
||||
yield [null, new \SplDoublyLinkedList()];
|
||||
yield ['b', ['a' => 'b']];
|
||||
yield ['a', ['a', 'b']];
|
||||
yield ['a', new Collection\Vector(['a', 'b'])];
|
||||
yield ['b', new Collection\Vector(['a' => 'b'])];
|
||||
yield ['b', new Collection\Map(['a' => 'b'])];
|
||||
yield [null, (static function () {
|
||||
yield null => null;
|
||||
})()];
|
||||
yield [null, (static function () {
|
||||
return;
|
||||
yield;
|
||||
})()];
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,41 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Collection;
|
||||
use Psl\Iter;
|
||||
|
||||
class FlattenTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testFlatten(): void
|
||||
{
|
||||
$result = Iter\flatten([
|
||||
[1, 2, 3],
|
||||
new Collection\Map(['a' => 'b', 'c' => 'd', 'e' => 'f']),
|
||||
(fn () => yield 'hey' => 'hello')()
|
||||
]);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,28 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Collection;
|
||||
use Psl\Iter;
|
||||
|
||||
class FlipTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testFlip(): void
|
||||
{
|
||||
$a = new Collection\MutableVector(['a', 'b']);
|
||||
$b = new Collection\MutableVector(['c', 'd']);
|
||||
|
||||
$iterable = new Iter\Iterator(['a' => $a, 'b' => $b]);
|
||||
|
||||
static::assertSame('a', Iter\first_key($iterable));
|
||||
static::assertSame($a, Iter\first($iterable));
|
||||
static::assertSame('b', Iter\last_key($iterable));
|
||||
static::assertSame($b, Iter\last($iterable));
|
||||
|
||||
$result = Iter\flip($iterable);
|
||||
|
||||
static::assertSame($a, Iter\first_key($result));
|
||||
static::assertSame('a', Iter\first($result));
|
||||
static::assertSame($b, Iter\last_key($result));
|
||||
static::assertSame('b', Iter\last($result));
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,41 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Iter;
|
||||
|
||||
class FromEntriesTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testEmptyEntries(): void
|
||||
{
|
||||
$actual = Iter\from_entries([]);
|
||||
self::assertCount(0, $actual);
|
||||
}
|
||||
|
||||
public function testFromEntries(): void
|
||||
{
|
||||
$actual = Iter\from_entries([
|
||||
[1, 'hello'],
|
||||
[2, 'world']
|
||||
]);
|
||||
|
||||
$array = Iter\to_array_with_keys($actual);
|
||||
|
||||
self::assertCount(2, $array);
|
||||
self::assertSame('hello', $array[1]);
|
||||
self::assertSame('world', $array[2]);
|
||||
}
|
||||
|
||||
public function testFromEntriesWithNonArrayKeyKeys(): void
|
||||
{
|
||||
$actual = Iter\from_entries([
|
||||
[['1', '2'], 'hello'],
|
||||
[['3', '4'], 'world']
|
||||
]);
|
||||
|
||||
self::assertSame('hello', Iter\first($actual));
|
||||
self::assertSame('world', Iter\last($actual));
|
||||
|
||||
self::assertSame(['1', '2'], Iter\first_key($actual));
|
||||
self::assertSame(['3', '4'], Iter\last_key($actual));
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,30 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Iter;
|
||||
|
||||
class FromKeysTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
public function testFromKeys(): void
|
||||
{
|
||||
$actual = Iter\from_keys(['hello', 'world'], fn (string $_key) => false);
|
||||
|
||||
self::assertSame('hello', Iter\first_key($actual));
|
||||
self::assertSame('world', Iter\last_key($actual));
|
||||
self::assertFalse(Iter\first($actual));
|
||||
self::assertFalse(Iter\last($actual));
|
||||
}
|
||||
|
||||
public function testFromKeysWithNonArrayKeyKeys(): void
|
||||
{
|
||||
$actual = Iter\from_keys([
|
||||
[1, 'hello'],
|
||||
[2, 'world']
|
||||
], fn (array $k) => $k[1]);
|
||||
|
||||
self::assertSame([1, 'hello'], Iter\first_key($actual));
|
||||
self::assertSame([2, 'world'], Iter\last_key($actual));
|
||||
self::assertSame('hello', Iter\first($actual));
|
||||
self::assertSame('world', Iter\last($actual));
|
||||
}
|
||||
}
|
||||
|
@ -5,8 +5,26 @@ declare(strict_types=1);
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Iter;
|
||||
|
||||
class IsEmptyTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
/**
|
||||
* @dataProvider provideData
|
||||
*/
|
||||
public function testIsEmpty(bool $expected, iterable $iterable): void
|
||||
{
|
||||
self::assertSame($expected, Iter\is_empty($iterable));
|
||||
}
|
||||
|
||||
public function provideData(): iterable
|
||||
{
|
||||
yield [true, []];
|
||||
yield [true, Iter\from_entries([])];
|
||||
yield [true, (fn () => yield from [])()];
|
||||
|
||||
yield [false, [null]];
|
||||
yield [false, [false]];
|
||||
yield [false, ['hello', 'world']];
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Iter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RewindableTest extends TestCase
|
||||
{
|
||||
// TODO: add tests.
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user