• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4const Stream = require('stream');
5const repl = require('repl');
6
7const tests = [
8  testSloppyMode,
9  testStrictMode,
10  testAutoMode,
11  testStrictModeTerminal,
12];
13
14tests.forEach(function(test) {
15  test();
16});
17
18function testSloppyMode() {
19  const cli = initRepl(repl.REPL_MODE_SLOPPY);
20
21  cli.input.emit('data', 'x = 3\n');
22  assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> ');
23  cli.output.accumulator.length = 0;
24
25  cli.input.emit('data', 'let y = 3\n');
26  assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
27}
28
29function testStrictMode() {
30  const cli = initRepl(repl.REPL_MODE_STRICT);
31
32  cli.input.emit('data', 'x = 3\n');
33  assert.match(cli.output.accumulator.join(''),
34               /ReferenceError: x is not defined/);
35  cli.output.accumulator.length = 0;
36
37  cli.input.emit('data', 'let y = 3\n');
38  assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
39}
40
41function testStrictModeTerminal() {
42  if (!process.features.inspector) {
43    console.warn('Test skipped: V8 inspector is disabled');
44    return;
45  }
46  // Verify that ReferenceErrors are reported in strict mode previews.
47  const cli = initRepl(repl.REPL_MODE_STRICT, {
48    terminal: true
49  });
50
51  cli.input.emit('data', 'xyz ');
52  assert.ok(
53    cli.output.accumulator.includes('\n// ReferenceError: xyz is not defined')
54  );
55}
56
57function testAutoMode() {
58  const cli = initRepl(repl.REPL_MODE_MAGIC);
59
60  cli.input.emit('data', 'x = 3\n');
61  assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> ');
62  cli.output.accumulator.length = 0;
63
64  cli.input.emit('data', 'let y = 3\n');
65  assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> ');
66}
67
68function initRepl(mode, options) {
69  const input = new Stream();
70  input.write = input.pause = input.resume = () => {};
71  input.readable = true;
72
73  const output = new Stream();
74  output.write = output.pause = output.resume = function(buf) {
75    output.accumulator.push(buf);
76  };
77  output.accumulator = [];
78  output.writable = true;
79
80  return repl.start({
81    input: input,
82    output: output,
83    useColors: false,
84    terminal: false,
85    replMode: mode,
86    ...options
87  });
88}
89