Last active
January 2, 2016 23:48
-
-
Save caleb531/8378353 to your computer and use it in GitHub Desktop.
A shallowEqual() assertion for QUnit, which is essentially a strict comparison of all top-level properties for each object (ignoring the prototype chain).
This file contains hidden or 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
// Determines if two objects are equal (ignoring prototypes) | |
QUnit.shallowEquiv = function(actual, expected) { | |
var key, equiv = true; | |
// Compare first object to second object | |
for (key in actual) { | |
// Keys must not be in prototype chain | |
if (actual.hasOwnProperty(key)) { | |
if (actual[key] !== expected[key]) { | |
equiv = false; | |
break; | |
} | |
} | |
} | |
// If objects are still potentially equivalent | |
if (equiv) { | |
// Compare second object to first object | |
// This handles the case where the first object is a subset of the other | |
for (key in expected) { | |
if (expected.hasOwnProperty(key)) { | |
if (expected[key] !== actual[key]) { | |
equiv = false; | |
break; | |
} | |
} | |
} | |
} | |
return equiv; | |
}; | |
// Define shallowEqual() and notShallowEqual() assertions | |
QUnit.extend(QUnit.assert, { | |
shallowEqual: function(actual, expected, message) { | |
var equiv = QUnit.shallowEquiv(actual, expected); | |
QUnit.push(equiv, actual, expected, message); | |
}, | |
notShallowEqual: function(actual, expected, message) { | |
var equiv = QUnit.shallowEquiv(actual, expected); | |
QUnit.push(!equiv, actual, expected, message); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment