• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const ArrayStream = require('../common/arraystream');
4const assert = require('assert');
5const repl = require('repl');
6const vm = require('vm');
7
8// Create a dummy stream that does nothing.
9const stream = new ArrayStream();
10
11// Test context when useGlobal is false.
12{
13  const r = repl.start({
14    input: stream,
15    output: stream,
16    useGlobal: false
17  });
18
19  let output = '';
20  stream.write = function(d) {
21    output += d;
22  };
23
24  // Ensure that the repl context gets its own "console" instance.
25  assert(r.context.console);
26
27  // Ensure that the repl console instance is not the global one.
28  assert.notStrictEqual(r.context.console, console);
29  assert.notStrictEqual(r.context.Object, Object);
30
31  stream.run(['({} instanceof Object)']);
32
33  assert.strictEqual(output, 'true\n> ');
34
35  const context = r.createContext();
36  // Ensure that the repl context gets its own "console" instance.
37  assert(context.console instanceof require('console').Console);
38
39  // Ensure that the repl's global property is the context.
40  assert.strictEqual(context.global, context);
41
42  // Ensure that the repl console instance is writable.
43  context.console = 'foo';
44  r.close();
45}
46
47// Test for context side effects.
48{
49  const server = repl.start({ input: stream, output: stream });
50
51  assert.ok(!server.underscoreAssigned);
52  assert.strictEqual(server.lines.length, 0);
53
54  // An assignment to '_' in the repl server
55  server.write('_ = 500;\n');
56  assert.ok(server.underscoreAssigned);
57  assert.strictEqual(server.lines.length, 1);
58  assert.strictEqual(server.lines[0], '_ = 500;');
59  assert.strictEqual(server.last, 500);
60
61  // Use the server to create a new context
62  const context = server.createContext();
63
64  // Ensure that creating a new context does not
65  // have side effects on the server
66  assert.ok(server.underscoreAssigned);
67  assert.strictEqual(server.lines.length, 1);
68  assert.strictEqual(server.lines[0], '_ = 500;');
69  assert.strictEqual(server.last, 500);
70
71  // Reset the server context
72  server.resetContext();
73  assert.ok(!server.underscoreAssigned);
74  assert.strictEqual(server.lines.length, 0);
75
76  // Ensure that assigning to '_' in the new context
77  // does not change the value in our server.
78  assert.ok(!server.underscoreAssigned);
79  vm.runInContext('_ = 1000;\n', context);
80
81  assert.ok(!server.underscoreAssigned);
82  assert.strictEqual(server.lines.length, 0);
83  server.close();
84}
85