2019-12-24 01:52:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\Iter;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-07-07 15:17:36 +02:00
|
|
|
use Psl\Exception\InvariantViolationException;
|
|
|
|
use Psl\Iter;
|
2019-12-24 01:52:07 +01:00
|
|
|
|
|
|
|
class IteratorTest extends TestCase
|
|
|
|
{
|
2020-07-07 15:17:36 +02:00
|
|
|
public function testSeek(): void
|
2019-12-24 01:52:07 +01:00
|
|
|
{
|
2020-07-07 15:17:36 +02:00
|
|
|
$iterator = new Iter\Iterator([1, 2, 3, 4, 5]);
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(1, $iterator->current());
|
2020-07-07 15:17:36 +02:00
|
|
|
$iterator->next();
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(2, $iterator->current());
|
2020-07-07 15:17:36 +02:00
|
|
|
$iterator->next();
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-07-07 15:17:36 +02:00
|
|
|
$iterator->seek(0);
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame(1, $iterator->current());
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 15:17:36 +02:00
|
|
|
public function testSeekThrowsForOutOfBoundIndex(): void
|
2019-12-24 01:52:07 +01:00
|
|
|
{
|
2020-07-07 15:17:36 +02:00
|
|
|
$iterator = new Iter\Iterator([1, 2, 3, 4, 5]);
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-07-07 15:17:36 +02:00
|
|
|
$this->expectException(InvariantViolationException::class);
|
2020-07-09 00:28:29 +02:00
|
|
|
$this->expectExceptionMessage('Position (30) is out-of-bounds.');
|
2019-12-24 01:52:07 +01:00
|
|
|
|
2020-07-07 15:17:36 +02:00
|
|
|
$iterator->seek(30);
|
2019-12-24 01:52:07 +01:00
|
|
|
}
|
|
|
|
}
|