Skip to content

Commit 5b3025e

Browse files
committed
repl: improve line queue tests
1 parent 0d2ccad commit 5b3025e

File tree

1 file changed

+110
-9
lines changed

1 file changed

+110
-9
lines changed

test/parallel/test-repl-line-queue.js

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,128 @@ require('../common');
55
const ArrayStream = require('../common/arraystream');
66

77
const assert = require('assert');
8-
const repl = require('repl');
98

109
function* expectedLines(lines) {
1110
for (const line of lines) {
1211
yield line;
1312
}
14-
throw new Error('Requested more lines than expected');
1513
}
1614

15+
const expectedDebug = expectedLines([
16+
[
17+
'line %j',
18+
'const x = await new Promise((r) => setTimeout(() => r(1), 500));',
19+
],
20+
[
21+
'eval %j',
22+
'const x = await new Promise((r) => setTimeout(() => r(1), 500));\n',
23+
],
24+
['queued line %j', 'x;'],
25+
['finish', null, undefined],
26+
['line %j', 'x;'],
27+
['eval %j', 'x;\n'],
28+
['finish', null, 1],
29+
['line %j', 'const y = 3;'],
30+
['eval %j', 'const y = 3;\n'],
31+
['finish', null, undefined],
32+
['line %j', 'x + y;'],
33+
['eval %j', 'x + y;\n'],
34+
['finish', null, 4],
35+
[ 'line %j', 'const z = 4;' ],
36+
[ 'eval %j', 'const z = 4;\n' ],
37+
[ 'finish', null, undefined ],
38+
[ 'queued line %j', 'z + 1' ],
39+
[ 'line %j', 'z + 1' ],
40+
[ 'eval %j', 'z + 1\n' ],
41+
[ 'finish', null, 5 ],
42+
]);
43+
44+
let calledDebug = false;
45+
require('internal/util/debuglog').debuglog = () => {
46+
return (...args) => {
47+
calledDebug = true;
48+
assert.deepStrictEqual(args, expectedDebug.next().value);
49+
};
50+
};
51+
52+
// Import `repl` after replacing original `debuglog`
53+
const repl = require('repl');
54+
1755
const putIn = new ArrayStream();
1856
repl.start({
1957
input: putIn,
2058
output: putIn,
21-
useGlobal: false
59+
useGlobal: false,
2260
});
2361

24-
const expectedOutput = expectedLines(['undefined\n', '> ', '1\n', '> ']);
25-
putIn.write = function(data) {
62+
const expectedOutput = expectedLines([
63+
'undefined\n',
64+
'> ',
65+
'1\n',
66+
'> ',
67+
'undefined\n',
68+
'> ',
69+
'4\n',
70+
'> ',
71+
'undefined\n',
72+
'> ',
73+
'5\n',
74+
'> ',
75+
]);
76+
77+
let calledOutput = false;
78+
function writeCallback(data) {
79+
calledOutput = true;
2680
assert.strictEqual(data, expectedOutput.next().value);
27-
};
81+
}
2882

29-
putIn.run([
30-
'const x = await new Promise((r) => setTimeout(() => r(1), 500));\nx;',
31-
]);
83+
putIn.write = writeCallback;
84+
85+
function clearCalled() {
86+
calledDebug = false;
87+
calledOutput = false;
88+
}
89+
90+
// Lines sent after an async command that hasn't finished
91+
// in the same write are enqueued
92+
function test1() {
93+
putIn.run([
94+
'const x = await new Promise((r) => setTimeout(() => r(1), 500));\nx;',
95+
]);
96+
assert(calledDebug);
97+
98+
setTimeout(() => {
99+
assert(calledOutput);
100+
clearCalled();
101+
102+
test2();
103+
}, 1000);
104+
}
105+
106+
// Lines sent after a sync command in the same write are not enqueued
107+
function test2() {
108+
putIn.run(['const y = 3;\nx + y;']);
109+
110+
assert(calledDebug);
111+
assert(calledOutput);
112+
clearCalled();
113+
114+
test3();
115+
}
116+
117+
// Lines sent during an output write event of a previous command
118+
// are enqueued
119+
function test3() {
120+
putIn.write = (data) => {
121+
writeCallback(data);
122+
putIn.write = writeCallback;
123+
putIn.run(['z + 1']);
124+
};
125+
126+
putIn.run(['const z = 4;']);
127+
128+
assert(calledDebug);
129+
assert(calledOutput);
130+
}
131+
132+
test1();

0 commit comments

Comments
 (0)