mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 23:56:58 +00:00

PR-URL: https://github.com/nodejs/node/pull/39928 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
89 lines
2.2 KiB
JavaScript
89 lines
2.2 KiB
JavaScript
'use strict';
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const Stream = require('stream');
|
|
const repl = require('repl');
|
|
|
|
const tests = [
|
|
testSloppyMode,
|
|
testStrictMode,
|
|
testAutoMode,
|
|
testStrictModeTerminal,
|
|
];
|
|
|
|
tests.forEach(function(test) {
|
|
test();
|
|
});
|
|
|
|
function testSloppyMode() {
|
|
const cli = initRepl(repl.REPL_MODE_SLOPPY);
|
|
|
|
cli.input.emit('data', 'x = 3\n');
|
|
assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> ');
|
|
cli.output.accumulator.length = 0;
|
|
|
|
cli.input.emit('data', 'let y = 3\n');
|
|
assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
|
|
}
|
|
|
|
function testStrictMode() {
|
|
const cli = initRepl(repl.REPL_MODE_STRICT);
|
|
|
|
cli.input.emit('data', 'x = 3\n');
|
|
assert.match(cli.output.accumulator.join(''),
|
|
/ReferenceError: x is not defined/);
|
|
cli.output.accumulator.length = 0;
|
|
|
|
cli.input.emit('data', 'let y = 3\n');
|
|
assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
|
|
}
|
|
|
|
function testStrictModeTerminal() {
|
|
if (!process.features.inspector) {
|
|
console.warn('Test skipped: V8 inspector is disabled');
|
|
return;
|
|
}
|
|
// Verify that ReferenceErrors are reported in strict mode previews.
|
|
const cli = initRepl(repl.REPL_MODE_STRICT, {
|
|
terminal: true
|
|
});
|
|
|
|
cli.input.emit('data', 'xyz ');
|
|
assert.ok(
|
|
cli.output.accumulator.includes('\n// ReferenceError: xyz is not defined')
|
|
);
|
|
}
|
|
|
|
function testAutoMode() {
|
|
const cli = initRepl(repl.REPL_MODE_MAGIC);
|
|
|
|
cli.input.emit('data', 'x = 3\n');
|
|
assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> ');
|
|
cli.output.accumulator.length = 0;
|
|
|
|
cli.input.emit('data', 'let y = 3\n');
|
|
assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
|
|
}
|
|
|
|
function initRepl(mode, options) {
|
|
const input = new Stream();
|
|
input.write = input.pause = input.resume = () => {};
|
|
input.readable = true;
|
|
|
|
const output = new Stream();
|
|
output.write = output.pause = output.resume = function(buf) {
|
|
output.accumulator.push(buf);
|
|
};
|
|
output.accumulator = [];
|
|
output.writable = true;
|
|
|
|
return repl.start({
|
|
input: input,
|
|
output: output,
|
|
useColors: false,
|
|
terminal: false,
|
|
replMode: mode,
|
|
...options
|
|
});
|
|
}
|