1'use strict'; 2 3require('../common'); 4 5const stream = require('stream'); 6const assert = require('assert'); 7const repl = require('repl'); 8 9let output = ''; 10const inputStream = new stream.PassThrough(); 11const outputStream = new stream.PassThrough(); 12outputStream.on('data', function(d) { 13 output += d; 14}); 15 16const r = repl.start({ 17 input: inputStream, 18 output: outputStream, 19 terminal: true 20}); 21 22r.defineCommand('say1', { 23 help: 'help for say1', 24 action: function(thing) { 25 output = ''; 26 this.output.write(`hello ${thing}\n`); 27 this.displayPrompt(); 28 } 29}); 30 31r.defineCommand('say2', function() { 32 output = ''; 33 this.output.write('hello from say2\n'); 34 this.displayPrompt(); 35}); 36 37inputStream.write('.help\n'); 38assert.match(output, /\n\.say1 {5}help for say1\n/); 39assert.match(output, /\n\.say2\n/); 40inputStream.write('.say1 node developer\n'); 41assert.ok(output.startsWith('hello node developer\n'), 42 `say1 output starts incorrectly: "${output}"`); 43assert.ok(output.includes('> '), 44 `say1 output does not include prompt: "${output}"`); 45inputStream.write('.say2 node developer\n'); 46assert.ok(output.startsWith('hello from say2\n'), 47 `say2 output starts incorrectly: "${output}"`); 48assert.ok(output.includes('> '), 49 `say2 output does not include prompt: "${output}"`); 50