From 3e8e2d597c96e5db344fd9c6585fc886a2b18f2d Mon Sep 17 00:00:00 2001 From: bornkiller Date: Wed, 17 Sep 2014 14:20:27 +0800 Subject: [PATCH] all method, any method support chained assertion now. --- index.js | 123 ++++++++++++----- package.json | 2 +- readme.md | 25 +++- test/main.js | 378 +++++++++++++++++++++++++++++++++------------------ 4 files changed, 357 insertions(+), 171 deletions(-) diff --git a/index.js b/index.js index 46a9bbc..66eefa9 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,7 @@ assert.first = function(assertion) { return stream; }; -assert.last = function(assertion) { +assert.second = function(assertion) { var assetStorage = []; var stream; var error; @@ -86,8 +86,7 @@ assert.last = function(assertion) { callback(); } else { try { - var length = assetStorage.length; - assertion(assetStorage[length-1]); + assertion(assetStorage[1]); this.emit('end'); callback(); } catch (err) { @@ -101,35 +100,79 @@ assert.last = function(assertion) { return stream; }; -assert.all = function(assertion) { +assert.last = function(assertion) { var assetStorage = []; var stream; - var flag; var error; stream = through(function (file, encoding, callback) { - this.push(file); - assetStorage.push(file); + if (file instanceof Error) { + error = file; + } else { + this.push(file); + assetStorage.push(file); + } callback(); }, function (callback) { - flag = assetStorage.every(function(file) { + if (error instanceof Error) { + this.push(error); + this.emit('end', error); + callback(); + } else { try { - assertion(file); - return true; + var length = assetStorage.length; + assertion(assetStorage[length-1]); + this.emit('end'); + callback(); } catch (err) { - error = err; - return false; + this.push(err); + this.emit('end', err); + callback(); } - }); + } + }); - if (flag) { - this.emit('end'); - callback(); + return stream; +}; + +assert.all = function(assertion) { + var assetStorage = []; + var stream; + var flag; + var error; + stream = through(function (file, encoding, callback) { + if (file instanceof Error) { + error = file; } else { + this.push(file); + assetStorage.push(file); + } + callback(); + }, function (callback) { + if (error instanceof Error) { + this.push(error); this.emit('end', error); callback(); - } + } else { + flag = assetStorage.every(function(file) { + try { + assertion(file); + return true; + } catch (err) { + error = err; + return false; + } + }); - }); + if (flag) { + this.emit('end'); + callback(); + } else { + this.push(error); + this.emit('end', error); + callback(); + } + } + }); return stream; }; @@ -140,28 +183,38 @@ assert.any = function(assertion) { var flag; var error; stream = through(function (file, encoding, callback) { - this.push(file); - assetStorage.push(file); + if (file instanceof Error) { + error = file; + } else { + this.push(file); + assetStorage.push(file); + } callback(); }, function (callback) { - flag = assetStorage.some(function(file) { - try { - assertion(file); - return true; - } catch (err) { - error = err; - return false; - } - }); - - if (flag) { - this.emit('end'); - callback(); - } else { + if (error instanceof Error) { + this.push(error); this.emit('end', error); callback(); - } + } else { + flag = assetStorage.some(function(file) { + try { + assertion(file); + return true; + } catch (err) { + error = err; + return false; + } + }); + if (flag) { + this.emit('end'); + callback(); + } else { + this.push(error); + this.emit('end', error); + callback(); + } + } }); return stream; diff --git a/package.json b/package.json index a2b37dc..d2a3472 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stream-assert-gulp", - "version": "0.3.0", + "version": "0.4.0", "description": "simplify assertion library for gulp streams", "main": "index.js", "scripts": { diff --git a/readme.md b/readme.md index c4a96a9..e6e65af 100644 --- a/readme.md +++ b/readme.md @@ -51,6 +51,9 @@ Calls `assertion` function on nth-child element in stream. Calls `assertion` function on first-child element in stream. +#### second(assertion) + +Calls `assertion` function on first-child element in stream. #### last(assertion) Calls `assertion` function on last-child element in stream. @@ -76,14 +79,24 @@ assertion passed, the `done` callback has no argument. when any assertion failed will jump to the end, and `done` callback has error(this assertion throwed) as first argument. -Till now, `length`, `nth`, `first`, `last` support chained asssertion, `all` and `any` -doesn't, just be careful with this. +Till now, `length`, `nth`, `first`, `last` support chained asssertion, also, `all` and `any` +works for chained assertion since v0.4.0. ```js -gulp.src('./test/fixtures/template.js') - .pipe(assert.first(function(data) { data.should.eql(1); })) - .pipe(assert.last(function(data) { data.should.eql(2); })) - .pipe(assert.nth(2, function(data) { data.should.eql(3); })) +gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.length(1)) + .pipe(assert.first(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .pipe(assert.nth(2, function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .pipe(assert.any(function(file) { + (file.isBuffer()).should.be.true; + })) + .pipe(assert.all(function(file) { + (file.isStream()).should.be.false; + })) .on('end', done); ``` diff --git a/test/main.js b/test/main.js index 1f1a2c8..e1af48b 100644 --- a/test/main.js +++ b/test/main.js @@ -5,196 +5,310 @@ require('mocha'); require('should'); describe('assert nth-child', function () { - it('should assert any child', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/love.js', './test/fixtures/destiny.js']) - .pipe(assert.nth(2, function(file) { - (file.contents.toString()).should.equal("define('love',[],function(){});"); - })) - .on('end', function(err) { - (typeof err === 'undefined').should.be.true; - done(); - }); - }); + it('should assert any child', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/love.js', './test/fixtures/destiny.js']) + .pipe(assert.nth(2, function(file) { + (file.contents.toString()).should.equal("define('love',[],function(){});"); + })) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); - it('should assert first child', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/love.js', './test/fixtures/destiny.js']) - .pipe(assert.first(function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); - })) - .on('end', function(err) { - (typeof err === 'undefined').should.be.true; - done(); - }); - }); + it('should assert first child', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/love.js', './test/fixtures/destiny.js']) + .pipe(assert.first(function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); - it('should assert last child', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/love.js', './test/fixtures/destiny.js']) - .pipe(assert.last(function(file) { - (file.contents.toString()).should.equal("define('destiny',[],function(){});"); - })) - .on('end', function(err) { - (typeof err === 'undefined').should.be.true; - done(); - }); - }); + it('should assert second child', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/love.js', './test/fixtures/destiny.js']) + .pipe(assert.second(function(file) { + (file.contents.toString()).should.equal("define('love',[],function(){});"); + })) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); + + it('should assert last child', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/love.js', './test/fixtures/destiny.js']) + .pipe(assert.last(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); }); describe('assert length', function () { - it('should assert source length when match', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.length(2)) - .on('end', function(err) { - (typeof err === 'undefined').should.be.true; - done(); - }) - }); + it('should assert source length when match', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.length(2)) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }) + }); - it('should assert source length when mismatch', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.length(1)) - .on('end', function(err) { - err.should.be.a.Error; - done(); - }) - }); + it('should assert source length when mismatch', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.length(1)) + .on('end', function(err) { + err.should.be.a.Error; + done(); + }) + }); }); describe('assert array', function () { - it('should assert all source', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.all(function(file) { - (file.contents.toString()).should.equal("define('destiny',[],function(){});"); - })) - .on('end', function(err) { - err.should.be.a.Error; - done(); - }) - }); + it('should assert all source', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.all(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .on('end', function(err) { + err.should.be.a.Error; + done(); + }) + }); - it('should assert any source', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.any(function(file) { - (file.contents.toString()).should.equal("define('destiny',[],function(){});"); - })) - .on('end', function(err) { - (typeof err === 'undefined').should.be.true; - done(); - }); - }); + it('should assert any source', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.any(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); }); describe('assert chain', function () { - it('should support assert chain when none error', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.first(function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); - })) - .pipe(assert.length(2)) - .on('end', function(err) { - (typeof err === 'undefined').should.be.true; - done(); - }); - }); + it('should transfer nothing in length chain when none error', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.length(2)) + .pipe(assert.length(2)) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); - it('should support assert chain when single error on length', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.length(2)) - .pipe(assert.length(3)) - .on('end', function(err) { - err.should.be.a.Error; - (err.message).should.equal('expected 2 to be 3'); - done(); - }) - }); + it('should transfer error in length assert chain when error pass', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.length(3)) + .pipe(assert.length(2)) + .pipe(assert.length(1)) + .on('end', function(err) { + err.should.be.a.Error; + (err.message).should.equal('expected 2 to be 3'); + done(); + }) + }); - it('should support assert chain when multi error on length', function (done) { - gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.length(1)) - .pipe(assert.length(3)) - .on('end', function(err) { - (err.message).should.equal('expected 2 to be 1'); - done(); - }) - }); + it('should transfer error in length assert chain when error not pass', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.length(2)) + .pipe(assert.length(1)) + .on('end', function(err) { + (err.message).should.equal('expected 2 to be 1'); + done(); + }) + }); - it('should support assert chain when single error on file', function (done) { + it('should transfer nothing in sequence assert chain when none error', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js', './test/fixtures/love.js']) + .pipe(assert.first(function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .pipe(assert.second(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .pipe(assert.last(function(file) { + (file.contents.toString()).should.equal("define('love',[],function(){});"); + })) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); + + it('should transfer error in sequence assert chain when error pass', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.first(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .pipe(assert.last(function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .pipe(assert.last(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .on('end', function(err) { + err.should.be.a.Error; + (err.actual).should.equal("define('template',[],function(){});"); + (err.expected).should.equal("define('destiny',[],function(){});"); + done(); + }); + }); + + it('should transfer error in sequence assert chain when error not pass', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js', './test/fixtures/love.js']) + .pipe(assert.first(function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .pipe(assert.second(function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .pipe(assert.last(function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .on('end', function(err) { + err.should.be.a.Error; + (err.actual).should.equal("define('love',[],function(){});"); + (err.expected).should.equal("define('template',[],function(){});"); + done(); + }); + }); + + it('should transfer nothing in nth assert chain when none error', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) + .pipe(assert.nth(1, function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .pipe(assert.nth(2, function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .on('end', function(err) { + (typeof err === 'undefined').should.be.true; + done(); + }); + }); + + it('should transfer error in nth assert chain when error pass', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js', './test/fixtures/love.js']) + .pipe(assert.nth(1, function(file) { + (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + })) + .pipe(assert.nth(2, function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .pipe(assert.nth(3, function(file) { + (file.contents.toString()).should.equal("define('love',[],function(){});"); + })) + .on('end', function(err) { + err.should.be.a.Error; + (err.actual).should.equal("define('template',[],function(){});"); + (err.expected).should.equal("define('destiny',[],function(){});"); + done(); + }); + }); + + it('should transfer error in nth assert chain when error not pass', function (done) { + gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js', './test/fixtures/love.js']) + .pipe(assert.nth(1, function(file) { + (file.contents.toString()).should.equal("define('template',[],function(){});"); + })) + .pipe(assert.nth(2, function(file) { + (file.contents.toString()).should.equal("define('love',[],function(){});"); + })) + .on('end', function(err) { + err.should.be.a.Error; + (err.actual).should.equal("define('destiny',[],function(){});"); + (err.expected).should.equal("define('love',[],function(){});"); + done(); + }); + }); + + it('should transfer nothing in array assert chain when none error about both', function (done) { gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.first(function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.all(function(file) { + (file.isBuffer()).should.be.true; })) - .pipe(assert.last(function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.any(function(file) { + (file.isStream()).should.be.false; })) .on('end', function(err) { - err.should.be.a.Error; - (err.actual).should.equal("define('destiny',[],function(){});"); + (typeof err === 'undefined').should.be.true; done(); }); }); - it('should support assert chain when multi error on file', function (done) { + it('should transfer error in array assert chain when error pass about all', function (done) { gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.first(function(file) { - (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + .pipe(assert.all(function(file) { + (file.isStream()).should.be.true; })) - .pipe(assert.last(function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.all(function(file) { + (file.isStream()).should.be.true; + })) + .pipe(assert.all(function(file) { + (file.isBuffer()).should.be.true; })) .on('end', function(err) { err.should.be.a.Error; - (err.actual).should.equal("define('template',[],function(){});"); done(); }); }); - it('should support assert chain when single error on nth-child', function (done) { + it('should transfer error in array assert chain when error not pass about all', function (done) { gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.nth(1, function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.all(function(file) { + (file.isStream()).should.be.false; })) - .pipe(assert.nth(2, function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.all(function(file) { + (file.isBuffer()).should.be.false; })) .on('end', function(err) { err.should.be.a.Error; - (err.actual).should.equal("define('destiny',[],function(){});"); done(); }); }); - it('should support assert chain when multi error on nth-child', function (done) { + it('should transfer error in array assert chain when error pass about any', function (done) { gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.nth(1, function(file) { - (file.contents.toString()).should.equal("define('destiny',[],function(){});"); + .pipe(assert.any(function(file) { + (file.isStream()).should.be.true; })) - .pipe(assert.nth(2, function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.any(function(file) { + (file.isStream()).should.be.true; + })) + .pipe(assert.any(function(file) { + (file.isBuffer()).should.be.true; })) .on('end', function(err) { err.should.be.a.Error; - (err.actual).should.equal("define('template',[],function(){});"); done(); }); }); - it('should support assert chain when single error on mixed assertion', function (done) { + it('should transfer error in array assert chain when error pass about any', function (done) { gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) - .pipe(assert.length(2)) - .pipe(assert.first(function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.any(function(file) { + (file.isStream()).should.be.false; })) - .pipe(assert.nth(2, function(file) { - (file.contents.toString()).should.equal("define('template',[],function(){});"); + .pipe(assert.any(function(file) { + (file.isBuffer()).should.be.false; })) .on('end', function(err) { err.should.be.a.Error; - (err.actual).should.equal("define('destiny',[],function(){});"); done(); }); }); - it('should support assert chain when multi error on mixed', function (done) { + it('should transfer nothing in mixed assert chain when none error', function (done) { gulp.src(['./test/fixtures/template.js', './test/fixtures/destiny.js']) .pipe(assert.length(1)) .pipe(assert.first(function(file) { @@ -203,6 +317,12 @@ describe('assert chain', function () { .pipe(assert.nth(2, function(file) { (file.contents.toString()).should.equal("define('template',[],function(){});"); })) + .pipe(assert.any(function(file) { + (file.isBuffer()).should.be.true; + })) + .pipe(assert.all(function(file) { + (file.isStream()).should.be.false; + })) .on('end', function(err) { err.should.be.a.Error; (err.actual).should.equal(2);