ObjectAssert.js revision d058ad04afcbeda9518866f1ce0b296f5f042592
09c638a157dd9d2ed9027215d9e254badfa019b8Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass /**
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * The ObjectAssert object provides functions to test JavaScript objects
daeb6d531149c45a2ceb543ae2cf1e56e5235bbeDav Glass * for a variety of cases.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass *
09c638a157dd9d2ed9027215d9e254badfa019b8Dav Glass * @class ObjectAssert
09c638a157dd9d2ed9027215d9e254badfa019b8Dav Glass * @static
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass */
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.ObjectAssert = {
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass areEqual: function(expected /*:Object*/, actual /*:Object*/, message /*:String*/) /*:Void*/ {
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Assert._increment();
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Object.each(expected, function(value, name){
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass if (expected[name] != actual[name]){
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass throw new Y.Assert.ComparisonFailure(Y.Assert._formatMessage(message, "Values should be equal for property " + name), expected[name], actual[name]);
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass }
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass });
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass },
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass /**
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * Asserts that an object has a property with the given name.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String} propertyName The name of the property to test.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {Object} object The object to search.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String} message (Optional) The message to display if the assertion fails.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @method has
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @static
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass */
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass has : function (propertyName /*:String*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Assert._increment();
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass if (!(propertyName in object)){
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Assert.fail(Y.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object."));
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass }
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass },
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass /**
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * Asserts that an object has all properties of a reference object.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {Object} refObject The object whose properties should be on the object to test.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {Object} object The object to search.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String} message (Optional) The message to display if the assertion fails.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @method hasAll
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @static
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass */
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass hasAll : function (refObject /*:Object*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
b357b56da58949fa86ab8e56983972e0db5cbffbDav Glass Y.Assert._increment();
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Object.each(refObject, function(value, name){
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass if (!(name in object)){
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Assert.fail(Y.Assert._formatMessage(message, "Property '" + name + "' not found on object."));
b357b56da58949fa86ab8e56983972e0db5cbffbDav Glass }
0083a85467aaec586c9609a224503db91ddc238dDav Glass });
b357b56da58949fa86ab8e56983972e0db5cbffbDav Glass },
b357b56da58949fa86ab8e56983972e0db5cbffbDav Glass
b357b56da58949fa86ab8e56983972e0db5cbffbDav Glass /**
b357b56da58949fa86ab8e56983972e0db5cbffbDav Glass * Asserts that a property with the given name exists on an object instance (not on its prototype).
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String} propertyName The name of the property to test.
b9bc793332c43ca5a4d2c836f912781c5d3974f7Dav Glass * @param {Object} object The object to search.
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass * @param {String} message (Optional) The message to display if the assertion fails.
029cb6cc7c47ef456b545670a3ec835dc57e8859Dav Glass * @method owns
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass * @static
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass */
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass owns : function (propertyName /*:String*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass Y.Assert._increment();
1e44d35dd310d594ecc977ee4ed7cf6ef3746045Dav Glass if (!object.hasOwnProperty(propertyName)){
1e44d35dd310d594ecc977ee4ed7cf6ef3746045Dav Glass Y.Assert.fail(Y.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
1e44d35dd310d594ecc977ee4ed7cf6ef3746045Dav Glass }
6e43f558138d36538a82459ec79e0279ffae22e0Dav Glass },
6e43f558138d36538a82459ec79e0279ffae22e0Dav Glass
6e43f558138d36538a82459ec79e0279ffae22e0Dav Glass /**
6e43f558138d36538a82459ec79e0279ffae22e0Dav Glass * Asserts that all properties on a given object also exist on an object instance (not on its prototype).
6e43f558138d36538a82459ec79e0279ffae22e0Dav Glass * @param {Object} refObject The object whose properties should be owned by the object to test.
6e43f558138d36538a82459ec79e0279ffae22e0Dav Glass * @param {Object} object The object to search.
6e43f558138d36538a82459ec79e0279ffae22e0Dav Glass * @param {String} message (Optional) The message to display if the assertion fails.
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass * @method ownsAll
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass * @static
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass */
5cbdc947eb0c9c5e840d59ff8e1dd49a0e2a1887Dav Glass ownsAll : function (refObject /*:Object*/, object /*:Object*/, message /*:String*/) /*:Void*/ {
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Assert._increment();
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Object.each(refObject, function(value, name){
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass if (!object.hasOwnProperty(name)){
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass Y.Assert.fail(Y.Assert._formatMessage(message, "Property '" + name + "' not found on object instance."));
dbf2c770f8178b12e8fe3c36bfa29df58ef13959Dav Glass }
});
}
};