1/**
2 *  File Name:          String/split-003.js
3 *  ECMA Section:       15.6.4.9
4 *  Description:        Based on ECMA 2 Draft 7 February 1999
5 *
6 *  Author:             christine@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-003.js";
28    var VERSION = "ECMA_2";
29    var TITLE   = "String.prototype.split( regexp, [,limit] )";
30
31    startTest();
32
33    // separartor is a regexp
34    // separator regexp value global setting is set
35    // string is an empty string
36    // if separator is an empty string, split each by character
37
38
39    AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] );
40
41    AddSplitCases( "hello", /l/, "/l/", ["he","","o"] );
42    AddLimitedSplitCases( "hello", /l/, "/l/", 0, [] );
43    AddLimitedSplitCases( "hello", /l/, "/l/", 1, ["he"] );
44    AddLimitedSplitCases( "hello", /l/, "/l/", 2, ["he",""] );
45    AddLimitedSplitCases( "hello", /l/, "/l/", 3, ["he","","o"] );
46    AddLimitedSplitCases( "hello", /l/, "/l/", 4, ["he","","o"] );
47    AddLimitedSplitCases( "hello", /l/, "/l/", void 0, ["he","","o"] );
48    AddLimitedSplitCases( "hello", /l/, "/l/", "hi", [] );
49    AddLimitedSplitCases( "hello", /l/, "/l/", undefined, ["he","","o"] );
50
51    AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] );
52    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 0, [] );
53    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 1, ["h"] );
54    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 2, ["h","e"] );
55    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 3, ["h","e","l"] );
56    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 4, ["h","e","l","l"] );
57    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", void 0,  ["h","e","l","l","o"] );
58    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", "hi",  [] );
59    AddLimitedSplitCases( "hello", new RegExp, "new RegExp", undefined,  ["h","e","l","l","o"] );
60
61    test();
62
63function AddSplitCases( string, separator, str_sep, split_array ) {
64    // verify that the result of split is an object of type Array
65    AddTestCase(
66        "( " + string  + " ).split(" + str_sep +").constructor == Array",
67        true,
68        string.split(separator).constructor == Array );
69
70    // check the number of items in the array
71    AddTestCase(
72        "( " + string  + " ).split(" + str_sep +").length",
73        split_array.length,
74        string.split(separator).length );
75
76    // check the value of each array item
77    var limit = (split_array.length > string.split(separator).length )
78        ? split_array.length : string.split(separator).length;
79
80    for ( var matches = 0; matches < split_array.length; matches++ ) {
81        AddTestCase(
82            "( " + string + " ).split(" + str_sep +")[" + matches +"]",
83            split_array[matches],
84            string.split( separator )[matches] );
85    }
86}
87
88function AddLimitedSplitCases(
89    string, separator, str_sep, limit, split_array ) {
90
91    // verify that the result of split is an object of type Array
92
93    AddTestCase(
94        "( " + string  + " ).split(" + str_sep +", " + limit +
95            " ).constructor == Array",
96        true,
97        string.split(separator, limit).constructor == Array );
98
99    // check the length of the array
100
101    AddTestCase(
102        "( " + string + " ).split(" + str_sep  +", " + limit + " ).length",
103        split_array.length,
104        string.split(separator, limit).length );
105
106    // check the value of each array item
107
108    var slimit = (split_array.length > string.split(separator).length )
109        ? split_array.length : string.split(separator, limit).length;
110
111    for ( var matches = 0; matches < slimit; matches++ ) {
112        AddTestCase(
113            "( " + string + " ).split(" + str_sep +", " + limit + " )[" + matches +"]",
114            split_array[matches],
115            string.split( separator, limit )[matches] );
116    }
117}
118