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 = /(at .*REPL\d+:)[0-9]+:[0-9]+/g;
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_cifunction run({ command, expected, ...extraREPLOptions }, i) {
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    ...extraREPLOptions
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  r.write(`${command}\n`);
281cb0ef41Sopenharmony_ci  console.log(i);
291cb0ef41Sopenharmony_ci  assert.strictEqual(
301cb0ef41Sopenharmony_ci    accum.replace(stackRegExp, '$1*:*'),
311cb0ef41Sopenharmony_ci    expected.replace(stackRegExp, '$1*:*')
321cb0ef41Sopenharmony_ci  );
331cb0ef41Sopenharmony_ci  r.close();
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst tests = [
371cb0ef41Sopenharmony_ci  {
381cb0ef41Sopenharmony_ci    // Test .load for a file that throws.
391cb0ef41Sopenharmony_ci    command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
401cb0ef41Sopenharmony_ci    expected: 'Uncaught Error: Whoops!\n    at REPL1:*:*\n' +
411cb0ef41Sopenharmony_ci              '    at d (REPL1:*:*)\n    at c (REPL1:*:*)\n' +
421cb0ef41Sopenharmony_ci              '    at b (REPL1:*:*)\n    at a (REPL1:*:*)\n'
431cb0ef41Sopenharmony_ci  },
441cb0ef41Sopenharmony_ci  {
451cb0ef41Sopenharmony_ci    command: 'let x y;',
461cb0ef41Sopenharmony_ci    expected: 'let x y;\n      ^\n\n' +
471cb0ef41Sopenharmony_ci              'Uncaught SyntaxError: Unexpected identifier\n'
481cb0ef41Sopenharmony_ci  },
491cb0ef41Sopenharmony_ci  {
501cb0ef41Sopenharmony_ci    command: 'throw new Error(\'Whoops!\')',
511cb0ef41Sopenharmony_ci    expected: 'Uncaught Error: Whoops!\n'
521cb0ef41Sopenharmony_ci  },
531cb0ef41Sopenharmony_ci  {
541cb0ef41Sopenharmony_ci    command: '(() => { const err = Error(\'Whoops!\'); ' +
551cb0ef41Sopenharmony_ci             'err.foo = \'bar\'; throw err; })()',
561cb0ef41Sopenharmony_ci    expected: "Uncaught Error: Whoops!\n    at REPL4:*:* {\n  foo: 'bar'\n}\n",
571cb0ef41Sopenharmony_ci  },
581cb0ef41Sopenharmony_ci  {
591cb0ef41Sopenharmony_ci    command: '(() => { const err = Error(\'Whoops!\'); ' +
601cb0ef41Sopenharmony_ci             'err.foo = \'bar\'; throw err; })()',
611cb0ef41Sopenharmony_ci    expected: 'Uncaught Error: Whoops!\n    at REPL5:*:* {\n  foo: ' +
621cb0ef41Sopenharmony_ci              "\u001b[32m'bar'\u001b[39m\n}\n",
631cb0ef41Sopenharmony_ci    useColors: true
641cb0ef41Sopenharmony_ci  },
651cb0ef41Sopenharmony_ci  {
661cb0ef41Sopenharmony_ci    command: 'foo = bar;',
671cb0ef41Sopenharmony_ci    expected: 'Uncaught ReferenceError: bar is not defined\n'
681cb0ef41Sopenharmony_ci  },
691cb0ef41Sopenharmony_ci  // Test anonymous IIFE.
701cb0ef41Sopenharmony_ci  {
711cb0ef41Sopenharmony_ci    command: '(function() { throw new Error(\'Whoops!\'); })()',
721cb0ef41Sopenharmony_ci    expected: 'Uncaught Error: Whoops!\n    at REPL7:*:*\n'
731cb0ef41Sopenharmony_ci  },
741cb0ef41Sopenharmony_ci];
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_citests.forEach(run);
77