]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* The contents of this file are subject to the Netscape Public |
2 | * License Version 1.1 (the "License"); you may not use this file | |
3 | * except in compliance with the License. You may obtain a copy of | |
4 | * the License at http://www.mozilla.org/NPL/ | |
5 | * | |
6 | * Software distributed under the License is distributed on an "AS | |
7 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
8 | * implied. See the License for the specific language governing | |
9 | * rights and limitations under the License. | |
10 | * | |
11 | * The Original Code is Mozilla Communicator client code, released March | |
12 | * 31, 1998. | |
13 | * | |
14 | * The Initial Developer of the Original Code is Netscape Communications | |
15 | * Corporation. Portions created by Netscape are | |
16 | * Copyright (C) 1998 Netscape Communications Corporation. All | |
17 | * Rights Reserved. | |
18 | * | |
19 | * Contributor(s): | |
20 | * | |
21 | */ | |
22 | /** | |
23 | File Name: 15.5.4.8-2.js | |
24 | ECMA Section: 15.5.4.8 String.prototype.split( separator ) | |
25 | Description: | |
26 | ||
27 | Returns an Array object into which substrings of the result of converting | |
28 | this object to a string have been stored. The substrings are determined by | |
29 | searching from left to right for occurrences of the given separator; these | |
30 | occurrences are not part of any substring in the returned array, but serve | |
31 | to divide up this string value. The separator may be a string of any length. | |
32 | ||
33 | As a special case, if the separator is the empty string, the string is split | |
34 | up into individual characters; the length of the result array equals the | |
35 | length of the string, and each substring contains one character. | |
36 | ||
37 | If the separator is not supplied, then the result array contains just one | |
38 | string, which is the string. | |
39 | ||
40 | When the split method is called with one argument separator, the following steps are taken: | |
41 | ||
42 | 1. Call ToString, giving it the this value as its argument. | |
43 | 2. Create a new Array object of length 0 and call it A. | |
44 | 3. If separator is not supplied, call the [[Put]] method of A with 0 and | |
45 | Result(1) as arguments, and then return A. | |
46 | 4. Call ToString(separator). | |
47 | 5. Compute the number of characters in Result(1). | |
48 | 6. Compute the number of characters in the string that is Result(4). | |
49 | 7. Let p be 0. | |
50 | 8. If Result(6) is zero (the separator string is empty), go to step 17. | |
51 | 9. Compute the smallest possible integer k not smaller than p such that | |
52 | k+Result(6) is not greater than Result(5), and for all nonnegative | |
53 | integers j less than Result(6), the character at position k+j of | |
54 | Result(1) is the same as the character at position j of Result(2); | |
55 | but if there is no such integer k, then go to step 14. | |
56 | 10. Compute a string value equal to the substring of Result(1), consisting | |
57 | of the characters at positions p through k1, inclusive. | |
58 | 11. Call the [[Put]] method of A with A.length and Result(10) as arguments. | |
59 | 12. Let p be k+Result(6). | |
60 | 13. Go to step 9. | |
61 | 14. Compute a string value equal to the substring of Result(1), consisting | |
62 | of the characters from position p to the end of Result(1). | |
63 | 15. Call the [[Put]] method of A with A.length and Result(14) as arguments. | |
64 | 16. Return A. | |
65 | 17. If p equals Result(5), return A. | |
66 | 18. Compute a string value equal to the substring of Result(1), consisting of | |
67 | the single character at position p. | |
68 | 19. Call the [[Put]] method of A with A.length and Result(18) as arguments. | |
69 | 20. Increase p by 1. | |
70 | 21. Go to step 17. | |
71 | ||
72 | Note that the split function is intentionally generic; it does not require that its this value be a String | |
73 | object. Therefore it can be transferred to other kinds of objects for use as a method. | |
74 | ||
75 | Author: christine@netscape.com | |
76 | Date: 12 november 1997 | |
77 | */ | |
78 | ||
79 | var SECTION = "15.5.4.8-2"; | |
80 | var VERSION = "ECMA_1"; | |
81 | startTest(); | |
82 | var TITLE = "String.prototype.split"; | |
83 | ||
84 | writeHeaderToLog( SECTION + " "+ TITLE); | |
85 | ||
86 | var testcases = getTestCases(); | |
87 | test(); | |
88 | ||
89 | function getTestCases() { | |
90 | var array = new Array(); | |
91 | var item = 0; | |
92 | ||
93 | // case where separator is the empty string. | |
94 | ||
95 | var TEST_STRING = "this is a string object"; | |
96 | ||
97 | array[item++] = new TestCase( SECTION, | |
98 | "var s = new String( "+ TEST_STRING +" ); s.split('').length", | |
99 | TEST_STRING.length, | |
100 | eval("var s = new String( TEST_STRING ); s.split('').length") ); | |
101 | ||
102 | for ( var i = 0; i < TEST_STRING.length; i++ ) { | |
103 | ||
104 | array[item++] = new TestCase( SECTION, | |
105 | "var s = new String( "+TEST_STRING+" ); s.split('')["+i+"]", | |
106 | TEST_STRING.charAt(i), | |
107 | eval("var s = new String( TEST_STRING ); s.split('')["+i+"]") ); | |
108 | } | |
109 | ||
110 | // case where the value of the separator is undefined. in this case. the value of the separator | |
111 | // should be ToString( separator ), or "undefined". | |
112 | ||
113 | var TEST_STRING = "thisundefinedisundefinedaundefinedstringundefinedobject"; | |
114 | var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); | |
115 | ||
116 | array[item++] = new TestCase( SECTION, | |
117 | "var s = new String( "+ TEST_STRING +" ); s.split(void 0).length", | |
118 | EXPECT_STRING.length, | |
119 | eval("var s = new String( TEST_STRING ); s.split(void 0).length") ); | |
120 | ||
121 | for ( var i = 0; i < EXPECT_STRING.length; i++ ) { | |
122 | array[item++] = new TestCase( SECTION, | |
123 | "var s = new String( "+TEST_STRING+" ); s.split(void 0)["+i+"]", | |
124 | EXPECT_STRING[i], | |
125 | eval("var s = new String( TEST_STRING ); s.split(void 0)["+i+"]") ); | |
126 | } | |
127 | ||
128 | // case where the value of the separator is null. in this case the value of the separator is "null". | |
129 | TEST_STRING = "thisnullisnullanullstringnullobject"; | |
130 | var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); | |
131 | ||
132 | array[item++] = new TestCase( SECTION, | |
133 | "var s = new String( "+ TEST_STRING +" ); s.split(null).length", | |
134 | EXPECT_STRING.length, | |
135 | eval("var s = new String( TEST_STRING ); s.split(null).length") ); | |
136 | ||
137 | for ( var i = 0; i < EXPECT_STRING.length; i++ ) { | |
138 | array[item++] = new TestCase( SECTION, | |
139 | "var s = new String( "+TEST_STRING+" ); s.split(null)["+i+"]", | |
140 | EXPECT_STRING[i], | |
141 | eval("var s = new String( TEST_STRING ); s.split(null)["+i+"]") ); | |
142 | } | |
143 | ||
144 | // case where the value of the separator is a boolean. | |
145 | TEST_STRING = "thistrueistrueatruestringtrueobject"; | |
146 | var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); | |
147 | ||
148 | array[item++] = new TestCase( SECTION, | |
149 | "var s = new String( "+ TEST_STRING +" ); s.split(true).length", | |
150 | EXPECT_STRING.length, | |
151 | eval("var s = new String( TEST_STRING ); s.split(true).length") ); | |
152 | ||
153 | for ( var i = 0; i < EXPECT_STRING.length; i++ ) { | |
154 | array[item++] = new TestCase( SECTION, | |
155 | "var s = new String( "+TEST_STRING+" ); s.split(true)["+i+"]", | |
156 | EXPECT_STRING[i], | |
157 | eval("var s = new String( TEST_STRING ); s.split(true)["+i+"]") ); | |
158 | } | |
159 | ||
160 | // case where the value of the separator is a number | |
161 | TEST_STRING = "this123is123a123string123object"; | |
162 | var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); | |
163 | ||
164 | array[item++] = new TestCase( SECTION, | |
165 | "var s = new String( "+ TEST_STRING +" ); s.split(123).length", | |
166 | EXPECT_STRING.length, | |
167 | eval("var s = new String( TEST_STRING ); s.split(123).length") ); | |
168 | ||
169 | for ( var i = 0; i < EXPECT_STRING.length; i++ ) { | |
170 | array[item++] = new TestCase( SECTION, | |
171 | "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]", | |
172 | EXPECT_STRING[i], | |
173 | eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") ); | |
174 | } | |
175 | ||
176 | ||
177 | // case where the value of the separator is a number | |
178 | TEST_STRING = "this123is123a123string123object"; | |
179 | var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" ); | |
180 | ||
181 | array[item++] = new TestCase( SECTION, | |
182 | "var s = new String( "+ TEST_STRING +" ); s.split(123).length", | |
183 | EXPECT_STRING.length, | |
184 | eval("var s = new String( TEST_STRING ); s.split(123).length") ); | |
185 | ||
186 | for ( var i = 0; i < EXPECT_STRING.length; i++ ) { | |
187 | array[item++] = new TestCase( SECTION, | |
188 | "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]", | |
189 | EXPECT_STRING[i], | |
190 | eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") ); | |
191 | } | |
192 | ||
193 | // case where the separator is not in the string | |
194 | TEST_STRING = "this is a string"; | |
195 | EXPECT_STRING = new Array( "this is a string" ); | |
196 | ||
197 | array[item++] = new TestCase( SECTION, | |
198 | "var s = new String( " + TEST_STRING + " ); s.split(':').length", | |
199 | 1, | |
200 | eval("var s = new String( TEST_STRING ); s.split(':').length") ); | |
201 | ||
202 | array[item++] = new TestCase( SECTION, | |
203 | "var s = new String( " + TEST_STRING + " ); s.split(':')[0]", | |
204 | TEST_STRING, | |
205 | eval("var s = new String( TEST_STRING ); s.split(':')[0]") ); | |
206 | ||
207 | // case where part but not all of separator is in the string. | |
208 | TEST_STRING = "this is a string"; | |
209 | EXPECT_STRING = new Array( "this is a string" ); | |
210 | array[item++] = new TestCase( SECTION, | |
211 | "var s = new String( " + TEST_STRING + " ); s.split('strings').length", | |
212 | 1, | |
213 | eval("var s = new String( TEST_STRING ); s.split('strings').length") ); | |
214 | ||
215 | array[item++] = new TestCase( SECTION, | |
216 | "var s = new String( " + TEST_STRING + " ); s.split('strings')[0]", | |
217 | TEST_STRING, | |
218 | eval("var s = new String( TEST_STRING ); s.split('strings')[0]") ); | |
219 | ||
220 | // case where the separator is at the end of the string | |
221 | TEST_STRING = "this is a string"; | |
222 | EXPECT_STRING = new Array( "this is a " ); | |
223 | array[item++] = new TestCase( SECTION, | |
224 | "var s = new String( " + TEST_STRING + " ); s.split('string').length", | |
225 | 2, | |
226 | eval("var s = new String( TEST_STRING ); s.split('string').length") ); | |
227 | ||
228 | for ( var i = 0; i < EXPECT_STRING.length; i++ ) { | |
229 | array[item++] = new TestCase( SECTION, | |
230 | "var s = new String( "+TEST_STRING+" ); s.split('string')["+i+"]", | |
231 | EXPECT_STRING[i], | |
232 | eval("var s = new String( TEST_STRING ); s.split('string')["+i+"]") ); | |
233 | } | |
234 | return array; | |
235 | } | |
236 | function test() { | |
237 | for ( tc=0; tc < testcases.length; tc++ ) { | |
238 | testcases[tc].passed = writeTestCaseResult( | |
239 | testcases[tc].expect, | |
240 | testcases[tc].actual, | |
241 | testcases[tc].description +" = "+ | |
242 | testcases[tc].actual ); | |
243 | ||
244 | testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; | |
245 | } | |
246 | stopTest(); | |
247 | return ( testcases ); | |
248 | } |