-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathTest_Cli.php
More file actions
572 lines (459 loc) · 27 KB
/
Test_Cli.php
File metadata and controls
572 lines (459 loc) · 27 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
use cli\Colors;
use WP_CLI\Tests\TestCase;
class Test_Cli extends TestCase {
function set_up() {
// Reset enable state
\cli\Colors::enable( null );
// Empty the cache
\cli\Colors::clearStringCache();
}
function test_string_length() {
$this->assertEquals( \cli\Colors::length( 'x' ), 1 );
$this->assertEquals( \cli\Colors::length( '日' ), 1 );
}
function test_string_width() {
$this->assertEquals( \cli\Colors::width( 'x' ), 1 );
$this->assertEquals( \cli\Colors::width( '日' ), 2 ); // Double-width char.
}
function test_encoded_string_length() {
$this->assertEquals( \cli\Colors::length( 'hello' ), 5 );
$this->assertEquals( \cli\Colors::length( 'óra' ), 3 );
$this->assertEquals( \cli\Colors::length( '日本語' ), 3 );
}
function test_encoded_string_width() {
$this->assertEquals( \cli\Colors::width( 'hello' ), 5 );
$this->assertEquals( \cli\Colors::width( 'óra' ), 3 );
$this->assertEquals( \cli\Colors::width( '日本語' ), 6 ); // 3 double-width chars.
}
function test_encoded_string_pad() {
$this->assertEquals( 6, strlen( \cli\Colors::pad( 'hello', 6 ) ) );
$this->assertEquals( 7, strlen( \cli\Colors::pad( 'óra', 6 ) ) ); // special characters take one byte
$this->assertEquals( 9, strlen( \cli\Colors::pad( '日本語', 6 ) ) ); // each character takes two bytes
$this->assertEquals( 17, strlen( \cli\Colors::pad( 'עִבְרִית', 6 ) ) ); // process Hebrew vowels
$this->assertEquals( 6, strlen( \cli\Colors::pad( 'hello', 6, false, false, STR_PAD_RIGHT ) ) );
$this->assertEquals( 7, strlen( \cli\Colors::pad( 'óra', 6, false, false, STR_PAD_LEFT ) ) ); // special characters take one byte
$this->assertEquals( 9, strlen( \cli\Colors::pad( '日本語', 6, false, false, STR_PAD_BOTH ) ) ); // each character takes two bytes
$this->assertSame( 4, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_RIGHT ), 'o' ) );
$this->assertSame( 9, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_LEFT ), 'o' ) );
$this->assertSame( 6, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_BOTH ), 'o' ) );
$this->assertSame( 1, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_RIGHT ), 'e' ) );
$this->assertSame( 6, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_LEFT ), 'e' ) );
$this->assertSame( 3, strpos( \cli\Colors::pad( 'hello', 10, false, false, STR_PAD_BOTH ), 'e' ) );
}
function test_colorized_string_pad() {
// Colors enabled.
Colors::enable( true );
$x = Colors::colorize( '%Gx%n', true ); // colorized `x` string
$ora = Colors::colorize( "%Góra%n", true ); // colorized `óra` string
$this->assertSame( 22, strlen( Colors::pad( $x, 11 ) ) );
$this->assertSame( 22, strlen( Colors::pad( $x, 11, false /*pre_colorized*/ ) ) );
$this->assertSame( 22, strlen( Colors::pad( $x, 11, true /*pre_colorized*/ ) ) );
$this->assertSame( 23, strlen( Colors::pad( $ora, 11 ) ) ); // +1 for two-byte "ó".
$this->assertSame( 23, strlen( Colors::pad( $ora, 11, false /*pre_colorized*/ ) ) );
$this->assertSame( 23, strlen( Colors::pad( $ora, 11, true /*pre_colorized*/ ) ) );
// Colors disabled.
Colors::disable( true );
$this->assertFalse( Colors::shouldColorize() );
$this->assertSame( 20, strlen( Colors::pad( $x, 20 ) ) );
$this->assertSame( 20, strlen( Colors::pad( $x, 20, false /*pre_colorized*/ ) ) );
$this->assertSame( 31, strlen( Colors::pad( $x, 20, true /*pre_colorized*/ ) ) );
$this->assertSame( 21, strlen( Colors::pad( $ora, 20 ) ) ); // +1 for two-byte "ó".
$this->assertSame( 21, strlen( Colors::pad( $ora, 20, false /*pre_colorized*/ ) ) );
$this->assertSame( 32, strlen( Colors::pad( $ora, 20, true /*pre_colorized*/ ) ) );
}
function test_encoded_substr() {
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'hello', 6), 0, 2 ), 'he' );
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( 'óra', 6), 0, 2 ), 'ór' );
$this->assertEquals( \cli\safe_substr( \cli\Colors::pad( '日本語', 6), 0, 2 ), '日本' );
$this->assertSame( 'el', \cli\safe_substr( Colors::pad( 'hello', 6 ), 1, 2 ) );
$this->assertSame( 'a ', \cli\safe_substr( Colors::pad( 'óra', 6 ), 2, 2 ) );
$this->assertSame( ' ', \cli\safe_substr( Colors::pad( 'óra', 6 ), 5, 2 ) );
$this->assertSame( '本語', \cli\safe_substr( Colors::pad( '日本語', 8 ), 1, 2 ) );
$this->assertSame( '語 ', \cli\safe_substr( Colors::pad( '日本語', 8 ), 2, 2 ) );
$this->assertSame( ' ', \cli\safe_substr( Colors::pad( '日本語', 8 ), -1 ) );
$this->assertSame( ' ', \cli\safe_substr( Colors::pad( '日本語', 8 ), -1, 2 ) );
$this->assertSame( '語 ', \cli\safe_substr( Colors::pad( '日本語', 8 ), -3, 3 ) );
}
function test_various_substr() {
// Save.
$test_safe_substr = getenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR' );
if ( function_exists( 'mb_detect_order' ) ) {
$mb_detect_order = mb_detect_order();
}
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR' );
// Latin, kana, Latin, Latin combining, Thai combining, Hangul.
$str = 'lムnöม้p를'; // 18 bytes.
// Large string.
$large_str_str_start = 65536 * 2;
$large_str = str_repeat( 'a', $large_str_str_start ) . $str;
$large_str_len = strlen( $large_str ); // 128K + 18 bytes.
if ( \cli\can_use_icu() ) {
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR=1' ); // Tests grapheme_substr().
$this->assertSame( '', \cli\safe_substr( $str, 0, 0 ) );
$this->assertSame( 'l', \cli\safe_substr( $str, 0, 1 ) );
$this->assertSame( 'lム', \cli\safe_substr( $str, 0, 2 ) );
$this->assertSame( 'lムn', \cli\safe_substr( $str, 0, 3 ) );
$this->assertSame( 'lムnö', \cli\safe_substr( $str, 0, 4 ) );
$this->assertSame( 'lムnöม้', \cli\safe_substr( $str, 0, 5 ) );
$this->assertSame( 'lムnöม้p', \cli\safe_substr( $str, 0, 6 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, 0, 7 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, 0, 8 ) );
$this->assertSame( '', \cli\safe_substr( $str, 19 ) ); // Start too large.
$this->assertSame( '', \cli\safe_substr( $str, 19, 7 ) ); // Start too large, with length.
$this->assertSame( '', \cli\safe_substr( $str, 8 ) ); // Start same as length.
$this->assertSame( '', \cli\safe_substr( $str, 8, 0 ) ); // Start same as length, with zero length.
$this->assertSame( '를', \cli\safe_substr( $str, -1 ) );
$this->assertSame( 'p를', \cli\safe_substr( $str, -2 ) );
$this->assertSame( 'ม้p를', \cli\safe_substr( $str, -3 ) );
$this->assertSame( 'öม้p를', \cli\safe_substr( $str, -4 ) );
$this->assertSame( 'öม้p', \cli\safe_substr( $str, -4, 3 ) );
$this->assertSame( 'nö', \cli\safe_substr( $str, -5, 2 ) );
$this->assertSame( 'ム', \cli\safe_substr( $str, -6, 1 ) );
$this->assertSame( 'ムnöม้p를', \cli\safe_substr( $str, -6 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, -7 ) );
$this->assertSame( 'lムnö', \cli\safe_substr( $str, -7, 4 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, -8 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, -9 ) ); // Negative start too large.
$this->assertSame( $large_str, \cli\safe_substr( $large_str, 0 ) );
$this->assertSame( '', \cli\safe_substr( $large_str, $large_str_str_start, 0 ) );
$this->assertSame( 'l', \cli\safe_substr( $large_str, $large_str_str_start, 1 ) );
$this->assertSame( 'lム', \cli\safe_substr( $large_str, $large_str_str_start, 2 ) );
$this->assertSame( 'p를', \cli\safe_substr( $large_str, -2 ) );
}
if ( \cli\can_use_pcre_x() ) {
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR=2' ); // Tests preg_split( '/\X/u' ).
$this->assertSame( '', \cli\safe_substr( $str, 0, 0 ) );
$this->assertSame( 'l', \cli\safe_substr( $str, 0, 1 ) );
$this->assertSame( 'lム', \cli\safe_substr( $str, 0, 2 ) );
$this->assertSame( 'lムn', \cli\safe_substr( $str, 0, 3 ) );
$this->assertSame( 'lムnö', \cli\safe_substr( $str, 0, 4 ) );
$this->assertSame( 'lムnöม้', \cli\safe_substr( $str, 0, 5 ) );
$this->assertSame( 'lムnöม้p', \cli\safe_substr( $str, 0, 6 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, 0, 7 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, 0, 8 ) );
$this->assertSame( '', \cli\safe_substr( $str, 19 ) ); // Start too large.
$this->assertSame( '', \cli\safe_substr( $str, 19, 7 ) ); // Start too large, with length.
$this->assertSame( '', \cli\safe_substr( $str, 8 ) ); // Start same as length.
$this->assertSame( '', \cli\safe_substr( $str, 8, 0 ) ); // Start same as length, with zero length.
$this->assertSame( '를', \cli\safe_substr( $str, -1 ) );
$this->assertSame( 'p를', \cli\safe_substr( $str, -2 ) );
$this->assertSame( 'ม้p를', \cli\safe_substr( $str, -3 ) );
$this->assertSame( 'öม้p를', \cli\safe_substr( $str, -4 ) );
$this->assertSame( 'öม้p', \cli\safe_substr( $str, -4, 3 ) );
$this->assertSame( 'nö', \cli\safe_substr( $str, -5, 2 ) );
$this->assertSame( 'ム', \cli\safe_substr( $str, -6, 1 ) );
$this->assertSame( 'ムnöม้p를', \cli\safe_substr( $str, -6 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, -7 ) );
$this->assertSame( 'lムnö', \cli\safe_substr( $str, -7, 4 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, -8 ) );
$this->assertSame( 'lムnöม้p를', \cli\safe_substr( $str, -9 ) ); // Negative start too large.
$this->assertSame( $large_str, \cli\safe_substr( $large_str, 0 ) );
$this->assertSame( '', \cli\safe_substr( $large_str, $large_str_str_start, 0 ) );
$this->assertSame( 'l', \cli\safe_substr( $large_str, $large_str_str_start, 1 ) );
$this->assertSame( 'lム', \cli\safe_substr( $large_str, $large_str_str_start, 2 ) );
$this->assertSame( 'p를', \cli\safe_substr( $large_str, -2 ) );
}
if ( function_exists( 'mb_substr' ) ) {
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR=4' ); // Tests mb_substr().
$this->assertSame( '', \cli\safe_substr( $str, 0, 0 ) );
$this->assertSame( 'l', \cli\safe_substr( $str, 0, 1 ) );
$this->assertSame( 'lム', \cli\safe_substr( $str, 0, 2 ) );
$this->assertSame( 'lムn', \cli\safe_substr( $str, 0, 3 ) );
$this->assertSame( 'lムno', \cli\safe_substr( $str, 0, 4 ) ); // Wrong.
}
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR=8' ); // Tests substr().
$this->assertSame( '', \cli\safe_substr( $str, 0, 0 ) );
$this->assertSame( 'l', \cli\safe_substr( $str, 0, 1 ) );
$this->assertSame( "l\xe3", \cli\safe_substr( $str, 0, 2 ) ); // Corrupt.
$this->assertSame( '', \cli\safe_substr( $str, strlen( $str ) + 1 ) ); // Return '' not false to match behavior of other methods when `$start` > strlen.
// Non-UTF-8 - both grapheme_substr() and preg_split( '/\X/u' ) will fail.
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR' );
if ( function_exists( 'mb_substr' ) && function_exists( 'mb_detect_order' ) ) {
// Latin-1
mb_detect_order( array( 'UTF-8', 'ISO-8859-1' ) );
$str = "\xe0b\xe7"; // "àbç" in ISO-8859-1
$this->assertSame( "\xe0b", \cli\safe_substr( $str, 0, 2 ) );
$this->assertSame( "\xe0b", mb_substr( $str, 0, 2, 'ISO-8859-1' ) );
}
// Restore.
putenv( false == $test_safe_substr ? 'PHP_CLI_TOOLS_TEST_SAFE_SUBSTR' : "PHP_CLI_TOOLS_TEST_SAFE_SUBSTR=$test_safe_substr" );
if ( function_exists( 'mb_detect_order' ) ) {
mb_detect_order( $mb_detect_order );
}
}
function test_is_width_encoded_substr() {
$this->assertSame( 'he', \cli\safe_substr( Colors::pad( 'hello', 6 ), 0, 2, true /*is_width*/ ) );
$this->assertSame( 'ór', \cli\safe_substr( Colors::pad( 'óra', 6 ), 0, 2, true /*is_width*/ ) );
$this->assertSame( '日', \cli\safe_substr( Colors::pad( '日本語', 8 ), 0, 2, true /*is_width*/ ) );
$this->assertSame( '日', \cli\safe_substr( Colors::pad( '日本語', 8 ), 0, 3, true /*is_width*/ ) );
$this->assertSame( '日本', \cli\safe_substr( Colors::pad( '日本語', 8 ), 0, 4, true /*is_width*/ ) );
$this->assertSame( '日本語', \cli\safe_substr( Colors::pad( '日本語', 8 ), 0, 6, true /*is_width*/ ) );
$this->assertSame( '日本語 ', \cli\safe_substr( Colors::pad( '日本語', 8 ), 0, 7, true /*is_width*/ ) );
$this->assertSame( 'el', \cli\safe_substr( Colors::pad( 'hello', 6 ), 1, 2, true /*is_width*/ ) );
$this->assertSame( 'a ', \cli\safe_substr( Colors::pad( 'óra', 6 ), 2, 2, true /*is_width*/ ) );
$this->assertSame( ' ', \cli\safe_substr( Colors::pad( 'óra', 6 ), 5, 2, true /*is_width*/ ) );
$this->assertSame( '', \cli\safe_substr( '1日4本語90', 0, 0, true /*is_width*/ ) );
$this->assertSame( '1', \cli\safe_substr( '1日4本語90', 0, 1, true /*is_width*/ ) );
$this->assertSame( '1', \cli\safe_substr( '1日4本語90', 0, 2, true /*is_width*/ ) );
$this->assertSame( '1日', \cli\safe_substr( '1日4本語90', 0, 3, true /*is_width*/ ) );
$this->assertSame( '1日4', \cli\safe_substr( '1日4本語90', 0, 4, true /*is_width*/ ) );
$this->assertSame( '1日4', \cli\safe_substr( '1日4本語90', 0, 5, true /*is_width*/ ) );
$this->assertSame( '1日4本', \cli\safe_substr( '1日4本語90', 0, 6, true /*is_width*/ ) );
$this->assertSame( '1日4本', \cli\safe_substr( '1日4本語90', 0, 7, true /*is_width*/ ) );
$this->assertSame( '1日4本語', \cli\safe_substr( '1日4本語90', 0, 8, true /*is_width*/ ) );
$this->assertSame( '1日4本語9', \cli\safe_substr( '1日4本語90', 0, 9, true /*is_width*/ ) );
$this->assertSame( '1日4本語90', \cli\safe_substr( '1日4本語90', 0, 10, true /*is_width*/ ) );
$this->assertSame( '1日4本語90', \cli\safe_substr( '1日4本語90', 0, 11, true /*is_width*/ ) );
$this->assertSame( '日', \cli\safe_substr( '1日4本語90', 1, 2, true /*is_width*/ ) );
$this->assertSame( '日4', \cli\safe_substr( '1日4本語90', 1, 3, true /*is_width*/ ) );
$this->assertSame( '4本語9', \cli\safe_substr( '1日4本語90', 2, 6, true /*is_width*/ ) );
$this->assertSame( '本', \cli\safe_substr( '1日4本語90', 3, 1, true /*is_width*/ ) );
$this->assertSame( '本', \cli\safe_substr( '1日4本語90', 3, 2, true /*is_width*/ ) );
$this->assertSame( '本', \cli\safe_substr( '1日4本語90', 3, 3, true /*is_width*/ ) );
$this->assertSame( '本語', \cli\safe_substr( '1日4本語90', 3, 4, true /*is_width*/ ) );
$this->assertSame( '本語9', \cli\safe_substr( '1日4本語90', 3, 5, true /*is_width*/ ) );
$this->assertSame( '0', \cli\safe_substr( '1日4本語90', 6, 1, true /*is_width*/ ) );
$this->assertSame( '', \cli\safe_substr( '1日4本語90', 7, 1, true /*is_width*/ ) );
$this->assertSame( '', \cli\safe_substr( '1日4本語90', 6, 0, true /*is_width*/ ) );
$this->assertSame( '0', \cli\safe_substr( '1日4本語90', -1, 3, true /*is_width*/ ) );
$this->assertSame( '90', \cli\safe_substr( '1日4本語90', -2, 3, true /*is_width*/ ) );
$this->assertSame( '語9', \cli\safe_substr( '1日4本語90', -3, 3, true /*is_width*/ ) );
$this->assertSame( '本語9', \cli\safe_substr( '1日4本語90', -4, 5, true /*is_width*/ ) );
}
function test_colorized_string_length() {
$this->assertEquals( \cli\Colors::length( \cli\Colors::colorize( '%Gx%n', true ) ), 1 );
$this->assertEquals( \cli\Colors::length( \cli\Colors::colorize( '%G日%n', true ) ), 1 );
}
function test_colorized_string_width() {
// Colors enabled.
Colors::enable( true );
$x = Colors::colorize( '%Gx%n', true );
$dw = Colors::colorize( '%G日%n', true ); // Double-width char.
$this->assertSame( 1, Colors::width( $x ) );
$this->assertSame( 1, Colors::width( $x, false /*pre_colorized*/ ) );
$this->assertSame( 1, Colors::width( $x, true /*pre_colorized*/ ) );
$this->assertSame( 2, Colors::width( $dw ) );
$this->assertSame( 2, Colors::width( $dw, false /*pre_colorized*/ ) );
$this->assertSame( 2, Colors::width( $dw, true /*pre_colorized*/ ) );
// Colors disabled.
Colors::disable( true );
$this->assertFalse( Colors::shouldColorize() );
$this->assertSame( 12, Colors::width( $x ) );
$this->assertSame( 12, Colors::width( $x, false /*pre_colorized*/ ) );
$this->assertSame( 1, Colors::width( $x, true /*pre_colorized*/ ) );
$this->assertSame( 13, Colors::width( $dw ) );
$this->assertSame( 13, Colors::width( $dw, false /*pre_colorized*/ ) );
$this->assertSame( 2, Colors::width( $dw, true /*pre_colorized*/ ) );
}
function test_colorize_string_is_colored() {
$original = '%Gx';
$colorized = "\033[32;1mx";
$this->assertEquals( \cli\Colors::colorize( $original, true ), $colorized );
}
function test_colorize_when_colorize_is_forced() {
$original = '%gx%n';
$this->assertEquals( \cli\Colors::colorize( $original, false ), 'x' );
}
function test_binary_string_is_converted_back_to_original_string() {
$string = 'x';
$string_with_color = '%b' . $string;
$colorized_string = "\033[34m$string";
// Ensure colorization is applied correctly
$this->assertEquals( \cli\Colors::colorize( $string_with_color, true ), $colorized_string );
// Ensure that the colorization is reverted
$this->assertEquals( \cli\Colors::decolorize( $colorized_string ), $string );
}
function test_string_cache() {
$string = 'x';
$string_with_color = '%k' . $string;
$colorized_string = "\033[30m$string";
// Ensure colorization works
$this->assertEquals( \cli\Colors::colorize( $string_with_color, true ), $colorized_string );
// Test that the value was cached appropriately
$test_cache = array(
'passed' => $string_with_color,
'colorized' => $colorized_string,
'decolorized' => $string,
);
$real_cache = \cli\Colors::getStringCache();
// Test that the cache value exists
$this->assertTrue( isset( $real_cache[ md5( $string_with_color ) ] ) );
// Test that the cache value is correctly set
$this->assertEquals( $test_cache, $real_cache[ md5( $string_with_color ) ] );
}
function test_string_cache_colorize() {
$string = 'x';
$string_with_color = '%k' . $string;
$colorized_string = "\033[30m$string";
// Colors enabled.
Colors::enable( true );
// Ensure colorization works
$this->assertSame( $colorized_string, Colors::colorize( $string_with_color ) );
$this->assertSame( $colorized_string, Colors::colorize( $string_with_color ) );
// Colors disabled.
Colors::disable( true );
$this->assertFalse( Colors::shouldColorize() );
// Ensure it doesn't come from the cache.
$this->assertSame( $string, Colors::colorize( $string_with_color ) );
$this->assertSame( $string, Colors::colorize( $string_with_color ) );
// Check that escaped % isn't stripped on putting into cache.
$string = 'x%%n';
$string_with_color = '%k' . $string;
$this->assertSame( 'x%n', Colors::colorize( $string_with_color ) );
$this->assertSame( 'x%n', Colors::colorize( $string_with_color ) );
}
function test_decolorize() {
// Colors enabled.
Colors::enable( true );
$string = '%kx%%n%n';
$colorized_string = Colors::colorize( $string );
$both_string = '%gfoo' . $colorized_string . 'bar%%%n';
$this->assertSame( 'x%n', Colors::decolorize( $string ) );
$this->assertSame( 'x', Colors::decolorize( $colorized_string ) );
$this->assertSame( 'fooxbar%', Colors::decolorize( $both_string ) );
$this->assertSame( $string, Colors::decolorize( $string, 1 /*keep_tokens*/ ) );
$this->assertSame( 'x%n', Colors::decolorize( $colorized_string, 1 /*keep_tokens*/ ) );
$this->assertSame( '%gfoox%nbar%%%n', Colors::decolorize( $both_string, 1 /*keep_tokens*/ ) );
$this->assertSame( 'x%n', Colors::decolorize( $string, 2 /*keep_encodings*/ ) );
$this->assertSame( '[30mx[0m', Colors::decolorize( $colorized_string, 2 /*keep_encodings*/ ) );
$this->assertSame( 'foo[30mx[0mbar%', Colors::decolorize( $both_string, 2 /*keep_encodings*/ ) );
$this->assertSame( $string, Colors::decolorize( $string, 3 /*noop*/ ) );
$this->assertSame( $colorized_string, Colors::decolorize( $colorized_string, 3 /*noop*/ ) );
$this->assertSame( $both_string, Colors::decolorize( $both_string, 3 /*noop*/ ) );
}
function test_strwidth() {
// Save.
$test_strwidth = getenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH' );
if ( function_exists( 'mb_detect_order' ) ) {
$mb_detect_order = mb_detect_order();
}
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH' );
// UTF-8.
// 4 characters, one a double-width Han = 5 spacing chars, with 2 combining chars. Adapted from http://unicode.org/faq/char_combmark.html#7 (combining acute accent added after "a").
$str = "a\xCC\x81\xE0\xA4\xA8\xE0\xA4\xBF\xE4\xBA\x9C\xF0\x90\x82\x83";
if ( \cli\can_use_icu() ) {
$this->assertSame( 5, \cli\strwidth( $str ) ); // Tests grapheme_strlen().
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=2' ); // Test preg_split( '/\X/u' ).
$this->assertSame( 5, \cli\strwidth( $str ) );
} else {
$this->assertSame( 5, \cli\strwidth( $str ) ); // Tests preg_split( '/\X/u' ).
}
if ( function_exists( 'mb_strwidth' ) && function_exists( 'mb_detect_order' ) ) {
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=4' ); // Test mb_strwidth().
mb_detect_order( array( 'UTF-8', 'ISO-8859-1' ) );
$this->assertSame( 5, \cli\strwidth( $str ) );
}
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=8' ); // Test safe_strlen().
if ( \cli\can_use_icu() || \cli\can_use_pcre_x() ) {
$this->assertSame( 4, \cli\strwidth( $str ) ); // safe_strlen() (correctly) does not account for double-width Han so out by 1.
} elseif ( function_exists( 'mb_strlen' ) && function_exists( 'mb_detect_order' ) ) {
$this->assertSame( 4, \cli\strwidth( $str ) ); // safe_strlen() (correctly) does not account for double-width Han so out by 1.
$this->assertSame( 6, mb_strlen( $str, 'UTF-8' ) );
} else {
$this->assertSame( 16, \cli\strwidth( $str ) ); // strlen() - no. of bytes.
$this->assertSame( 16, strlen( $str ) );
}
// Nepali जस्ट ट॓स्ट गर्दै - 1st word: 3 spacing + 1 combining, 2nd word: 3 spacing + 2 combining, 3rd word: 3 spacing + 2 combining = 9 spacing chars + 2 spaces = 11 chars.
// Note: ICU's grapheme_strlen() treats Devanagari conjuncts (consonant + virama + consonant) as single graphemes.
// Modern ICU versions (54.1+) return 8 for this string, while PCRE \X returns 11.
$str = "\xe0\xa4\x9c\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\x9f \xe0\xa4\x9f\xe0\xa5\x93\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\x9f \xe0\xa4\x97\xe0\xa4\xb0\xe0\xa5\x8d\xe0\xa4\xa6\xe0\xa5\x88";
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH' );
if ( \cli\can_use_icu() ) {
$this->assertSame( 8, \cli\strwidth( $str ) ); // Tests grapheme_strlen() - ICU treats conjuncts as single graphemes.
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=2' ); // Test preg_split( '/\X/u' ).
$this->assertSame( 11, \cli\strwidth( $str ) );
} else {
$this->assertSame( 11, \cli\strwidth( $str ) ); // Tests preg_split( '/\X/u' ).
}
if ( function_exists( 'mb_strwidth' ) && function_exists( 'mb_detect_order' ) ) {
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=4' ); // Test mb_strwidth().
mb_detect_order( array( 'UTF-8' ) );
$this->assertSame( 11, \cli\strwidth( $str ) );
}
// Non-UTF-8 - both grapheme_strlen() and preg_split( '/\X/u' ) will fail.
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH' );
if ( function_exists( 'mb_strwidth' ) && function_exists( 'mb_detect_order' ) ) {
// Latin-1
mb_detect_order( array( 'UTF-8', 'ISO-8859-1' ) );
$str = "\xe0b\xe7"; // "àbç" in ISO-8859-1
$this->assertSame( 3, \cli\strwidth( $str ) ); // Test mb_strwidth().
$this->assertSame( 3, mb_strwidth( $str, 'ISO-8859-1' ) );
// Shift JIS.
mb_detect_order( array( 'UTF-8', 'SJIS' ) );
$str = "\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd\x90\xa2\x8a\x45!"; // "こャにちは世界!" ("Hello world!") in Shift JIS - 7 double-width chars plus Latin exclamation mark.
$this->assertSame( 15, \cli\strwidth( $str ) ); // Test mb_strwidth().
$this->assertSame( 15, mb_strwidth( $str, 'SJIS' ) );
putenv( 'PHP_CLI_TOOLS_TEST_STRWIDTH=8' ); // Test safe_strlen().
if ( function_exists( 'mb_strlen' ) && function_exists( 'mb_detect_order' ) ) {
$this->assertSame( 8, \cli\strwidth( $str ) ); // mb_strlen() - doesn't allow for double-width.
$this->assertSame( 8, mb_strlen( $str, 'SJIS' ) );
} else {
$this->assertSame( 15, \cli\strwidth( $str ) ); // strlen() - no. of bytes.
$this->assertSame( 15, strlen( $str ) );
}
}
// Restore.
putenv( false == $test_strwidth ? 'PHP_CLI_TOOLS_TEST_STRWIDTH' : "PHP_CLI_TOOLS_TEST_STRWIDTH=$test_strwidth" );
if ( function_exists( 'mb_detect_order' ) ) {
mb_detect_order( $mb_detect_order );
}
}
function test_safe_strlen() {
// Save.
$test_safe_strlen = getenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN' );
if ( function_exists( 'mb_detect_order' ) ) {
$mb_detect_order = mb_detect_order();
}
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN' );
// UTF-8.
// ASCII l, 3-byte kana, ASCII n, ASCII o + 2-byte combining umlaut, 6-byte Thai combining, ASCII, 3-byte Hangul. grapheme length 7, bytes 18.
$str = 'lムnöม้p를';
if ( \cli\can_use_icu() ) {
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN' ); // Test grapheme_strlen().
$this->assertSame( 7, \cli\safe_strlen( $str ) );
if ( \cli\can_use_pcre_x() ) {
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN=2' ); // Test preg_split( '/\X/u' ).
$this->assertSame( 7, \cli\safe_strlen( $str ) );
}
} elseif ( \cli\can_use_pcre_x() ) {
$this->assertSame( 7, \cli\safe_strlen( $str ) ); // Tests preg_split( '/\X/u' ).
} else {
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN=8' ); // Test strlen().
$this->assertSame( 18, \cli\safe_strlen( $str ) ); // strlen() - no. of bytes.
$this->assertSame( 18, strlen( $str ) );
}
if ( function_exists( 'mb_strlen' ) && function_exists( 'mb_detect_order' ) ) {
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN=4' ); // Test mb_strlen().
mb_detect_order( array( 'UTF-8', 'ISO-8859-1' ) );
$this->assertSame( 7, \cli\safe_strlen( $str ) );
$this->assertSame( 9, mb_strlen( $str, 'UTF-8' ) ); // mb_strlen() - counts the 2 combining chars.
}
// Non-UTF-8 - both grapheme_strlen() and preg_split( '/\X/u' ) will fail.
putenv( 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN' );
if ( function_exists( 'mb_strlen' ) && function_exists( 'mb_detect_order' ) ) {
// Latin-1
mb_detect_order( array( 'UTF-8', 'ISO-8859-1' ) );
$str = "\xe0b\xe7"; // "àbç" in ISO-8859-1
$this->assertSame( 3, \cli\safe_strlen( $str ) ); // Test mb_strlen().
$this->assertSame( 3, mb_strlen( $str, 'ISO-8859-1' ) );
}
// Restore.
putenv( false == $test_safe_strlen ? 'PHP_CLI_TOOLS_TEST_SAFE_STRLEN' : "PHP_CLI_TOOLS_TEST_SAFE_STRLEN=$test_safe_strlen" );
if ( function_exists( 'mb_detect_order' ) ) {
mb_detect_order( $mb_detect_order );
}
}
function test_render_with_color_tokens_and_sprintf_args_colors_disabled() {
Colors::disable( true );
// Color tokens in format string must not cause "sprintf(): Too few arguments".
$result = \cli\render( '[%C%k%s%N] Starting!', '2024-01-01 12:00:00' );
$this->assertSame( '[2024-01-01 12:00:00] Starting!', $result );
}
function test_render_with_color_tokens_and_sprintf_args_colors_enabled() {
Colors::enable( true );
$result = \cli\render( '[%C%k%s%N] Starting!', '2024-01-01 12:00:00' );
$this->assertStringContainsString( '2024-01-01 12:00:00', $result );
$this->assertStringContainsString( 'Starting!', $result );
}
}