From 6baca02e7646cb98c9e7a1b746eaaabbbbba522b Mon Sep 17 00:00:00 2001 From: cloudhead Date: Mon, 14 Jun 2010 14:28:40 -0400 Subject: [PATCH] nicer output. refactor of formatters --- bin/vows | 5 ++- lib/vows/console.js | 42 +++++++++++++++++++------ lib/vows/reporters/dot-matrix.js | 22 +++++-------- lib/vows/reporters/spec.js | 53 +++++++++++++++++++++----------- lib/vows/reporters/watch.js | 16 ++-------- 5 files changed, 82 insertions(+), 56 deletions(-) diff --git a/bin/vows b/bin/vows index 0a89475..6b503fa 100755 --- a/bin/vows +++ b/bin/vows @@ -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')); diff --git a/lib/vows/console.js b/lib/vows/console.js index 4202a0f..79a118a 100644 --- a/lib/vows/console.js +++ b/lib/vows/console.js @@ -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) { @@ -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; }; diff --git a/lib/vows/reporters/dot-matrix.js b/lib/vows/reporters/dot-matrix.js index 98bad51..e6f2d70 100644 --- a/lib/vows/reporters/dot-matrix.js +++ b/lib/vows/reporters/dot-matrix.js @@ -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); // @@ -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(''); } diff --git a/lib/vows/reporters/spec.js b/lib/vows/reporters/spec.js index cd8a96b..43aa26c 100644 --- a/lib/vows/reporters/spec.js +++ b/lib/vows/reporters/spec.js @@ -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'); @@ -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); }; diff --git a/lib/vows/reporters/watch.js b/lib/vows/reporters/watch.js index 5d59790..e0f98e5 100644 --- a/lib/vows/reporters/watch.js +++ b/lib/vows/reporters/watch.js @@ -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); // @@ -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;