Skip to content

Instantly share code, notes, and snippets.

@ebi
Created March 6, 2012 09:29

Revisions

  1. ebi created this gist Mar 6, 2012.
    36 changes: 36 additions & 0 deletions gistfile1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    /**
    * 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));
    }
    }
    }