API Docs for: 3.18.1
Show:

File: test/js/ObjectAssert.js

  1.  
  2. /**
  3. * The ObjectAssert object provides functions to test JavaScript objects
  4. * for a variety of cases.
  5. * @namespace Test
  6. * @module test
  7. * @class ObjectAssert
  8. * @static
  9. */
  10. YUITest.ObjectAssert = {
  11.  
  12. /**
  13. * Asserts that an object has all of the same properties
  14. * and property values as the other.
  15. * @param {Object} expected The object with all expected properties and values.
  16. * @param {Object} actual The object to inspect.
  17. * @param {String} message (Optional) The message to display if the assertion fails.
  18. * @method areEqual
  19. * @static
  20. * @deprecated
  21. */
  22. areEqual: function(expected, actual, message) {
  23. YUITest.Assert._increment();
  24.  
  25. var expectedKeys = YUITest.Object.keys(expected),
  26. actualKeys = YUITest.Object.keys(actual);
  27.  
  28. //first check keys array length
  29. if (expectedKeys.length != actualKeys.length){
  30. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Object should have " + expectedKeys.length + " keys but has " + actualKeys.length));
  31. }
  32.  
  33. //then check values
  34. for (var name in expected){
  35. if (expected.hasOwnProperty(name)){
  36. if (expected[name] != actual[name]){
  37. throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values should be equal for property " + name), expected[name], actual[name]);
  38. }
  39. }
  40. }
  41. },
  42.  
  43. /**
  44. * Asserts that an object has a property with the given name.
  45. * @param {String} propertyName The name of the property to test.
  46. * @param {Object} object The object to search.
  47. * @param {String} message (Optional) The message to display if the assertion fails.
  48. * @method hasKey
  49. * @static
  50. * @deprecated Use ownsOrInheritsKey() instead
  51. */
  52. hasKey: function (propertyName, object, message) {
  53. YUITest.ObjectAssert.ownsOrInheritsKey(propertyName, object, message);
  54. },
  55.  
  56. /**
  57. * Asserts that an object has all properties of a reference object.
  58. * @param {Array} properties An array of property names that should be on the object.
  59. * @param {Object} object The object to search.
  60. * @param {String} message (Optional) The message to display if the assertion fails.
  61. * @method hasKeys
  62. * @static
  63. * @deprecated Use ownsOrInheritsKeys() instead
  64. */
  65. hasKeys: function (properties, object, message) {
  66. YUITest.ObjectAssert.ownsOrInheritsKeys(properties, object, message);
  67. },
  68.  
  69. /**
  70. * Asserts that a property with the given name exists on an object's prototype.
  71. * @param {String} propertyName The name of the property to test.
  72. * @param {Object} object The object to search.
  73. * @param {String} message (Optional) The message to display if the assertion fails.
  74. * @method inheritsKey
  75. * @static
  76. */
  77. inheritsKey: function (propertyName, object, message) {
  78. YUITest.Assert._increment();
  79. if (!(propertyName in object && !object.hasOwnProperty(propertyName))){
  80. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
  81. }
  82. },
  83.  
  84. /**
  85. * Asserts that all properties exist on an object prototype.
  86. * @param {Array} properties An array of property names that should be on the object.
  87. * @param {Object} object The object to search.
  88. * @param {String} message (Optional) The message to display if the assertion fails.
  89. * @method inheritsKeys
  90. * @static
  91. */
  92. inheritsKeys: function (properties, object, message) {
  93. YUITest.Assert._increment();
  94. for (var i=0; i < properties.length; i++){
  95. if (!(propertyName in object && !object.hasOwnProperty(properties[i]))){
  96. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object instance."));
  97. }
  98. }
  99. },
  100.  
  101. /**
  102. * Asserts that a property with the given name exists on an object instance (not on its prototype).
  103. * @param {String} propertyName The name of the property to test.
  104. * @param {Object} object The object to search.
  105. * @param {String} message (Optional) The message to display if the assertion fails.
  106. * @method ownsKey
  107. * @static
  108. */
  109. ownsKey: function (propertyName, object, message) {
  110. YUITest.Assert._increment();
  111. if (!object.hasOwnProperty(propertyName)){
  112. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object instance."));
  113. }
  114. },
  115.  
  116. /**
  117. * Asserts that all properties exist on an object instance (not on its prototype).
  118. * @param {Array} properties An array of property names that should be on the object.
  119. * @param {Object} object The object to search.
  120. * @param {String} message (Optional) The message to display if the assertion fails.
  121. * @method ownsKeys
  122. * @static
  123. */
  124. ownsKeys: function (properties, object, message) {
  125. YUITest.Assert._increment();
  126. for (var i=0; i < properties.length; i++){
  127. if (!object.hasOwnProperty(properties[i])){
  128. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object instance."));
  129. }
  130. }
  131. },
  132.  
  133. /**
  134. * Asserts that an object owns no properties.
  135. * @param {Object} object The object to check.
  136. * @param {String} message (Optional) The message to display if the assertion fails.
  137. * @method ownsNoKeys
  138. * @static
  139. */
  140. ownsNoKeys : function (object, message) {
  141. YUITest.Assert._increment();
  142. var count = YUITest.Object.keys(object).length;
  143.  
  144. if (count !== 0){
  145. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Object owns " + count + " properties but should own none."));
  146. }
  147.  
  148. },
  149.  
  150. /**
  151. * Asserts that an object has a property with the given name.
  152. * @param {String} propertyName The name of the property to test.
  153. * @param {Object} object The object to search.
  154. * @param {String} message (Optional) The message to display if the assertion fails.
  155. * @method ownsOrInheritsKey
  156. * @static
  157. */
  158. ownsOrInheritsKey: function (propertyName, object, message) {
  159. YUITest.Assert._increment();
  160. if (!(propertyName in object)){
  161. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + propertyName + "' not found on object."));
  162. }
  163. },
  164.  
  165. /**
  166. * Asserts that an object has all properties of a reference object.
  167. * @param {Array} properties An array of property names that should be on the object.
  168. * @param {Object} object The object to search.
  169. * @param {String} message (Optional) The message to display if the assertion fails.
  170. * @method ownsOrInheritsKeys
  171. * @static
  172. */
  173. ownsOrInheritsKeys: function (properties, object, message) {
  174. YUITest.Assert._increment();
  175. for (var i=0; i < properties.length; i++){
  176. if (!(properties[i] in object)){
  177. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Property '" + properties[i] + "' not found on object."));
  178. }
  179. }
  180. }
  181. };
  182.