Skip to content

Commit

Permalink
nicer output. refactor of formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Jun 14, 2010
1 parent 4a1a65a commit 6baca02
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 56 deletions.
5 changes: 4 additions & 1 deletion bin/vows
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ var path = require('path'),
fs = require('fs'),
events = require('events');

var inspect = require('eyes').inspector({ stream: null });
var inspect = require('eyes').inspector({
stream: null,
styles: { string: 'grey', regexp: 'grey' }
});

require.paths.unshift(path.join(__dirname, '..', 'lib'));

Expand Down
42 changes: 32 additions & 10 deletions lib/vows/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ this.stylize = function stylize(str, style) {
'\033[' + styles[style][1] + 'm';
};

var $ = this.$ = function (str) {
str = new(String)(str);

['bold', 'grey', 'yellow', 'red', 'green', 'white', 'cyan', 'italic'].forEach(function (style) {
Object.defineProperty(str, style, {
get: function () {
return exports.$(exports.stylize(this, style));
}
});
});
return str;
};

this.puts = function (options) {
var stylize = exports.stylize;
return function (args) {
Expand All @@ -29,20 +42,29 @@ this.puts = function (options) {
};

this.result = function (event) {
var result = event.honored + " honored, " +
event.broken + " broken, " +
event.errored + " errored, " +
event.pending + " pending",
style = event.honored === event.total ? ('green')
: (event.pending ? 'cyan' : (event.errored ? 'red' : 'yellow')),
buffer = [];
var result = [], buffer = [], time = '', header;
var status = (event.errored && 'errored') || (event.broken && 'broken') ||
(event.honored && 'honored') || (event.pending && 'pending');

event.honored && result.push($(event.honored).bold + " honored");
event.broken && result.push($(event.broken).bold + " broken");
event.errored && result.push($(event.errored).bold + " errored");
event.pending && result.push($(event.pending).bold + " pending");

result = result.join(' ∙ ');

header = {
honored: '✓ ' + $('OK').bold.green,
broken: '✗ ' + $('Broken').bold.yellow,
errored: '✗ ' + $('Errored').bold.red,
pending: '- ' + $('Pending').bold.cyan
}[status] + ' » ';

if ('time' in event) {
buffer.push("Verified " + event.total + " vows in " +
(event.time.toFixed(3) + " seconds.\n"));
time = ' (' + event.time.toFixed(3) + 's)';
time = this.stylize(time, 'grey');
}
buffer.push(this.stylize(result, style));
buffer.push(header + result + time);

return buffer;
};
Expand Down
22 changes: 8 additions & 14 deletions lib/vows/reporters/dot-matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var sys = require('sys');

var options = {};
var console = require('vows/console');
var spec = require('vows/reporters/spec');
var stylize = console.stylize,
puts = console.puts(options);
//
Expand All @@ -28,27 +29,20 @@ this.report = function (data, s) {
break;
case 'vow':
if (event.status === 'honored') {
sys.print(stylize('.', 'green'));
sys.print(stylize('·', 'green'));
} else if (event.status === 'pending') {
sys.print(stylize('.', 'cyan'));
sys.print(stylize('-', 'cyan'));
} else {
if (lastContext !== event.context) {
lastContext = event.context;
messages.push(event.context);
messages.push(spec.contextText(event.context));
}
if (event.status === 'broken') {
sys.print(stylize('B', 'yellow'));
messages.push(' - ' + stylize(event.title, 'yellow'));
messages.push(' ~ ' + event.exception);
sys.print(stylize('✗', 'yellow'));
messages.push(spec.vowText(event));
} else if (event.status === 'errored') {
sys.print(stylize('E', 'red'));
messages.push(' - ' + stylize(event.title, 'red'));
if (event.exception.type === 'promise') {
messages.push(' * ' + stylize("An 'error' event was caught: " +
stylize(event.exception.error, 'bold'), 'red'));
} else {
messages.push(' ! ' + stylize(event.exception, 'red'));
}
sys.print(stylize('✗', 'red'));
messages.push(spec.vowText(event));
}
messages.push('');
}
Expand Down
53 changes: 35 additions & 18 deletions lib/vows/reporters/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,13 @@ this.report = function (data, s) {

switch (data[0]) {
case 'subject':
puts('\n' + stylize(event, 'underline') + '\n');
puts('\n' + stylize(event, 'bold') + '\n');
break;
case 'context':
puts(event);
puts(this.contextText(event));
break;
case 'vow':
puts(' - ' + stylize(event.title, ({
honored: 'green',
broken: 'yellow',
errored: 'red',
pending: 'cyan'
})[event.status]));
if (event.status === 'broken') {
puts(' ~ ' + event.exception);
} else if (event.status === 'errored') {
if (event.exception.type === 'promise') {
puts(' * ' + stylize("An 'error' event was caught: " +
stylize(event.exception.error, 'bold'), 'red'));
} else {
puts(' ! ' + stylize(event.exception, 'red'));
}
}
puts(this.vowText(event));
break;
case 'end':
sys.print('\n');
Expand All @@ -52,6 +37,38 @@ this.report = function (data, s) {
}
};

this.contextText = function (event) {
return ' ' + event;
};

this.vowText = function (event) {
var buffer = [];

buffer.push(' ' + {
honored: ' ✓ ',
broken: ' ✗ ',
errored: ' ✗ ',
pending: ' - '
}[event.status] + stylize(event.title, ({
honored: 'green',
broken: 'yellow',
errored: 'red',
pending: 'cyan'
})[event.status]));

if (event.status === 'broken') {
buffer.push(' » ' + event.exception);
} else if (event.status === 'errored') {
if (event.exception.type === 'promise') {
buffer.push(' * ' + stylize("An 'error' event was caught: " +
stylize(event.exception.error, 'bold'), 'red'));
} else {
buffer.push(' ' + stylize(event.exception, 'red'));
}
}
return buffer.join('\n');
};

this.print = function (str) {
sys.print(str);
};
16 changes: 3 additions & 13 deletions lib/vows/reporters/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var sys = require('sys');

var options = {};
var console = require('vows/console');
var spec = require('vows/reporters/spec');
var stylize = console.stylize,
puts = console.puts(options);
//
Expand All @@ -23,20 +24,9 @@ this.report = function (data) {
if (['honored', 'pending'].indexOf(event.status) === -1) {
if (lastContext !== event.context) {
lastContext = event.context;
puts(event.context);
}
if (event.status === 'broken') {
puts(' - ' + stylize(event.title, 'yellow'));
puts(' ~ ' + event.exception);
} else if (event.status === 'errored') {
puts(' - ' + stylize(event.title, 'red'));
if (event.exception.type === 'promise') {
puts(' * ' + stylize("An 'error' event was caught: " +
stylize(event.exception.error, 'bold'), 'red'));
} else {
puts(' ! ' + stylize(event.exception, 'red'));
}
puts(spec.contextText(event.context));
}
puts(spec.vowText(event));
puts('');
}
break;
Expand Down

0 comments on commit 6baca02

Please sign in to comment.