/
ColorTest.php
204 lines (187 loc) · 7.31 KB
/
ColorTest.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util;
use const DIRECTORY_SEPARATOR;
use function str_repeat;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
#[CoversClass(Color::class)]
#[Small]
#[TestDox('Basic ANSI color highlighting support')]
final class ColorTest extends TestCase
{
public static function colorizeProvider(): array
{
return [
'no color' => ['', 'string', 'string'],
'one color' => ['fg-blue', 'string', "\x1b[34mstring\x1b[0m"],
'multiple colors' => ['bold,dim,fg-blue,bg-yellow', 'string', "\x1b[1;2;34;43mstring\x1b[0m"],
'invalid color' => ['fg-invalid', 'some text', 'some text'],
'valid and invalid colors' => ['fg-invalid,bg-blue', 'some text', "\e[44msome text\e[0m"],
'empty string' => ['fg-blue', '', ''],
];
}
public static function colorizePathProvider(): array
{
$sep = DIRECTORY_SEPARATOR;
$sepDim = Color::dim($sep);
return [
'null previous path' => [
null,
$sep . 'php' . $sep . 'unit' . $sep . 'test.phpt',
false,
$sepDim . 'php' . $sepDim . 'unit' . $sepDim . 'test.phpt',
],
'empty previous path' => [
'',
$sep . 'php' . $sep . 'unit' . $sep . 'test.phpt',
false,
$sepDim . 'php' . $sepDim . 'unit' . $sepDim . 'test.phpt',
],
'from root' => [
$sep,
$sep . 'php' . $sep . 'unit' . $sep . 'test.phpt',
false,
$sepDim . 'php' . $sepDim . 'unit' . $sepDim . 'test.phpt',
],
'partial part' => [
$sep . 'php' . $sep,
$sep . 'php' . $sep . 'unit' . $sep . 'test.phpt',
false,
Color::dim($sep . 'php' . $sep) . 'unit' . $sepDim . 'test.phpt',
],
'colorize filename' => [
'',
$sep . '_d-i.r' . $sep . 't-e_s.t.phpt',
true,
$sepDim . '_d-i.r' . $sepDim . 't' . Color::dim('-') . 'e' . Color::dim('_') . 's' . Color::dim('.') . 't' . Color::dim('.phpt'),
],
];
}
public static function colorizeTextBoxProvider(): array
{
return [
'fitting text' => [
40, // simulate 40 char wide terminal
'this is fine' . PHP_EOL .
PHP_EOL .
'all lines fit nicely' . PHP_EOL .
'bottom text',
Color::colorize('red', 'this is fine ') . PHP_EOL .
Color::colorize('red', ' ') . PHP_EOL .
Color::colorize('red', 'all lines fit nicely') . PHP_EOL .
Color::colorize('red', 'bottom text '),
],
'oversize text' => [
20, // simulate 20 char wide terminal
'this is also fine' . PHP_EOL .
PHP_EOL .
'the very long lines do not stretch the whole textbox' . PHP_EOL .
'anymore',
Color::colorize('red', 'this is also fine ') . PHP_EOL .
Color::colorize('red', ' ') . PHP_EOL .
Color::colorize('red', 'the very long lines do not stretch the whole textbox') . PHP_EOL .
Color::colorize('red', 'anymore '),
],
'default terminal width cap' => [
80, // simulate (default) 80 char wide terminal
str_repeat('.123456789', 8) . PHP_EOL .
'this is a shorter line',
Color::colorize('red', str_repeat('.123456789', 8)) . PHP_EOL .
Color::colorize('red', 'this is a shorter line '),
],
];
}
public static function whitespacedStringProvider(): array
{
return [
['no-spaces',
'no-spaces',
],
[
' space invaders ',
"\e[2m·\e[22mspace\e[2m···\e[22minvaders\e[2m·\e[22m",
],
[
"\tindent, space and \\n\n\\r\r",
"\e[2m⇥\e[22mindent,\e[2m·\e[22mspace\e[2m·\e[22mand\e[2m·\e[22m\\n\e[2m↵\e[22m\\r\e[2m⟵\e[22m",
],
];
}
public static function unnamedDataSetProvider(): array
{
return [
[1],
[2],
];
}
public static function namedDataSetProvider(): array
{
return [
'one' => [1],
'two' => [2],
];
}
#[TestDox('Colorize with $_dataName')]
#[DataProvider('colorizeProvider')]
public function testColorize(string $color, string $buffer, string $expected): void
{
$this->assertSame($expected, Color::colorize($color, $buffer));
}
#[TestDox('Colorize path $path after $prevPath')]
#[DataProvider('colorizePathProvider')]
public function testColorizePath(?string $prevPath, string $path, bool $colorizeFilename, string $expected): void
{
$this->assertSame($expected, Color::colorizePath($path, $prevPath, $colorizeFilename));
}
#[TestDox('Colorize an autosizing text box')]
#[DataProvider('colorizeTextBoxProvider')]
public function testColorizeTextBox(int $columns, string $buffer, string $expected): void
{
$this->assertSame($expected, Color::colorizeTextBox('red', $buffer, $columns));
}
#[TestDox('dim($m) and colorize(\'dim\',$m) return different ANSI codes')]
public function testDimAndColorizeDimAreDifferent(): void
{
$buffer = 'some string';
$this->assertNotSame(Color::dim($buffer), Color::colorize('dim', $buffer));
}
#[DataProvider('whitespacedStringProvider')]
#[TestDox('Visualize all whitespace characters in $actual')]
public function testVisibleWhitespace(string $actual, string $expected): void
{
$this->assertSame($expected, Color::visualizeWhitespace($actual, true));
}
#[TestDox('Visualize whitespace but ignore EOL')]
public function testVisualizeWhitespaceButIgnoreEol(): void
{
$string = "line1\nline2\n";
$this->assertSame($string, Color::visualizeWhitespace($string, false));
}
#[DataProvider('unnamedDataSetProvider')]
public function testPrettifyUnnamedDataprovider(int $value): void
{
$this->assertSame($value, $value);
}
#[DataProvider('namedDataSetProvider')]
public function testPrettifyNamedDataprovider(int $value): void
{
$this->assertSame($value, $value);
}
#[DataProvider('namedDataSetProvider')]
#[TestDox('TestDox shows name of data set $_dataName with value $value')]
public function testTestdoxDatanameAsParameter(int $value): void
{
$this->assertSame($value, $value);
}
}