-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathtest.js
More file actions
218 lines (184 loc) · 7.07 KB
/
test.js
File metadata and controls
218 lines (184 loc) · 7.07 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
import test from 'ava';
import ansiEscapes from 'ansi-escapes';
import * as ansiCodes from './fixtures/ansi-codes.js';
import ansiRegex from './index.js';
const consumptionCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+1234567890-=[]{};\':"./>?,<\\|';
// Testing against codes found at: http://ascii-table.com/ansi-escape-sequences-vt-100.php
test('match ansi code in a string', t => {
t.regex('foo\u001B[4mcake\u001B[0m', ansiRegex());
t.regex('\u001B[4mcake\u001B[0m', ansiRegex());
t.regex('foo\u001B[4mcake\u001B[0m', ansiRegex());
t.regex('\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m', ansiRegex());
t.regex('foo\u001B[mfoo', ansiRegex());
});
test('match ansi code from ls command', t => {
t.regex('\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfoo\u001B[0m', ansiRegex());
});
test('match reset;setfg;setbg;italics;strike;underline sequence in a string', t => {
t.regex('\u001B[0;33;49;3;9;4mbar\u001B[0m', ansiRegex());
t.is('foo\u001B[0;33;49;3;9;4mbar'.match(ansiRegex())[0], '\u001B[0;33;49;3;9;4m');
});
test('match clear tabs sequence in a string', t => {
t.regex('foo\u001B[0gbar', ansiRegex());
t.is('foo\u001B[0gbar'.match(ansiRegex())[0], '\u001B[0g');
});
test('match clear line from cursor right in a string', t => {
t.regex('foo\u001B[Kbar', ansiRegex());
t.is('foo\u001B[Kbar'.match(ansiRegex())[0], '\u001B[K');
});
test('match clear screen in a string', t => {
t.regex('foo\u001B[2Jbar', ansiRegex());
t.is('foo\u001B[2Jbar'.match(ansiRegex())[0], '\u001B[2J');
});
test('match only first', t => {
t.is('foo\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true})).length, 1);
});
test('match terminal link', t => {
for (const ST of ['\u0007', '\u001B\u005C', '\u009C']) {
t.regex(`\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}click\u001B]8;;${ST}`, ansiRegex());
t.regex(`\u001B]8;;mailto:no-replay@mail.com${ST}mail\u001B]8;;${ST}`, ansiRegex());
t.deepEqual(`\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}click\u001B]8;;${ST}`.match(ansiRegex()), [
`\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}`,
`\u001B]8;;${ST}`,
]);
t.deepEqual(`\u001B]8;;mailto:no-reply@mail.com${ST}mail-me\u001B]8;;${ST}`.match(ansiRegex()), [
`\u001B]8;;mailto:no-reply@mail.com${ST}`,
`\u001B]8;;${ST}`,
]);
}
});
test('match terminal link with plus in URL', t => {
for (const ST of ['\u0007', '\u001B\u005C', '\u009C']) {
const seqOpen = `\u001B]8;;https://www.example.com/?q=hello+world${ST}`;
const seqClose = `\u001B]8;;${ST}`;
const value = `${seqOpen}hello${seqClose}`;
t.deepEqual(value.match(ansiRegex()), [seqOpen, seqClose]);
t.is(value.replace(ansiRegex(), ''), 'hello');
}
});
test('match "change icon name and window title" in string', t => {
t.is('\u001B]0;sg@tota:~/git/\u0007\u001B[01;32m[sg@tota\u001B[01;37m misc-tests\u001B[01;32m]$'.match(ansiRegex())[0], '\u001B]0;sg@tota:~/git/\u0007');
});
test('match colon-separated sequence arguments', t => {
t.regex('\u001B[38:2:68:68:68:48:2:0:0:0m', ansiRegex());
t.is('\u001B[38:2:68:68:68:48:2:0:0:0m'.match(ansiRegex())[0], '\u001B[38:2:68:68:68:48:2:0:0:0m');
});
test('match colon-separated underline variants', t => {
for (const code of ['\u001B[4:0m', '\u001B[4:1m', '\u001B[4:2m', '\u001B[4:3m', '\u001B[4:4m', '\u001B[4:5m']) {
t.regex(code, ansiRegex());
t.is(code.match(ansiRegex())[0], code);
}
});
test('match colon-separated indexed color (38:5)', t => {
const code = '\u001B[38:5:123m';
t.regex(code, ansiRegex());
t.is(code.match(ansiRegex())[0], code);
});
test('match colon-separated indexed background color (48:5)', t => {
const code = '\u001B[48:5:200m';
t.regex(code, ansiRegex());
t.is(code.match(ansiRegex())[0], code);
});
test('match colon-separated underline color palette index (58:5)', t => {
const code = '\u001B[58:5:200m';
t.regex(code, ansiRegex());
t.is(code.match(ansiRegex())[0], code);
});
test('match colon-separated RGB colors (38:2::R:G:B and 48:2::R:G:B)', t => {
for (const code of ['\u001B[38:2::12:34:56m', '\u001B[48:2::200:201:202m']) {
t.regex(code, ansiRegex());
t.is(code.match(ansiRegex())[0], code);
}
});
test('match colon-separated underline color RGB (58:2::R:G:B)', t => {
const code = '\u001B[58:2::255:0:0m';
t.regex(code, ansiRegex());
t.is(code.match(ansiRegex())[0], code);
});
test('match colon-separated RGBA foreground/background (38:6, 48:6)', t => {
for (const code of ['\u001B[38:6::255:0:0:128m', '\u001B[48:6::0:0:0:64m']) {
t.regex(code, ansiRegex());
t.is(code.match(ansiRegex())[0], code);
}
});
test('colon-separated sequences should not overconsume', t => {
const samples = [
'\u001B[4:5mX',
'\u001B[38:5:123mX',
'\u001B[58:2::255:0:0mX',
'\u001B[38:2::12:34:56mX',
'\u001B[48:2::200:201:202mX',
];
for (const inputString of samples) {
const match = inputString.match(ansiRegex())[0];
t.truthy(match);
t.is(inputString.replace(ansiRegex(), ''), 'X');
}
});
test('does not match bracketed text without ESC', t => {
const samples = [
'[38:2:68:68:68m',
'[4:5m',
'some [0m text',
'plain [58:2::255:0:0m words',
];
for (const inputString of samples) {
t.is(inputString.match(ansiRegex()), null);
}
});
test('does not match incomplete CSI', t => {
const inputString = '\u001B[';
t.is(inputString.match(ansiRegex()), null);
});
test('does not match ESC followed by unsupported final', t => {
const inputString = 'pre\u001B`post';
t.is(inputString.match(ansiRegex()), null);
});
// Testing against extended codes (excluding codes ending in 0-9)
for (const [codeSetKey, codeSetValue] of Object.entries(ansiCodes)) {
for (const [code, codeInfo] of codeSetValue) {
const shouldSkip = /\d$/.test(code);
const skipText = shouldSkip ? '[SKIP] ' : '';
const ecode = `\u001B${code}`;
test(`${codeSetKey} - ${skipText}${code} → ${codeInfo[0]}`, t => {
if (shouldSkip) {
t.pass();
return;
}
const string = `hel${ecode}lo`;
t.regex(string, ansiRegex());
t.is(string.match(ansiRegex())[0], ecode);
t.is(string.replace(ansiRegex(), ''), 'hello');
});
test(`${codeSetKey} - ${skipText}${code} should not overconsume`, t => {
if (shouldSkip) {
t.pass();
return;
}
for (const character of consumptionCharacters) {
const string = ecode + character;
t.regex(string, ansiRegex());
t.is(string.match(ansiRegex())[0], ecode);
t.is(string.replace(ansiRegex(), ''), character);
}
});
}
}
const escapeCodeFunctionArguments = [1, 2];
const escapeCodeIgnoresList = new Set(['beep', 'image', 'iTerm']);
const escapeCodeResultMap = new Map([['link', escapeCodeFunctionArguments[0]]]);
for (const [key, escapeCode] of Object.entries(ansiEscapes)) {
if (escapeCodeIgnoresList.has(key)) {
continue;
}
const escapeCodeValue = typeof escapeCode === 'function'
? escapeCode(...escapeCodeFunctionArguments)
: escapeCode;
test(`ansi-escapes ${key}`, t => {
for (const character of consumptionCharacters) {
const string = escapeCodeValue + character;
const result = (escapeCodeResultMap.get(key) || '') + character;
t.is(string.replace(ansiRegex(), ''), result);
}
});
}