|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const repl = require('repl'); |
| 6 | +const stream = require('stream'); |
| 7 | + |
| 8 | +testSloppyMode(); |
| 9 | +testStrictMode(); |
| 10 | +testResetContext(); |
| 11 | +testMagicMode(); |
| 12 | + |
| 13 | +function testSloppyMode() { |
| 14 | + const r = initRepl(repl.REPL_MODE_SLOPPY); |
| 15 | + |
| 16 | + // cannot use `let` in sloppy mode |
| 17 | + r.write(`_; // initial value undefined |
| 18 | + var x = 10; // evaluates to undefined |
| 19 | + _; // still undefined |
| 20 | + y = 10; // evaluates to 10 |
| 21 | + _; // 10 from last eval |
| 22 | + _ = 20; // explicitly set to 20 |
| 23 | + _; // 20 from user input |
| 24 | + _ = 30; // make sure we can set it twice and no prompt |
| 25 | + _; // 30 from user input |
| 26 | + y = 40; // make sure eval doesn't change _ |
| 27 | + _; // remains 30 from user input |
| 28 | + `); |
| 29 | + |
| 30 | + assertOutput(r.output, [ |
| 31 | + 'undefined', |
| 32 | + 'undefined', |
| 33 | + 'undefined', |
| 34 | + '10', |
| 35 | + '10', |
| 36 | + 'Expression assignment to _ now disabled.', |
| 37 | + '20', |
| 38 | + '20', |
| 39 | + '30', |
| 40 | + '30', |
| 41 | + '40', |
| 42 | + '30' |
| 43 | + ]); |
| 44 | +} |
| 45 | + |
| 46 | +function testStrictMode() { |
| 47 | + const r = initRepl(repl.REPL_MODE_STRICT); |
| 48 | + |
| 49 | + r.write(`_; // initial value undefined |
| 50 | + var x = 10; // evaluates to undefined |
| 51 | + _; // still undefined |
| 52 | + let _ = 20; // use 'let' only in strict mode - evals to undefined |
| 53 | + _; // 20 from user input |
| 54 | + _ = 30; // make sure we can set it twice and no prompt |
| 55 | + _; // 30 from user input |
| 56 | + var y = 40; // make sure eval doesn't change _ |
| 57 | + _; // remains 30 from user input |
| 58 | + function f() { let _ = 50; } // undefined |
| 59 | + f(); // undefined |
| 60 | + _; // remains 30 from user input |
| 61 | + `); |
| 62 | + |
| 63 | + assertOutput(r.output, [ |
| 64 | + 'undefined', |
| 65 | + 'undefined', |
| 66 | + 'undefined', |
| 67 | + 'undefined', |
| 68 | + '20', |
| 69 | + '30', |
| 70 | + '30', |
| 71 | + 'undefined', |
| 72 | + '30', |
| 73 | + 'undefined', |
| 74 | + 'undefined', |
| 75 | + '30' |
| 76 | + ]); |
| 77 | +} |
| 78 | + |
| 79 | +function testMagicMode() { |
| 80 | + const r = initRepl(repl.REPL_MODE_MAGIC); |
| 81 | + |
| 82 | + r.write(`_; // initial value undefined |
| 83 | + x = 10; // |
| 84 | + _; // last eval - 10 |
| 85 | + let _ = 20; // undefined |
| 86 | + _; // 20 from user input |
| 87 | + _ = 30; // make sure we can set it twice and no prompt |
| 88 | + _; // 30 from user input |
| 89 | + var y = 40; // make sure eval doesn't change _ |
| 90 | + _; // remains 30 from user input |
| 91 | + function f() { let _ = 50; return _; } // undefined |
| 92 | + f(); // 50 |
| 93 | + _; // remains 30 from user input |
| 94 | + `); |
| 95 | + |
| 96 | + assertOutput(r.output, [ |
| 97 | + 'undefined', |
| 98 | + '10', |
| 99 | + '10', |
| 100 | + 'undefined', |
| 101 | + '20', |
| 102 | + '30', |
| 103 | + '30', |
| 104 | + 'undefined', |
| 105 | + '30', |
| 106 | + 'undefined', |
| 107 | + '50', |
| 108 | + '30' |
| 109 | + ]); |
| 110 | +} |
| 111 | + |
| 112 | +function testResetContext() { |
| 113 | + const r = initRepl(repl.REPL_MODE_SLOPPY); |
| 114 | + |
| 115 | + r.write(`_ = 10; // explicitly set to 10 |
| 116 | + _; // 10 from user input |
| 117 | + .clear // Clearing context... |
| 118 | + _; // remains 10 |
| 119 | + x = 20; // but behavior reverts to last eval |
| 120 | + _; // expect 20 |
| 121 | + `); |
| 122 | + |
| 123 | + assertOutput(r.output, [ |
| 124 | + 'Expression assignment to _ now disabled.', |
| 125 | + '10', |
| 126 | + '10', |
| 127 | + 'Clearing context...', |
| 128 | + '10', |
| 129 | + '20', |
| 130 | + '20' |
| 131 | + ]); |
| 132 | +} |
| 133 | + |
| 134 | +function initRepl(mode) { |
| 135 | + const inputStream = new stream.PassThrough(); |
| 136 | + const outputStream = new stream.PassThrough(); |
| 137 | + outputStream.accum = ''; |
| 138 | + |
| 139 | + outputStream.on('data', (data) => { |
| 140 | + outputStream.accum += data; |
| 141 | + }); |
| 142 | + |
| 143 | + return repl.start({ |
| 144 | + input: inputStream, |
| 145 | + output: outputStream, |
| 146 | + useColors: false, |
| 147 | + terminal: false, |
| 148 | + prompt: '', |
| 149 | + replMode: mode |
| 150 | + }); |
| 151 | +} |
| 152 | + |
| 153 | +function assertOutput(output, expected) { |
| 154 | + const lines = output.accum.trim().split('\n'); |
| 155 | + assert.deepStrictEqual(lines, expected); |
| 156 | +} |
0 commit comments