Skip to content

Commit

Permalink
all method, any method support chained assertion now.
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-xiao-jian committed Sep 17, 2014
1 parent ac3bcb7 commit 3e8e2d5
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 171 deletions.
123 changes: 88 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ assert.first = function(assertion) {
return stream;
};

assert.last = function(assertion) {
assert.second = function(assertion) {
var assetStorage = [];
var stream;
var error;
Expand All @@ -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) {
Expand All @@ -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;
};
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
25 changes: 19 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
```

Expand Down

0 comments on commit 3e8e2d5

Please sign in to comment.