|
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-001.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-001.js"; |
|
34 var VERSION = "ECMA_2"; |
|
35 var TITLE = "String.prototype.split( regexp, [,limit] )"; |
|
36 |
|
37 startTest(); |
|
38 |
|
39 // the separator is not supplied |
|
40 // separator is undefined |
|
41 // separator is an empty string |
|
42 |
|
43 AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] ); |
|
44 AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] ); |
|
45 |
|
46 // separartor is a regexp |
|
47 // separator regexp value global setting is set |
|
48 // string is an empty string |
|
49 // if separator is an empty string, split each by character |
|
50 |
|
51 // this is not a String object |
|
52 |
|
53 // limit is not a number |
|
54 // limit is undefined |
|
55 // limit is larger than 2^32-1 |
|
56 // limit is a negative number |
|
57 |
|
58 test(); |
|
59 |
|
60 function AddSplitCases( string, separator, str_sep, split_array ) { |
|
61 |
|
62 // verify that the result of split is an object of type Array |
|
63 AddTestCase( |
|
64 "( " + string + " ).split(" + str_sep +").constructor == Array", |
|
65 true, |
|
66 string.split(separator).constructor == Array ); |
|
67 |
|
68 // check the number of items in the array |
|
69 AddTestCase( |
|
70 "( " + string + " ).split(" + str_sep +").length", |
|
71 split_array.length, |
|
72 string.split(separator).length ); |
|
73 |
|
74 // check the value of each array item |
|
75 var limit = (split_array.length > string.split(separator).length ) |
|
76 ? split_array.length : string.split(separator).length; |
|
77 |
|
78 for ( var matches = 0; matches < split_array.length; matches++ ) { |
|
79 AddTestCase( |
|
80 "( " + string + " ).split(" + str_sep +")[" + matches +"]", |
|
81 split_array[matches], |
|
82 string.split( separator )[matches] ); |
|
83 } |
|
84 } |
|
85 |
|
86 function AddLimitedSplitCases( |
|
87 string, separator, str_sep, limit, str_limit, split_array ) { |
|
88 |
|
89 // verify that the result of split is an object of type Array |
|
90 |
|
91 AddTestCase( |
|
92 "( " + string + " ).split(" + str_sep +", " + str_limit + |
|
93 " ).constructor == Array", |
|
94 true, |
|
95 string.split(separator, limit).constructor == Array ); |
|
96 |
|
97 // check the length of the array |
|
98 |
|
99 AddTestCase( |
|
100 "( " + string + " ).split(" + str_sep +", " + str_limit + " ).length", |
|
101 length, |
|
102 string.split(separator).length ); |
|
103 |
|
104 // check the value of each array item |
|
105 |
|
106 for ( var matches = 0; matches < split_array.length; matches++ ) { |
|
107 AddTestCase( |
|
108 "( " + string + " ).split(" + str_sep +", " + str_limit + " )[" + matches +"]", |
|
109 split_array[matches], |
|
110 string.split( separator )[matches] ); |
|
111 } |
|
112 } |