API Docs for: 3.8.0
Show:

File: test/js/ArrayAssert.js


  1. /**
  2.  * The ArrayAssert object provides functions to test JavaScript array objects
  3.  * for a variety of cases.
  4.  * @namespace Test
  5.  * @module test
  6.  * @class ArrayAssert
  7.  * @static
  8.  */
  9.  
  10. YUITest.ArrayAssert = {

  11.     //=========================================================================
  12.     // Private methods
  13.     //=========================================================================
  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.     /**
  38.      * Simple some() implementation for an array. Defers to native
  39.      * if available.
  40.      * @param {Array} haystack The array to search.
  41.      * @param {Function} matcher The function to run on each value.
  42.      * @return {Boolean} True if any value, when run through the matcher,
  43.      *      returns true.
  44.      * @method _some
  45.      * @private
  46.      */
  47.     _some: function(haystack, matcher){
  48.         if (haystack.some){
  49.             return haystack.some(matcher);
  50.         } else {
  51.             for (var i=0; i < haystack.length; i++){
  52.                 if (matcher(haystack[i])){
  53.                     return true;
  54.                 }
  55.             }
  56.             return false;
  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.        
  71.         YUITest.Assert._increment();              

  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.      * Asserts that a set of values are present in an array. This uses the triple equals
  78.      * sign so no type coercion may occur. For this assertion to pass, all values must
  79.      * be found.
  80.      * @param {Object[]} needles An array of values that are expected in the array.
  81.      * @param {Array} haystack An array of values to check.
  82.      * @param {String} message (Optional) The message to display if the assertion fails.
  83.      * @method containsItems
  84.      * @static
  85.      */
  86.     containsItems : function (needles, haystack,
  87.                            message) {
  88.         YUITest.Assert._increment();              

  89.         //begin checking values
  90.         for (var i=0; i < needles.length; i++){
  91.             if (this._indexOf(haystack, needles[i]) == -1){
  92.                 YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value " + needles[i] + " (" + (typeof needles[i]) + ") not found in array [" + haystack + "]."));
  93.             }
  94.         }
  95.     },

  96.     /**
  97.      * Asserts that a value matching some condition is present in an array. This uses
  98.      * a function to determine a match.
  99.      * @param {Function} matcher A function that returns true if the items matches or false if not.
  100.      * @param {Array} haystack An array of values.
  101.      * @param {String} message (Optional) The message to display if the assertion fails.
  102.      * @method containsMatch
  103.      * @static
  104.      */
  105.     containsMatch : function (matcher, haystack,
  106.                            message) {
  107.        
  108.         YUITest.Assert._increment();              
  109.         //check for valid matcher
  110.         if (typeof matcher != "function"){
  111.             throw new TypeError("ArrayAssert.containsMatch(): First argument must be a function.");
  112.         }
  113.        
  114.         if (!this._some(haystack, matcher)){
  115.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "No match found in array [" + haystack + "]."));
  116.         }
  117.     },

  118.     /**
  119.      * Asserts that a value is not present in an array. This uses the triple equals
  120.      * Asserts that a value is not present in an array. This uses the triple equals
  121.      * sign so no type coercion may occur.
  122.      * @param {Object} needle The value that is expected in the array.
  123.      * @param {Array} haystack An array of values.
  124.      * @param {String} message (Optional) The message to display if the assertion fails.
  125.      * @method doesNotContain
  126.      * @static
  127.      */
  128.     doesNotContain : function (needle, haystack,
  129.                            message) {
  130.        
  131.         YUITest.Assert._increment();              

  132.         if (this._indexOf(haystack, needle) > -1){
  133.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
  134.         }
  135.     },

  136.     /**
  137.      * Asserts that a set of values are not present in an array. This uses the triple equals
  138.      * sign so no type coercion may occur. For this assertion to pass, all values must
  139.      * not be found.
  140.      * @param {Object[]} needles An array of values that are not expected in the array.
  141.      * @param {Array} haystack An array of values to check.
  142.      * @param {String} message (Optional) The message to display if the assertion fails.
  143.      * @method doesNotContainItems
  144.      * @static
  145.      */
  146.     doesNotContainItems : function (needles, haystack,
  147.                            message) {

  148.         YUITest.Assert._increment();              

  149.         for (var i=0; i < needles.length; i++){
  150.             if (this._indexOf(haystack, needles[i]) > -1){
  151.                 YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
  152.             }
  153.         }

  154.     },
  155.        
  156.     /**
  157.      * Asserts that no values matching a condition are present in an array. This uses
  158.      * a function to determine a match.
  159.      * @param {Function} matcher A function that returns true if the item matches or false if not.
  160.      * @param {Array} haystack An array of values.
  161.      * @param {String} message (Optional) The message to display if the assertion fails.
  162.      * @method doesNotContainMatch
  163.      * @static
  164.      */
  165.     doesNotContainMatch : function (matcher, haystack,
  166.                            message) {
  167.        
  168.         YUITest.Assert._increment();    
  169.      
  170.         //check for valid matcher
  171.         if (typeof matcher != "function"){
  172.             throw new TypeError("ArrayAssert.doesNotContainMatch(): First argument must be a function.");
  173.         }
  174.        
  175.         if (this._some(haystack, matcher)){
  176.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value found in array [" + haystack + "]."));
  177.         }
  178.     },
  179.        
  180.     /**
  181.      * Asserts that the given value is contained in an array at the specified index.
  182.      * This uses the triple equals sign so no type coercion will occur.
  183.      * @param {Object} needle The value to look for.
  184.      * @param {Array} haystack The array to search in.
  185.      * @param {int} index The index at which the value should exist.
  186.      * @param {String} message (Optional) The message to display if the assertion fails.
  187.      * @method indexOf
  188.      * @static
  189.      */
  190.     indexOf : function (needle, haystack, index, message) {
  191.    
  192.         YUITest.Assert._increment();    

  193.         //try to find the value in the array
  194.         for (var i=0; i < haystack.length; i++){
  195.             if (haystack[i] === needle){
  196.                 if (index != i){
  197.                     YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value exists at index " + i + " but should be at index " + index + "."));                    
  198.                 }
  199.                 return;
  200.             }
  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.     /**
  208.      * Asserts that the values in an array are equal, and in the same position,
  209.      * as values in another array. This uses the double equals sign
  210.      * so type coercion may occur. Note that the array objects themselves
  211.      * need not be the same for this test to pass.
  212.      * @param {Array} expected An array of the expected values.
  213.      * @param {Array} actual Any array of the actual values.
  214.      * @param {String} message (Optional) The message to display if the assertion fails.
  215.      * @method itemsAreEqual
  216.      * @static
  217.      */
  218.     itemsAreEqual : function (expected, actual,
  219.                            message) {
  220.        
  221.         YUITest.Assert._increment();    
  222.        
  223.         //first make sure they're array-like (this can probably be improved)
  224.         if (typeof expected != "object" || typeof actual != "object"){
  225.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value should be an array."));
  226.         }
  227.        
  228.         //next check array length
  229.         if (expected.length != actual.length){
  230.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length + "."));
  231.         }
  232.        
  233.         //begin checking values
  234.         for (var i=0; i < expected.length; i++){
  235.             if (expected[i] != actual[i]){
  236.                 throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not equal."), expected[i], actual[i]);
  237.             }
  238.         }
  239.     },
  240.    
  241.     /**
  242.      * Asserts that the values in an array are equivalent, and in the same position,
  243.      * as values in another array. This uses a function to determine if the values
  244.      * are equivalent. Note that the array objects themselves
  245.      * need not be the same for this test to pass.
  246.      * @param {Array} expected An array of the expected values.
  247.      * @param {Array} actual Any array of the actual values.
  248.      * @param {Function} comparator A function that returns true if the values are equivalent
  249.      *      or false if not.
  250.      * @param {String} message (Optional) The message to display if the assertion fails.
  251.      * @return {Void}
  252.      * @method itemsAreEquivalent
  253.      * @static
  254.      */
  255.     itemsAreEquivalent : function (expected, actual,
  256.                            comparator, message) {
  257.        
  258.         YUITest.Assert._increment();    

  259.         //make sure the comparator is valid
  260.         if (typeof comparator != "function"){
  261.             throw new TypeError("ArrayAssert.itemsAreEquivalent(): Third argument must be a function.");
  262.         }
  263.        
  264.         //first check array length
  265.         if (expected.length != actual.length){
  266.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length));
  267.         }
  268.        
  269.         //begin checking values
  270.         for (var i=0; i < expected.length; i++){
  271.             if (!comparator(expected[i], actual[i])){
  272.                 throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not equivalent."), expected[i], actual[i]);
  273.             }
  274.         }
  275.     },
  276.    
  277.     /**
  278.      * Asserts that an array is empty.
  279.      * @param {Array} actual The array to test.
  280.      * @param {String} message (Optional) The message to display if the assertion fails.
  281.      * @method isEmpty
  282.      * @static
  283.      */
  284.     isEmpty : function (actual, message) {        
  285.         YUITest.Assert._increment();    
  286.         if (actual.length > 0){
  287.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should be empty."));
  288.         }
  289.     },    
  290.    
  291.     /**
  292.      * Asserts that an array is not empty.
  293.      * @param {Array} actual The array to test.
  294.      * @param {String} message (Optional) The message to display if the assertion fails.
  295.      * @method isNotEmpty
  296.      * @static
  297.      */
  298.     isNotEmpty : function (actual, message) {        
  299.         YUITest.Assert._increment();    
  300.         if (actual.length === 0){
  301.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should not be empty."));
  302.         }
  303.     },    
  304.    
  305.     /**
  306.      * Asserts that the values in an array are the same, and in the same position,
  307.      * as values in another array. This uses the triple equals sign
  308.      * so no type coercion will occur. Note that the array objects themselves
  309.      * need not be the same for this test to pass.
  310.      * @param {Array} expected An array of the expected values.
  311.      * @param {Array} actual Any array of the actual values.
  312.      * @param {String} message (Optional) The message to display if the assertion fails.
  313.      * @method itemsAreSame
  314.      * @static
  315.      */
  316.     itemsAreSame : function (expected, actual,
  317.                           message) {
  318.        
  319.         YUITest.Assert._increment();    

  320.         //first check array length
  321.         if (expected.length != actual.length){
  322.             YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Array should have a length of " + expected.length + " but has a length of " + actual.length));
  323.         }
  324.                    
  325.         //begin checking values
  326.         for (var i=0; i < expected.length; i++){
  327.             if (expected[i] !== actual[i]){
  328.                 throw new YUITest.ComparisonFailure(YUITest.Assert._formatMessage(message, "Values in position " + i + " are not the same."), expected[i], actual[i]);
  329.             }
  330.         }
  331.     },
  332.    
  333.     /**
  334.      * Asserts that the given value is contained in an array at the specified index,
  335.      * starting from the back of the array.
  336.      * This uses the triple equals sign so no type coercion will occur.
  337.      * @param {Object} needle The value to look for.
  338.      * @param {Array} haystack The array to search in.
  339.      * @param {int} index The index at which the value should exist.
  340.      * @param {String} message (Optional) The message to display if the assertion fails.
  341.      * @method lastIndexOf
  342.      * @static
  343.      */
  344.     lastIndexOf : function (needle, haystack, index, message) {
  345.    
  346.         //try to find the value in the array
  347.         for (var i=haystack.length; i >= 0; i--){
  348.             if (haystack[i] === needle){
  349.                 if (index != i){
  350.                     YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value exists at index " + i + " but should be at index " + index + "."));                    
  351.                 }
  352.                 return;
  353.             }
  354.         }
  355.        
  356.         //if it makes it here, it wasn't found at all
  357.         YUITest.Assert.fail(YUITest.Assert._formatMessage(message, "Value doesn't exist in array."));        
  358.     }
  359.    
  360. };

  361.