API Docs for: 3.10.3
Show:

File: test/js/ArrayAssert.js

  1.  
  2. /**
  3. * The ArrayAssert object provides functions to test JavaScript array objects
  4. * for a variety of cases.
  5. * @namespace Test
  6. * @module test
  7. * @class ArrayAssert
  8. * @static
  9. */
  10. YUITest.ArrayAssert = {
  11.  
  12. //=========================================================================
  13. // Private methods
  14. //=========================================================================
  15. /**
  16. * Simple indexOf() implementation for an array. Defers to native
  17. * if available.
  18. * @param {Array} haystack The array to search.
  19. * @param {Variant} needle The value to locate.
  20. * @return {int} The index of the needle if found or -1 if not.
  21. * @method _indexOf
  22. * @private
  23. */
  24. _indexOf: function(haystack, needle){
  25. if (haystack.indexOf){
  26. return haystack.indexOf(needle);
  27. } else {
  28. for (var i=0; i < haystack.length; i++){
  29. if (haystack[i] === needle){
  30. return i;
  31. }
  32. }
  33. return -1;
  34. }
  35. },
  36. /**
  37. * Simple some() implementation for an array. Defers to native
  38. * if available.
  39. * @param {Array} haystack The array to search.
  40. * @param {Function} matcher The function to run on each value.
  41. * @return {Boolean} True if any value, when run through the matcher,
  42. * returns true.
  43. * @method _some
  44. * @private
  45. */
  46. _some: function(haystack, matcher){
  47. if (haystack.some){
  48. return haystack.some(matcher);
  49. } else {
  50. for (var i=0; i < haystack.length; i++){
  51. if (matcher(haystack[i])){
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. },
  58.  
  59. /**
  60. * Asserts that a value is present in an array. This uses the triple equals
  61. * sign so no type coercion may occur.
  62. * @param {Object} needle The value that is expected in the array.
  63. * @param {Array} haystack An array of values.
  64. * @param {String} message (Optional) The message to display if the assertion fails.
  65. * @method contains
  66. * @static
  67. */
  68. contains : function (needle, haystack,
  69. message) {
  70. YUITest.Assert._increment();
  71.  
  72. if (this._indexOf(haystack, needle) == -1){
  73. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value " + needle + " (" + (typeof needle) + ") not found in array [" + haystack + "]."));
  74. }
  75. },
  76.  
  77. /**
  78. * Asserts that a set of values are present in an array. This uses the triple equals
  79. * sign so no type coercion may occur. For this assertion to pass, all values must
  80. * be found.
  81. * @param {Object[]} needles An array of values that are expected in the array.
  82. * @param {Array} haystack An array of values to check.
  83. * @param {String} message (Optional) The message to display if the assertion fails.
  84. * @method containsItems
  85. * @static
  86. */
  87. containsItems : function (needles, haystack,
  88. message) {
  89. YUITest.Assert._increment();
  90.  
  91. //begin checking values
  92. for (var i=0; i < needles.length; i++){
  93. if (this._indexOf(haystack, needles[i]) == -1){
  94. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value " + needles[i] + " (" + (typeof needles[i]) + ") not found in array [" + haystack + "]."));
  95. }
  96. }
  97. },
  98.  
  99. /**
  100. * Asserts that a value matching some condition is present in an array. This uses
  101. * a function to determine a match.
  102. * @param {Function} matcher A function that returns true if the items matches or false if not.
  103. * @param {Array} haystack An array of values.
  104. * @param {String} message (Optional) The message to display if the assertion fails.
  105. * @method containsMatch
  106. * @static
  107. */
  108. containsMatch : function (matcher, haystack,
  109. message) {
  110. YUITest.Assert._increment();
  111. //check for valid matcher
  112. if (typeof matcher != "function"){
  113. throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
  114. }
  115. if (!this._some(haystack, matcher)){
  116. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "No match found in array [" + haystack + "]."));
  117. }
  118. },
  119.  
  120. /**
  121. * Asserts that a value is not present in an array. This uses the triple equals
  122. * Asserts that a value is not present in an array. This uses the triple equals
  123. * sign so no type coercion may occur.
  124. * @param {Object} needle The value that is expected in the array.
  125. * @param {Array} haystack An array of values.
  126. * @param {String} message (Optional) The message to display if the assertion fails.
  127. * @method doesNotContain
  128. * @static
  129. */
  130. doesNotContain : function (needle, haystack,
  131. message) {
  132. YUITest.Assert._increment();
  133.  
  134. if (this._indexOf(haystack, needle) > -1){
  135. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
  136. }
  137. },
  138.  
  139. /**
  140. * Asserts that a set of values are not present in an array. This uses the triple equals
  141. * sign so no type coercion may occur. For this assertion to pass, all values must
  142. * not be found.
  143. * @param {Object[]} needles An array of values that are not expected in the array.
  144. * @param {Array} haystack An array of values to check.
  145. * @param {String} message (Optional) The message to display if the assertion fails.
  146. * @method doesNotContainItems
  147. * @static
  148. */
  149. doesNotContainItems : function (needles, haystack,
  150. message) {
  151.  
  152. YUITest.Assert._increment();
  153.  
  154. for (var i=0; i < needles.length; i++){
  155. if (this._indexOf(haystack, needles[i]) > -1){
  156. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
  157. }
  158. }
  159.  
  160. },
  161. /**
  162. * Asserts that no values matching a condition are present in an array. This uses
  163. * a function to determine a match.
  164. * @param {Function} matcher A function that returns true if the item matches or false if not.
  165. * @param {Array} haystack An array of values.
  166. * @param {String} message (Optional) The message to display if the assertion fails.
  167. * @method doesNotContainMatch
  168. * @static
  169. */
  170. doesNotContainMatch : function (matcher, haystack,
  171. message) {
  172. YUITest.Assert._increment();
  173. //check for valid matcher
  174. if (typeof matcher != "function"){
  175. throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
  176. }
  177. if (this._some(haystack, matcher)){
  178. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
  179. }
  180. },
  181. /**
  182. * Asserts that the given value is contained in an array at the specified index.
  183. * This uses the triple equals sign so no type coercion will occur.
  184. * @param {Object} needle The value to look for.
  185. * @param {Array} haystack The array to search in.
  186. * @param {int} index The index at which the value should exist.
  187. * @param {String} message (Optional) The message to display if the assertion fails.
  188. * @method indexOf
  189. * @static
  190. */
  191. indexOf : function (needle, haystack, index, message) {
  192. YUITest.Assert._increment();
  193.  
  194. //try to find the value in the array
  195. for (var i=0; i < haystack.length; i++){
  196. if (haystack[i] === needle){
  197. if (index != i){
  198. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value exists at index " + i + " but should be at index " + index + "."));
  199. }
  200. return;
  201. }
  202. }
  203. //if it makes it here, it wasn't found at all
  204. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value doesn't exist in array [" + haystack + "]."));
  205. },
  206. /**
  207. * Asserts that the values in an array are equal, and in the same position,
  208. * as values in another array. This uses the double equals sign
  209. * so type coercion may occur. Note that the array objects themselves
  210. * need not be the same for this test to pass.
  211. * @param {Array} expected An array of the expected values.
  212. * @param {Array} actual Any array of the actual values.
  213. * @param {String} message (Optional) The message to display if the assertion fails.
  214. * @method itemsAreEqual
  215. * @static
  216. */
  217. itemsAreEqual : function (expected, actual,
  218. message) {
  219. YUITest.Assert._increment();
  220. //first make sure they're array-like (this can probably be improved)
  221. if (typeof expected != "object" || typeof actual != "object"){
  222. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value should be an array."));
  223. }
  224. //next check array length
  225. if (expected.length != actual.length){
  226. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length + "."));
  227. }
  228. //begin checking values
  229. for (var i=0; i < expected.length; i++){
  230. if (expected[i] != actual[i]){
  231. throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not equal."), expected[i], actual[i]);
  232. }
  233. }
  234. },
  235. /**
  236. * Asserts that the values in an array are equivalent, and in the same position,
  237. * as values in another array. This uses a function to determine if the values
  238. * are equivalent. Note that the array objects themselves
  239. * need not be the same for this test to pass.
  240. * @param {Array} expected An array of the expected values.
  241. * @param {Array} actual Any array of the actual values.
  242. * @param {Function} comparator A function that returns true if the values are equivalent
  243. * or false if not.
  244. * @param {String} message (Optional) The message to display if the assertion fails.
  245. * @return {Void}
  246. * @method itemsAreEquivalent
  247. * @static
  248. */
  249. itemsAreEquivalent : function (expected, actual,
  250. comparator, message) {
  251. YUITest.Assert._increment();
  252.  
  253. //make sure the comparator is valid
  254. if (typeof comparator != "function"){
  255. throw new TypeError("ArrayAssert.itemsAreEquivalent(): Third argument must be a function.");
  256. }
  257. //first check array length
  258. if (expected.length != actual.length){
  259. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length));
  260. }
  261. //begin checking values
  262. for (var i=0; i < expected.length; i++){
  263. if (!comparator(expected[i], actual[i])){
  264. throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not equivalent."), expected[i], actual[i]);
  265. }
  266. }
  267. },
  268. /**
  269. * Asserts that an array is empty.
  270. * @param {Array} actual The array to test.
  271. * @param {String} message (Optional) The message to display if the assertion fails.
  272. * @method isEmpty
  273. * @static
  274. */
  275. isEmpty : function (actual, message) {
  276. YUITest.Assert._increment();
  277. if (actual.length > 0){
  278. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should be empty."));
  279. }
  280. },
  281. /**
  282. * Asserts that an array is not empty.
  283. * @param {Array} actual The array to test.
  284. * @param {String} message (Optional) The message to display if the assertion fails.
  285. * @method isNotEmpty
  286. * @static
  287. */
  288. isNotEmpty : function (actual, message) {
  289. YUITest.Assert._increment();
  290. if (actual.length === 0){
  291. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should not be empty."));
  292. }
  293. },
  294. /**
  295. * Asserts that the values in an array are the same, and in the same position,
  296. * as values in another array. This uses the triple equals sign
  297. * so no type coercion will occur. Note that the array objects themselves
  298. * need not be the same for this test to pass.
  299. * @param {Array} expected An array of the expected values.
  300. * @param {Array} actual Any array of the actual values.
  301. * @param {String} message (Optional) The message to display if the assertion fails.
  302. * @method itemsAreSame
  303. * @static
  304. */
  305. itemsAreSame : function (expected, actual,
  306. message) {
  307. YUITest.Assert._increment();
  308.  
  309. //first check array length
  310. if (expected.length != actual.length){
  311. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length));
  312. }
  313. //begin checking values
  314. for (var i=0; i < expected.length; i++){
  315. if (expected[i] !== actual[i]){
  316. throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not the same."), expected[i], actual[i]);
  317. }
  318. }
  319. },
  320. /**
  321. * Asserts that the given value is contained in an array at the specified index,
  322. * starting from the back of the array.
  323. * This uses the triple equals sign so no type coercion will occur.
  324. * @param {Object} needle The value to look for.
  325. * @param {Array} haystack The array to search in.
  326. * @param {int} index The index at which the value should exist.
  327. * @param {String} message (Optional) The message to display if the assertion fails.
  328. * @method lastIndexOf
  329. * @static
  330. */
  331. lastIndexOf : function (needle, haystack, index, message) {
  332. //try to find the value in the array
  333. for (var i=haystack.length; i >= 0; i--){
  334. if (haystack[i] === needle){
  335. if (index != i){
  336. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value exists at index " + i + " but should be at index " + index + "."));
  337. }
  338. return;
  339. }
  340. }
  341. //if it makes it here, it wasn't found at all
  342. YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value doesn't exist in array."));
  343. }
  344. };
  345.