这是indexloc提供的服务,不要输入任何密码
Skip to content

Added the notion of pessimistic usage. #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: pass fail test
.PHONY: pass fail mix test

TAP = ./node_modules/tape/bin/tape
MIN = ./bin/tap-difflet
Expand All @@ -9,4 +9,10 @@ pass:
fail:
@$(TAP) test/fail.js | $(MIN)

mix:
@$(TAP) test/mix.js | $(MIN)

mixp:
@$(TAP) test/mix.js | $(MIN) -p

test: pass fail
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ npm install tap-difflet --save-dev
tape test/*.js | tap-difflet
~~~

For pessimistic usage specify -p or --pessimist - this will only output the errors and the summary.

~~~ text
tape test/*.js | tap-difflet -p
~~~

## Output

![tap-difflet](http://i.imgur.com/8uFAvXU.png)
Expand Down
118 changes: 93 additions & 25 deletions bin/tap-difflet
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,74 @@ var tap = parser();
var out = through();
var dup = duplexer(tap, out);


/* Pessimism */

function userSpecifiedPessimistic (args) {
for (var ai=2; ai < args.length; ++ai) {
if (args[ai] === '-p' || args[ai] === '--pessimist') {
return true;
}
}

return false;
};


if (userSpecifiedPessimistic(process.argv)) {
var anAssertFailed = true;
var originalOut = out;
var buffer = null;
tap.on('comment', function(comment) {
if (buffer && anAssertFailed) {
for (var bi=0; bi<buffer.length; ++bi) {
originalOut.push(buffer[bi]);
}
}

buffer = null;
anAssertFailed = false;
out = originalOut;

if (getCommentType(comment) === CommentType.OTHER) {
buffer = [];
out = buffer;
}
});

tap.on('assert', function(res) {
if (!res.ok) {
anAssertFailed = true;
}
});
}

/* Helpers */
var CommentType = {
OTHER: 1,
TESTS: 2,
PASS: 3,
FAIL: 4,
OK: 5
};

function getCommentType(comment) {
if (/^tests\s+[0-9]+$/gi.test(comment)) {
return CommentType.TESTS;
}
else if (/^pass\s+[0-9]+$/gi.test(comment)) {
return CommentType.PASS;
}
else if (/^fail\s+[0-9]+$/gi.test(comment)) {
return CommentType.FAIL;
}
else if (/^ok$/gi.test(comment)) {
return CommentType.OK;
}
else {
return CommentType.OTHER;
}
};

function output(str) {
out.push(str);
Expand All @@ -31,25 +98,26 @@ var timer = hirestime();
var errors = [];

tap.on('comment', function(comment) {
if (/^tests\s+[0-9]+$/gi.test(comment)) {
output('\n');
comment = chalk.white.bold(comment);
}
else if (/^pass\s+[0-9]+$/gi.test(comment)) {
comment = chalk.green.bold(comment);
}
else if (/^fail\s+[0-9]+$/gi.test(comment)) {
comment = chalk.red.bold(comment);
}
else if (/^ok$/gi.test(comment)) {
return;
}
else {
output('\n');
comment = chalk.white.bold(comment);
}
var commentType = getCommentType(comment);
if (commentType === CommentType.TESTS) {
output('\n');
comment = chalk.white.bold(comment);
}
else if (commentType === CommentType.PASS) {
comment = chalk.green.bold(comment);
}
else if (commentType === CommentType.FAIL) {
comment = chalk.red.bold(comment);
}
else if (commentType === CommentType.OK) {
return;
}
else {
output('\n');
comment = chalk.white.bold(comment);
}

output(' ' + comment + '\n');
output(' ' + comment + '\n');
});

tap.on('assert', function(res) {
Expand Down Expand Up @@ -90,10 +158,10 @@ tap.on('results', function(res) {

tap.on('diag', function (diag) {
var expected, actual, at,
gotExpected = true,
gotActual = true,
gotAt = true,
str = '';
gotExpected = true,
gotActual = true,
gotAt = true,
str = '';

if (diag.hasOwnProperty('expected')) {
expected = diag.expected;
Expand All @@ -120,15 +188,15 @@ tap.on('diag', function (diag) {

if (gotActual && gotExpected) {
if (typeof expected !== typeof actual ||
typeof expected === "object" && (!actual || !expected)) {
typeof expected === 'object' && (!actual || !expected)) {
str = 'Expected ' + typeof expected + ' but got ' + typeof actual;
} else if (typeof expected === "string") {
} else if (typeof expected === 'string') {
if (str.indexOf('\n') >= 0) {
str = ansidiff.lines(expected, actual);
} else {
str = ansidiff.chars(expected, actual);
}
} else if (typeof expected === "object") {
} else if (typeof expected === 'object') {
str = objdiff(expected, actual);
} else {
str = chalk.white('Expected ') + chalk.bold(''+expected) + chalk.white(' but got ') + chalk.bold(''+actual);
Expand Down
2 changes: 2 additions & 0 deletions test/mix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require ('./pass');
require ('./fail');