endtoend-test-psl/tests/Psl/Collection/AbstractMapTest.php

579 lines
17 KiB
PHP
Raw Normal View History

2020-02-21 02:03:40 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Collection;
2020-03-01 21:52:25 +01:00
use PHPUnit\Framework\TestCase;
2020-08-08 06:44:37 +02:00
use Psl\Collection\MapInterface;
use Psl\Collection\VectorInterface;
2020-02-21 02:03:40 +01:00
use Psl\Exception\InvariantViolationException;
2020-03-01 21:52:25 +01:00
use Psl\Str;
2020-02-21 02:03:40 +01:00
/**
* @covers \Psl\Collection\AbstractMap
* @covers \Psl\Collection\AbstractAccessibleCollection
*/
abstract class AbstractMapTest extends TestCase
{
/**
* The Map class being currently tested.
*
* @psalm-var class-string<IMap>
*/
2020-08-08 06:44:37 +02:00
protected string $mapClass = MapInterface::class;
2020-02-21 02:03:40 +01:00
/**
* The Vector class used for values, keys .. etc.
*
* @psalm-var class-string<IVector>
*/
2020-08-08 06:44:37 +02:00
protected string $vectorClass = VectorInterface::class;
2020-02-21 02:03:40 +01:00
public function testIsEmpty(): void
{
self::assertTrue($this->create([])->isEmpty());
self::assertFalse($this->create(['foo' => 'bar'])->isEmpty());
self::assertEmpty($this->create(['foo' => null])->isEmpty());
2020-02-21 02:03:40 +01:00
}
public function testCount(): void
{
self::assertCount(0, $this->create([]));
self::assertCount(1, $this->create(['foo' => 'bar']));
self::assertSame(5, $this->create([
2020-02-21 02:03:40 +01:00
1 => 'foo',
2 => 'bar',
4 => 'baz',
8 => 'qux',
16 => 'hax' // ??
])->count());
}
public function testValues(): void
{
$map = $this->create([
'foo' => 1,
'bar' => 2,
'baz' => 3,
]);
$values = $map->values();
self::assertInstanceOf($this->vectorClass, $values);
2020-02-21 02:03:40 +01:00
self::assertCount(3, $values);
2020-02-21 02:03:40 +01:00
self::assertSame(1, $values->at(0));
self::assertSame(2, $values->at(1));
self::assertSame(3, $values->at(2));
2020-02-21 02:03:40 +01:00
$map = $this->create([]);
2020-02-21 02:03:40 +01:00
$values = $map->values();
self::assertInstanceOf($this->vectorClass, $values);
2020-02-21 02:03:40 +01:00
self::assertCount(0, $values);
2020-02-21 02:03:40 +01:00
}
2020-09-01 07:50:56 +02:00
public function testJsonSerialize(): void
{
$map = $this->create([
'foo' => 1,
'bar' => 2,
'baz' => 3,
]);
$array = $map->jsonSerialize();
static::assertSame([
'foo' => 1,
'bar' => 2,
'baz' => 3,
], $array);
}
2020-02-21 02:03:40 +01:00
public function testKeys(): void
{
$map = $this->create([
2020-02-21 02:03:40 +01:00
'foo' => 1,
'bar' => 2,
'baz' => 3,
]);
$keys = $map->keys();
self::assertInstanceOf($this->vectorClass, $keys);
self::assertCount(3, $keys);
self::assertSame('foo', $keys->at(0));
self::assertSame('bar', $keys->at(1));
self::assertSame('baz', $keys->at(2));
2020-02-21 02:03:40 +01:00
$map = $this->create([]);
2020-02-21 02:03:40 +01:00
$keys = $map->keys();
self::assertInstanceOf($this->vectorClass, $keys);
self::assertCount(0, $keys);
2020-02-21 02:03:40 +01:00
}
public function testFilter(): void
{
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$filtered = $map->filter(fn (string $item) => Str\contains($item, 'b'));
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $filtered);
self::assertNotSame($map, $filtered);
self::assertContains('bar', $filtered);
self::assertContains('baz', $filtered);
self::assertNotContains('foo', $filtered);
self::assertNotContains('qux', $filtered);
self::assertCount(2, $filtered);
2020-02-21 02:03:40 +01:00
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$filtered = $map->filter(fn (string $item) => Str\contains($item, 'hello'));
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $filtered);
self::assertNotContains('bar', $filtered);
self::assertNotContains('baz', $filtered);
self::assertNotContains('foo', $filtered);
self::assertNotContains('qux', $filtered);
self::assertCount(0, $filtered);
2020-02-21 02:03:40 +01:00
}
public function testFilterWithKey(): void
{
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$filtered = $map->filterWithKey(fn (int $k, string $v) => 'foo' === $v || 3 === $k);
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $filtered);
self::assertNotSame($map, $filtered);
self::assertContains('foo', $filtered);
self::assertContains('qux', $filtered);
self::assertNotContains('bar', $filtered);
self::assertNotContains('baz', $filtered);
self::assertCount(2, $filtered);
2020-02-21 02:03:40 +01:00
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$filtered = $map->filterWithKey(fn (int $k, string $v) => 4 === $k);
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $filtered);
self::assertNotContains('bar', $filtered);
self::assertNotContains('baz', $filtered);
self::assertNotContains('foo', $filtered);
self::assertNotContains('qux', $filtered);
self::assertCount(0, $filtered);
2020-02-21 02:03:40 +01:00
}
public function testMap(): void
{
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$mapped = $map->map(fn (string $item) => Str\uppercase($item));
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $mapped);
self::assertSame([
2020-02-21 02:03:40 +01:00
0 => 'FOO',
1 => 'BAR',
2 => 'BAZ',
3 => 'QUX',
], $mapped->toArray());
self::assertNotSame($map, $mapped);
self::assertCount(4, $mapped);
2020-02-21 02:03:40 +01:00
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$mapped = $map->map(fn (string $item) => $item);
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $mapped);
self::assertNotSame($map, $mapped);
self::assertSame($map->toArray(), $mapped->toArray());
self::assertCount(4, $mapped);
2020-02-21 02:03:40 +01:00
}
public function testMapWithKey(): void
{
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$mapped = $map->mapWithKey(fn (int $k, string $v) => Str\format('%s ( %d )', $v, $k));
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $mapped);
self::assertSame([
2020-02-21 02:03:40 +01:00
0 => 'foo ( 0 )',
1 => 'bar ( 1 )',
2 => 'baz ( 2 )',
3 => 'qux ( 3 )',
], $mapped->toArray());
self::assertNotSame($map, $mapped);
self::assertCount(4, $mapped);
2020-02-21 02:03:40 +01:00
$map = $this->create([
0 => 'foo',
1 => 'bar',
2 => 'baz',
3 => 'qux',
]);
2020-03-01 21:52:25 +01:00
$mapped = $map->mapWithKey(fn (int $k, string $v) => $k);
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $mapped);
self::assertNotSame($map, $mapped);
self::assertSame($map->keys()->toArray(), $mapped->toArray());
self::assertCount(4, $mapped);
2020-02-21 02:03:40 +01:00
2020-03-01 21:52:25 +01:00
$mapped = $map->mapWithKey(fn (int $k, string $v) => $v);
2020-02-21 02:03:40 +01:00
self::assertInstanceOf($this->mapClass, $mapped);
self::assertNotSame($map, $mapped);
self::assertSame($map->toArray(), $mapped->toArray());
self::assertCount(4, $mapped);
2020-02-21 02:03:40 +01:00
}
public function testFirst(): void
{
$map = $this->create([]);
self::assertNull($map->first());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => null]);
self::assertNull($map->first());
2020-02-21 02:03:40 +01:00
$map = $this->create([0 => 'foo']);
self::assertSame('foo', $map->first());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
self::assertSame('bar', $map->first());
2020-02-21 02:03:40 +01:00
}
public function testFirstKey(): void
{
$map = $this->create([]);
self::assertNull($map->firstKey());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => null]);
self::assertSame('foo', $map->firstKey());
2020-02-21 02:03:40 +01:00
$map = $this->create([0 => 'foo']);
self::assertSame(0, $map->firstKey());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
self::assertSame('foo', $map->firstKey());
2020-02-21 02:03:40 +01:00
}
public function testLast(): void
{
$map = $this->create([]);
self::assertNull($map->last());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => null]);
self::assertNull($map->last());
2020-02-21 02:03:40 +01:00
$map = $this->create([0 => 'foo']);
self::assertSame('foo', $map->last());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
self::assertSame('qux', $map->last());
2020-02-21 02:03:40 +01:00
}
public function testLastKey(): void
{
$map = $this->create([]);
self::assertNull($map->lastKey());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => null]);
self::assertSame('foo', $map->lastKey());
2020-02-21 02:03:40 +01:00
$map = $this->create([0 => 'foo']);
self::assertSame(0, $map->lastKey());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
self::assertSame('baz', $map->lastKey());
2020-02-21 02:03:40 +01:00
}
public function testLinearSearch(): void
{
$map = $this->create([]);
self::assertNull($map->linearSearch('foo'));
2020-02-21 02:03:40 +01:00
$map = $this->create([
'foo' => 'bar',
'baz' => 'qux',
]);
self::assertSame('foo', $map->linearSearch('bar'));
self::assertSame('baz', $map->linearSearch('qux'));
self::assertNull($map->linearSearch('foo'));
self::assertNull($map->linearSearch('baz'));
2020-02-21 02:03:40 +01:00
}
public function testZip(): void
{
$map = $this->create([]);
2020-02-21 02:03:40 +01:00
$zipped = $map->zip([]);
self::assertInstanceOf($this->mapClass, $zipped);
self::assertCount(0, $zipped);
2020-02-21 02:03:40 +01:00
$map = $this->create([]);
2020-02-21 02:03:40 +01:00
$zipped = $map->zip([1, 2]);
self::assertInstanceOf($this->mapClass, $zipped);
self::assertCount(0, $zipped);
2020-02-21 02:03:40 +01:00
$map = $this->create([1 => 'foo', 2 => 'bar']);
2020-02-21 02:03:40 +01:00
$zipped = $map->zip([]);
self::assertInstanceOf($this->mapClass, $zipped);
self::assertCount(0, $zipped);
2020-02-21 02:03:40 +01:00
$map = $this->create([1 => 'foo', 2 => 'bar']);
2020-02-21 02:03:40 +01:00
$zipped = $map->zip(['baz', 'qux']);
self::assertInstanceOf($this->mapClass, $zipped);
self::assertCount(2, $zipped);
self::assertSame(['foo', 'baz'], $zipped->at(1));
self::assertSame(['bar', 'qux'], $zipped->at(2));
2020-02-21 02:03:40 +01:00
$map = $this->create([1 => 'foo', 2 => 'bar', 3 => 'baz', 4 => 'qux']);
2020-02-21 02:03:40 +01:00
$zipped = $map->zip(['hello', 'world']);
self::assertInstanceOf($this->mapClass, $zipped);
self::assertCount(2, $zipped);
self::assertSame(['foo', 'hello'], $zipped->at(1));
self::assertSame(['bar', 'world'], $zipped->at(2));
2020-02-21 02:03:40 +01:00
$map = $this->create([1 => 'hello', 2 => 'world']);
2020-02-21 02:03:40 +01:00
$zipped = $map->zip(['foo', 'bar', 'baz', 'qux']);
self::assertInstanceOf($this->mapClass, $zipped);
self::assertCount(2, $zipped);
self::assertSame(['hello', 'foo'], $zipped->at(1));
self::assertSame(['world', 'bar'], $zipped->at(2));
2020-02-21 02:03:40 +01:00
}
public function testTake(): void
{
$map = $this->create([]);
2020-02-21 02:03:40 +01:00
$rest = $map->take(2);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-02-21 02:03:40 +01:00
$rest = $map->take(4);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(2, $rest);
self::assertSame($map->toArray(), $rest->toArray());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-02-21 02:03:40 +01:00
$rest = $map->take(1);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(1, $rest);
self::assertSame('bar', $rest->at('foo'));
2020-02-21 02:03:40 +01:00
}
public function testTakeWhile(): void
{
$map = $this->create([]);
2020-03-01 21:52:25 +01:00
$rest = $map->takeWhile(fn ($v) => false);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create([]);
2020-03-01 21:52:25 +01:00
$rest = $map->takeWhile(fn ($v) => true);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-03-01 21:52:25 +01:00
$rest = $map->takeWhile(fn ($v) => true);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(2, $rest);
self::assertSame($map->toArray(), $rest->toArray());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-03-01 21:52:25 +01:00
$rest = $map->takeWhile(fn ($v) => 'bar' === $v);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(1, $rest);
self::assertSame('bar', $rest->at('foo'));
2020-02-21 02:03:40 +01:00
}
public function testDrop(): void
{
$map = $this->create([]);
2020-02-21 02:03:40 +01:00
$rest = $map->drop(2);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-02-21 02:03:40 +01:00
$rest = $map->drop(4);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-02-21 02:03:40 +01:00
$rest = $map->drop(1);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(1, $rest);
self::assertSame('qux', $rest->at('baz'));
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-02-21 02:03:40 +01:00
$rest = $map->drop(0);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(2, $rest);
self::assertSame($map->toArray(), $rest->toArray());
2020-02-21 02:03:40 +01:00
}
public function testDropWhile(): void
{
$map = $this->create([]);
2020-03-01 21:52:25 +01:00
$rest = $map->dropWhile(fn ($v) => true);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create([]);
2020-03-01 21:52:25 +01:00
$rest = $map->dropWhile(fn ($v) => false);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-03-01 21:52:25 +01:00
$rest = $map->dropWhile(fn ($v) => true);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(0, $rest);
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-03-01 21:52:25 +01:00
$rest = $map->dropWhile(fn ($v) => false);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(2, $rest);
self::assertSame($map->toArray(), $rest->toArray());
2020-02-21 02:03:40 +01:00
$map = $this->create(['foo' => 'bar', 'baz' => 'qux']);
2020-03-01 21:52:25 +01:00
$rest = $map->dropWhile(fn ($v) => 'bar' === $v);
self::assertInstanceOf($this->mapClass, $rest);
self::assertNotSame($map, $rest);
self::assertCount(1, $rest);
self::assertSame('qux', $rest->at('baz'));
2020-02-21 02:03:40 +01:00
}
public function testSlice(): void
{
$map = $this->create([
0 => 'foo',
1 => 'foo',
2 => 'bar',
3 => 'bar',
4 => 'baz',
5 => 'baz',
6 => 'qux',
7 => 'qux',
]);
$slice1 = $map->slice(0, 1);
self::assertInstanceOf($this->mapClass, $slice1);
self::assertNotSame($slice1, $map);
self::assertCount(1, $slice1);
self::assertSame('foo', $slice1->at(0));
2020-02-21 02:03:40 +01:00
$slice2 = $map->slice(2, 4);
self::assertInstanceOf($this->mapClass, $slice1);
self::assertNotSame($slice2, $map);
self::assertCount(4, $slice2);
self::assertSame([
2020-02-21 02:03:40 +01:00
2 => 'bar',
3 => 'bar',
4 => 'baz',
5 => 'baz',
], $slice2->toArray());
}
public function testAt(): void
{
$map = $this->create([
'foo' => 'hello',
'bar' => 'world',
]);
self::assertSame('hello', $map->at('foo'));
self::assertSame('world', $map->at('bar'));
2020-02-21 02:03:40 +01:00
$this->expectException(InvariantViolationException::class);
2020-07-09 00:28:29 +02:00
$this->expectExceptionMessage('Key (baz) is out-of-bounds.');
2020-02-21 02:03:40 +01:00
$map->at('baz');
}
public function testContains(): void
{
$map = $this->create([
'foo' => 'hello',
'bar' => 'world',
]);
self::assertTrue($map->contains('foo'));
self::assertTrue($map->contains('bar'));
self::assertFalse($map->contains('baz'));
2020-02-21 02:03:40 +01:00
}
public function testGet(): void
{
$map = $this->create([
'foo' => 'hello',
'bar' => 'world',
]);
self::assertSame('hello', $map->get('foo'));
self::assertSame('world', $map->get('bar'));
self::assertNull($map->get('baz'));
2020-02-21 02:03:40 +01:00
}
2020-03-01 21:52:25 +01:00
/**
* @template Tk of array-key
* @template Tv
*
* @psalm-param iterable<Tk, Tv> $items
*
* @psalm-return IMap<Tk, Tv>
*/
2020-08-08 06:44:37 +02:00
abstract protected function create(iterable $items): MapInterface;
2020-02-21 02:03:40 +01:00
}