|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const repl = require('repl'); |
| 5 | +const ArrayStream = require('../common/arraystream'); |
| 6 | +const assert = require('assert'); |
| 7 | + |
| 8 | +const completionTests = [ |
| 9 | + { send: 'Promise.reject().' }, |
| 10 | + { send: 'let p = Promise.reject().' }, |
| 11 | + { send: 'Promise.resolve().' }, |
| 12 | + { send: 'Promise.resolve().then(() => {}).' }, |
| 13 | + { send: `async function f() {throw new Error('test');}; f().` }, |
| 14 | + { send: `async function f() {}; f().` }, |
| 15 | +]; |
| 16 | + |
| 17 | +const tests = [ |
| 18 | + { |
| 19 | + send: 'Promise.reject()', |
| 20 | + expect: /Promise \{[\s\S]*?Uncaught undefined\n?$/ |
| 21 | + }, |
| 22 | + { |
| 23 | + send: 'let p = Promise.reject()', |
| 24 | + expect: /undefined\nUncaught undefined\n?$/ |
| 25 | + }, |
| 26 | + { |
| 27 | + send: `Promise.resolve()`, |
| 28 | + expect: /Promise \{[\s\S]*?}\n?$/ |
| 29 | + }, |
| 30 | + { |
| 31 | + send: `Promise.resolve().then(() => {})`, |
| 32 | + expect: /Promise \{[\s\S]*?}\n?$/ |
| 33 | + }, |
| 34 | + { |
| 35 | + send: `async function f() { throw new Error('test'); };f();`, |
| 36 | + expect: /Promise \{[\s\S]*?<rejected> Error: test[\s\S]*?Uncaught Error: test[\s\S]*?\n?$/ |
| 37 | + }, |
| 38 | + { |
| 39 | + send: `async function f() {};f();`, |
| 40 | + expect: /Promise \{[\s\S]*?}\n?$/ |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | + |
| 45 | +(async function() { |
| 46 | + await runReplCompleteTests(completionTests); |
| 47 | + await runReplTests(tests); |
| 48 | +})().then(common.mustCall()); |
| 49 | + |
| 50 | +async function runReplCompleteTests(tests) { |
| 51 | + let outputText = ''; |
| 52 | + for (const { send } of tests) { |
| 53 | + const input = new ArrayStream(); |
| 54 | + const output = new ArrayStream(); |
| 55 | + |
| 56 | + function write(data) { |
| 57 | + outputText += data; |
| 58 | + } |
| 59 | + output.write = write; |
| 60 | + const replServer = repl.start({ |
| 61 | + prompt: '', |
| 62 | + input, |
| 63 | + output: output, |
| 64 | + allowBlockingCompletions: true, |
| 65 | + terminal: true |
| 66 | + }); |
| 67 | + |
| 68 | + input.emit('data', send); |
| 69 | + await new Promise((resolve) => { |
| 70 | + setTimeout(() => { |
| 71 | + assert.strictEqual(outputText.includes(send), true); |
| 72 | + assert.strictEqual(outputText.split(send).length - 1, 1); |
| 73 | + replServer.close(); |
| 74 | + resolve(); |
| 75 | + }, 10); |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +async function runReplTests(tests) { |
| 81 | + for (const { send, expect } of tests) { |
| 82 | + const input = new ArrayStream(); |
| 83 | + const output = new ArrayStream(); |
| 84 | + let outputText = ''; |
| 85 | + function write(data) { |
| 86 | + outputText += data; |
| 87 | + } |
| 88 | + output.write = write; |
| 89 | + const replServer = repl.start({ |
| 90 | + prompt: '', |
| 91 | + input, |
| 92 | + output: output, |
| 93 | + }); |
| 94 | + input.emit('data', `${send}\n`); |
| 95 | + await new Promise((resolve) => { |
| 96 | + setTimeout(() => { |
| 97 | + assert.match(outputText, expect); |
| 98 | + replServer.close(); |
| 99 | + resolve(); |
| 100 | + }, 10); |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments