|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 * File Name: String/split-003.js |
|
9 * ECMA Section: 15.6.4.9 |
|
10 * Description: Based on ECMA 2 Draft 7 February 1999 |
|
11 * |
|
12 * Author: christine@netscape.com |
|
13 * Date: 19 February 1999 |
|
14 */ |
|
15 |
|
16 /* |
|
17 * Since regular expressions have been part of JavaScript since 1.2, there |
|
18 * are already tests for regular expressions in the js1_2/regexp folder. |
|
19 * |
|
20 * These new tests try to supplement the existing tests, and verify that |
|
21 * our implementation of RegExp conforms to the ECMA specification, but |
|
22 * does not try to be as exhaustive as in previous tests. |
|
23 * |
|
24 * The [,limit] argument to String.split is new, and not covered in any |
|
25 * existing tests. |
|
26 * |
|
27 * String.split cases are covered in ecma/String/15.5.4.8-*.js. |
|
28 * String.split where separator is a RegExp are in |
|
29 * js1_2/regexp/string_split.js |
|
30 * |
|
31 */ |
|
32 |
|
33 var SECTION = "ecma_2/String/split-003.js"; |
|
34 var VERSION = "ECMA_2"; |
|
35 var TITLE = "String.prototype.split( regexp, [,limit] )"; |
|
36 |
|
37 startTest(); |
|
38 |
|
39 // separator is a regexp |
|
40 // separator regexp value global setting is set |
|
41 // string is an empty string |
|
42 // if separator is an empty string, split each by character |
|
43 |
|
44 |
|
45 AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] ); |
|
46 |
|
47 AddSplitCases( "hello", /l/, "/l/", ["he","","o"] ); |
|
48 AddLimitedSplitCases( "hello", /l/, "/l/", 0, [] ); |
|
49 AddLimitedSplitCases( "hello", /l/, "/l/", 1, ["he"] ); |
|
50 AddLimitedSplitCases( "hello", /l/, "/l/", 2, ["he",""] ); |
|
51 AddLimitedSplitCases( "hello", /l/, "/l/", 3, ["he","","o"] ); |
|
52 AddLimitedSplitCases( "hello", /l/, "/l/", 4, ["he","","o"] ); |
|
53 AddLimitedSplitCases( "hello", /l/, "/l/", void 0, ["he","","o"] ); |
|
54 AddLimitedSplitCases( "hello", /l/, "/l/", "hi", [] ); |
|
55 AddLimitedSplitCases( "hello", /l/, "/l/", undefined, ["he","","o"] ); |
|
56 |
|
57 AddSplitCases( "hello", new RegExp, "new RegExp", ["h","e","l","l","o"] ); |
|
58 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 0, [] ); |
|
59 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 1, ["h"] ); |
|
60 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 2, ["h","e"] ); |
|
61 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 3, ["h","e","l"] ); |
|
62 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", 4, ["h","e","l","l"] ); |
|
63 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", void 0, ["h","e","l","l","o"] ); |
|
64 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", "hi", [] ); |
|
65 AddLimitedSplitCases( "hello", new RegExp, "new RegExp", undefined, ["h","e","l","l","o"] ); |
|
66 |
|
67 test(); |
|
68 |
|
69 function AddSplitCases( string, separator, str_sep, split_array ) { |
|
70 // verify that the result of split is an object of type Array |
|
71 AddTestCase( |
|
72 "( " + string + " ).split(" + str_sep +").constructor == Array", |
|
73 true, |
|
74 string.split(separator).constructor == Array ); |
|
75 |
|
76 // check the number of items in the array |
|
77 AddTestCase( |
|
78 "( " + string + " ).split(" + str_sep +").length", |
|
79 split_array.length, |
|
80 string.split(separator).length ); |
|
81 |
|
82 // check the value of each array item |
|
83 var limit = (split_array.length > string.split(separator).length ) |
|
84 ? split_array.length : string.split(separator).length; |
|
85 |
|
86 for ( var matches = 0; matches < split_array.length; matches++ ) { |
|
87 AddTestCase( |
|
88 "( " + string + " ).split(" + str_sep +")[" + matches +"]", |
|
89 split_array[matches], |
|
90 string.split( separator )[matches] ); |
|
91 } |
|
92 } |
|
93 |
|
94 function AddLimitedSplitCases( |
|
95 string, separator, str_sep, limit, split_array ) { |
|
96 |
|
97 // verify that the result of split is an object of type Array |
|
98 |
|
99 AddTestCase( |
|
100 "( " + string + " ).split(" + str_sep +", " + limit + |
|
101 " ).constructor == Array", |
|
102 true, |
|
103 string.split(separator, limit).constructor == Array ); |
|
104 |
|
105 // check the length of the array |
|
106 |
|
107 AddTestCase( |
|
108 "( " + string + " ).split(" + str_sep +", " + limit + " ).length", |
|
109 split_array.length, |
|
110 string.split(separator, limit).length ); |
|
111 |
|
112 // check the value of each array item |
|
113 |
|
114 var slimit = (split_array.length > string.split(separator).length ) |
|
115 ? split_array.length : string.split(separator, limit).length; |
|
116 |
|
117 for ( var matches = 0; matches < slimit; matches++ ) { |
|
118 AddTestCase( |
|
119 "( " + string + " ).split(" + str_sep +", " + limit + " )[" + matches +"]", |
|
120 split_array[matches], |
|
121 string.split( separator, limit )[matches] ); |
|
122 } |
|
123 } |