Skip to content

Commit

Permalink
Add ability for wait function to take a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Fryslie committed Jan 28, 2015
1 parent 770e5b3 commit 2517e39
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -55,9 +55,10 @@ Throw an error if it does not.
The expectation can be a string (the line should contain the expected value as
a substring) or a RegExp (the line should match the expression).

### function wait (expectation)
### function wait (expectation, callback)

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

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

The callback will be called for every line that matches the expectation.

### function sendline (line)

* line {string} Output to write to the child process.
Expand Down
8 changes: 6 additions & 2 deletions lib/nexpect.js
Expand Up @@ -24,9 +24,13 @@ function chain (context) {

return chain(context);
},
wait: function (expectation) {
wait: function (expectation, callback) {
var _wait = function _wait (data) {
return testExpectation(data, expectation);
var val = testExpectation(data, expectation);
if (val === true && typeof callback === 'function') {
callback(data);
}
return val;
};

_wait.shift = false;
Expand Down
35 changes: 34 additions & 1 deletion test/nexpect-test.js
Expand Up @@ -85,7 +85,40 @@ vows.describe('nexpect').addBatch({
.wait('second')
.sendline('second-prompt')
.wait('this-never-shows-up')
)
),
"when a callback is provided and output is matched": {
topic: function() {
var expect = nexpect.spawn(path.join(__dirname, 'fixtures', 'prompt-and-respond'))
.wait('first', this.callback)
.sendline('first-prompt')
.expect('first-prompt')
.wait('second')
.sendline('second-prompt')
.expect('second-prompt').run(function() {});
},
'should call callback': function(matchData, b) {
assert.ok(matchData.indexOf('first') > 0, "Found 'first' in output")
}
},
"when a callback is provided and output is not matched": {
topic: function() {
var args = {hasRunCallback: false},
waitCallback = function() {
args.hasRunCallback = true;
};

var expect = nexpect.spawn(path.join(__dirname, 'fixtures', 'prompt-and-respond'))
.wait('first')
.sendline('first-prompt')
.expect('first-prompt')
.wait('second')
.sendline('second-prompt')
.wait('this-never-shows-up', waitCallback).run(this.callback.bind(this, args));
},
'should not call callback': function(args, a) {
assert.equal(args.hasRunCallback, false, 'Should not have run callback');
}
}
},
"when options.stripColors is set": assertSpawn(
nexpect.spawn(path.join(__dirname, 'fixtures', 'log-colors'), { stripColors: true })
Expand Down

0 comments on commit 2517e39

Please sign in to comment.