node/test/parallel/test-repl-mode.js
JungMinu fce4b981ea repl,util: insert carriage returns in output
`\n` is not enough for Linux with some custom stream
add carriage returns to ensure that the output is displayed correctly
using `\r\n` should not be a problem, even on non-Windows platforms.

Fixes: https://github.com/nodejs/node/issues/7954
PR-URL: https://github.com/nodejs/node/pull/8028
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2016-08-13 23:03:30 +09:00

85 lines
1.8 KiB
JavaScript

'use strict';
var common = require('../common');
var assert = require('assert');
var Stream = require('stream');
var repl = require('repl');
common.globalCheck = false;
var tests = [
testSloppyMode,
testStrictMode,
testAutoMode
];
tests.forEach(function(test) {
test();
});
function testSloppyMode() {
var cli = initRepl(repl.REPL_MODE_SLOPPY);
cli.input.emit('data', `
x = 3
`.trim() + '\r\n');
assert.equal(cli.output.accumulator.join(''), '> 3\r\n> ');
cli.output.accumulator.length = 0;
cli.input.emit('data', `
let y = 3
`.trim() + '\r\n');
assert.equal(cli.output.accumulator.join(''), 'undefined\r\n> ');
}
function testStrictMode() {
var cli = initRepl(repl.REPL_MODE_STRICT);
cli.input.emit('data', `
x = 3
`.trim() + '\r\n');
assert.ok(/ReferenceError: x is not defined/.test(
cli.output.accumulator.join('')));
cli.output.accumulator.length = 0;
cli.input.emit('data', `
let y = 3
`.trim() + '\r\n');
assert.equal(cli.output.accumulator.join(''), 'undefined\r\n> ');
}
function testAutoMode() {
var cli = initRepl(repl.REPL_MODE_MAGIC);
cli.input.emit('data', `
x = 3
`.trim() + '\r\n');
assert.equal(cli.output.accumulator.join(''), '> 3\r\n> ');
cli.output.accumulator.length = 0;
cli.input.emit('data', `
let y = 3
`.trim() + '\r\n');
assert.equal(cli.output.accumulator.join(''), 'undefined\r\n> ');
}
function initRepl(mode) {
var input = new Stream();
input.write = input.pause = input.resume = function() {};
input.readable = true;
var 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
});
}