Skip to content
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

Add object diff #589

Closed
wants to merge 3 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
37 changes: 31 additions & 6 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

var tty = require('tty')
, diff = require('diff')
, ms = require('../ms');
, ms = require('../ms')
, jsDump = require("jsDump");

/**
* Save timer references to avoid Sinon interfering (see GH-237).
Expand Down Expand Up @@ -144,7 +145,15 @@ exports.list = function(failures){
, expected = err.expected;

// actual / expected diff
if ('string' == typeof actual && 'string' == typeof expected) {
if (err.showDiff && undefined != actual && undefined != expected) {
jsDump.sortKeys = true

if ('string' != typeof actual)
err.actual = jsDump.parse(actual)

if ('string' != typeof expected)
err.expected = jsDump.parse(expected)

var len = Math.max(actual.length, expected.length);

if (len < 20) msg = errorDiff(err, 'Chars');
Expand Down Expand Up @@ -326,10 +335,26 @@ function pad(str, len) {

function errorDiff(err, type) {
return diff['diff' + type](err.actual, err.expected).map(function(str){
str.value = str.value
.replace(/\t/g, '<tab>')
.replace(/\r/g, '<CR>')
.replace(/\n/g, '<LF>\n');
if (str.added || str.removed) {
// replace all invisible chars if they are the only chars
if (/^[ \t\r\n]+$/.test(str))
str.value = str.value
.replace(/\t/g, '<tab>')
.replace(/\r/g, '<CR>')
.replace(/\n/g, '<LF>\n');
// otherwise only replace invisible chars at both ends
else
str.value = str.value
.replace(/^\t+|\t+$/, function(match) {
return match.replace(/\t/g, '<tab>');
})
.replace(/^\r+|\r+$/, function(match) {
return match.replace(/\r/g, '<CR>');
})
.replace(/^\n+|\n+$/, function(match) {
return match.replace(/\n/g, '<LF>\n');
});
}
if (str.added) return colorLines('diff added', str.value);
if (str.removed) return colorLines('diff removed', str.value);
return str.value;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
, "debug": "*"
, "mkdirp": "0.3.3"
, "ms": "0.3.0"
, "jsDump": "1.x"
}
, "devDependencies": {
"should": "*"
Expand Down