forked from wp-cli/php-cli-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-table-ascii.php
280 lines (254 loc) · 7.98 KB
/
test-table-ascii.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
use cli\Streams;
use cli\Table;
use cli\table\Ascii;
use cli\Colors;
use WP_CLI\Tests\TestCase;
/**
* Class Test_Table_Ascii
*
* Acceptance tests for ASCII table drawing.
* It will redirect STDOUT to temporary file and check that output matches with expected
*/
class Test_Table_Ascii extends TestCase {
/**
* @var string Path to temporary file, where STDOUT output will be redirected during tests
*/
private $_mockFile;
/**
* @var \cli\Table Instance
*/
private $_instance;
/**
* Creates instance and redirects STDOUT to temporary file
*/
public function set_up() {
$this->_mockFile = tempnam(sys_get_temp_dir(), 'temp');
$resource = fopen($this->_mockFile, 'wb');
Streams::setStream('out', $resource);
$this->_instance = new Table();
$this->_instance->setRenderer(new Ascii());
}
/**
* Cleans temporary file
*/
public function tear_down() {
if (file_exists($this->_mockFile)) {
unlink($this->_mockFile);
}
}
/**
* Draw simple One column table
*/
public function testDrawOneColumnTable() {
$headers = array('Test Header');
$rows = array(
array('x'),
);
$output = <<<'OUT'
+-------------+
| Test Header |
+-------------+
| x |
+-------------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Draw simple One column table with colored string
* Output should look like:
* +-------------+
* | Test Header |
* +-------------+
* | x |
* +-------------+
*
* where `x` character has green color.
* At the same time it checks that `green` defined in `cli\Colors` really looks as `green`.
*/
public function testDrawOneColumnColoredTable() {
Colors::enable( true );
$headers = array('Test Header');
$rows = array(
array(Colors::colorize('%Gx%n', true)),
);
// green `x`
$x = "\x1B\x5B\x33\x32\x3B\x31\x6Dx\x1B\x5B\x30\x6D";
$output = <<<OUT
+-------------+
| Test Header |
+-------------+
| $x |
+-------------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Check it works with colors disabled.
*/
public function testDrawOneColumnColorDisabledTable() {
Colors::disable( true );
$this->assertFalse( Colors::shouldColorize() );
$headers = array('Test Header');
$rows = array(
array('%Gx%n'),
);
$output = <<<OUT
+-------------+
| Test Header |
+-------------+
| %Gx%n |
+-------------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Checks that spacing and borders are handled correctly in table
*/
public function testSpacingInTable() {
$headers = array('A', ' ', 'C', '');
$rows = array(
array(' ', 'B1', '', 'D1'),
array('A2', '', ' C2', null),
);
$output = <<<'OUT'
+-------+------+-----+----+
| A | | C | |
+-------+------+-----+----+
| | B1 | | D1 |
| A2 | | C2 | |
+-------+------+-----+----+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Test correct table indentation and border positions for multibyte strings
*/
public function testTableWithMultibyteStrings() {
$headers = array('German', 'French', 'Russian', 'Chinese');
$rows = array(
array('Schätzen', 'Apprécier', 'Оценить', '欣賞'),
);
$output = <<<'OUT'
+----------+-----------+---------+---------+
| German | French | Russian | Chinese |
+----------+-----------+---------+---------+
| Schätzen | Apprécier | Оценить | 欣賞 |
+----------+-----------+---------+---------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Test that % gets escaped correctly.
*/
public function testTableWithPercentCharacters() {
$headers = array( 'Heading', 'Heading2', 'Heading3' );
$rows = array(
array( '% at start', 'at end %', 'in % middle' )
);
$output = <<<'OUT'
+------------+----------+-------------+
| Heading | Heading2 | Heading3 |
+------------+----------+-------------+
| % at start | at end % | in % middle |
+------------+----------+-------------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Test that a % is appropriately padded in the table
*/
public function testTablePaddingWithPercentCharacters() {
$headers = array( 'ID', 'post_title', 'post_name' );
$rows = array(
array(
3,
'10%',
''
),
array(
1,
'Hello world!',
'hello-world'
),
);
$output = <<<'OUT'
+----+--------------+-------------+
| ID | post_title | post_name |
+----+--------------+-------------+
| 3 | 10% | |
| 1 | Hello world! | hello-world |
+----+--------------+-------------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Draw wide multiplication Table.
* Example with many columns, many rows
*/
public function testDrawMultiplicationTable() {
$maxFactor = 16;
$headers = array_merge(array('x'), range(1, $maxFactor));
for ($i = 1, $rows = array(); $i <= $maxFactor; ++$i) {
$rows[] = array_merge(array($i), range($i, $i * $maxFactor, $i));
}
$output = <<<'OUT'
+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| x | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 |
| 3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 | 39 | 42 | 45 | 48 |
| 4 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 44 | 48 | 52 | 56 | 60 | 64 |
| 5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 |
| 6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 66 | 72 | 78 | 84 | 90 | 96 |
| 7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 | 77 | 84 | 91 | 98 | 105 | 112 |
| 8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 | 104 | 112 | 120 | 128 |
| 9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 | 99 | 108 | 117 | 126 | 135 | 144 |
| 10 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 110 | 120 | 130 | 140 | 150 | 160 |
| 11 | 11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99 | 110 | 121 | 132 | 143 | 154 | 165 | 176 |
| 12 | 12 | 24 | 36 | 48 | 60 | 72 | 84 | 96 | 108 | 120 | 132 | 144 | 156 | 168 | 180 | 192 |
| 13 | 13 | 26 | 39 | 52 | 65 | 78 | 91 | 104 | 117 | 130 | 143 | 156 | 169 | 182 | 195 | 208 |
| 14 | 14 | 28 | 42 | 56 | 70 | 84 | 98 | 112 | 126 | 140 | 154 | 168 | 182 | 196 | 210 | 224 |
| 15 | 15 | 30 | 45 | 60 | 75 | 90 | 105 | 120 | 135 | 150 | 165 | 180 | 195 | 210 | 225 | 240 |
| 16 | 16 | 32 | 48 | 64 | 80 | 96 | 112 | 128 | 144 | 160 | 176 | 192 | 208 | 224 | 240 | 256 |
+----+----+----+----+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Draw a table with headers but no data
*/
public function testDrawWithHeadersNoData() {
$headers = array('header 1', 'header 2');
$rows = array();
$output = <<<'OUT'
+----------+----------+
| header 1 | header 2 |
+----------+----------+
+----------+----------+
OUT;
$this->assertInOutEquals(array($headers, $rows), $output);
}
/**
* Verifies that Input and Output equals,
* Sugar method for fast access from tests
*
* @param array $input First element is header array, second element is rows array
* @param mixed $output Expected output
*/
private function assertInOutEquals(array $input, $output) {
$this->_instance->setHeaders($input[0]);
$this->_instance->setRows($input[1]);
$this->_instance->display();
$this->assertOutFileEqualsWith($output);
}
/**
* Checks that contents of input string and temporary file match
*
* @param mixed $expected Expected output
*/
private function assertOutFileEqualsWith($expected) {
$this->assertEquals($expected, file_get_contents($this->_mockFile));
}
}