Created
October 15, 2013 19:32
-
-
Save serby/6997351 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash') | |
module.exports = function (actual, expected, ignoreObjectLength, ignoreFunctions) { | |
/* jshint maxcomplexity: 7 */ | |
if (arguments.length < 2) { | |
throw new Error('Not enough arguments') | |
} | |
if (typeof actual !== 'object') { | |
throw new Error('actual must be an object') | |
} | |
if (typeof expected !== 'object') { | |
throw new Error('expected must be an object') | |
} | |
if (typeof ignoreFunctions !== 'undefined' && ignoreFunctions === true) { | |
actual = removeFunctions(actual) | |
expected = removeFunctions(expected) | |
} | |
var keys = Object.keys(actual) | |
try { | |
if (typeof ignoreObjectLength === 'undefined' || ignoreObjectLength === false) { | |
keys.length.should.equal(Object.keys(expected).length) | |
} | |
} catch (e) { | |
e.message = 'Object length issue: ' + e.message + ' - difference: ' | |
+ 'Left:' + _.difference(keys, Object.keys(expected)) + ' Right:' + _.difference(Object.keys(expected), keys) | |
throw e | |
} | |
keys.forEach(function (key) { | |
var value = actual[key] | |
try { | |
if (Array.isArray(value) || typeof expected[key] === 'object' && expected[key] !== null) { | |
expected[key].should.eql(value) | |
} else { | |
actual.should.have.property(key, value) | |
} | |
} catch (e) { | |
e.message = 'Issue with ' + key + ': ' + e.message | |
throw e | |
} | |
}) | |
} | |
function removeFunctions(object) { | |
if (_.isObject(object)) { | |
object = _.clone(object) | |
_.each(Object.keys(object), function(key) { | |
if (_.isObject(object[key])) { | |
object[key] = removeFunctions(object[key]) | |
} | |
if (_.isFunction(object[key])) { | |
delete object[key] | |
} | |
}) | |
} | |
return object | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment