-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-repl-custom-eval.js
More file actions
135 lines (116 loc) Β· 3.84 KB
/
test-repl-custom-eval.js
File metadata and controls
135 lines (116 loc) Β· 3.84 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
'use strict';
require('../common');
const ArrayStream = require('../common/arraystream');
const assert = require('assert');
const { describe, it } = require('node:test');
const repl = require('repl');
function getReplOutput(input, replOptions, run = true) {
const inputStream = new ArrayStream();
const outputStream = new ArrayStream();
repl.start({
input: inputStream,
output: outputStream,
...replOptions,
});
let output = '';
outputStream.write = (chunk) => (output += chunk);
inputStream.emit('data', input);
if (run) {
inputStream.run(['']);
}
return output;
}
describe('repl with custom eval', { concurrency: true }, () => {
it('uses the custom eval function as expected', () => {
const output = getReplOutput('Convert this to upper case', {
terminal: true,
eval: (code, _ctx, _replRes, cb) => cb(null, code.toUpperCase()),
});
assert.match(
output,
/Convert this to upper case\r\n'CONVERT THIS TO UPPER CASE\\n'/
);
});
it('surfaces errors as expected', () => {
const output = getReplOutput('Convert this to upper case', {
terminal: true,
eval: (_code, _ctx, _replRes, cb) => cb(new Error('Testing Error')),
});
assert.match(output, /Uncaught Error: Testing Error\n/);
});
it('provides a repl context to the eval callback', async () => {
const context = await new Promise((resolve) => {
const r = repl.start({
eval: (_cmd, context) => resolve(context),
});
r.context = { foo: 'bar' };
r.write('\n.exit\n');
});
assert.strictEqual(context.foo, 'bar');
});
it('provides the global context to the eval callback', async () => {
const context = await new Promise((resolve) => {
const r = repl.start({
useGlobal: true,
eval: (_cmd, context) => resolve(context),
});
global.foo = 'global_foo';
r.write('\n.exit\n');
});
assert.strictEqual(context.foo, 'global_foo');
delete global.foo;
});
it('inherits variables from the global context but does not use it afterwords if `useGlobal` is false', async () => {
global.bar = 'global_bar';
const context = await new Promise((resolve) => {
const r = repl.start({
useGlobal: false,
eval: (_cmd, context) => resolve(context),
});
global.baz = 'global_baz';
r.write('\n.exit\n');
});
assert.strictEqual(context.bar, 'global_bar');
assert.notStrictEqual(context.baz, 'global_baz');
delete global.bar;
delete global.baz;
});
/**
* Default preprocessor transforms
* function f() {} to
* var f = function f() {}
* This test ensures that original input is preserved.
* Reference: https://github.com/nodejs/node/issues/9743
*/
it('preserves the original input', async () => {
const cmd = await new Promise((resolve) => {
const r = repl.start({
eval: (cmd) => resolve(cmd),
});
r.write('function f() {}\n.exit\n');
});
assert.strictEqual(cmd, 'function f() {}\n');
});
it("doesn't show previews by default", () => {
const input = "'Hello custom' + ' eval World!'";
const output = getReplOutput(input, {
terminal: true,
eval: (code, _ctx, _replRes, cb) => cb(null, eval(code)),
}, false);
assert.strictEqual(output, input);
assert.doesNotMatch(output, /Hello custom eval World!/);
});
it('does show previews if `preview` is set to `true`', () => {
const input = "'Hello custom' + ' eval World!'";
const output = getReplOutput(input, {
terminal: true,
eval: (code, _ctx, _replRes, cb) => cb(null, eval(code)),
preview: true,
}, false);
const escapedInput = input.replace(/\+/g, '\\+'); // TODO: migrate to `RegExp.escape` when it's available.
assert.match(
output,
new RegExp(`${escapedInput}\n// 'Hello custom eval World!'`)
);
});
});