forked from wp-cli/php-cli-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-table.php
246 lines (189 loc) · 9.54 KB
/
test-table.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
<?php
use cli\Colors, cli\Table, cli\Table\Ascii;
use WP_CLI\Tests\TestCase;
/**
* Tests for cli\Table
*/
class Test_Table extends TestCase {
public function test_column_value_too_long_ascii() {
$constraint_width = 80;
$table = new cli\Table;
$renderer = new cli\Table\Ascii;
$renderer->setConstraintWidth( $constraint_width );
$table->setRenderer( $renderer );
$table->setHeaders( array( 'Field', 'Value' ) );
$table->addRow( array( 'description', 'The 2012 theme for WordPress is a fully responsive theme that looks great on any device. Features include a front page template with its own widgets, an optional display font, styling for post formats on both index and single views, and an optional no-sidebar page template. Make it yours with a custom menu, header image, and background.' ) );
$table->addRow( array( 'author', '<a href="http://wordpress.org/" title="Visit author homepage">the WordPress team</a>' ) );
$out = $table->getDisplayLines();
$this->assertCount( 12, $out );
$this->assertEquals( $constraint_width, strlen( $out[0] ) );
$this->assertEquals( $constraint_width, strlen( $out[1] ) );
$this->assertEquals( $constraint_width, strlen( $out[2] ) );
$this->assertEquals( $constraint_width, strlen( $out[3] ) );
$this->assertEquals( $constraint_width, strlen( $out[4] ) );
$this->assertEquals( $constraint_width, strlen( $out[5] ) );
$this->assertEquals( $constraint_width, strlen( $out[6] ) );
$this->assertEquals( $constraint_width, strlen( $out[7] ) );
$this->assertEquals( $constraint_width, strlen( $out[8] ) );
$this->assertEquals( $constraint_width, strlen( $out[9] ) );
$this->assertEquals( $constraint_width, strlen( $out[10] ) );
$this->assertEquals( $constraint_width, strlen( $out[11] ) );
$constraint_width = 81;
$renderer = new cli\Table\Ascii;
$renderer->setConstraintWidth( $constraint_width );
$table->setRenderer( $renderer );
$out = $table->getDisplayLines();
for ( $i = 0; $i < count( $out ); $i++ ) {
$this->assertEquals( $constraint_width, strlen( $out[ $i ] ) );
}
}
public function test_column_value_too_long_with_multibytes() {
$constraint_width = 80;
$table = new cli\Table;
$renderer = new cli\Table\Ascii;
$renderer->setConstraintWidth( $constraint_width );
$table->setRenderer( $renderer );
$table->setHeaders( array( 'Field', 'Value' ) );
$table->addRow( array( '1この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。2この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。', 'こんにちは' ) );
$table->addRow( array( 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.', 'Hello' ) );
$out = $table->getDisplayLines();
for ( $i = 0; $i < count( $out ); $i++ ) {
$this->assertEquals( $constraint_width, \cli\strwidth( $out[$i] ) );
}
$constraint_width = 81;
$renderer = new cli\Table\Ascii;
$renderer->setConstraintWidth( $constraint_width );
$table->setRenderer( $renderer );
$out = $table->getDisplayLines();
for ( $i = 0; $i < count( $out ); $i++ ) {
$this->assertEquals( $constraint_width, \cli\strwidth( $out[$i] ) );
}
}
public function test_column_odd_single_width_with_double_width() {
$dummy = new cli\Table;
$renderer = new cli\Table\Ascii;
$strip_borders = function ( $a ) {
return array_map( function ( $v ) {
return substr( $v, 2, -2 );
}, $a );
};
$renderer->setWidths( array( 10 ) );
// 1 single-width, 6 double-width, 1 single-width, 2 double-width, 1 half-width, 2 double-width.
$out = $renderer->row( array( '1あいうえおか2きくカけこ' ) );
$result = $strip_borders( explode( "\n", $out ) );
$this->assertSame( 3, count( $result ) );
$this->assertSame( '1あいうえ ', $result[0] ); // 1 single width, 4 double-width, space = 10.
$this->assertSame( 'おか2きくカ', $result[1] ); // 2 double-width, 1 single-width, 2 double-width, 1 half-width = 10.
$this->assertSame( 'けこ ', $result[2] ); // 2 double-width, 8 spaces = 10.
// Minimum width 1.
$renderer->setWidths( array( 1 ) );
$out = $renderer->row( array( '1あいうえおか2きくカけこ' ) );
$result = $strip_borders( explode( "\n", $out ) );
$this->assertSame( 13, count( $result ) );
// Uneven rows.
$this->assertSame( '1', $result[0] );
$this->assertSame( 'あ', $result[1] );
// Zero width does no wrapping.
$renderer->setWidths( array( 0 ) );
$out = $renderer->row( array( '1あいうえおか2きくカけこ' ) );
$result = $strip_borders( explode( "\n", $out ) );
$this->assertSame( 1, count( $result ) );
}
public function test_column_fullwidth_and_combining() {
$constraint_width = 80;
$table = new cli\Table;
$renderer = new cli\Table\Ascii;
$renderer->setConstraintWidth( $constraint_width );
$table->setRenderer( $renderer );
$table->setHeaders( array( 'Field', 'Value' ) );
$table->addRow( array( 'ID', 2151 ) );
$table->addRow( array( 'post_author', 1 ) );
$table->addRow( array( 'post_title', 'only-english-lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-sed-do-eiusmod-tempor-incididunt-ut-labore' ) );
$table->addRow( array( 'post_content',
//'ให้รู้จัก ให้หาหนทางใหม่' .
'♫ มีอีกหลายต่อหลายคน เขาอดทนก็เพื่อรัก' . "\n" .
'รักผลักดันให้รู้จัก ให้หาหนทางใหม่' . "\r\n" .
'ฉันจะล้มตั้งหลายที ดีที่รักมาฉุดไว้' . "\r\n" .
'รักสร้างสรรค์สิ่งมากมาย และหลอมละลายทุกหัวใจ' . "\r\n" .
'จะมาร้ายดียังไง แต่ใจก็ยังต้องการ' . "\r\n" .
'ในทุกๆ วัน โลกหมุนด้วยความรัก ♫' . "\n" .
'ขอแสดงความยินดี งานแต่งพี่ Earn & Menn' ."\r\n" .
'เที่ยวปายหน้าร้อน ก็เที่ยวได้เหมือนกันน่ะ' . "\r\n" .
' ジョバンニはまっ赤になってうなずきました。けれどもいつかジョバンニの眼のなかには涙がいっぱいになりました。そうだ僕は知っていたのだ、もちろんカムパネルラも知っている。' ."\r\n" .
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore' . "\n" .
''
) );
$out = $table->getDisplayLines();
for ( $i = 0; $i < count( $out ); $i++ ) {
$this->assertEquals( $constraint_width, \cli\strwidth( $out[$i] ) );
}
$constraint_width = 81;
$renderer = new cli\Table\Ascii;
$renderer->setConstraintWidth( $constraint_width );
$table->setRenderer( $renderer );
$out = $table->getDisplayLines();
for ( $i = 0; $i < count( $out ); $i++ ) {
$this->assertEquals( $constraint_width, \cli\strwidth( $out[$i] ) );
}
$constraint_width = 200;
$renderer = new cli\Table\Ascii;
$renderer->setConstraintWidth( $constraint_width );
$table->setRenderer( $renderer );
$out = $table->getDisplayLines();
for ( $i = 0; $i < count( $out ); $i++ ) {
$this->assertEquals( $constraint_width, \cli\strwidth( $out[$i] ) );
}
}
public function test_ascii_pre_colorized_widths() {
Colors::enable( true );
$headers = array( 'package', 'version', 'result' );
$items = array(
array( Colors::colorize( '%ygaa/gaa-kabes%n' ), 'dev-master', Colors::colorize( "%rx%n" ) ),
array( Colors::colorize( '%ygaa/gaa-log%n' ), '*', Colors::colorize( "%gok%n" ) ),
array( Colors::colorize( '%ygaa/gaa-nonsense%n' ), 'v3.0.11', Colors::colorize( "%rx%n" ) ),
array( Colors::colorize( '%ygaa/gaa-100%%new%n' ), 'v100%new', Colors::colorize( "%gok%n" ) ),
);
// Disable colorization, as `\WP_CLI\Formatter::show_table()` does for Ascii tables.
Colors::disable( true );
$this->assertFalse( Colors::shouldColorize() );
// Account for colorization of columns 0 & 2.
$table = new Table;
$renderer = new Ascii;
$table->setRenderer( $renderer );
$table->setAsciiPreColorized( array( true, false, true ) );
$table->setHeaders( $headers );
$table->setRows( $items );
$out = $table->getDisplayLines();
// "+ 4" accommodates 3 borders and header.
$this->assertSame( 4 + 4, count( $out ) );
// Borders & header.
$this->assertSame( 42, strlen( $out[0] ) );
$this->assertSame( 42, strlen( $out[1] ) );
$this->assertSame( 42, strlen( $out[2] ) );
$this->assertSame( 42, strlen( $out[7] ) );
// Data.
$this->assertSame( 60, strlen( $out[3] ) );
$this->assertSame( 60, strlen( $out[4] ) );
$this->assertSame( 60, strlen( $out[5] ) );
$this->assertSame( 60, strlen( $out[6] ) );
// Don't account for colorization of columns 0 & 2.
$table = new Table;
$renderer = new Ascii;
$table->setRenderer( $renderer );
$table->setHeaders( $headers );
$table->setRows( $items );
$out = $table->getDisplayLines();
// "+ 4" accommodates 3 borders and header.
$this->assertSame( 4 + 4, count( $out ) );
// Borders & header.
$this->assertSame( 56, strlen( $out[0] ) );
$this->assertSame( 56, strlen( $out[1] ) );
$this->assertSame( 56, strlen( $out[2] ) );
$this->assertSame( 56, strlen( $out[7] ) );
// Data.
$this->assertSame( 56, strlen( $out[3] ) );
$this->assertSame( 56, strlen( $out[4] ) );
$this->assertSame( 56, strlen( $out[5] ) );
$this->assertSame( 56, strlen( $out[6] ) );
}
}