File: Strings.before%28%29.phpt

package info (click to toggle)
php-nette-utils 4.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,420 kB
  • sloc: php: 4,069; xml: 12; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,595 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

/**
 * Test: Nette\Utils\Strings::before()
 */

declare(strict_types=1);

use Nette\Utils\Strings;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test('extracts substring before a delimiter with occurrence parameter', function () {
	$foo = '0123456789a123456789b123456789c';
	Assert::same('', Strings::before($foo, '0', 1));
	Assert::same('012345678', Strings::before($foo, '9', 1));
	Assert::same('0123456', Strings::before($foo, '789', 1));
	Assert::same('', Strings::before($foo, '', 1));
	Assert::same('0123456789a123456789b123456789c', Strings::before($foo, '', -1));
	Assert::same('0123456789a123456789b123456789', Strings::before($foo, 'c', -1));
	Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', -1));
	Assert::same('0123456789a123456789b12345678', Strings::before($foo, '9', 3));
	Assert::same('0123456789a123456789b123456', Strings::before($foo, '789', 3));
	Assert::same('012345678', Strings::before($foo, '9', -3));
	Assert::same('0123456', Strings::before($foo, '789', -3));
	Assert::null(Strings::before($foo, '9', 0));
	Assert::null(Strings::before($foo, 'not-in-string'));
	Assert::null(Strings::before($foo, 'b', -2));
	Assert::null(Strings::before($foo, 'b', 2));
});


test('processes Unicode strings in substring extraction before delimiter', function () {
	$foo = "I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}ti\u{F8}n"; // Iñtërnâtiônàlizætiøn
	Assert::same("I\u{F1}t\u{EB}rn\u{E2}", Strings::before($foo, 'ti', 1));
	Assert::same("I\u{F1}t\u{EB}rn\u{E2}ti\u{F4}n\u{E0}liz\u{E6}", Strings::before($foo, 'ti', 2));
});