Skip to content

Commit 70a8975

Browse files
committed
Merge pull request #23 from brandon-fryslie/allow_wait_function_to_take_a_callback
Add ability for wait function to take a callback
2 parents 27ad995 + 2517e39 commit 70a8975

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ Throw an error if it does not.
5555
The expectation can be a string (the line should contain the expected value as
5656
a substring) or a RegExp (the line should match the expression).
5757

58-
### function wait (expectation)
58+
### function wait (expectation, callback)
5959

6060
* expectation {string|RegExp} Output to assert on the target stream
61+
* callback {Function} **Optional** Callback to be called when output matches stream
6162

6263
Wait for a line of output that matches the expectation, discarding lines
6364
that do not match.
@@ -67,6 +68,8 @@ Throw an error if no such line was found.
6768
The expectation can be a string (the line should contain the expected value as
6869
a substring) or a RegExp (the line should match the expression).
6970

71+
The callback will be called for every line that matches the expectation.
72+
7073
### function sendline (line)
7174

7275
* line {string} Output to write to the child process.

lib/nexpect.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ function chain (context) {
2424

2525
return chain(context);
2626
},
27-
wait: function (expectation) {
27+
wait: function (expectation, callback) {
2828
var _wait = function _wait (data) {
29-
return testExpectation(data, expectation);
29+
var val = testExpectation(data, expectation);
30+
if (val === true && typeof callback === 'function') {
31+
callback(data);
32+
}
33+
return val;
3034
};
3135

3236
_wait.shift = false;

test/nexpect-test.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,40 @@ vows.describe('nexpect').addBatch({
8585
.wait('second')
8686
.sendline('second-prompt')
8787
.wait('this-never-shows-up')
88-
)
88+
),
89+
"when a callback is provided and output is matched": {
90+
topic: function() {
91+
var expect = nexpect.spawn(path.join(__dirname, 'fixtures', 'prompt-and-respond'))
92+
.wait('first', this.callback)
93+
.sendline('first-prompt')
94+
.expect('first-prompt')
95+
.wait('second')
96+
.sendline('second-prompt')
97+
.expect('second-prompt').run(function() {});
98+
},
99+
'should call callback': function(matchData, b) {
100+
assert.ok(matchData.indexOf('first') > 0, "Found 'first' in output")
101+
}
102+
},
103+
"when a callback is provided and output is not matched": {
104+
topic: function() {
105+
var args = {hasRunCallback: false},
106+
waitCallback = function() {
107+
args.hasRunCallback = true;
108+
};
109+
110+
var expect = nexpect.spawn(path.join(__dirname, 'fixtures', 'prompt-and-respond'))
111+
.wait('first')
112+
.sendline('first-prompt')
113+
.expect('first-prompt')
114+
.wait('second')
115+
.sendline('second-prompt')
116+
.wait('this-never-shows-up', waitCallback).run(this.callback.bind(this, args));
117+
},
118+
'should not call callback': function(args, a) {
119+
assert.equal(args.hasRunCallback, false, 'Should not have run callback');
120+
}
121+
}
89122
},
90123
"when options.stripColors is set": assertSpawn(
91124
nexpect.spawn(path.join(__dirname, 'fixtures', 'log-colors'), { stripColors: true })

0 commit comments

Comments
 (0)