Skip to content

Commit

Permalink
Add Mocha-style multi-line string diffs.
Browse files Browse the repository at this point in the history
Instead of using eyes for strings, use diff to present
a colored diff of the expected vs. actual results.
  • Loading branch information
reid committed May 17, 2012
1 parent 6aa9673 commit cbd6123
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 18 deletions.
93 changes: 88 additions & 5 deletions lib/assert/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
var stylize = require('../vows/console').stylize;
var inspect = require('../vows/console').inspect;
var diff = require('diff');

/**
* Pad the given `str` to `len`.
*
* @param {String} str
* @param {String} len
* @return {String}
* @api private
*/

function pad(str, len) {
str = String(str);
return Array(len - str.length + 1).join(' ') + str;
}

/**
* Color lines for `str`, using the color `name`.
*
* @param {String} name
* @param {String} str
* @return {String}
* @api private
*/

function styleLines(str, name) {
return str.split('\n').map(function(str){
return stylize(str, name);
}).join('\n');
}

/**
* Return a character diff for `err`.
*
* @param {Error} err
* @return {String}
* @api private
*/

function errorDiff(err, type) {
return diff['diff' + type](err.actual, err.expected).map(function(str){
if (/^(\n+)$/.test(str.value)) str.value = Array(++RegExp.$1.length).join('<newline>');
if (str.added) return styleLines(str.value, 'green');
if (str.removed) return styleLines(str.value, 'red');
return str.value;
}).join('');
}

require('assert').AssertionError.prototype.toString = function () {
var that = this,
Expand All @@ -10,14 +57,50 @@ require('assert').AssertionError.prototype.toString = function () {
}

function parse(str) {
var actual = inspect(that.actual, {showHidden: that.actual instanceof Error}),
expected;
var actual = that.actual,
expected = that.expected,
msg, len;

if (
'string' === typeof actual &&
'string' === typeof expected
) {
len = Math.max(actual.length, expected.length);

if (len < 20) msg = errorDiff(that, 'Chars');
else msg = errorDiff(that, 'Words');

// linenos
var lines = msg.split('\n');
if (lines.length > 4) {
var width = String(lines.length).length;
msg = lines.map(function(str, i){
return pad(++i, width) + ' |' + ' ' + str;
}).join('\n');
}

// legend
msg = '\n'
+ stylize('actual', 'green')
+ ' '
+ stylize('expected', 'red')
+ '\n\n'
+ msg
+ '\n';

// indent
msg = msg.replace(/^/gm, ' ');

return msg;
}

actual = inspect(actual, {showHidden: actual instanceof Error});

if (that.expected instanceof Function) {
expected = that.expected.name;
if (expected instanceof Function) {
expected = expected.name;
}
else {
expected = inspect(that.expected, {showHidden: that.actual instanceof Error});
expected = inspect(expected, {showHidden: actual instanceof Error});
}

return str.replace(/{actual}/g, actual).
Expand Down
48 changes: 35 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
{
"name" : "vows",
"description" : "Asynchronous BDD & continuous integration for node.js",
"url" : "http://vowsjs.org",
"keywords" : ["testing", "spec", "test", "BDD"],
"author" : "Alexis Sellier <self@cloudhead.net>",
"contributors" : [{ "name": "Charlie Robbins", "email": "charlie.robbins@gmail.com" }],
"dependencies" : {"eyes": ">=0.1.6"},
"main" : "./lib/vows",
"bin" : { "vows": "./bin/vows" },
"directories" : { "test": "./test", "bin": "./bin" },
"version" : "0.6.2",
"scripts" : {"test": "./bin/vows --spec"},
"engines" : {"node": ">=0.2.6"}
"name": "vows",
"description": "Asynchronous BDD & continuous integration for node.js",
"url": "http://vowsjs.org",
"keywords": [
"testing",
"spec",
"test",
"BDD"
],
"author": "Alexis Sellier <self@cloudhead.net>",
"contributors": [
{
"name": "Charlie Robbins",
"email": "charlie.robbins@gmail.com"
}
],
"dependencies": {
"eyes": ">=0.1.6",
"diff": "~1.0.3"
},
"main": "./lib/vows",
"bin": {
"vows": "./bin/vows"
},
"directories": {
"test": "./test",
"bin": "./bin"
},
"version": "0.6.2",
"scripts": {
"test": "./bin/vows --spec"
},
"engines": {
"node": ">=0.2.6"
}
}

0 comments on commit cbd6123

Please sign in to comment.