Skip to content

Commit 4302648

Browse files
benglrvagg
authored andcommitted
test: add test for repl.defineCommand()
It also tests displayPrompt by checking for '> '. PR-URL: #3908 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent c61237d commit 4302648

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const stream = require('stream'),
6+
assert = require('assert'),
7+
repl = require('repl');
8+
9+
var output = '';
10+
const inputStream = new stream.PassThrough();
11+
const outputStream = new stream.PassThrough();
12+
outputStream.on('data', function(d) {
13+
output += d;
14+
});
15+
16+
const r = repl.start({
17+
input: inputStream,
18+
output: outputStream,
19+
terminal: true
20+
});
21+
22+
r.defineCommand('say1', {
23+
help: 'help for say1',
24+
action: function(thing) {
25+
output = '';
26+
this.write('hello ' + thing);
27+
this.displayPrompt();
28+
}
29+
});
30+
31+
r.defineCommand('say2', function() {
32+
output = '';
33+
this.write('hello from say2');
34+
this.displayPrompt();
35+
});
36+
37+
inputStream.write('.help\n');
38+
assert(/\nsay1\thelp for say1\n/.test(output), 'help for say1 not present');
39+
assert(/\nsay2\t\n/.test(output), 'help for say2 not present');
40+
inputStream.write('.say1 node developer\n');
41+
assert(/> hello node developer/.test(output), 'say1 outputted incorrectly');
42+
inputStream.write('.say2 node developer\n');
43+
assert(/> hello from say2/.test(output), 'say2 outputted incorrectly');

0 commit comments

Comments
 (0)