improved stream mocks for older versions and increased compatabity from 0.8 up

This commit is contained in:
Spencer Alger
2013-12-12 20:07:31 -07:00
parent de25e652cf
commit dea18fcd7d
19 changed files with 352 additions and 16464 deletions

View File

@ -10,6 +10,10 @@ var sinon = require('sinon');
var util = require('util');
var Readable = require('stream').Readable;
if (!Readable) {
Readable = require('events').EventEmitter;
}
function MockIncommingMessage() {
var self = this;

View File

@ -1,22 +0,0 @@
/**
* Just a buffer really, but one that implements just a few methods similar
* to the old writable streams, but without the same internals as streams 2.0
* Writables.
*
* @type {Constuctor}
*/
module.exports = MockOldWritableStream;
var util = require('util');
function MockOldWritableStream(opts) {
var queue = [];
this.write = function (chunk) {
queue.push(chunk);
};
this.end = function () {
queue.push(null);
};
}

View File

@ -2,15 +2,76 @@
* Just a buffer really, but one that implements the Writable class
* @type {Constuctor}
*/
module.exports = MockWritableStream;
var Writable = require('stream').Writable;
var util = require('util');
var MockWritableStream; // defined simply for 0.10+, in detail for older versions
var Writable = require('stream').Writable;
function MockWritableStream(opts) {
Writable.call(this, opts);
if (Writable) {
// nice and simple for streams 2
MockWritableStream = module.exports = function (opts) {
Writable.call(this, opts);
this._write = function (chunk, encoding, cb) {};
this._write = function (chunk, encoding, cb) {};
};
util.inherits(MockWritableStream, Writable);
return;
}
util.inherits(MockWritableStream, Writable);
// Node < 0.10 did not provide a usefull stream abstract
var Stream = require('stream').Stream;
module.exports = MockWritableStream = function (opts) {
Stream.call(this);
this.writable = true;
};
util.inherits(MockWritableStream, Stream);
MockWritableStream.prototype.write = function (data) {
if (!this.writable) {
this.emit('error', new Error('stream not writable'));
return false;
}
var cb;
if (typeof(arguments[arguments.length - 1]) === 'function') {
cb = arguments[arguments.length - 1];
}
if (cb) {
process.nextTick(cb);
}
};
MockWritableStream.prototype.end = function (data, encoding, cb) {
if (typeof(data) === 'function') {
cb = data;
} else if (typeof(encoding) === 'function') {
cb = encoding;
this.write(data);
} else if (arguments.length > 0) {
this.write(data, encoding);
}
this.writable = false;
};
MockWritableStream.prototype.destroy = function (cb) {
var self = this;
if (!this.writable) {
if (cb) {
process.nextTick(function () { cb(null); });
}
return;
}
this.writable = false;
process.nextTick(function () {
if (cb) {
cb(null);
}
self.emit('close');
});
};
// There is no shutdown() for files.
MockWritableStream.prototype.destroySoon = MockWritableStream.prototype.end;