Skip to content

Commit

Permalink
Merge pull request #1493 from a8m/master
Browse files Browse the repository at this point in the history
[Fix]: Diff viewer(#1229, #1132, ...)
  • Loading branch information
Travis Jeffery committed Jan 18, 2015
2 parents cfe26ea + f43006a commit e130e31
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 48 deletions.
3 changes: 1 addition & 2 deletions lib/reporters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ exports.list = function(failures){
if (err.uncaught) {
msg = 'Uncaught ' + msg;
}

// explicitly show diff
if (err.showDiff && sameType(actual, expected)) {

Expand Down Expand Up @@ -386,7 +385,7 @@ function unifiedDiff(err, escape) {
function notBlank(line) {
return line != null;
}
msg = diff.createPatch('string', err.actual, err.expected);
var msg = diff.createPatch('string', err.actual, err.expected);
var lines = msg.split('\n').splice(4);
return '\n '
+ colorLines('diff added', '+ expected') + ' '
Expand Down
137 changes: 109 additions & 28 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ exports.filter = function(arr, fn){

exports.keys = Object.keys || function(obj) {
var keys = []
, has = Object.prototype.hasOwnProperty // for `window` on <=IE8
, has = Object.prototype.hasOwnProperty; // for `window` on <=IE8

for (var key in obj) {
if (has.call(obj, key)) {
Expand Down Expand Up @@ -157,6 +157,26 @@ exports.watch = function(files, fn){
});
};

/**
* Array.isArray (<=IE8)
*
* @param {Object} obj
* @return {Boolean}
* @api private
*/
var isArray = Array.isArray || function (obj) {
return '[object Array]' == {}.toString.call(obj);
};

/**
* @description
* Buffer.prototype.toJSON polyfill
* @type {Function}
*/
Buffer.prototype.toJSON = Buffer.prototype.toJSON || function () {
return Array.prototype.slice.call(this, 0);
};

/**
* Ignored files.
*/
Expand All @@ -179,15 +199,15 @@ exports.files = function(dir, ext, ret){
var re = new RegExp('\\.(' + ext.join('|') + ')$');

fs.readdirSync(dir)
.filter(ignored)
.forEach(function(path){
path = join(dir, path);
if (fs.statSync(path).isDirectory()) {
exports.files(path, ext, ret);
} else if (path.match(re)) {
ret.push(path);
}
});
.filter(ignored)
.forEach(function(path){
path = join(dir, path);
if (fs.statSync(path).isDirectory()) {
exports.files(path, ext, ret);
} else if (path.match(re)) {
ret.push(path);
}
});

return ret;
};
Expand Down Expand Up @@ -364,30 +384,94 @@ exports.type = function type(value) {
*/

exports.stringify = function(value) {
var prop,
type = exports.type(value);

if (type === 'null' || type === 'undefined') {
return '[' + type + ']';
}

if (type === 'date') {
return '[Date: ' + value.toISOString() + ']';
}
var type = exports.type(value);

if (!~exports.indexOf(['object', 'array', 'function'], type)) {
return value.toString();
if(type != 'buffer') {
return jsonStringify(value);
}
var json = value.toJSON();
// Based on the toJSON result
return jsonStringify(json.data && json.type ? json.data : json, 2)
.replace(/,(\n|$)/g, '$1');
}

for (prop in value) {
for (var prop in value) {
if (Object.prototype.hasOwnProperty.call(value, prop)) {
return JSON.stringify(exports.canonicalize(value), null, 2).replace(/,(\n|$)/g, '$1');
return jsonStringify(exports.canonicalize(value), 2).replace(/,(\n|$)/g, '$1');
}
}

return emptyRepresentation(value, type);
};

/**
* @description
* like JSON.stringify but more sense.
* @param {Object} object
* @param {Number=} spaces
* @param {number=} depth
* @returns {*}
* @private
*/
function jsonStringify(object, spaces, depth) {
if(typeof spaces == 'undefined') return _stringify(object); // primitive types

depth = depth || 1;
var space = spaces * depth
, str = isArray(object) ? '[' : '{'
, end = isArray(object) ? ']' : '}'
, length = object.length || exports.keys(object).length
, repeat = function(s, n) { return new Array(n).join(s); }; // `.repeat()` polyfill

function _stringify(val) {
switch (exports.type(val)) {
case 'null':
case 'undefined':
val = '[' + val + ']';
break;
case 'array':
case 'object':
val = jsonStringify(val, spaces, depth + 1);
break;
case 'boolean':
case 'regexp':
case 'number':
val = val === 0 && (1/val) === -Infinity // `-0`
? '-0'
: val.toString();
break;
case 'date':
val = '[Date: ' + val.toISOString() + ']';
break;
case 'buffer':
var json = val.toJSON();
// Based on the toJSON result
json = json.data && json.type ? json.data : json;
val = '[Buffer: ' + jsonStringify(json, 2, depth + 1) + ']';
break;
default:
val = (val == '[Function]' || val == '[Circular]')
? val
: '"' + val + '"'; //string
}
return val;
}

for(var i in object) {
if(!object.hasOwnProperty(i)) continue; // not my business
--length;
str += '\n ' + repeat(' ', space)
+ (isArray(object) ? '' : '"' + i + '": ') // key
+ _stringify(object[i]) // value
+ (length ? ',' : ''); // comma
}

return str + (str.length != 1 // [], {}
? '\n' + repeat(' ', --space) + end
: end);
}

/**
* Return if obj is a Buffer
* @param {Object} arg
Expand Down Expand Up @@ -434,8 +518,6 @@ exports.canonicalize = function(value, stack) {

switch(type) {
case 'undefined':
canonicalizedObj = '[undefined]';
break;
case 'buffer':
case 'null':
canonicalizedObj = value;
Expand All @@ -447,9 +529,6 @@ exports.canonicalize = function(value, stack) {
});
});
break;
case 'date':
canonicalizedObj = '[Date: ' + value.toISOString() + ']';
break;
case 'function':
for (prop in value) {
canonicalizedObj = {};
Expand All @@ -468,7 +547,9 @@ exports.canonicalize = function(value, stack) {
});
});
break;
case 'date':
case 'number':
case 'regexp':
case 'boolean':
canonicalizedObj = value;
break;
Expand Down
Loading

0 comments on commit e130e31

Please sign in to comment.