endtoend-test-psl/tests/Psl/Iter/SliceTest.php

50 lines
1.3 KiB
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Iter;
use PHPUnit\Framework\TestCase;
2020-09-01 07:50:23 +02:00
use Psl;
2020-09-01 07:56:15 +02:00
use Psl\Iter;
2019-12-24 01:52:07 +01:00
final class SliceTest extends TestCase
2019-12-24 01:52:07 +01:00
{
2020-09-01 07:50:23 +02:00
public function testSlice(): void
{
$result = Iter\slice([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], 5);
static::assertSame([0, 1, 2, 3, 4, 5], Iter\to_array($result));
2020-09-01 07:50:23 +02:00
}
public function testSliceWithLength(): void
{
$result = Iter\slice([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], 5, 3);
static::assertSame([0, 1, 2], Iter\to_array($result));
2020-09-01 07:50:23 +02:00
}
public function testSliceWithZeroLength(): void
{
$result = Iter\slice([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], 5, 0);
static::assertSame([], Iter\to_array($result));
2020-09-01 07:50:23 +02:00
}
2020-09-01 07:56:15 +02:00
public function testSliceThrowsIfStartIsNegative(): void
2020-09-01 07:50:23 +02:00
{
$this->expectException(Psl\Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Start offset must be non-negative.');
Iter\slice([1, 2, 3], -3);
}
2020-09-01 07:56:15 +02:00
public function testSliceThrowsIfLengthIsNegative(): void
2020-09-01 07:50:23 +02:00
{
$this->expectException(Psl\Exception\InvariantViolationException::class);
$this->expectExceptionMessage('Length must be non-negative.');
Iter\slice([1, 2, 3], 1, -3);
}
2019-12-24 01:52:07 +01:00
}