Skip to content

Commit

Permalink
Fix summary output. For #67
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Mar 31, 2014
1 parent 7a23c2b commit 9649720
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 64 deletions.
3 changes: 2 additions & 1 deletion lib/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ exports.execute = function (skipTraverse) {

var reporter = new Reporters[argv.r]({ threshold: argv.t, silence: argv.s });
reporter.print = Output.print;
reporter.format = Output.format;
reporter.console = Output.console;
reporter.colors = Output.colors;

var report = {
tests: [],
Expand Down
47 changes: 25 additions & 22 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,42 @@ internals.colors = {
'red': 31,
'green': 32,
'magenta': 35,
'red-bg': 41,
'green-bg': 42
'redBg': 41,
'greenBg': 42
};


exports.print = function (/* format, arg1, arg2, ..., arg3 */) {
exports.print = function (text) {

var formatted = exports.format.apply(this, arguments);
process.stdout.write(formatted);
process.stdout.write(text);
};


exports.format = function (format /*, arg1, arg2, ..., arg3 */) {
exports.console = function (text) {

var args = Array.prototype.slice.call(arguments, 1);
var i = 0;
var formatted = format.replace(/%s/g, function ($0) {
process.stdout.write(text);
};

if (i >= args.length) {
return $0;
}

return args[i++];
});
internals.color = function (name) {

var colorized = formatted.replace(/#([a-z\-]+)\[([^\]]*)\]/g, function ($0, $1, $2) {
if (Tty.isatty(1) && Tty.isatty(2)) {
var color = '\u001b[' + internals.colors[name] + 'm';
return function (text) { return color + text + '\u001b[0m'; };
}

if (Tty.isatty(1) && Tty.isatty(2)) {
return '\u001b[' + internals.colors[$1] + 'm' + $2 + '\u001b[0m';
}
return function (text) { return text; };
};

return $2;
});

return colorized;
};
exports.colors = function () {

var colors = {};
var names = Object.keys(internals.colors);
for (var i = 0, il = names.length; i < il; ++i) {
var name = names[i];
colors[name] = internals.color(name);
}

return colors;
}();
6 changes: 3 additions & 3 deletions lib/reporters/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ internals.Reporter.prototype.start = function (notebook) {
internals.Reporter.prototype.test = function (test) {

if (!this.count) {
this.print('\n ');
this.console('\n ');
}

this.count++;

if ((this.count - 1) % internals.width === 0) {
this.print('\n ');
this.console('\n ');
}

this.print(test.err ? '#red[x]' : '.');
this.console(test.err ? this.colors.red('x') : '.');
};


Expand Down
8 changes: 6 additions & 2 deletions lib/reporters/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ internals.Reporter.prototype.test = function (test) {
this.last = test.path;

var spacer = internals.spacer(test.path.length * 2);
var format = spacer + Utils.color(test.err ? '#red[\u2716 %s) %s]' : '#green[\u2714] #gray[%s) %s]\n');
this.print(format, test.id, test.relativeTitle);
if (test.err) {
this.print(spacer + this.colors.red('\u2716' + test.id + ') ' + test.relativeTitle) + '\n');
}
else {
this.print(spacer + this.colors.green('\u2714') + ' ' + this.colors.gray(test.id + ') ' + test.relativeTitle) + '\n');
}
};


Expand Down
67 changes: 40 additions & 27 deletions lib/reporters/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ internals.Reporter.prototype.test = function (test) {

internals.Reporter.prototype.end = function (notebook) {

var self = this;
// Colors

var red = this.colors.red;
var redBg = this.colors.redBg;
var green = this.colors.green;
var greenBg = this.colors.greenBg;
var magenta = this.colors.magenta;
var gray = this.colors.gray;

// Tests

Expand All @@ -37,68 +44,69 @@ internals.Reporter.prototype.end = function (notebook) {

var output = '\n\n ';
if (!failures.length) {
output += '#green[' + notebook.tests.length + ' tests complete] #gray[(' + notebook.ms + ' ms)]\n\n';
output += green(notebook.tests.length + ' tests complete') + ' (' + notebook.ms + ' ms)\n\n';
}
else {
output += '#red[' + failures.length + ' of ' + notebook.tests.length + ' tests failed]#gray[:]\n\n';
output += red(failures.length + ' of ' + notebook.tests.length + ' tests failed:') + '\n\n';

for (var i = 0, il = failures.length; i < il; ++i) {
var test = failures[i];
var message = test.err.message || '';
var stack = test.err.stack || message;
var index = stack.indexOf(message) + message.length;
var escape = true;

// Actual vs Expected

if (test.err.showDiff) {
escape = false;
test.err.actual = JSON.stringify(test.err.actual, null, 2);
test.err.expected = JSON.stringify(test.err.expected, null, 2);
}

if (typeof test.err.actual === 'string' &&
typeof test.err.expected === 'string') {

var len = Math.max(test.err.actual.length, test.err.expected.length);
var type = (len < 20 ? 'diffChars' : 'diffWords');
var comparison = Diff[type](test.err.actual, test.err.expected);

output += ' #black[' + test.id + ') ' + test.title + ':]\n\n' +
' #red-bg[actual] #green-bg[expected]\n\n';
output += ' ' + test.id + ') ' + test.title + ':\n\n' +
' ' + redBg('actual') + ' ' + greenBg('expected') + '\n\n ';

var comparison = Diff.diffWords(test.err.actual, test.err.expected);
for (var c = 0, cl = comparison.length; c < cl; ++c) {
var item = comparison[c];
var value = item.value;

if (escape) {
if (!test.err.showDiff) {
value = value.replace(/\t/g, '\\t').replace(/\r/g, '\\r').replace(/\n/g, '\\n\n');
}

var lines = value.split('\n');
for (var l = 0, ll = lines.length; l < ll; ++l) {
if (l) {
output += '\n ';
}

output += ' ';
if (item.added || item.removed) {
output += (item.added ? '#green-bg' : '#red-bg') + '[' + lines[l] + ']';
output += item.added ? greenBg(lines[l]) : redBg(lines[l]);
}
else {
output += lines[l];
}

output += '\n';
}
}

output += '\n\n';
}
else {
output += ' ' + test.id + ') ' + test.title + ':\n\n' +
' ' + red(stack.slice(0, index)) + '\n';

var boomError = test.err.actual && test.err.actual.isBoom && test.err.actual.message;
var msg = stack.slice(0, index);
output += this.format(' #black[%s) %s:]\n #red[%s]\n' + (boomError ? ' #magenta[Message: ' + boomError + ']\n' : ''), test.id, test.title, msg);
if (boomError) {
output += ' ' + magenta('Message: ' + boomError) + '\n';
}

output += '\n';
}

if (test.timeout) {
if (!test.timeout) {
var isChai = stack.indexOf('chai') !== -1;
stack = stack.slice(index ? index + 1 : index).replace(/^/gm, ' ');
if (isChai) {
Expand All @@ -108,7 +116,7 @@ internals.Reporter.prototype.end = function (notebook) {
}
}

output += this.format('#gray[%s]\n', stack);
output += gray(stack) + '\n';
}

output += '\n';
Expand All @@ -121,21 +129,24 @@ internals.Reporter.prototype.end = function (notebook) {

if (notebook.leaks) {
if (notebook.leaks.length) {
output += '#gray[The following leaks were detected]:\n\n';
output += red(' The following leaks were detected:') + '\n\n';
for (var i = 0, il = notebook.leaks.length; i < il; ++i) {
output += '#red[' + notebook.leaks[i] + ']\n';
output += red(notebook.leaks[i]) + '\n';
}
}
else {
output += '#green[ No global variable leaks detected.]\n';
output += green(' No global variable leaks detected.') + '\n';
}

output += '\n';
}

// Coverage

var coverage = notebook.coverage;
if (coverage) {
output += 'Coverage: ' + coverage.percent.toFixed(2) + '%\n';
var status = 'Coverage: ' + coverage.percent.toFixed(2) + '%';
output += coverage.percent === 100 ? green(status) : red(status) + '\n';
if (coverage.percent < 100) {
output += '\n';
coverage.files.forEach(function (file) {
Expand All @@ -150,17 +161,19 @@ internals.Reporter.prototype.end = function (notebook) {
});

if (missingLines.length) {
output += file.filename + ' missing coverage on line(s): ' + missingLines.join(', ') + '\n';
output += red(file.filename + ' missing coverage on line(s): ' + missingLines.join(', ')) + '\n';
}
});

if (coverage.percent < this.settings.threshold || isNaN(coverage.percent)) {
output += 'Code coverage below threshold: ' + coverage.percent.toFixed(2) + ' < ' + this.settings.threshold + '\n';
output += red('Code coverage below threshold: ' + coverage.percent.toFixed(2) + ' < ' + this.settings.threshold) + '\n';
}
}

output += '\n';
}

return this.format(output);
return output;
};


Expand Down
18 changes: 9 additions & 9 deletions lib/reporters/tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports = module.exports = internals.Reporter = function (options) {

internals.Reporter.prototype.start = function (notebook) {

console.log('%d..%d', 1, notebook.count);
this.print('1..' + notebook.count + '\n');
};


Expand All @@ -26,25 +26,25 @@ internals.Reporter.prototype.test = function (test) {
var title = test.title.replace(/#/g, '');
if (test.err) {
this.failures++;
console.log('not ok %d %s', test.id, title);
this.print('not ok ' + test.id + ' ' + title + '\n');
if (test.err.stack) {
console.log(test.err.stack.replace(/^/gm, ' '));
this.print(test.err.stack.replace(/^/gm, ' ') + '\n');
}
}
else {
this.passes++;
console.log('ok %d %s', test.id, title);
this.print('ok ' + test.id + ' ' + title + '\n');
}
};


internals.Reporter.prototype.end = function (notebook) {

console.log('# tests ' + (this.passes + this.failures));
console.log('# pass ' + this.passes);
console.log('# fail ' + this.failures);
this.print('# tests ' + (this.passes + this.failures) + '\n');
this.print('# pass ' + this.passes + '\n');
this.print('# fail ' + this.failures + '\n');
};


// console.log('ok %d # SKIP %s', test.id, title);
// console.log('not ok %d # TODO %s', test.id, title);
// ('ok %d # SKIP %s', test.id, title)
// ('not ok %d # TODO %s', test.id, title)

0 comments on commit 9649720

Please sign in to comment.