pax_global_header00006660000000000000000000000064125561022020014505gustar00rootroot0000000000000052 comment=83b8cc82680ba6c24a25c4b4222bd4dd098bc086 stream-assert-2.0.3/000077500000000000000000000000001255610220200143015ustar00rootroot00000000000000stream-assert-2.0.3/.editorconfig000066400000000000000000000003621255610220200167570ustar00rootroot00000000000000# editorconfig.org root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [package.json] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false stream-assert-2.0.3/.gitignore000066400000000000000000000000441255610220200162670ustar00rootroot00000000000000node_modules coverage npm-debug.log stream-assert-2.0.3/.jshintrc000066400000000000000000000004531255610220200161300ustar00rootroot00000000000000{ "node": true, "esnext": true, "bitwise": true, "camelcase": true, "curly": true, "eqeqeq": true, "immed": true, "indent": 4, "newcap": true, "noarg": true, "quotmark": "single", "regexp": true, "undef": true, "unused": true, "strict": false, "trailing": true, "smarttabs": true } stream-assert-2.0.3/.npmignore000066400000000000000000000001041255610220200162730ustar00rootroot00000000000000img test .travis.yml .jshintrc .editorconfig coverage npm-debug.log stream-assert-2.0.3/.travis.yml000066400000000000000000000000741255610220200164130ustar00rootroot00000000000000language: node_js node_js: - '0.10' - '0.12' - 'iojs' stream-assert-2.0.3/assertStream.js000066400000000000000000000012071255610220200173140ustar00rootroot00000000000000var through = require('through2'); function assertStream(options, transform, flush) { options = options || {}; if (typeof options === 'function') { flush = transform; transform = options; options = {}; } options.highWatermark = options.highWatermark || 16; options.objectMode = options.objectMode || true; var stream = through(options, transform, flush); stream.on('pipe', function (source) { source.on('assertion', function (err) { stream.emit('assertion', err); }); }); stream.assertion = function (message) { this.emit('assertion', new Error(message)); }; return stream; } module.exports = assertStream; stream-assert-2.0.3/index.js000066400000000000000000000043661255610220200157570ustar00rootroot00000000000000var assertStream = require('./assertStream.js'); var assert = {}; function equal(expected) { if (typeof expected !== 'function') { return function (data) { if (data !== expected) { throw new Error(expected + ' is not equal ' + data); } }; } return expected; } var defaults = assert.defaults = { highWatermark: 16, objectMode: true }; assert.all = function (assertion) { var i = 0; return assertStream(defaults, function (obj, enc, cb) { try { assertion(obj); i++; cb(null, obj); } catch (err) { this.assertion('Element on ' + i + ' position is not passing assertion: ' + err.message); } }); }; assert.any = function (assertion) { var matched = false; return assertStream(defaults, function (obj, enc, cb) { try { assertion(obj); matched = true; } catch (err) { } cb(null, obj); }, function (cb) { if (!matched) { return this.assertion('Nothing passing assertion'); } cb(); }); }; assert.end = function (done) { var stream = assertStream(defaults, function (obj, enc, cb) { cb(); }); stream.on('finish', done || function () {}); stream.on('assertion', done || function () {}); return stream; }; assert.first = function (assertion) { return assert.nth(0, assertion); }; assert.last = function (assertion) { var lastItem; return assertStream(defaults, function (obj, enc, cb) { cb(null, lastItem = obj); }, function (cb) { try { assertion(lastItem); cb(); } catch (err) { this.assertion('Last element is not passing assertion: ' + err.message); } }); }; assert.length = function (expected) { var i = 0; return assertStream(defaults, function (obj, enc, cb) { i++; cb(null, obj); }, function (cb) { var assertion = equal(expected); try { assertion(i); cb(); } catch (err) { this.assertion('Expected length ' + err.message); } }); }; assert.nth = function (n, assertion) { var i = 0; return assertStream(defaults, function (obj, enc, cb) { if (i === n) { try { assertion(obj); i++; cb(null, obj); } catch (err) { this.assertion(n + ' position is not passing assertion: ' + err.message); } } else { i++; cb(null, obj); } }); }; assert.second = function (assertion) { return assert.nth(1, assertion); }; module.exports = assert; stream-assert-2.0.3/package.json000066400000000000000000000013161255610220200165700ustar00rootroot00000000000000{ "name": "stream-assert", "version": "2.0.3", "description": "Assertion library for streams", "main": "index.js", "scripts": { "test": "mocha -R spec" }, "repository": { "type": "git", "url": "git://github.com/floatdrop/stream-assert.git" }, "keywords": [ "Assert", "stream" ], "author": "Vsevolod Strukchinsky ", "license": "MIT", "bugs": { "url": "https://github.com/floatdrop/stream-assert/issues" }, "homepage": "https://github.com/floatdrop/stream-assert", "devDependencies": { "funsert": "^0.1.0", "into-stream": "^2.0.0", "mocha": "^1.21.4", "should": "^4.0.4" }, "dependencies": { "through2": "^0.6.1" } } stream-assert-2.0.3/readme.md000066400000000000000000000037331255610220200160660ustar00rootroot00000000000000# stream-assert [![Build Status][travis-image]][travis-url] Assert streams with ease. ## Usage ```js var intoStream = require('into-stream'); var assert = require('stream-assert'); var is = require('funsert'); intoStream([1, 2, 3]) .pipe(assert.first(is.equal(1))) .pipe(assert.second(is.equal(2))) .pipe(assert.nth(2, is.equal(3))) .pipe(assert.length(1)) .pipe(assert.end(console.log)); ``` ## Chaining Assertions are chained through passing `assertion` from pipe to pipe. If you want inject assertions in the middle of pipeline, you can attach `on('assertion')` handler to manualy catch assertions (instead of placing `assert.end`). ## API ### stream-assert Builder for asserting stream. #### nth(n, assertion) Calls `assertion` function on `nth` element in stream. #### first(assertion) > alias to nth(0, obj) #### second(assertion) > alias to nth(1, obj) #### last(assertion) Calls `assertion` function on the last element in stream. #### length(len) Asserting, that length of stream is equal `len` at the end of the stream. #### all(assertion) Checking that all elements in stream pass assertion function. #### any(assertion) Checking that at least one of elements in stream pass assertion function. #### end([cb]) Since streams has internal [buffer and highWatermark](http://nodejs.org/api/stream.html#stream_buffering), that stops data flow, when reached — test stream needs a dumping point, that will flush that buffer. `assert.end` will dump all data to `/dev/null` — so all pipes after this point will not get any data. ### assert.defaults Type: `Object` Contains defaults, that will be passed to `through` constructor. * `highWatermark` — by default, will be equal `16`. If you don't want to use `assert.end`, then you can increase it. ## License MIT (c) 2014 Vsevolod Strukchinsky [travis-url]: http://travis-ci.org/floatdrop/stream-assert [travis-image]: http://img.shields.io/travis/floatdrop/stream-assert.svg?branch=master&style=flat stream-assert-2.0.3/test/000077500000000000000000000000001255610220200152605ustar00rootroot00000000000000stream-assert-2.0.3/test/all.js000066400000000000000000000012251255610220200163660ustar00rootroot00000000000000/* global describe, it */ var intoStream = require('into-stream').obj; var assert = require('../'); var should = require('should'); var is = require('funsert'); describe('assert.all', function () { it('should check all elements in stream', function (done) { intoStream([1, 1]) .pipe(assert.all(is.equal(1))) .pipe(assert.end(done)); }); it('should emit end with error on wrong assertion', function (done) { intoStream([1, 2]) .pipe(assert.all(is.equal(1))) .pipe(assert.end(function (err) { should.exist(err); err.message.should.eql('Element on 1 position is not passing assertion: 2 is not equal 1'); done(); })); }); }); stream-assert-2.0.3/test/any.js000066400000000000000000000011561255610220200164100ustar00rootroot00000000000000/* global describe, it */ var intoStream = require('into-stream').obj; var assert = require('../'); var should = require('should'); var is = require('funsert'); describe('assert.any', function () { it('should find matching element in stream', function (done) { intoStream([1, 2]) .pipe(assert.any(is.equal(2))) .pipe(assert.end(done)); }); it('should emit end with error on wrong assertion', function (done) { intoStream([1]) .pipe(assert.any(is.equal(2))) .pipe(assert.end(function (err) { should.exist(err); err.message.should.eql('Nothing passing assertion'); done(); })); }); }); stream-assert-2.0.3/test/end.js000066400000000000000000000011371255610220200163660ustar00rootroot00000000000000/* global describe, it */ var intoStream = require('into-stream').obj; var assert = require('../'); var should = require('should'); describe('assert.end', function () { it('should help dump long streams', function (done) { var long = []; for (var i = 0; i < 100; i++) { long.push(i); } intoStream(long) .pipe(assert.end(done)); }); it('should invoke callback with error', function (done) { var long = []; for (var i = 0; i < 100; i++) { long.push(i); } intoStream(long) .pipe(assert.length(1)) .pipe(assert.end(function (err) { should.exist(err); done(); })); }); }); stream-assert-2.0.3/test/last.js000066400000000000000000000012011255610220200165530ustar00rootroot00000000000000/* global describe, it */ var intoStream = require('into-stream').obj; var assert = require('../'); var should = require('should'); var is = require('funsert'); describe('assert.last', function () { it('should check last object', function (done) { intoStream([1, 2]) .pipe(assert.last(is.equal(2))) .pipe(assert.end(done)); }); it('should emit end with error on wrong assertion', function (done) { intoStream([1]) .pipe(assert.last(is.equal(2))) .pipe(assert.end(function (err) { should.exist(err); err.message.should.eql('Last element is not passing assertion: 1 is not equal 2'); done(); })); }); }); stream-assert-2.0.3/test/length.js000066400000000000000000000013641255610220200171030ustar00rootroot00000000000000/* global describe, it */ var intoStream = require('into-stream').obj; var assert = require('../'); var should = require('should'); var is = require('funsert'); describe('assert.length', function () { it('should validate stream length', function (done) { intoStream([1]) .pipe(assert.length(1)) .pipe(assert.end(done)); }); it('should emit end with error on wrong assertion', function (done) { intoStream([1]) .pipe(assert.length(2)) .pipe(assert.end(function (err) { should.exist(err); err.message.should.eql('Expected length 2 is not equal 1'); done(); })); }); it('should accept function as assertion', function (done) { intoStream([1]) .pipe(assert.length(is.equal(1))) .pipe(assert.end(done)); }); }); stream-assert-2.0.3/test/nth.js000066400000000000000000000016371255610220200164160ustar00rootroot00000000000000/* global describe, it */ var intoStream = require('into-stream').obj; var assert = require('../'); var should = require('should'); var is = require('funsert'); describe('assert.nth', function () { it('should check nth object', function (done) { intoStream([1]) .pipe(assert.nth(0, is.equal(1))) .pipe(assert.end(done)); }); it('should emit error on wrong nth object', function (done) { intoStream([1, 2]) .pipe(assert.nth(0, is.equal(2))) .pipe(assert.end(function (err) { should.exist(err); err.message.should.eql('0 position is not passing assertion: 1 is not equal 2'); done(); })); }); it('should have first shortcut', function (done) { intoStream([1, 2, 3]) .pipe(assert.first(is.equal(1))) .pipe(assert.end(done)); }); it('should have second shortcut', function (done) { intoStream([1, 2, 3]) .pipe(assert.second(is.equal(2))) .pipe(assert.end(done)); }); }); stream-assert-2.0.3/test/piping.js000066400000000000000000000017551255610220200171140ustar00rootroot00000000000000/* global describe, it */ var intoStream = require('into-stream').obj; var assert = require('../'); var should = require('should'); var is = require('funsert'); describe('assert piping', function () { it('should fail with first assertion', function (done) { intoStream([1, 2]) .pipe(assert.length(1)) .pipe(assert.length(2)) .pipe(assert.end(function (err) { should.exist(err); err.message.should.eql('Expected length 1 is not equal 2'); done(); })); }); it('should fail with second assertion', function (done) { intoStream([1, 2]) .pipe(assert.length(2)) .pipe(assert.length(1)) .pipe(assert.end(function (err) { should.exist(err); err.message.should.eql('Expected length 1 is not equal 2'); done(); })); }); it('should support piping after all', function (done) { intoStream([1, 1, 1]) .pipe(assert.all(is.equal(1))) .pipe(assert.length(3)) .pipe(assert.end(function (err) { should.not.exist(err); done(); })); }); }); stream-assert-2.0.3/test/stream.js000066400000000000000000000013411255610220200171100ustar00rootroot00000000000000/* global describe, it */ var assert = require('../assertStream'); var should = require('should'); describe('assert.stream', function () { it('should pass assertion event to next stream in pipeline', function (done) { var s1 = assert(); var s2 = assert(); s1.pipe(s2).on('assertion', function (error) { should.exist(error); done(); }); s1.emit('assertion', new Error('Bang!')); }); it('should emit end with error only once', function (done) { var s1 = assert(function (obj, enc, cb) { if (obj === 2) { this.assertion('Bang!'); } else { cb(null, obj); } }); var s2 = assert(); s1.pipe(s2).on('assertion', function (error) { should.exist(error); done(); }); s1.end(2); }); });