Skip to content

Commit ed83a1c

Browse files
BridgeARMylesBorins
authored andcommitted
test: refactor and simplify test-repl-preview
Use an object to indicate which part belongs to the input and which to the output. On top of that this also simplifies the expected output by automatically inserting the default repl line for previews and by automatically checking for the correct output line length. PR-URL: #32154 Reviewed-By: Shelley Vohr <codebytere@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1ecd407 commit ed83a1c

File tree

1 file changed

+90
-62
lines changed

1 file changed

+90
-62
lines changed

test/parallel/test-repl-preview.js

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const common = require('../common');
44
const assert = require('assert');
55
const { REPLServer } = require('repl');
66
const { Stream } = require('stream');
7+
const { inspect } = require('util');
78

89
common.skipIfInspectorDisabled();
910

@@ -76,84 +77,111 @@ async function tests(options) {
7677
'function koo() { console.log("abc"); }',
7778
'a = undefined;'
7879
]);
79-
const testCases = [
80-
['foo', [2, 4], '[Function: foo]',
81-
'foo',
82-
'\x1B[90m[Function: foo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
83-
'\x1B[36m[Function: foo]\x1B[39m',
84-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
85-
['koo', [2, 4], '[Function: koo]',
86-
'k\x1B[90moo\x1B[39m\x1B[9G\x1B[0Ko\x1B[90mo\x1B[39m\x1B[10G\x1B[0Ko',
87-
'\x1B[90m[Function: koo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
88-
'\x1B[36m[Function: koo]\x1B[39m',
89-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
90-
['a', [1, 2], undefined],
91-
[" { b: 1 }['b'] === 1", [2, 6], '\x1B[33mtrue\x1B[39m',
92-
" { b: 1 }['b']",
93-
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
94-
'\x1B[90m1\x1B[39m\x1B[23G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
95-
'\x1B[90mtrue\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
96-
'\x1B[33mtrue\x1B[39m',
97-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
98-
],
99-
["{ b: 1 }['b'] === 1;", [2, 7], '\x1B[33mfalse\x1B[39m',
100-
"{ b: 1 }['b']",
101-
'\x1B[90m1\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
102-
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
103-
'\x1B[90mtrue\x1B[39m\x1B[27G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
104-
'\x1B[90mfalse\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
105-
'\x1B[33mfalse\x1B[39m',
106-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
107-
],
108-
['{ a: true }', [2, 3], '{ a: \x1B[33mtrue\x1B[39m }',
109-
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke }\r',
110-
'{ a: \x1B[33mtrue\x1B[39m }',
111-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
112-
['1n + 2n', [2, 5], '\x1B[33m3n\x1B[39m',
113-
'1n + 2',
114-
'\x1B[90mType[39m\x1B[14G\x1B[1A\x1B[1B\x1B[2K\x1B[1An',
115-
'\x1B[90m3n\x1B[39m\x1B[15G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
116-
'\x1B[33m3n\x1B[39m',
117-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
118-
['{ a: true };', [2, 4], '\x1B[33mtrue\x1B[39m',
119-
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke };',
120-
'\x1B[90mtrue\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
121-
'\x1B[33mtrue\x1B[39m',
122-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
123-
[' \t { a: true};', [2, 5], '\x1B[33mtrue\x1B[39m',
124-
' { a: tru\x1B[90me\x1B[39m\x1B[18G\x1B[0Ke}',
125-
'\x1B[90m{ a: true }\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
126-
'\x1B[90mtrue\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
127-
'\x1B[33mtrue\x1B[39m',
128-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
129-
['{};1', [2, 4], '\x1B[33m1\x1B[39m',
130-
'{};1',
131-
'\x1B[90m1\x1B[39m\x1B[12G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
132-
'\x1B[33m1\x1B[39m',
133-
'\x1B[1G\x1B[0Jrepl > \x1B[8G'
80+
81+
const testCases = [{
82+
input: 'foo',
83+
noPreview: '[Function: foo]',
84+
preview: [
85+
'foo',
86+
'\x1B[90m[Function: foo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
87+
'\x1B[36m[Function: foo]\x1B[39m'
88+
]
89+
}, {
90+
input: 'koo',
91+
noPreview: '[Function: koo]',
92+
preview: [
93+
'k\x1B[90moo\x1B[39m\x1B[9G\x1B[0Ko\x1B[90mo\x1B[39m\x1B[10G\x1B[0Ko',
94+
'\x1B[90m[Function: koo]\x1B[39m\x1B[11G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
95+
'\x1B[36m[Function: koo]\x1B[39m'
96+
]
97+
}, {
98+
input: 'a',
99+
noPreview: 'repl > ', // No "undefined" output.
100+
preview: ['a\r'] // No "undefined" preview.
101+
}, {
102+
input: " { b: 1 }['b'] === 1",
103+
noPreview: '\x1B[33mtrue\x1B[39m',
104+
preview: [
105+
" { b: 1 }['b']",
106+
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
107+
'\x1B[90m1\x1B[39m\x1B[23G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
108+
'\x1B[90mtrue\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
109+
'\x1B[33mtrue\x1B[39m'
110+
]
111+
}, {
112+
input: "{ b: 1 }['b'] === 1;",
113+
noPreview: '\x1B[33mfalse\x1B[39m',
114+
preview: [
115+
"{ b: 1 }['b']",
116+
'\x1B[90m1\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A ',
117+
'\x1B[90m1\x1B[39m\x1B[22G\x1B[1A\x1B[1B\x1B[2K\x1B[1A=== 1',
118+
'\x1B[90mtrue\x1B[39m\x1B[27G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
119+
'\x1B[90mfalse\x1B[39m\x1B[28G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
120+
'\x1B[33mfalse\x1B[39m'
121+
]
122+
}, {
123+
input: '{ a: true }',
124+
noPreview: '{ a: \x1B[33mtrue\x1B[39m }',
125+
preview: [
126+
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke }\r',
127+
'{ a: \x1B[33mtrue\x1B[39m }'
128+
]
129+
}, {
130+
input: '{ a: true };',
131+
noPreview: '\x1B[33mtrue\x1B[39m',
132+
preview: [
133+
'{ a: tru\x1B[90me\x1B[39m\x1B[16G\x1B[0Ke };',
134+
'\x1B[90mtrue\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
135+
'\x1B[33mtrue\x1B[39m'
136+
]
137+
}, {
138+
input: ' \t { a: true};',
139+
noPreview: '\x1B[33mtrue\x1B[39m',
140+
preview: [
141+
' { a: tru\x1B[90me\x1B[39m\x1B[18G\x1B[0Ke}',
142+
'\x1B[90m{ a: true }\x1B[39m\x1B[20G\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
143+
'\x1B[90mtrue\x1B[39m\x1B[21G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
144+
'\x1B[33mtrue\x1B[39m'
145+
]
146+
}, {
147+
input: '1n + 2n',
148+
noPreview: '\x1B[33m3n\x1B[39m',
149+
preview: [
150+
'1n + 2',
151+
'\x1B[90mType[39m\x1B[14G\x1B[1A\x1B[1B\x1B[2K\x1B[1An',
152+
'\x1B[90m3n\x1B[39m\x1B[15G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
153+
'\x1B[33m3n\x1B[39m'
154+
]
155+
}, {
156+
input: '{};1',
157+
noPreview: '\x1B[33m1\x1B[39m',
158+
preview: [
159+
'{};1',
160+
'\x1B[90m1\x1B[39m\x1B[12G\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
161+
'\x1B[33m1\x1B[39m'
134162
]
135-
];
163+
}];
136164

137165
const hasPreview = repl.terminal &&
138166
(options.preview !== undefined ? !!options.preview : true);
139167

140-
for (const [input, length, expected, ...preview] of testCases) {
168+
for (const { input, noPreview, preview } of testCases) {
141169
console.log(`Testing ${input}`);
142170

143171
const toBeRun = input.split('\n');
144172
let lines = await runAndWait(toBeRun, repl);
145173

146-
assert.strictEqual(lines.length, length[+hasPreview]);
147-
if (expected === undefined) {
148-
assert(!lines.some((e) => e.includes('undefined')));
149-
} else if (hasPreview) {
174+
if (hasPreview) {
150175
// Remove error messages. That allows the code to run in different
151176
// engines.
152177
// eslint-disable-next-line no-control-regex
153178
lines = lines.map((line) => line.replace(/Error: .+?\x1B/, ''));
179+
assert.strictEqual(lines.pop(), '\x1B[1G\x1B[0Jrepl > \x1B[8G');
154180
assert.deepStrictEqual(lines, preview);
155181
} else {
156-
assert.ok(lines[0].includes(expected), lines);
182+
assert.ok(lines[0].includes(noPreview), lines.map(inspect));
183+
if (preview.length !== 1 || preview[0] !== `${input}\r`)
184+
assert.strictEqual(lines.length, 2);
157185
}
158186
}
159187
}

0 commit comments

Comments
 (0)