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

287 lines
7.3 KiB
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Collection;
use PHPUnit\Framework\TestCase;
2019-12-25 22:45:52 +01:00
use Psl\Arr;
use Psl\Collection;
use Psl\Exception;
use Psl\Iter;
use Psl\Str;
2019-12-24 01:52:07 +01:00
class PairTest extends TestCase
{
public function testItems(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$items = $pair->items();
self::assertCount(2, $items);
self::assertSame('foo', $items[0]);
self::assertSame('bar', $items[1]);
2019-12-24 01:52:07 +01:00
}
public function testIsEmpty(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
self::assertFalse($pair->isEmpty());
2019-12-24 01:52:07 +01:00
}
public function testCount(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
self::assertCount(2, $pair);
$vector = $pair->filter(fn ($v) => 'foo' === $v);
self::assertInstanceOf(Collection\ImmVector::class, $vector);
self::assertCount(1, $vector);
$vector = $pair->filter(fn ($v) => false);
self::assertInstanceOf(Collection\ImmVector::class, $vector);
self::assertCount(0, $vector);
}
public function testToArray(): void
{
$pair = new Collection\Pair('foo', 'bar');
$array = $pair->toArray();
self::assertTrue(Arr\contains($array, 'foo'));
self::assertTrue(Arr\contains($array, 'bar'));
$vector = $pair->filter(fn ($v) => false);
$array = $vector->toArray();
self::assertEmpty($array);
2019-12-24 01:52:07 +01:00
}
public function testAt(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
self::assertSame('foo', $pair->at(0));
self::assertSame('bar', $pair->at(1));
$this->expectException(Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Key (2) is out-of-bound.');
$pair->at(2);
2019-12-24 01:52:07 +01:00
}
public function testContainsKey(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
self::assertTrue($pair->containsKey(0));
self::assertTrue($pair->containsKey(1));
self::assertFalse($pair->containsKey(2));
self::assertFalse($pair->containsKey(3));
2019-12-24 01:52:07 +01:00
}
public function testGet(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
self::assertSame('foo', $pair->get(0));
self::assertSame('bar', $pair->get(1));
self::assertNull($pair->get(3));
2019-12-24 01:52:07 +01:00
}
2019-12-25 22:45:52 +01:00
public function testValues(): void
2019-12-24 01:52:07 +01:00
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$values = $pair->values();
self::assertCount(2, $values);
self::assertSame('foo', $values->at(0));
self::assertSame('bar', $values->at(1));
2019-12-24 01:52:07 +01:00
}
2019-12-25 22:45:52 +01:00
public function testKeys(): void
2019-12-24 01:52:07 +01:00
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$keys = $pair->keys();
self::assertCount(2, $keys);
self::assertSame(0, $keys->at(0));
self::assertSame(1, $keys->at(1));
2019-12-24 01:52:07 +01:00
}
2019-12-25 22:45:52 +01:00
public function testMap(): void
2019-12-24 01:52:07 +01:00
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$vector = $pair->map(fn ($value) => Str\uppercase($value));
self::assertSame('FOO', $vector->at(0));
self::assertSame('BAR', $vector->at(1));
2019-12-24 01:52:07 +01:00
}
2019-12-25 22:45:52 +01:00
public function testMapWithKey(): void
2019-12-24 01:52:07 +01:00
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$vector = $pair->mapWithKey(fn ($key, $value) => Str\format('%s (%d)', $value, $key));
self::assertSame('foo (0)', $vector->at(0));
self::assertSame('bar (1)', $vector->at(1));
2019-12-24 01:52:07 +01:00
}
2019-12-25 22:45:52 +01:00
public function testFilter(): void
2019-12-24 01:52:07 +01:00
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
$pair = $pair->filter(fn ($value) => $value >= 2);
self::assertCount(1, $pair);
$array = $pair->toArray();
self::assertFalse(Arr\contains($array, 1));
self::assertTrue(Arr\contains($array, 2));
2019-12-24 01:52:07 +01:00
}
2019-12-25 22:45:52 +01:00
public function testFilterWithKey(): void
2019-12-24 01:52:07 +01:00
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$vector = $pair->filterWithKey(fn ($key, $value) => 0 === $key);
self::assertCount(1, $vector);
$array = $vector->toArray();
self::assertTrue(Arr\contains($array, 'foo'));
self::assertFalse(Arr\contains($array, 'bar'));
2019-12-24 01:52:07 +01:00
}
public function testZip(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
$vector = $pair->zip(Iter\range(4, 8));
self::assertCount(2, $vector);
/** @var Collection\Pair $first */
$first = $vector->at(0);
self::assertInstanceOf(Collection\Pair::class, $first);
self::assertSame(1, $first->first());
self::assertSame(4, $first->last());
/** @var Collection\Pair $second */
$second = $vector->at(1);
self::assertInstanceOf(Collection\Pair::class, $second);
self::assertSame(2, $second->first());
self::assertSame(5, $second->last());
2019-12-24 01:52:07 +01:00
}
public function testTake(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$vector = $pair->take(1);
self::assertCount(1, $vector);
self::assertSame(['foo'], $vector->toArray());
2019-12-24 01:52:07 +01:00
}
public function testTakeWhile(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$vector = $pair->takeWhile(fn ($value) => 'bar' !== $value);
self::assertCount(1, $vector);
self::assertSame(['foo'], $vector->toArray());
2019-12-24 01:52:07 +01:00
}
public function testDrop(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$vector = $pair->drop(1);
self::assertCount(1, $vector);
self::assertSame(['bar'], $vector->toArray());
2019-12-24 01:52:07 +01:00
}
public function testDropWhile(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$vector = $pair->dropWhile(fn ($value) => 'bar' !== $value);
self::assertCount(1, $vector);
self::assertSame(['bar'], $vector->toArray());
2019-12-24 01:52:07 +01:00
}
public function testSlice(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair('foo', 'bar');
$pair = $pair->slice(0, 1);
self::assertCount(1, $pair);
self::assertSame(['foo'], $pair->toArray());
2019-12-24 01:52:07 +01:00
}
public function testConcat(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
$vector = $pair->concat(Iter\range(3, 10));
self::assertCount(10, $vector);
self::assertSame([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], $vector->toArray());
2019-12-24 01:52:07 +01:00
}
public function testFirst(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
self::assertSame(1, $pair->first());
2019-12-24 01:52:07 +01:00
}
public function testFirstKey(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
self::assertSame(0, $pair->firstKey());
2019-12-24 01:52:07 +01:00
}
public function testLast(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
self::assertSame(2, $pair->last());
2019-12-24 01:52:07 +01:00
}
public function testLastKey(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
2019-12-24 01:52:07 +01:00
2019-12-25 22:45:52 +01:00
self::assertSame(1, $pair->lastKey());
2019-12-24 01:52:07 +01:00
}
public function testGetIterator(): void
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
$iterator = $pair->getIterator();
self::assertInstanceOf(Iter\Iterator::class, $iterator);
$array = \iterator_to_array($iterator);
self::assertSame([1, 2], $array);
2019-12-24 01:52:07 +01:00
}
2019-12-25 22:45:52 +01:00
public function testLinearSearch(): void
2019-12-24 01:52:07 +01:00
{
2019-12-25 22:45:52 +01:00
$pair = new Collection\Pair(1, 2);
self::assertSame(0, $pair->linearSearch(1));
self::assertSame(1, $pair->linearSearch(2));
self::assertNull($pair->linearSearch(5));
2019-12-24 01:52:07 +01:00
}
}