2015-11-19 01:42:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('../common');
|
|
|
|
|
2016-01-13 20:42:45 +00:00
|
|
|
const stream = require('stream');
|
|
|
|
const assert = require('assert');
|
|
|
|
const repl = require('repl');
|
2015-11-19 01:42:05 +00:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let output = '';
|
2015-11-19 01:42:05 +00:00
|
|
|
const inputStream = new stream.PassThrough();
|
|
|
|
const outputStream = new stream.PassThrough();
|
|
|
|
outputStream.on('data', function(d) {
|
|
|
|
output += d;
|
|
|
|
});
|
|
|
|
|
|
|
|
const r = repl.start({
|
|
|
|
input: inputStream,
|
|
|
|
output: outputStream,
|
|
|
|
terminal: true
|
|
|
|
});
|
|
|
|
|
|
|
|
r.defineCommand('say1', {
|
|
|
|
help: 'help for say1',
|
|
|
|
action: function(thing) {
|
|
|
|
output = '';
|
2017-12-20 20:31:22 +00:00
|
|
|
this.output.write(`hello ${thing}\n`);
|
2015-11-19 01:42:05 +00:00
|
|
|
this.displayPrompt();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
r.defineCommand('say2', function() {
|
|
|
|
output = '';
|
2017-12-20 20:31:22 +00:00
|
|
|
this.output.write('hello from say2\n');
|
2015-11-19 01:42:05 +00:00
|
|
|
this.displayPrompt();
|
|
|
|
});
|
|
|
|
|
2016-08-17 14:47:13 +00:00
|
|
|
inputStream.write('.help\n');
|
2022-01-09 16:45:19 +00:00
|
|
|
assert.match(output, /\n\.say1 {5}help for say1\n/);
|
2021-08-29 08:14:22 +00:00
|
|
|
assert.match(output, /\n\.say2\n/);
|
2016-08-17 14:47:13 +00:00
|
|
|
inputStream.write('.say1 node developer\n');
|
2017-12-20 20:31:22 +00:00
|
|
|
assert.ok(output.startsWith('hello node developer\n'),
|
|
|
|
`say1 output starts incorrectly: "${output}"`);
|
|
|
|
assert.ok(output.includes('> '),
|
|
|
|
`say1 output does not include prompt: "${output}"`);
|
2016-08-17 14:47:13 +00:00
|
|
|
inputStream.write('.say2 node developer\n');
|
2017-12-20 20:31:22 +00:00
|
|
|
assert.ok(output.startsWith('hello from say2\n'),
|
|
|
|
`say2 output starts incorrectly: "${output}"`);
|
|
|
|
assert.ok(output.includes('> '),
|
|
|
|
`say2 output does not include prompt: "${output}"`);
|