Home | History | Annotate | Download | only in String
      1 /**
      2  *  File Name:          String/split-001.js
      3  *  ECMA Section:       15.6.4.9
      4  *  Description:        Based on ECMA 2 Draft 7 February 1999
      5  *
      6  *  Author:             christine (at) netscape.com
      7  *  Date:               19 February 1999
      8  */
      9 
     10 /*
     11  * Since regular expressions have been part of JavaScript since 1.2, there
     12  * are already tests for regular expressions in the js1_2/regexp folder.
     13  *
     14  * These new tests try to supplement the existing tests, and verify that
     15  * our implementation of RegExp conforms to the ECMA specification, but
     16  * does not try to be as exhaustive as in previous tests.
     17  *
     18  * The [,limit] argument to String.split is new, and not covered in any
     19  * existing tests.
     20  *
     21  * String.split cases are covered in ecma/String/15.5.4.8-*.js.
     22  * String.split where separator is a RegExp are in
     23  * js1_2/regexp/string_split.js
     24  *
     25  */
     26 
     27     var SECTION = "ecma_2/String/split-001.js";
     28     var VERSION = "ECMA_2";
     29     var TITLE   = "String.prototype.split( regexp, [,limit] )";
     30 
     31     startTest();
     32 
     33     // the separator is not supplied
     34     // separator is undefined
     35     // separator is an empty string
     36 
     37     AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
     38     AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
     39 
     40     // separartor is a regexp
     41     // separator regexp value global setting is set
     42     // string is an empty string
     43     // if separator is an empty string, split each by character
     44 
     45     // this is not a String object
     46 
     47     // limit is not a number
     48     // limit is undefined
     49     // limit is larger than 2^32-1
     50     // limit is a negative number
     51 
     52     test();
     53 
     54 function AddSplitCases( string, separator, str_sep, split_array ) {
     55 
     56     // verify that the result of split is an object of type Array
     57     AddTestCase(
     58         "( " + string  + " ).split(" + str_sep +").constructor == Array",
     59         true,
     60         string.split(separator).constructor == Array );
     61 
     62     // check the number of items in the array
     63     AddTestCase(
     64         "( " + string  + " ).split(" + str_sep +").length",
     65         split_array.length,
     66         string.split(separator).length );
     67 
     68     // check the value of each array item
     69     var limit = (split_array.length > string.split(separator).length )
     70         ? split_array.length : string.split(separator).length;
     71 
     72     for ( var matches = 0; matches < split_array.length; matches++ ) {
     73         AddTestCase(
     74             "( " + string + " ).split(" + str_sep +")[" + matches +"]",
     75             split_array[matches],
     76             string.split( separator )[matches] );
     77     }
     78 }
     79 
     80 function AddLimitedSplitCases(
     81     string, separator, str_sep, limit, str_limit, split_array ) {
     82 
     83     // verify that the result of split is an object of type Array
     84 
     85     AddTestCase(
     86         "( " + string  + " ).split(" + str_sep +", " + str_limit +
     87             " ).constructor == Array",
     88         true,
     89         string.split(separator, limit).constructor == Array );
     90 
     91     // check the length of the array
     92 
     93     AddTestCase(
     94         "( " + string + " ).split(" + str_sep  +", " + str_limit + " ).length",
     95         length,
     96         string.split(separator).length );
     97 
     98     // check the value of each array item
     99 
    100     for ( var matches = 0; matches < split_array.length; matches++ ) {
    101         AddTestCase(
    102             "( " + string + " ).split(" + str_sep +", " + str_limit + " )[" + matches +"]",
    103             split_array[matches],
    104             string.split( separator )[matches] );
    105     }
    106 }
    107