File: Strings.pad.phpt

package info (click to toggle)
php-nette-utils 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: php: 4,219; xml: 9; makefile: 4
file content (141 lines) | stat: -rw-r--r-- 4,493 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

/**
 * Test: Nette\Utils\Strings::padLeft() & padRight()
 */

declare(strict_types=1);

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


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


test('padLeft with ASCII strings', function () {
	Assert::same('00042', Strings::padLeft('42', 5, '0'));
	Assert::same('xxxhello', Strings::padLeft('hello', 8, 'x'));
	Assert::same('abcabcabc123', Strings::padLeft('123', 12, 'abc'));
});


test('padRight with ASCII strings', function () {
	Assert::same('42000', Strings::padRight('42', 5, '0'));
	Assert::same('helloxxx', Strings::padRight('hello', 8, 'x'));
	Assert::same('123abcabcabc', Strings::padRight('123', 12, 'abc'));
});


test('padLeft with empty string', function () {
	Assert::same('-----', Strings::padLeft('', 5, '-'));
	Assert::same('     ', Strings::padLeft('', 5));
	Assert::same('', Strings::padLeft('', 0, '-'));
});


test('padRight with empty string', function () {
	Assert::same('-----', Strings::padRight('', 5, '-'));
	Assert::same('     ', Strings::padRight('', 5));
	Assert::same('', Strings::padRight('', 0, '-'));
});


test('padLeft with exact length', function () {
	Assert::same('hello', Strings::padLeft('hello', 5, 'x'));
	Assert::same('test', Strings::padLeft('test', 4, '0'));
});


test('padRight with exact length', function () {
	Assert::same('hello', Strings::padRight('hello', 5, 'x'));
	Assert::same('test', Strings::padRight('test', 4, '0'));
});


test('padLeft with longer padding than needed', function () {
	Assert::same('abchi', Strings::padLeft('hi', 5, 'abcdefgh'));
	Assert::same('xyztest', Strings::padLeft('test', 7, 'xyzuvw'));
});


test('padRight with longer padding than needed', function () {
	Assert::same('hiabc', Strings::padRight('hi', 5, 'abcdefgh'));
	Assert::same('testxyz', Strings::padRight('test', 7, 'xyzuvw'));
});


test('padLeft with multi-byte padding string', function () {
	// ŽLU padded with ŤOU
	Assert::same('ŤOUŤOUŤŽLU', Strings::padLeft("\u{17D}LU", 10, "\u{164}OU"));
	Assert::same('ŤOUŤOUŽLU', Strings::padLeft("\u{17D}LU", 9, "\u{164}OU"));
});


test('padLeft returns original when length is reached', function () {
	Assert::same('ŽLU', Strings::padLeft("\u{17D}LU", 3, "\u{164}OU"));
	Assert::same('ŽLU', Strings::padLeft("\u{17D}LU", 0, "\u{164}OU"));
	Assert::same('ŽLU', Strings::padLeft("\u{17D}LU", -1, "\u{164}OU"));
});


test('padLeft with single multi-byte character', function () {
	Assert::same('ŤŤŤŤŤŤŤŽLU', Strings::padLeft("\u{17D}LU", 10, "\u{164}"));
	Assert::same('ŽLU', Strings::padLeft("\u{17D}LU", 3, "\u{164}"));
});


test('padLeft with default space padding', function () {
	Assert::same('       ŽLU', Strings::padLeft("\u{17D}LU", 10));
	Assert::same('     hello', Strings::padLeft('hello', 10));
});


test('padRight with multi-byte padding string', function () {
	Assert::same('ŽLUŤOUŤOUŤ', Strings::padRight("\u{17D}LU", 10, "\u{164}OU"));
	Assert::same('ŽLUŤOUŤOU', Strings::padRight("\u{17D}LU", 9, "\u{164}OU"));
});


test('padRight returns original when length is reached', function () {
	Assert::same('ŽLU', Strings::padRight("\u{17D}LU", 3, "\u{164}OU"));
	Assert::same('ŽLU', Strings::padRight("\u{17D}LU", 0, "\u{164}OU"));
	Assert::same('ŽLU', Strings::padRight("\u{17D}LU", -1, "\u{164}OU"));
});


test('padRight with single multi-byte character', function () {
	Assert::same('ŽLUŤŤŤŤŤŤŤ', Strings::padRight("\u{17D}LU", 10, "\u{164}"));
	Assert::same('ŽLU', Strings::padRight("\u{17D}LU", 3, "\u{164}"));
});


test('padRight with default space padding', function () {
	Assert::same('ŽLU       ', Strings::padRight("\u{17D}LU", 10));
	Assert::same('hello     ', Strings::padRight('hello', 10));
});


test('padLeft with emoji', function () {
	Assert::same('😀😀😀hi', Strings::padLeft('hi', 5, '😀'));
	Assert::same('😀😀test', Strings::padLeft('test', 6, '😀'));
});


test('padRight with emoji', function () {
	Assert::same('hi😀😀😀', Strings::padRight('hi', 5, '😀'));
	Assert::same('test😀😀', Strings::padRight('test', 6, '😀'));
});


test('padLeft handles combining characters', function () {
	// man + combining tilde = mañana
	Assert::same('..man', Strings::padLeft('man', 5, '.'));
	Assert::same("..man\u{303}", Strings::padLeft("man\u{303}", 6, '.'));
});


test('padRight handles combining characters', function () {
	Assert::same('man..', Strings::padRight('man', 5, '.'));
	Assert::same("man\u{303}..", Strings::padRight("man\u{303}", 6, '.'));
});