Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLI stdin/out/err streams; evented mocks. Ref yui#95.
The CLI now expects to be provided with streams for: - stdin - stdout - stderr This replaces the old options: - readableStream - writableStream - putsFn - errorFn Removing putsFn and errorFn discourages usage of Node.js stdio (e.g., console.*), which is one less thing to mock. The CLI.prototype's puts() and error() methods still allow for `printf`-like formatting by using util.format(). Create new mock Readable and Writable Streams for testing. The MockWritableStream supports expect(), which will fire a callback when the expected string is provided. This lets us do async testing of the CLI.
- Loading branch information
Showing
5 changed files
with
158 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
"use strict"; | ||
|
||
/** | ||
* @module streams | ||
*/ | ||
|
||
var util = require("util"); | ||
var EventEmitter2 = require("../../lib/events").EventEmitter2; | ||
|
||
function makeString(data) { | ||
if (Buffer.isBuffer(data)) { | ||
data = data.toString("utf8"); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
function WRITE(data) { | ||
this.emit("data", makeString(data)); | ||
} | ||
|
||
function NOOP() {} | ||
|
||
/** | ||
* @class MockReadableStream | ||
* @constructor | ||
* @extends YetiEventEmitter2 | ||
*/ | ||
function MockReadableStream() { | ||
EventEmitter2.call(this); | ||
} | ||
|
||
util.inherits(MockReadableStream, EventEmitter2); | ||
|
||
/** | ||
* No-op. | ||
* | ||
* @method setEncoding | ||
*/ | ||
MockReadableStream.prototype.setEncoding = NOOP; | ||
|
||
/** | ||
* No-op. | ||
* | ||
* @method resume | ||
*/ | ||
MockReadableStream.prototype.resume = NOOP; | ||
|
||
/** | ||
* Emit the `data` event with first argument | ||
* as a String. | ||
* | ||
* @method write | ||
* @param {String|Buffer} data Data. | ||
*/ | ||
MockReadableStream.prototype.write = WRITE; | ||
|
||
/** | ||
* @class MockWritableStream | ||
* @constructor | ||
* @extends YetiEventEmitter2 | ||
*/ | ||
function MockWritableStream() { | ||
EventEmitter2.call(this); | ||
} | ||
|
||
util.inherits(MockWritableStream, EventEmitter2); | ||
|
||
/** | ||
* No-op. | ||
* | ||
* @method end | ||
*/ | ||
MockWritableStream.prototype.end = NOOP; | ||
|
||
/** | ||
* Emit the `data` event with first argument | ||
* as a String. | ||
* | ||
* @method write | ||
* @param {String|Buffer} data Data. | ||
*/ | ||
MockWritableStream.prototype.write = WRITE; | ||
|
||
/** | ||
* Call the given callback when expectedString | ||
* is written to this stream. The callback recieves | ||
* a string of all data written since the expect call. | ||
* | ||
* @method expect | ||
* @param {String} expectedString Expected string. | ||
* @param {Function} cb Callback. | ||
* @param {null} cb.err Error for callback, always null. | ||
* @param {String} cb.data All data written between expectedString | ||
* appearing and calling expect. | ||
*/ | ||
MockWritableStream.prototype.expect = function (expectedString, cb) { | ||
var self = this, | ||
dataEvents = []; | ||
|
||
self.on("data", function ondata(data) { | ||
data = makeString(data); | ||
|
||
dataEvents.push(data); | ||
|
||
if (data.indexOf(expectedString) !== -1) { | ||
self.removeListener("data", ondata); | ||
cb(null, dataEvents.join("")); | ||
} | ||
}); | ||
}; | ||
|
||
exports.MockReadableStream = MockReadableStream; | ||
exports.MockWritableStream = MockWritableStream; |
This file was deleted.
Oops, something went wrong.