pax_global_header00006660000000000000000000000064126732735770014535gustar00rootroot0000000000000052 comment=e147252dcabc632a5c778d8b55806f7bd7d5f436 streamtest-1.2.2/000077500000000000000000000000001267327357700137325ustar00rootroot00000000000000streamtest-1.2.2/.gitignore000066400000000000000000000000531267327357700157200ustar00rootroot00000000000000*~ *.swp node_modules/ .git .coveralls.yml streamtest-1.2.2/.npmignore000066400000000000000000000000531267327357700157270ustar00rootroot00000000000000*~ *.swp node_modules/ .git .coveralls.yml streamtest-1.2.2/.travis.yml000066400000000000000000000001751267327357700160460ustar00rootroot00000000000000language: node_js node_js: - "0.10" - "0.12" - "4" - "5" before_script: - npm install --dev script: - npm test streamtest-1.2.2/LICENSE000077500000000000000000000021251267327357700147420ustar00rootroot00000000000000The MIT License Copyright (c) 2013-2015 Nicolas Froidure, Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. streamtest-1.2.2/README.md000066400000000000000000000100331267327357700152060ustar00rootroot00000000000000# streamtest `streamtest` is a set of utils to test your stream based modules accross various stream implementations of NodeJS. In fact, this [repository's tests](https://github.com/nfroidure/streamtest/blob/master/tests/index.mocha.js) is a perfect example of its own usage ;). [![NPM version](https://badge.fury.io/js/streamtest.png)](https://npmjs.org/package/streamtest) [![Build status](https://secure.travis-ci.org/nfroidure/streamtest.png)](https://travis-ci.org/nfroidure/streamtest) [![Dependency Status](https://david-dm.org/nfroidure/streamtest.png)](https://david-dm.org/nfroidure/streamtest) [![devDependency Status](https://david-dm.org/nfroidure/streamtest/dev-status.png)](https://david-dm.org/nfroidure/streamtest#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/nfroidure/streamtest/badge.png?branch=master)](https://coveralls.io/r/nfroidure/streamtest?branch=master) [![Code Climate](https://codeclimate.com/github/nfroidure/streamtest.png)](https://codeclimate.com/github/nfroidure/streamtest) ## Installation First install `streamtest` in your project: ```sh npm install --save-dev streamtest ``` ## Getting started Then, use it: ```js var StreamTest = require('streamtest'); describe('My Stream Lib', function() { // Iterating through versions StreamTest.versions.forEach(function(version) { describe('for ' + version + ' streams', function() { // here goes your code it('should work', function(done) { StreamTest[version].fromChunks(['a ', 'chunk', 'and', 'another']) .pipe(myTestedStream) .pipe(StreamTest[version].toText(function(err, text) { if(err) { done(err); } assert.equal(text, 'the text as i should be'); done(); })); }); }); }); }); ``` ## API ### StreamTest.versions:Array List of supported versions (currently v1 and v2). ### StreamTest[version]:Object Object available for each version containing the following methods. ### StreamTest[version].fromChunks(chunks:Array, timeout:Number) Create a readable stream streaming `chunks` each `timeout` milliseconds and then end. Usefull for testing buffer based streams. ### StreamTest[version].fromObjects(objects:Array, timeout:Number) Create a readable stream streaming `objects` each `timeout` milliseconds and then end. Usefull for testing objectMode based streams. ### StreamTest[version].fromErroredChunks(err:Error, chunks:Array, timeout:Number) Create a readable stream streaming `chunks` each `timeout` milliseconds, emit the `err` error and then end. Usefull for testing buffer based streams. ### StreamTest[version].fromErroredObjects(err:Error, objects:Array, timeout:Number) Create a readable stream streaming `objects` each `timeout` milliseconds, emit the `err` error and then end. Usefull for testing objectMode based streams. ### StreamTest[version].toChunks(cb:Function) Create a writable stream collecting written `chunks` and calling the `cb` function when it finishes. The `cb` function take in an error and an Array of chunks. ### StreamTest[version].toObjects(cb:Function) Create a writable stream collecting written `chunks` and calling the `cb` function when it finishes. The `cb` function take in an error and an Array of objects. ### StreamTest[version].toText(cb:Function) Create a writable stream collecting written `chunks` and calling the `cb` function when it finishes with the whole content converted to text. The `cb` function take in an error and a string. ## Contribute Feel free to submit us your improvements. To do so, you must accept to publish your code under the MIT license. To start contributing, first run the following to setup the development environment: ```sh git clone git@github.com:nfroidure/streamtest.git cd streamtest npm install ``` Then, run the tests: ```sh npm test ``` ## Stats [![NPM](https://nodei.co/npm/streamtest.png?downloads=true&stars=true)](https://nodei.co/npm/streamtest/) [![NPM](https://nodei.co/npm-dl/streamtest.png)](https://nodei.co/npm/streamtest/) streamtest-1.2.2/package.json000066400000000000000000000020171267327357700162200ustar00rootroot00000000000000{ "name": "streamtest", "version": "1.2.2", "description": "Streams test library", "main": "src/index.js", "scripts": { "test": "mocha tests/*.mocha.js", "coveralls": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- tests/*.mocha.js -R spec -t 5000 && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", "cover": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report html -- tests/*.mocha.js -R spec -t 5000" }, "repository": { "type": "git", "url": "git@github.com:nfroidure/streamtest.git" }, "keywords": [ "test", "streams", "stream1", "stream2" ], "author": "Nicolas Froidure", "license": "MIT", "bugs": { "url": "https://github.com/nfroidure/streamtest/issues" }, "homepage": "https://github.com/nfroidure/streamtest", "devDependencies": { "coveralls": "^2.11.8", "istanbul": "^0.4.2", "mocha": "^2.4.5" }, "dependencies": { "readable-stream": "^2.0.5" } } streamtest-1.2.2/src/000077500000000000000000000000001267327357700145215ustar00rootroot00000000000000streamtest-1.2.2/src/index.js000066400000000000000000000163221267327357700161720ustar00rootroot00000000000000var Stream = require('stream'); var StreamTest = { versions: ['v1', 'v2'], // Node lt 0.10 streams v1: { readable: function v1Readable(options) { var stream = new Stream(options); stream.readable = true; return stream; }, fromObjects: function v1FromObjects(objects, timeout) { var stream = StreamTest.v1.readable(); StreamTest.v1.__emitToStream(stream, objects || [], timeout); return stream; }, fromErroredObjects: function v1FromErroredObjects(err, objects, timeout) { var stream = StreamTest.v1.readable(); StreamTest.v1.__emitToStream(stream, objects || [], timeout, function() { stream.emit('error', err); }); return stream; }, fromChunks: function v1FromChunks() { return StreamTest.v1.fromObjects.apply(this, arguments); }, fromErroredChunks: function v1FromErroredChunks() { return StreamTest.v1.fromErroredObjects.apply(this, arguments); }, __emitToStream: function v1EmitToStream(stream, chunks, timeout, endcb) { setTimeout(function() { if(!chunks.length) { setTimeout(stream.emit.bind(stream, 'end'), timeout || 0); if(endcb) { endcb(); } } else { stream.emit('data', chunks.shift()); StreamTest.v1.__emitToStream(stream, chunks, timeout, endcb); } }, timeout || 0); }, writable: function v1Writable(options) { var stream = new Stream(options); stream.writable = true; return stream; }, toObjects: function v1ToObjects(cb) { var objs = []; var stream = StreamTest.v1.writable(); stream.write = function(obj) { objs.push(obj); }; stream.end = function() { cb(null, objs); }; stream.on('error', function(err) { cb(err); }); return stream; }, toChunks: function v1ToChunks(cb) { var chunks = []; var stream = StreamTest.v1.writable(); stream.write = function(chunk, encoding) { chunks.push(Buffer(chunk)); }; stream.end = function() { cb(null, chunks); }; stream.on('error', function(err) { cb(err); }); return stream; }, toText: function v1ToText(cb) { return StreamTest.v1.toChunks(function(err, chunks) { if(err) { return cb(err); } cb(null, Buffer.concat(chunks).toString()); }); }, syncReadableChunks: function v1SyncReadableChunks(chunks) { return StreamTest.v1.readable(); }, syncReadableObjects: function v1SyncReadableObjects(chunks) { return StreamTest.v1.readable(); }, syncWrite: function syncWrite(stream, chunks) { chunks = chunks || []; if(!chunks.length) { stream.emit('end'); } else { stream.emit('data', chunks.shift()); StreamTest.v1.syncWrite(stream, chunks); } }, syncError: function v1SyncError(stream, err, chunks) { chunks = chunks || []; if(!chunks.length) { stream.emit('error', err); stream.emit('end'); } else { stream.emit('data', chunks.shift()); StreamTest.v1.syncWrite(stream, err, chunks); } } }, // Node gte 0.10 streams v2: { readable: function v2Readable(options) { var stream = new Stream.Readable(options); return stream; }, fromObjects: function v2FromObjects(objects, timeout) { var stream = StreamTest.v2.readable({objectMode: true}); objects = objects || []; stream._read = function() { var object = null; if(objects.length) { object = objects.shift(); } setTimeout(function() { stream.push(object); }, timeout || 0); }; return stream; }, fromErroredObjects: function v2FromErroredObjects(err, objects, timeout) { var stream = StreamTest.v2.readable({objectMode: true}); objects = objects || []; stream._read = function() { var object = null; if(objects.length) { object = objects.shift(); } else { setTimeout(function() { stream.emit('error', err); }, timeout || 0); } setTimeout(function() { stream.push(object); }, timeout || 0); }; return stream; }, fromChunks: function v2FromChunks(chunks, timeout) { var stream = StreamTest.v2.readable(); chunks = chunks || []; stream._read = function() { var chunk = null; if(chunks.length) { chunk = chunks.shift(); } setTimeout(function() { stream.push(chunk); }, timeout || 0); }; return stream; }, fromErroredChunks: function v2FromErroredChunks(err, chunks, timeout) { var stream = StreamTest.v2.readable(); chunks = chunks || []; stream._read = function() { var chunk = null; if(chunks.length) { chunk = chunks.shift(); } else { setTimeout(function() { stream.emit('error', err); }, timeout || 0); } setTimeout(function() { stream.push(chunk); }, timeout || 0); }; return stream; }, writable: function v2Writable(options) { var stream = new Stream.Writable(options); return stream; }, toObjects: function v2ToObjects(cb) { var stream = new StreamTest.v2.writable({objectMode: true}); var objs = []; stream._write = function (obj, unused, done) { objs.push(obj); done(); }; stream.on('finish', function() { cb(null, objs); }); stream.on('error', function(err) { cb(err); }); return stream; }, toChunks: function v2ToChunks(cb) { var stream = new StreamTest.v2.writable(); var chunks = []; stream._write = function (chunk, encoding, done) { if(encoding && 'buffer' != encoding) { chunk = Buffer(chunk.toString(encoding)); } chunks.push(chunk); done(); }; stream.on('finish', function() { cb(null, chunks); }); stream.on('error', function(err) { cb(err); }); return stream; }, toText: function v2ToText(cb) { return StreamTest.v2.toChunks(function(err, chunks) { if(err) { return cb(err); } cb(null, Buffer.concat(chunks).toString()); }); }, syncReadableChunks: function v2SyncReadableChunks(chunks) { return new Stream.PassThrough(); }, syncReadableObjects: function v2SyncReadableObjects(chunks) { return new Stream.PassThrough({objectMode: true}); }, syncWrite: function v2SyncWrite(stream, chunks) { chunks = chunks || []; if(!chunks.length) { stream.end(); } else { stream.write(chunks.shift()); StreamTest.v2.syncWrite(stream, chunks); } }, syncError: function v2SyncError(stream, err, chunks) { chunks = chunks || []; if(!chunks.length) { stream.emit('error', err); stream.end(); } else { stream.write(chunks.shift()); StreamTest.v2.syncError(stream, err, chunks); } } } }; module.exports = StreamTest; streamtest-1.2.2/tests/000077500000000000000000000000001267327357700150745ustar00rootroot00000000000000streamtest-1.2.2/tests/index.mocha.js000066400000000000000000000067711267327357700176420ustar00rootroot00000000000000var assert = require('assert'); var StreamTest = require('../src'); describe('StreamTest', function() { describe('.versions', function() { it('should contain versions', function() { assert.equal(StreamTest.versions.join(','), 'v1,v2'); }); }); StreamTest.versions.forEach(function(version) { describe('for ' + version + ' streams', function() { it('should work with buffers', function(done) { var expectedBuffers = [Buffer('test'), Buffer('test2')]; var inputStream = StreamTest[version].fromChunks(expectedBuffers.slice(0)); var outputStream = StreamTest[version].toChunks(function(err, buffers) { if(err) { return done(err); } assert.deepEqual(buffers, expectedBuffers); done(); }); inputStream.pipe(outputStream); }); it('should report errors with buffers', function(done) { var expectedBuffers = [Buffer('test'), Buffer('test2')]; var inputStream = StreamTest[version].fromErroredChunks(new Error('Ooops'), expectedBuffers.slice(0)); var outputStream = StreamTest[version].toChunks(function(err, buffers) { assert(err); assert(!buffers); done(); }); inputStream.on('error', function(err) { outputStream.emit('error', err); }); inputStream.pipe(outputStream); }); it('should work when wanting whole text', function(done) { var expectedBuffers = ['test', 'test2']; var inputStream = StreamTest[version].fromObjects(expectedBuffers.slice(0)); var outputStream = StreamTest[version].toText(function(err, buffers) { if(err) { return done(err); } assert.deepEqual(buffers, expectedBuffers.join('')); done(); }); inputStream.pipe(outputStream); }); it('should report errors when wanting whole text', function(done) { var expectedBuffers = [Buffer('test'), Buffer('test2')]; var inputStream = StreamTest[version].fromErroredChunks(new Error('Ooops'), expectedBuffers.slice(0)); inputStream.on('error', function(err) { outputStream.emit('error', err); }); var outputStream = StreamTest[version].toText(function(err, buffers) { assert(err); assert(!buffers); done(); }); inputStream.pipe(outputStream); }); it('should work with objects', function(done) { var expectedObjs = [{ test: 'test' }, { test: 'test2' }]; var inputStream = StreamTest[version].fromObjects(expectedObjs.slice(0)); var outputStream = StreamTest[version].toObjects(function(err, objs) { if(err) { return done(err); } assert.deepEqual(objs, expectedObjs); done(); }); inputStream.pipe(outputStream); }); it('should report errors with objects', function(done) { var expectedObjs = [{ test: 'test' }, { test: 'test2' }]; var inputStream = StreamTest[version].fromErroredObjects(new Error('Ooops'), expectedObjs.slice(0)); inputStream.on('error', function(err) { outputStream.emit('error', err); }); var outputStream = StreamTest[version].toObjects(function(err, objs) { assert(err); assert(!objs); done(); }); inputStream.pipe(outputStream); }); }); }); });