266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith/**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * The ObjectAssert object provides functions to test JavaScript objects
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * for a variety of cases.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @namespace Test
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @class ObjectAssert
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke SmithYUITest.ObjectAssert = {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that an object has all of the same properties
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * and property values as the other.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} expected The object with all expected properties and values.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} actual The object to inspect.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method areEqual
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @deprecated
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith areEqual: function(expected, actual, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert._increment();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith var expectedKeys = YUITest.Object.keys(expected),
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith actualKeys = YUITest.Object.keys(actual);
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith //first check keys array length
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (expectedKeys.length != actualKeys.length){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Object should have " + expectedKeys.length + " keys but has " + actualKeys.length));
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith //then check values
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith for (var name in expected){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (expected.hasOwnProperty(name)){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (expected[name] != actual[name]){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values should be equal for property " + name), expected[name], actual[name]);
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith },
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that an object has a property with the given name.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} propertyName The name of the property to test.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method hasKey
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @deprecated Use ownsOrInheritsKey() instead
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith hasKey: function (propertyName, object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.ObjectAssert.ownsOrInheritsKey(propertyName, object, message);
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith },
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that an object has all properties of a reference object.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Array} properties An array of property names that should be on the object.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method hasKeys
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @deprecated Use ownsOrInheritsKeys() instead
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith hasKeys: function (properties, object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.ObjectAssert.ownsOrInheritsKeys(properties, object, message);
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith },
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that a property with the given name exists on an object's prototype.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} propertyName The name of the property to test.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method inheritsKey
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith inheritsKey: function (propertyName, object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert._increment();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (!(propertyName in object && !object.hasOwnProperty(propertyName))){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith },
cee75dadf989ebf19ebf2ff1de3d8e41a659c9d0Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that all properties exist on an object prototype.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Array} properties An array of property names that should be on the object.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method inheritsKeys
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith inheritsKeys: function (properties, object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert._increment();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith for (var i=0; i < properties.length; i++){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (!(propertyName in object && !object.hasOwnProperty(properties[i]))){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object instance."));
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith },
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that a property with the given name exists on an object instance (not on its prototype).
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} propertyName The name of the property to test.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method ownsKey
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith ownsKey: function (propertyName, object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert._increment();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (!object.hasOwnProperty(propertyName)){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith },
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore * Asserts that all properties exist on an object instance (not on its prototype).
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Array} properties An array of property names that should be on the object.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method ownsKeys
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith ownsKeys: function (properties, object, message) {
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore YUITest.Assert._increment();
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore for (var i=0; i < properties.length; i++){
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore if (!object.hasOwnProperty(properties[i])){
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object instance."));
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore }
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore }
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore },
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore /**
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore * Asserts that an object owns no properties.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to check.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method ownsNoKeys
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith ownsNoKeys : function (object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert._increment();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith var count = 0,
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith name;
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith for (name in object){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (object.hasOwnProperty(name)){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith count++;
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (count !== 0){
b99f5105fd25efdbea655f7a50b225bace962ad2Luke Smith YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Object owns " + count + " properties but should own none."));
b99f5105fd25efdbea655f7a50b225bace962ad2Luke Smith }
6c3809c00bf30559173504452b89ee2a7166b60cyui-plug
4f366cb8b47ab852f69faf6da46bc350bc1afea8yui-plug },
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that an object has a property with the given name.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} propertyName The name of the property to test.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @method ownsOrInheritsKey
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @static
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith */
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith ownsOrInheritsKey: function (propertyName, object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert._increment();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith if (!(propertyName in object)){
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object."));
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith },
4969799abcdbfb7be34c4bbe20c2051bd50e1391Adam Moore
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith /**
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * Asserts that an object has all properties of a reference object.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Array} properties An array of property names that should be on the object.
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith * @param {Object} object The object to search.
6af8358a6bd80bcc795828ce62c1ecd22daab6a1Luke Smith * @param {String} message (Optional) The message to display if the assertion fails.
6af8358a6bd80bcc795828ce62c1ecd22daab6a1Luke Smith * @method ownsOrInheritsKeys
6af8358a6bd80bcc795828ce62c1ecd22daab6a1Luke Smith * @static
6af8358a6bd80bcc795828ce62c1ecd22daab6a1Luke Smith */
6af8358a6bd80bcc795828ce62c1ecd22daab6a1Luke Smith ownsOrInheritsKeys: function (properties, object, message) {
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith YUITest.Assert._increment();
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith for (var i=0; i < properties.length; i++){
6af8358a6bd80bcc795828ce62c1ecd22daab6a1Luke Smith if (!(properties[i] in object)){
6af8358a6bd80bcc795828ce62c1ecd22daab6a1Luke Smith YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object."));
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith }
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith};
266bfbd67fc220029bdadabd3c49e733f9f39360Luke Smith