Skip to content

Instantly share code, notes, and snippets.

@ebi
Created March 6, 2012 09:29
Show Gist options
  • Save ebi/1985289 to your computer and use it in GitHub Desktop.
Save ebi/1985289 to your computer and use it in GitHub Desktop.
YUItest deepequal
/**
* Assert that an object has all of the same properties and property values
* as the expected one. It doesn't consider prototype properties.
* @param {Object} expected The object with all expected properties and values.
* @param {Object} actual The object to inspect.
* @param {String} message (Optional) The message to display if the assertion fails.
* @method areSame
* @static
*/
areSame: function (expected, actual, message) {
YUITest.Assert._increment();
if ('object' !== typeof actual) {
throw new YUITest.UnexpectedValue(YUITest.Assert._formatMessage(message, "Value should be an object."), actual);
}
if ('object' !== typeof expected) {
throw new YUITest.UnexpectedValue(YUITest.Assert._formatMessage(message, "Value should be an object."), expected);
}
for (var key in expected) {
if (expected.hasOwnProperty(key)) {
if ('object' === typeof actual[key] || 'object' === typeof expected[key]) {
YUITest.ObjectAssert.areSame(expected[key], actual[key], message);
} else if (actual[key] !== expected[key]) {
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values should be equal for property " + key), expected[key], actual[key]);
}
}
}
for (key in actual) {
if (actual.hasOwnProperty(key) && 'undefined' === typeof expected[key]) {
throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, 'Property is not defined on actual object ' + key));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment