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

63 lines
1.8 KiB
PHP
Raw Normal View History

2019-12-24 01:52:07 +01:00
<?php
declare(strict_types=1);
namespace Psl\Tests\Str;
use PHPUnit\Framework\TestCase;
2019-12-26 23:10:57 +01:00
use Psl\Exception;
2019-12-26 23:10:57 +01:00
use Psl\Str;
2019-12-24 01:52:07 +01:00
class SliceTest extends TestCase
{
2019-12-26 23:10:57 +01:00
/**
* @dataProvider provideData
*/
public function testSlice(string $expected, string $string, int $offset, ?int $length = null): void
{
self::assertSame($expected, Str\slice($string, $offset, $length));
}
public function provideData(): array
{
return [
['', '', 0, 0, ],
['Hello', 'Hello, World!', 0, 5],
['Hello, World!', 'Hello, World!', 0],
['World', 'Hello, World!', 7, 5],
['سيف', 'مرحبا سيف', 6, 3],
['اهلا', 'اهلا بكم', 0, 4],
['destiny', 'People linked by destiny will always find each other.', 17, 7],
['lö ', 'héllö wôrld', 3, 3, ],
['lö wôrld', 'héllö wôrld', 3, null, ],
['', 'héllö wôrld', 3, 0],
['', 'fôo', 3, null, ],
['', 'fôo', 3, 12, ],
['wôrld', 'héllö wôrld', -5, null, ],
['wôrld', 'héllö wôrld', -5, 100, ],
['wôr', 'héllö wôrld', -5, 3, ],
];
}
public function testSliceThrowsForNegativeLength(): void
{
$this->expectException(Exception\InvariantViolationException::class);
Str\slice('Hello', 0, -1);
}
public function testSliceThrowsForOutOfBoundOffset(): void
{
$this->expectException(Exception\InvariantViolationException::class);
Str\slice('Hello', 10);
}
public function testSliceThrowsForNegativeOutOfBoundOffset(): void
{
$this->expectException(Exception\InvariantViolationException::class);
Str\slice('hello', -6);
}
2019-12-24 01:52:07 +01:00
}