2017-09-11 18:24:21 -04:00
|
|
|
'use strict';
|
2018-08-21 14:47:47 -07:00
|
|
|
require('../common');
|
|
|
|
const ArrayStream = require('../common/arraystream');
|
2017-09-11 18:24:21 -04:00
|
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
const assert = require('assert');
|
|
|
|
const repl = require('repl');
|
|
|
|
|
2019-07-09 13:40:01 +02:00
|
|
|
const stackRegExp = /(at .*repl:)[0-9]+:[0-9]+/g;
|
2017-09-11 18:24:21 -04:00
|
|
|
|
2019-09-23 19:38:12 +02:00
|
|
|
function run({ command, expected, ...extraREPLOptions }, i) {
|
2017-09-11 18:24:21 -04:00
|
|
|
let accum = '';
|
|
|
|
|
2018-08-21 14:47:47 -07:00
|
|
|
const inputStream = new ArrayStream();
|
|
|
|
const outputStream = new ArrayStream();
|
2017-09-11 18:24:21 -04:00
|
|
|
|
|
|
|
outputStream.write = (data) => accum += data.replace('\r', '');
|
|
|
|
|
|
|
|
const r = repl.start({
|
|
|
|
prompt: '',
|
|
|
|
input: inputStream,
|
|
|
|
output: outputStream,
|
|
|
|
terminal: false,
|
2019-02-28 11:03:37 +01:00
|
|
|
useColors: false,
|
|
|
|
...extraREPLOptions
|
2017-09-11 18:24:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
r.write(`${command}\n`);
|
2019-09-23 19:38:12 +02:00
|
|
|
console.log(i);
|
2019-07-09 13:40:01 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
accum.replace(stackRegExp, '$1*:*'),
|
|
|
|
expected.replace(stackRegExp, '$1*:*')
|
|
|
|
);
|
2017-09-11 18:24:21 -04:00
|
|
|
r.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{
|
2019-04-05 22:18:16 +02:00
|
|
|
// Test .load for a file that throws.
|
2017-09-11 18:24:21 -04:00
|
|
|
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught Error: Whoops!\n at repl:*:*\n' +
|
2019-07-09 13:40:01 +02:00
|
|
|
' at d (repl:*:*)\n at c (repl:*:*)\n' +
|
|
|
|
' at b (repl:*:*)\n at a (repl:*:*)\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: 'let x y;',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'let x y;\n ^\n\n' +
|
|
|
|
'Uncaught SyntaxError: Unexpected identifier\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: 'throw new Error(\'Whoops!\')',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught Error: Whoops!\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
2019-02-28 11:03:37 +01:00
|
|
|
{
|
|
|
|
command: '(() => { const err = Error(\'Whoops!\'); ' +
|
|
|
|
'err.foo = \'bar\'; throw err; })()',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: "Uncaught Error: Whoops!\n at repl:*:* {\n foo: 'bar'\n}\n",
|
2019-02-28 11:03:37 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
command: '(() => { const err = Error(\'Whoops!\'); ' +
|
|
|
|
'err.foo = \'bar\'; throw err; })()',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught Error: Whoops!\n at repl:*:* {\n foo: ' +
|
2019-04-05 22:18:16 +02:00
|
|
|
"\u001b[32m'bar'\u001b[39m\n}\n",
|
2019-02-28 11:03:37 +01:00
|
|
|
useColors: true
|
|
|
|
},
|
2017-09-11 18:24:21 -04:00
|
|
|
{
|
|
|
|
command: 'foo = bar;',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught ReferenceError: bar is not defined\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
},
|
2019-04-05 22:18:16 +02:00
|
|
|
// Test anonymous IIFE.
|
2017-09-11 18:24:21 -04:00
|
|
|
{
|
|
|
|
command: '(function() { throw new Error(\'Whoops!\'); })()',
|
2019-09-23 19:38:12 +02:00
|
|
|
expected: 'Uncaught Error: Whoops!\n at repl:*:*\n'
|
2017-09-11 18:24:21 -04:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
tests.forEach(run);
|