-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathutils_test.dart
More file actions
451 lines (408 loc) · 17 KB
/
utils_test.dart
File metadata and controls
451 lines (408 loc) · 17 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
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:args/src/utils.dart';
import 'package:test/test.dart';
const _lineLength = 40;
const _longLine = 'This is a long line that needs to be wrapped.';
final _longLineWithNewlines =
'This is a long line with newlines that\nneeds to be wrapped.\n\n'
'${'0123456789' * 5}';
final _indentedLongLineWithNewlines =
' This is an indented long line with newlines that\nneeds to be wrapped.'
'\n\tAnd preserves tabs.\n \n ${'0123456789' * 5}';
const _shortLine = 'Short line.';
const _indentedLongLine = ' This is an indented long line that needs to be '
'wrapped and indentation preserved.';
const _ansiReset = 'This is normal text. \x1B[0m<- Reset point.';
const _ansiBoldTextSpecificReset = 'This is normal, \x1B[1mthis is bold\x1B[22m, and this uses specific reset.';
const _ansiMixedStyles = 'Normal, \x1B[31mRed\x1B[0m, \x1B[1mBold\x1B[0m, \x1B[4mUnderline\x1B[0m, \x1B[1;34mBold Blue\x1B[0m, Normal again.';
const _ansiLongSequence = 'Start \x1B[1;3;4;5;7;9;31;42;38;5;196;48;5;226m Beaucoup formatting! \x1B[0m End';
const _ansiCombined256 = '\x1B[1;38;5;27;48;5;220mBold Bright Blue FG (27) on Gold BG (220)\x1B[0m';
const _ansiCombinedTrueColor = '\x1B[4;48;2;50;50;50;38;2;150;250;150mUnderlined Light Green FG on Dark Grey BG\x1B[0m';
void main() {
group('padding', () {
test('can pad on the right.', () {
expect(padRight('foo', 6), equals('foo '));
});
});
group('text wrapping', () {
test("doesn't wrap short lines.", () {
expect(wrapText(_shortLine, length: _lineLength), equals(_shortLine));
});
test("doesn't wrap at all if not given a length", () {
expect(wrapText(_longLine), equals(_longLine));
});
test('able to wrap long lines', () {
expect(wrapText(_longLine, length: _lineLength), equals('''
This is a long line that needs to be
wrapped.'''));
});
test('wrap long lines with no whitespace', () {
expect(wrapText('0123456789' * 5, length: _lineLength), equals('''
0123456789012345678901234567890123456789
0123456789'''));
});
test('refuses to wrap to a column smaller than 10 characters', () {
expect(wrapText('$_longLine ${'0123456789' * 4}', length: 1), equals('''
This is a
long line
that needs
to be
wrapped.
0123456789
0123456789
0123456789
0123456789'''));
});
test('preserves indentation', () {
expect(wrapText(_indentedLongLine, length: _lineLength), equals('''
This is an indented long line that
needs to be wrapped and indentation
preserved.'''));
});
test('preserves indentation and stripping trailing whitespace', () {
expect(wrapText('$_indentedLongLine ', length: _lineLength), equals('''
This is an indented long line that
needs to be wrapped and indentation
preserved.'''));
});
test('wraps text with newlines', () {
expect(wrapText(_longLineWithNewlines, length: _lineLength), equals('''
This is a long line with newlines that
needs to be wrapped.
0123456789012345678901234567890123456789
0123456789'''));
});
test('preserves indentation in the presence of newlines', () {
expect(wrapText(_indentedLongLineWithNewlines, length: _lineLength),
equals('''
This is an indented long line with
newlines that
needs to be wrapped.
\tAnd preserves tabs.
01234567890123456789012345678901234567
890123456789'''));
});
test('removes trailing whitespace when wrapping', () {
expect(wrapText('$_longLine \t', length: _lineLength), equals('''
This is a long line that needs to be
wrapped.'''));
});
test('preserves trailing whitespace when not wrapping', () {
expect(wrapText('$_longLine \t'), equals('$_longLine \t'));
});
test('honors hangingIndent parameter', () {
expect(
wrapText(_longLine, length: _lineLength, hangingIndent: 6), equals('''
This is a long line that needs to be
wrapped.'''));
});
test('handles hangingIndent with a single unwrapped line.', () {
expect(wrapText(_shortLine, length: _lineLength, hangingIndent: 6),
equals('''
Short line.'''));
});
test(
'handles hangingIndent with two unwrapped lines and the second is empty.',
() {
expect(wrapText('$_shortLine\n', length: _lineLength, hangingIndent: 6),
equals('''
Short line.
'''));
},
);
test('honors hangingIndent parameter on already indented line.', () {
expect(wrapText(_indentedLongLine, length: _lineLength, hangingIndent: 6),
equals('''
This is an indented long line that
needs to be wrapped and
indentation preserved.'''));
});
test('honors hangingIndent parameter on already indented line.', () {
expect(
wrapText(_indentedLongLineWithNewlines,
length: _lineLength, hangingIndent: 6),
equals('''
This is an indented long line with
newlines that
needs to be wrapped.
And preserves tabs.
01234567890123456789012345678901234567
890123456789'''));
});
});
group('text wrapping as lines', () {
test("doesn't wrap short lines.", () {
expect(wrapTextAsLines(_shortLine, length: _lineLength),
equals([_shortLine]));
});
test("doesn't wrap at all if not given a length", () {
expect(wrapTextAsLines(_longLine), equals([_longLine]));
});
test('able to wrap long lines', () {
expect(wrapTextAsLines(_longLine, length: _lineLength),
equals(['This is a long line that needs to be', 'wrapped.']));
});
test('wrap long lines with no whitespace', () {
expect(wrapTextAsLines('0123456789' * 5, length: _lineLength),
equals(['0123456789012345678901234567890123456789', '0123456789']));
});
test('refuses to wrap to a column smaller than 10 characters', () {
expect(
wrapTextAsLines('$_longLine ${'0123456789' * 4}', length: 1),
equals([
'This is a',
'long line',
'that needs',
'to be',
'wrapped.',
'0123456789',
'0123456789',
'0123456789',
'0123456789'
]));
});
test("doesn't preserve indentation", () {
expect(
wrapTextAsLines(_indentedLongLine, length: _lineLength),
equals([
'This is an indented long line that needs',
'to be wrapped and indentation preserved.'
]));
});
test('strips trailing whitespace', () {
expect(
wrapTextAsLines('$_indentedLongLine ', length: _lineLength),
equals([
'This is an indented long line that needs',
'to be wrapped and indentation preserved.'
]));
});
test('splits text with newlines properly', () {
expect(
wrapTextAsLines(_longLineWithNewlines, length: _lineLength),
equals([
'This is a long line with newlines that',
'needs to be wrapped.',
'',
'0123456789012345678901234567890123456789',
'0123456789'
]));
});
test('does not preserves indentation in the presence of newlines', () {
expect(
wrapTextAsLines(_indentedLongLineWithNewlines, length: _lineLength),
equals([
'This is an indented long line with',
'newlines that',
'needs to be wrapped.',
'And preserves tabs.',
'',
'0123456789012345678901234567890123456789',
'0123456789'
]));
});
test('removes trailing whitespace when wrapping', () {
expect(wrapTextAsLines('$_longLine \t', length: _lineLength),
equals(['This is a long line that needs to be', 'wrapped.']));
});
test('preserves trailing whitespace when not wrapping', () {
expect(
wrapTextAsLines('$_longLine \t'), equals(['$_longLine \t']));
});
});
group('text lengthWithoutAnsi is correct with no ANSI sequences', () {
test('lengthWithoutAnsi returns correct length on lines without ansi', () {
expect(_longLine.lengthWithoutAnsi, equals(_longLine.length));
});
test('lengthWithoutAnsi returns correct length on lines newlines and without ansi', () {
expect(_longLineWithNewlines.lengthWithoutAnsi, equals(_longLineWithNewlines.length));
});
test('lengthWithoutAnsi returns correct length on lines indented/newlines and without ansi', () {
expect(_indentedLongLineWithNewlines.lengthWithoutAnsi, equals(_indentedLongLineWithNewlines.length));
});
test('lengthWithoutAnsi returns correct length on short line without ansi', () {
expect(_shortLine.lengthWithoutAnsi, equals(_shortLine.length));
});
});
group('lengthWithoutAnsi is correct with no ANSI sequences', () {
test('lengthWithoutAnsi returns correct length on lines without ansi', () {
expect(_longLine.lengthWithoutAnsi, equals(_longLine.length));
});
test('lengthWithoutAnsi returns correct length on lines newlines and without ansi', () {
expect(_longLineWithNewlines.lengthWithoutAnsi, equals(_longLineWithNewlines.length));
});
test('lengthWithoutAnsi returns correct length on lines indented/newlines and without ansi', () {
expect(_indentedLongLineWithNewlines.lengthWithoutAnsi, equals(_indentedLongLineWithNewlines.length));
});
test('lengthWithoutAnsi returns correct length on short line without ansi', () {
expect(_shortLine.lengthWithoutAnsi, equals(_shortLine.length));
});
});
group('lengthWithoutAnsi is correct with variety of ANSI sequences', () {
test('lengthWithoutAnsi returns correct length - ansi reset', () {
expect(_ansiReset.lengthWithoutAnsi, equals(36));
});
test('lengthWithoutAnsi returns correct length - ansi bold, bold specific reset', () {
expect(_ansiBoldTextSpecificReset.lengthWithoutAnsi, equals(59));
});
test('lengthWithoutAnsi returns correct length - ansi mixed styles', () {
expect(_ansiMixedStyles.lengthWithoutAnsi, equals(54));
});
test('lengthWithoutAnsi returns correct length- ansi long sequence', () {
expect(_ansiLongSequence.lengthWithoutAnsi, equals(32));
});
test('lengthWithoutAnsi returns correct length - ansi 256 color sequence', () {
expect(_ansiCombined256.lengthWithoutAnsi, equals(41));
});
test('lengthWithoutAnsi returns correct length - ansi true color sequences', () {
expect(_ansiCombinedTrueColor.lengthWithoutAnsi, equals(41));
});
});
group('ANSI RegEx Systematic Tests', () {
test('Identifies standard SGR (Select Graphic Rendition) codes', () {
const reset = '\x1b[0m';
const boldRed = '\x1b[1;31m';
const bgBlue = '\x1b[44m';
expect(reset.ansiLength, equals(4));
expect(boldRed.ansiLength, equals(7));
expect(bgBlue.ansiLength, equals(5));
});
test('Identifies Private Mode sequences (starting with ?)', () {
const hideCursor = '\x1b[?25l';
const showCursor = '\x1b[?25h';
expect(hideCursor.ansiLength, equals(6));
expect(showCursor.ansiLength, equals(6));
});
test('Matches every valid termination character (A-Z, a-z)', () {
// CSI sequences usually end in the range 0x40 to 0x7E
// We check all standard alphabetic termination characters.
for (int i = 65; i <= 122; i++) {
if (i > 90 && i < 97) continue; // Skip non-alphas like [ \ ] ^ _ `
final char = String.fromCharCode(i);
final sequence = '\x1b[1;2;3$char';
// The RegEx should match the entire string
expect(sequence.ansiLength, equals(sequence.length),
reason: 'Failed on character: $char (ASCII $i)');
}
});
test('Correctly calculates length in mixed strings', () {
const text = 'Hello \x1b[32mWorld\x1b[0m';
// "Hello " (6) + "World" (5) = 11 visible
// "\x1b[32m" (5) + "\x1b[0m" (4) = 9 ANSI
expect(text.ansiLength, equals(9));
expect(text.stripAnsi().length, equals(11));
expect(text.length, equals(20));
});
test('Handles complex semicolon separators', () {
const complex = '\x1b[38;5;209;48;5;255m'; // Extended 256-color sequence
expect(complex.ansiLength, equals(20));
});
test('Does not match partial or broken sequences', () {
const broken = ' \x1b[31'; // Missing the terminator 'm'
expect(broken.ansiLength, equals(0));
const justEsc = '\x1b';
expect(justEsc.ansiLength, equals(0));
});
});
group('AnsiStringExtension specific getters', () {
test('ansiLength returns the literal character count of sequences', () {
// ESC [ 0 m (4 chars)
expect('\x1B[0m'.ansiLength, equals(4));
// ESC [ 3 8 ; 5 ; 2 0 9 m (11 chars)
expect('\x1B[38;5;209m'.ansiLength, equals(11));
});
test('hasAnsi correctly identifies presence of sequences', () {
expect(_ansiReset.hasAnsi(), isTrue);
expect(_ansiMixedStyles.hasAnsi(), isTrue);
expect(_shortLine.hasAnsi(), isFalse);
expect('Plain text'.hasAnsi(), isFalse);
});
test('lengthWithoutAnsi and ansiLength sum to total length', () {
final cases = [
_ansiReset,
_ansiBoldTextSpecificReset,
_ansiMixedStyles,
_ansiCombined256,
_ansiCombinedTrueColor
];
for (var testCase in cases) {
expect(testCase.lengthWithoutAnsi + testCase.ansiLength,
equals(testCase.length),
reason: 'Failed sum check for: $testCase');
}
});
});
group('ANSI-aware padding', () {
test('padRight accounts for ANSI length to align visually', () {
// "Red" is 3 visual chars, but 12 literal chars
// \x1B[31mRed\x1B[0m
const red = '\x1B[31mRed\x1B[0m';
// We want a visual width of 10.
// Traditional padRight(10) would see 12 chars and add nothing.
// Our utility padRight should add 7 spaces (10 - 3 visual).
final padded = padRight(red, 10);
expect(padded.lengthWithoutAnsi, equals(10));
expect(padded.startsWith(red), isTrue);
expect(padded.endsWith(' ' * 7), isTrue);
});
test('padRight works with plain text', () {
expect(padRight('foo', 6), equals('foo '));
});
});
group('Complex/Edge ANSI sequences', () {
test('handles multiple adjacent sequences', () {
const adjacent = '\x1b[1m\x1b[31m\x1b[4mText\x1b[0m';
// [1m (4) + [31m (5) + [4m (4) + [0m (4) = 17 ANSI chars
expect(adjacent.ansiLength, equals(17));
expect(adjacent.lengthWithoutAnsi, equals(4));
});
test('handles sequences with question marks (private modes)', () {
const hideCursor = '\x1b[?25l'; // Common in CLI apps
expect(hideCursor.ansiLength, equals(6));
expect(hideCursor.lengthWithoutAnsi, equals(0));
});
});
group('Advanced ANSI/ECMA-48 RegEx Tests', () {
test('Matches sequences with Intermediate Bytes correctly', () {
// CSI 1 Space q (Set cursor style)
// Here, the space is an Intermediate Byte (\x20)
const setCursorStyle = '\x1b[1 q';
expect(setCursorStyle.ansiLength, equals(5));
expect(setCursorStyle.stripAnsi(), equals(''));
});
test('Ensures it does NOT match sequences that violate the order', () {
// The standard requires: Parameters (0-9:;<=>?) THEN Intermediates (Space!"#$%&'()*+,-./) THEN Final (@-~)
// Test 1: Final byte 'm' appearing before an intermediate byte '/'
// The RegEx should stop at 'm', leaving the '/' and space behind.
const invalidOrder = '\x1b[m/ ';
expect(invalidOrder.ansiLength, equals(3)); // Matches '\x1b[m'
expect(invalidOrder.stripAnsi(), equals('/ '));
// Test 2: Parameter byte '?' appearing after a final byte 'm'
const paramsAfterFinal = '\x1b[m?';
expect(paramsAfterFinal.ansiLength, equals(3));
expect(paramsAfterFinal.stripAnsi(), equals('?'));
});
test('Matches every character in the allowed ranges', () {
// Parameter Range: < = > ? ; : and digits
const params = '\x1b[0123456789:;<=>?m';
expect(params.ansiLength, equals(params.length));
// Intermediate Range: Space ! " # $ % & ' ( ) * + , - . /
const intermediates = '\x1b[ !\"#\$%&\'()*+,-./m';
expect(intermediates.ansiLength, equals(intermediates.length));
});
test('Strictly terminates at the first Final Byte', () {
// In the string below, 'H' is a final byte.
// Even though 'm' is also a valid final byte, the sequence must end at 'H'.
const twoFinals = '\x1b[1H;24m';
expect(twoFinals.ansiLength, equals(4)); // Matches only '\x1b[1H'
expect(twoFinals.stripAnsi(), equals(';24m'));
});
test('Handles the "private" parameter range correctly', () {
// High-end terminal features often use the < = > ? prefix
const decvtpatch = '\x1b[>4;2m';
expect(decvtpatch.ansiLength, equals(7));
});
});
}