Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Clean up #1079

Travis Jefferybe674a20a7fe
diff --git a/lib/reporters/base.js b/lib/reporters/base.js
index e771695b..661126cf 100644
--- a/lib/reporters/base.js
+++ b/lib/reporters/base.js
@@ -180,8 +180,8 @@ exports.list = function(failures){
// explicitly show diff
if (err.showDiff && sameType(actual, expected)) {
escape = false;
- err.actual = actual = stringify(canonicalizeObject(actual));
- err.expected = expected = stringify(canonicalizeObject(expected));
+ err.actual = actual = stringify(canonicalize(actual));
+ err.expected = expected = stringify(canonicalize(expected));
}
// actual / expected diff
@@ -446,7 +446,7 @@ function colorLines(name, str) {
/**
* Stringify `obj`.
*
- * @param {Mixed} obj
+ * @param {Object} obj
* @return {String}
* @api private
*/
@@ -457,37 +457,38 @@ function stringify(obj) {
}
/**
- * Return a new object that has the in sorted order.
- * @param {Mixed} obj
- * @return {Mixed}
+ * Return a new object that has the keys in sorted order.
+ * @param {Object} obj
+ * @return {Object}
+ * @api private
*/
-function canonicalizeObject(obj, stack) {
- if (!stack) {
- stack = [];
- } else if (utils.indexOf(stack, obj) !== -1) {
- // There's a cycle, bail out
- return obj;
- }
- stack = stack || [];
- if ('[object Array]' == {}.toString.call(obj)) {
- stack.push(obj);
- var returnValue = utils.map(obj, function (item) {
- return canonicalizeObject(item, stack);
- });
- stack.pop();
- return returnValue;
- } else if (typeof obj === 'object' && obj !== null) {
- stack.push(obj);
- var sortedObj = {};
- utils.forEach(utils.keys(obj).sort(), function (key) {
- sortedObj[key] = canonicalizeObject(obj[key], stack);
- });
- stack.pop();
- return sortedObj;
- } else {
- return obj;
- }
-}
+
+ function canonicalize(obj, stack) {
+ stack = stack || [];
+
+ if (utils.indexOf(stack, obj) !== -1) return obj;
+
+ var canonicalizedObj;
+
+ if ('[object Array]' == {}.toString.call(obj)) {
+ stack.push(obj);
+ canonicalizedObj = utils.map(obj, function(item) {
+ return canonicalize(item, stack);
+ });
+ stack.pop();
+ } else if (typeof obj === 'object' && obj !== null) {
+ stack.push(obj);
+ canonicalizedObj = {};
+ utils.forEach(utils.keys(obj).sort(), function(key) {
+ canonicalizedObj[key] = canonicalize(obj[key], stack);
+ });
+ stack.pop();
+ } else {
+ canonicalizedObj = obj;
+ }
+
+ return canonicalizedObj;
+ }
/**
* Check that a / b have the same type.
@@ -504,4 +505,3 @@ function sameType(a, b) {
return a == b;
}
-
diff --git a/mocha.js b/mocha.js
index 07ae9266..5bb0ef21 100644
--- a/mocha.js
+++ b/mocha.js
@@ -1840,7 +1840,8 @@ require.register("reporters/base.js", function(module, exports, require){
var tty = require('browser/tty')
, diff = require('browser/diff')
- , ms = require('../ms');
+ , ms = require('../ms')
+ , utils = require('../utils');
/**
* Save timer references to avoid Sinon interfering (see GH-237).
@@ -2014,8 +2015,8 @@ exports.list = function(failures){
// explicitly show diff
if (err.showDiff && sameType(actual, expected)) {
escape = false;
- err.actual = actual = stringify(actual);
- err.expected = expected = stringify(expected);
+ err.actual = actual = stringify(canonicalize(actual));
+ err.expected = expected = stringify(canonicalize(expected));
}
// actual / expected diff
@@ -2280,7 +2281,7 @@ function colorLines(name, str) {
/**
* Stringify `obj`.
*
- * @param {Mixed} obj
+ * @param {Object} obj
* @return {String}
* @api private
*/
@@ -2290,6 +2291,40 @@ function stringify(obj) {
return JSON.stringify(obj, null, 2);
}
+/**
+ * Return a new object that has the keys in sorted order.
+ * @param {Object} obj
+ * @return {Object}
+ * @api private
+ */
+
+ function canonicalize(obj, stack) {
+ stack = stack || [];
+
+ if (utils.indexOf(stack, obj) !== -1) return obj;
+
+ var canonicalizedObj;
+
+ if ('[object Array]' == {}.toString.call(obj)) {
+ stack.push(obj);
+ canonicalizedObj = utils.map(obj, function(item) {
+ return canonicalize(item, stack);
+ });
+ stack.pop();
+ } else if (typeof obj === 'object' && obj !== null) {
+ stack.push(obj);
+ canonicalizedObj = {};
+ utils.forEach(utils.keys(obj).sort(), function(key) {
+ canonicalizedObj[key] = canonicalize(obj[key], stack);
+ });
+ stack.pop();
+ } else {
+ canonicalizedObj = obj;
+ }
+
+ return canonicalizedObj;
+ }
+
/**
* Check that a / b have the same type.
*
@@ -2306,7 +2341,6 @@ function sameType(a, b) {
}
-
}); // module: reporters/base.js
require.register("reporters/doc.js", function(module, exports, require){
@@ -5332,6 +5366,22 @@ exports.forEach = function(arr, fn, scope){
fn.call(scope, arr[i], i);
};
+/**
+ * Array#map (<=IE8)
+ *
+ * @param {Array} array
+ * @param {Function} fn
+ * @param {Object} scope
+ * @api private
+ */
+
+exports.map = function(arr, fn, scope){
+ var result = [];
+ for (var i = 0, l = arr.length; i < l; i++)
+ result.push(fn.call(scope, arr[i], i));
+ return result;
+};
+
/**
* Array#indexOf (<=IE8)
*