11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst ArrayStream = require('../common/arraystream');
41cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst repl = require('repl');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst stackRegExp = /(REPL\d+):[0-9]+:[0-9]+/g;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifunction run({ command, expected }) {
111cb0ef41Sopenharmony_ci  let accum = '';
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  const inputStream = new ArrayStream();
141cb0ef41Sopenharmony_ci  const outputStream = new ArrayStream();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  outputStream.write = (data) => accum += data.replace('\r', '');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  const r = repl.start({
191cb0ef41Sopenharmony_ci    prompt: '',
201cb0ef41Sopenharmony_ci    input: inputStream,
211cb0ef41Sopenharmony_ci    output: outputStream,
221cb0ef41Sopenharmony_ci    terminal: false,
231cb0ef41Sopenharmony_ci    useColors: false
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  r.write(`${command}\n`);
271cb0ef41Sopenharmony_ci  assert.strictEqual(
281cb0ef41Sopenharmony_ci    accum.replace(stackRegExp, '$1:*:*'),
291cb0ef41Sopenharmony_ci    expected.replace(stackRegExp, '$1:*:*')
301cb0ef41Sopenharmony_ci  );
311cb0ef41Sopenharmony_ci  r.close();
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst origPrepareStackTrace = Error.prepareStackTrace;
351cb0ef41Sopenharmony_ciError.prepareStackTrace = (err, stack) => {
361cb0ef41Sopenharmony_ci  if (err instanceof SyntaxError)
371cb0ef41Sopenharmony_ci    return err.toString();
381cb0ef41Sopenharmony_ci  stack.push(err);
391cb0ef41Sopenharmony_ci  return stack.reverse().join('--->\n');
401cb0ef41Sopenharmony_ci};
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciprocess.on('uncaughtException', (e) => {
431cb0ef41Sopenharmony_ci  Error.prepareStackTrace = origPrepareStackTrace;
441cb0ef41Sopenharmony_ci  throw e;
451cb0ef41Sopenharmony_ci});
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciconst tests = [
481cb0ef41Sopenharmony_ci  {
491cb0ef41Sopenharmony_ci    // test .load for a file that throws
501cb0ef41Sopenharmony_ci    command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
511cb0ef41Sopenharmony_ci    expected: 'Uncaught Error: Whoops!--->\nREPL1:*:*--->\nd (REPL1:*:*)' +
521cb0ef41Sopenharmony_ci              '--->\nc (REPL1:*:*)--->\nb (REPL1:*:*)--->\na (REPL1:*:*)\n'
531cb0ef41Sopenharmony_ci  },
541cb0ef41Sopenharmony_ci  {
551cb0ef41Sopenharmony_ci    command: 'let x y;',
561cb0ef41Sopenharmony_ci    expected: 'let x y;\n      ^\n\n' +
571cb0ef41Sopenharmony_ci              'Uncaught SyntaxError: Unexpected identifier\n'
581cb0ef41Sopenharmony_ci  },
591cb0ef41Sopenharmony_ci  {
601cb0ef41Sopenharmony_ci    command: 'throw new Error(\'Whoops!\')',
611cb0ef41Sopenharmony_ci    expected: 'Uncaught Error: Whoops!\n'
621cb0ef41Sopenharmony_ci  },
631cb0ef41Sopenharmony_ci  {
641cb0ef41Sopenharmony_ci    command: 'foo = bar;',
651cb0ef41Sopenharmony_ci    expected: 'Uncaught ReferenceError: bar is not defined\n'
661cb0ef41Sopenharmony_ci  },
671cb0ef41Sopenharmony_ci  // test anonymous IIFE
681cb0ef41Sopenharmony_ci  {
691cb0ef41Sopenharmony_ci    command: '(function() { throw new Error(\'Whoops!\'); })()',
701cb0ef41Sopenharmony_ci    expected: 'Uncaught Error: Whoops!--->\nREPL5:*:*\n'
711cb0ef41Sopenharmony_ci  },
721cb0ef41Sopenharmony_ci];
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_citests.forEach(run);
75