OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // Flags: --harmony-strings | |
29 | |
30 assertEquals(1, String.prototype.startsWith.length); | |
31 | |
32 var testString = "Hello World"; | |
33 assertTrue(testString.startsWith("")); | |
34 assertTrue(testString.startsWith("Hello")); | |
35 assertFalse(testString.startsWith("hello")); | |
36 assertFalse(testString.startsWith("Hello World!")); | |
37 assertFalse(testString.startsWith(null)); | |
38 assertFalse(testString.startsWith(undefined)); | |
39 | |
40 assertTrue("null".startsWith(null)); | |
41 assertTrue("undefined".startsWith(undefined)); | |
42 | |
43 var georgianUnicodeString = "\u10D0\u10D1\u10D2\u10D3\u10D4\u10D5\u10D6\u10D7"; | |
44 assertTrue(georgianUnicodeString.startsWith(georgianUnicodeString)); | |
45 assertTrue(georgianUnicodeString.startsWith("\u10D0\u10D1\u10D2")); | |
46 assertFalse(georgianUnicodeString.startsWith("\u10D8")); | |
47 | |
48 assertThrows("String.prototype.startsWith.call(null, 'test')", TypeError); | |
49 assertThrows("String.prototype.startsWith.call(null, null)", TypeError); | |
50 assertThrows("String.prototype.startsWith.call(undefined, undefined)", TypeError
); | |
51 | |
52 assertThrows("String.prototype.startsWith.apply(null, ['test'])", TypeError); | |
53 assertThrows("String.prototype.startsWith.apply(null, [null])", TypeError); | |
54 assertThrows("String.prototype.startsWith.apply(undefined, [undefined])", TypeEr
ror); | |
55 | |
56 var TEST_INPUT = [{ | |
57 msg: "Empty string", val: "" | |
58 }, { | |
59 msg: "Number 1234.34", val: 1234.34 | |
60 }, { | |
61 msg: "Integer number 0", val: 0 | |
62 }, { | |
63 msg: "Negative number -1", val: -1 | |
64 }, { | |
65 msg: "Boolean true", val: true | |
66 }, { | |
67 msg: "Boolean false", val: false | |
68 }, { | |
69 msg: "Empty array []", val: [] | |
70 }, { | |
71 msg: "Empty object {}", val: {} | |
72 }, { | |
73 msg: "Array of size 3", val: new Array(3) | |
74 }]; | |
75 | |
76 function testNonStringValues() { | |
77 var i = 0; | |
78 var l = TEST_INPUT.length; | |
79 | |
80 for (; i < l; i++) { | |
81 var e = TEST_INPUT[i]; | |
82 var v = e.val; | |
83 var s = String(v); | |
84 assertTrue(s.startsWith(v), e.msg); | |
85 assertTrue(String.prototype.startsWith.call(v, v), e.msg); | |
86 assertTrue(String.prototype.startsWith.apply(v, [v]), e.msg); | |
87 } | |
88 } | |
89 testNonStringValues(); | |
90 | |
91 var CustomType = function(value) { | |
92 this.startsWith = String.prototype.startsWith; | |
93 this.toString = function() { | |
94 return String(value); | |
95 } | |
96 }; | |
97 | |
98 function testCutomType() { | |
99 var i = 0; | |
100 var l = TEST_INPUT.length; | |
101 | |
102 for (; i < l; i++) { | |
103 var e = TEST_INPUT[i]; | |
104 var v = e.val; | |
105 var o = new CustomType(v); | |
106 assertTrue(o.startsWith(v), e.msg); | |
107 } | |
108 } | |
109 testCutomType(); | |
110 | |
111 // Test cases found in FF | |
112 assertTrue("abc".startsWith("abc")); | |
113 assertTrue("abcd".startsWith("abc")); | |
114 assertTrue("abc".startsWith("a")); | |
115 assertFalse("abc".startsWith("abcd")); | |
116 assertFalse("abc".startsWith("bcde")); | |
117 assertFalse("abc".startsWith("b")); | |
118 assertTrue("abc".startsWith("abc", 0)); | |
119 assertFalse("abc".startsWith("bc", 0)); | |
120 assertTrue("abc".startsWith("bc", 1)); | |
121 assertFalse("abc".startsWith("c", 1)); | |
122 assertFalse("abc".startsWith("abc", 1)); | |
123 assertTrue("abc".startsWith("c", 2)); | |
124 assertFalse("abc".startsWith("d", 2)); | |
125 assertFalse("abc".startsWith("dcd", 2)); | |
126 assertFalse("abc".startsWith("a", 42)); | |
127 assertFalse("abc".startsWith("a", Infinity)); | |
128 assertTrue("abc".startsWith("a", NaN)); | |
129 assertFalse("abc".startsWith("b", NaN)); | |
130 assertTrue("abc".startsWith("ab", -43)); | |
131 assertTrue("abc".startsWith("ab", -Infinity)); | |
132 assertFalse("abc".startsWith("bc", -42)); | |
133 assertFalse("abc".startsWith("bc", -Infinity)); | |
134 | |
135 // Test cases taken from | |
136 // https://github.com/mathiasbynens/String.prototype.startsWith/blob/master/test
s/tests.js | |
137 Object.prototype[1] = 2; // try to break `arguments[1]` | |
138 | |
139 assertEquals(String.prototype.startsWith.length, 1); | |
140 assertEquals(String.prototype.propertyIsEnumerable("startsWith"), false); | |
141 | |
142 assertEquals("undefined".startsWith(), true); | |
143 assertEquals("undefined".startsWith(undefined), true); | |
144 assertEquals("undefined".startsWith(null), false); | |
145 assertEquals("null".startsWith(), false); | |
146 assertEquals("null".startsWith(undefined), false); | |
147 assertEquals("null".startsWith(null), true); | |
148 | |
149 assertEquals("abc".startsWith(), false); | |
150 assertEquals("abc".startsWith(""), true); | |
151 assertEquals("abc".startsWith("\0"), false); | |
152 assertEquals("abc".startsWith("a"), true); | |
153 assertEquals("abc".startsWith("b"), false); | |
154 assertEquals("abc".startsWith("ab"), true); | |
155 assertEquals("abc".startsWith("bc"), false); | |
156 assertEquals("abc".startsWith("abc"), true); | |
157 assertEquals("abc".startsWith("bcd"), false); | |
158 assertEquals("abc".startsWith("abcd"), false); | |
159 assertEquals("abc".startsWith("bcde"), false); | |
160 | |
161 assertEquals("abc".startsWith("", NaN), true); | |
162 assertEquals("abc".startsWith("\0", NaN), false); | |
163 assertEquals("abc".startsWith("a", NaN), true); | |
164 assertEquals("abc".startsWith("b", NaN), false); | |
165 assertEquals("abc".startsWith("ab", NaN), true); | |
166 assertEquals("abc".startsWith("bc", NaN), false); | |
167 assertEquals("abc".startsWith("abc", NaN), true); | |
168 assertEquals("abc".startsWith("bcd", NaN), false); | |
169 assertEquals("abc".startsWith("abcd", NaN), false); | |
170 assertEquals("abc".startsWith("bcde", NaN), false); | |
171 | |
172 assertEquals("abc".startsWith("", false), true); | |
173 assertEquals("abc".startsWith("\0", false), false); | |
174 assertEquals("abc".startsWith("a", false), true); | |
175 assertEquals("abc".startsWith("b", false), false); | |
176 assertEquals("abc".startsWith("ab", false), true); | |
177 assertEquals("abc".startsWith("bc", false), false); | |
178 assertEquals("abc".startsWith("abc", false), true); | |
179 assertEquals("abc".startsWith("bcd", false), false); | |
180 assertEquals("abc".startsWith("abcd", false), false); | |
181 assertEquals("abc".startsWith("bcde", false), false); | |
182 | |
183 assertEquals("abc".startsWith("", undefined), true); | |
184 assertEquals("abc".startsWith("\0", undefined), false); | |
185 assertEquals("abc".startsWith("a", undefined), true); | |
186 assertEquals("abc".startsWith("b", undefined), false); | |
187 assertEquals("abc".startsWith("ab", undefined), true); | |
188 assertEquals("abc".startsWith("bc", undefined), false); | |
189 assertEquals("abc".startsWith("abc", undefined), true); | |
190 assertEquals("abc".startsWith("bcd", undefined), false); | |
191 assertEquals("abc".startsWith("abcd", undefined), false); | |
192 assertEquals("abc".startsWith("bcde", undefined), false); | |
193 | |
194 assertEquals("abc".startsWith("", null), true); | |
195 assertEquals("abc".startsWith("\0", null), false); | |
196 assertEquals("abc".startsWith("a", null), true); | |
197 assertEquals("abc".startsWith("b", null), false); | |
198 assertEquals("abc".startsWith("ab", null), true); | |
199 assertEquals("abc".startsWith("bc", null), false); | |
200 assertEquals("abc".startsWith("abc", null), true); | |
201 assertEquals("abc".startsWith("bcd", null), false); | |
202 assertEquals("abc".startsWith("abcd", null), false); | |
203 assertEquals("abc".startsWith("bcde", null), false); | |
204 | |
205 assertEquals("abc".startsWith("", -Infinity), true); | |
206 assertEquals("abc".startsWith("\0", -Infinity), false); | |
207 assertEquals("abc".startsWith("a", -Infinity), true); | |
208 assertEquals("abc".startsWith("b", -Infinity), false); | |
209 assertEquals("abc".startsWith("ab", -Infinity), true); | |
210 assertEquals("abc".startsWith("bc", -Infinity), false); | |
211 assertEquals("abc".startsWith("abc", -Infinity), true); | |
212 assertEquals("abc".startsWith("bcd", -Infinity), false); | |
213 assertEquals("abc".startsWith("abcd", -Infinity), false); | |
214 assertEquals("abc".startsWith("bcde", -Infinity), false); | |
215 | |
216 assertEquals("abc".startsWith("", -1), true); | |
217 assertEquals("abc".startsWith("\0", -1), false); | |
218 assertEquals("abc".startsWith("a", -1), true); | |
219 assertEquals("abc".startsWith("b", -1), false); | |
220 assertEquals("abc".startsWith("ab", -1), true); | |
221 assertEquals("abc".startsWith("bc", -1), false); | |
222 assertEquals("abc".startsWith("abc", -1), true); | |
223 assertEquals("abc".startsWith("bcd", -1), false); | |
224 assertEquals("abc".startsWith("abcd", -1), false); | |
225 assertEquals("abc".startsWith("bcde", -1), false); | |
226 | |
227 assertEquals("abc".startsWith("", -0), true); | |
228 assertEquals("abc".startsWith("\0", -0), false); | |
229 assertEquals("abc".startsWith("a", -0), true); | |
230 assertEquals("abc".startsWith("b", -0), false); | |
231 assertEquals("abc".startsWith("ab", -0), true); | |
232 assertEquals("abc".startsWith("bc", -0), false); | |
233 assertEquals("abc".startsWith("abc", -0), true); | |
234 assertEquals("abc".startsWith("bcd", -0), false); | |
235 assertEquals("abc".startsWith("abcd", -0), false); | |
236 assertEquals("abc".startsWith("bcde", -0), false); | |
237 | |
238 assertEquals("abc".startsWith("", +0), true); | |
239 assertEquals("abc".startsWith("\0", +0), false); | |
240 assertEquals("abc".startsWith("a", +0), true); | |
241 assertEquals("abc".startsWith("b", +0), false); | |
242 assertEquals("abc".startsWith("ab", +0), true); | |
243 assertEquals("abc".startsWith("bc", +0), false); | |
244 assertEquals("abc".startsWith("abc", +0), true); | |
245 assertEquals("abc".startsWith("bcd", +0), false); | |
246 assertEquals("abc".startsWith("abcd", +0), false); | |
247 assertEquals("abc".startsWith("bcde", +0), false); | |
248 | |
249 assertEquals("abc".startsWith("", 1), true); | |
250 assertEquals("abc".startsWith("\0", 1), false); | |
251 assertEquals("abc".startsWith("a", 1), false); | |
252 assertEquals("abc".startsWith("b", 1), true); | |
253 assertEquals("abc".startsWith("ab", 1), false); | |
254 assertEquals("abc".startsWith("bc", 1), true); | |
255 assertEquals("abc".startsWith("abc", 1), false); | |
256 assertEquals("abc".startsWith("bcd", 1), false); | |
257 assertEquals("abc".startsWith("abcd", 1), false); | |
258 assertEquals("abc".startsWith("bcde", 1), false); | |
259 | |
260 assertEquals("abc".startsWith("", +Infinity), true); | |
261 assertEquals("abc".startsWith("\0", +Infinity), false); | |
262 assertEquals("abc".startsWith("a", +Infinity), false); | |
263 assertEquals("abc".startsWith("b", +Infinity), false); | |
264 assertEquals("abc".startsWith("ab", +Infinity), false); | |
265 assertEquals("abc".startsWith("bc", +Infinity), false); | |
266 assertEquals("abc".startsWith("abc", +Infinity), false); | |
267 assertEquals("abc".startsWith("bcd", +Infinity), false); | |
268 assertEquals("abc".startsWith("abcd", +Infinity), false); | |
269 assertEquals("abc".startsWith("bcde", +Infinity), false); | |
270 | |
271 assertEquals("abc".startsWith("", true), true); | |
272 assertEquals("abc".startsWith("\0", true), false); | |
273 assertEquals("abc".startsWith("a", true), false); | |
274 assertEquals("abc".startsWith("b", true), true); | |
275 assertEquals("abc".startsWith("ab", true), false); | |
276 assertEquals("abc".startsWith("bc", true), true); | |
277 assertEquals("abc".startsWith("abc", true), false); | |
278 assertEquals("abc".startsWith("bcd", true), false); | |
279 assertEquals("abc".startsWith("abcd", true), false); | |
280 assertEquals("abc".startsWith("bcde", true), false); | |
281 | |
282 assertEquals("abc".startsWith("", "x"), true); | |
283 assertEquals("abc".startsWith("\0", "x"), false); | |
284 assertEquals("abc".startsWith("a", "x"), true); | |
285 assertEquals("abc".startsWith("b", "x"), false); | |
286 assertEquals("abc".startsWith("ab", "x"), true); | |
287 assertEquals("abc".startsWith("bc", "x"), false); | |
288 assertEquals("abc".startsWith("abc", "x"), true); | |
289 assertEquals("abc".startsWith("bcd", "x"), false); | |
290 assertEquals("abc".startsWith("abcd", "x"), false); | |
291 assertEquals("abc".startsWith("bcde", "x"), false); | |
292 | |
293 assertEquals("[a-z]+(bar)?".startsWith("[a-z]+"), true); | |
294 assertThrows(function() { "[a-z]+(bar)?".startsWith(/[a-z]+/); }, TypeError); | |
295 assertEquals("[a-z]+(bar)?".startsWith("(bar)?", 6), true); | |
296 assertThrows(function() { "[a-z]+(bar)?".startsWith(/(bar)?/); }, TypeError); | |
297 assertThrows(function() { "[a-z]+/(bar)?/".startsWith(/(bar)?/); }, TypeError); | |
298 | |
299 // http://mathiasbynens.be/notes/javascript-unicode#poo-test | |
300 var string = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9"; | |
301 assertEquals(string.startsWith(""), true); | |
302 assertEquals(string.startsWith("\xF1t\xEBr"), false); | |
303 assertEquals(string.startsWith("\xF1t\xEBr", 1), true); | |
304 assertEquals(string.startsWith("\xE0liz\xE6"), false); | |
305 assertEquals(string.startsWith("\xE0liz\xE6", 11), true); | |
306 assertEquals(string.startsWith("\xF8n\u2603\uD83D\uDCA9"), false); | |
307 assertEquals(string.startsWith("\xF8n\u2603\uD83D\uDCA9", 18), true); | |
308 assertEquals(string.startsWith("\u2603"), false); | |
309 assertEquals(string.startsWith("\u2603", 20), true); | |
310 assertEquals(string.startsWith("\uD83D\uDCA9"), false); | |
311 assertEquals(string.startsWith("\uD83D\uDCA9", 21), true); | |
312 | |
313 assertThrows(function() { | |
314 String.prototype.startsWith.call(undefined); | |
315 }, TypeError); | |
316 assertThrows(function() { | |
317 String.prototype.startsWith.call(undefined, "b"); | |
318 }, TypeError); | |
319 assertThrows(function() { | |
320 String.prototype.startsWith.call(undefined, "b", 4); | |
321 }, TypeError); | |
322 assertThrows(function() { | |
323 String.prototype.startsWith.call(null); | |
324 }, TypeError); | |
325 assertThrows(function() { | |
326 String.prototype.startsWith.call(null, "b"); | |
327 }, TypeError); | |
328 assertThrows(function() { | |
329 String.prototype.startsWith.call(null, "b", 4); | |
330 }, TypeError); | |
331 assertEquals(String.prototype.startsWith.call(42, "2"), false); | |
332 assertEquals(String.prototype.startsWith.call(42, "4"), true); | |
333 assertEquals(String.prototype.startsWith.call(42, "b", 4), false); | |
334 assertEquals(String.prototype.startsWith.call(42, "2", 1), true); | |
335 assertEquals(String.prototype.startsWith.call(42, "2", 4), false); | |
336 assertEquals(String.prototype.startsWith.call({ | |
337 "toString": function() { return "abc"; } | |
338 }, "b", 0), false); | |
339 assertEquals(String.prototype.startsWith.call({ | |
340 "toString": function() { return "abc"; } | |
341 }, "b", 1), true); | |
342 assertEquals(String.prototype.startsWith.call({ | |
343 "toString": function() { return "abc"; } | |
344 }, "b", 2), false); | |
345 assertThrows(function() { | |
346 String.prototype.startsWith.call({ | |
347 "toString": function() { throw RangeError(); } | |
348 }, /./); | |
349 }, RangeError); | |
350 assertThrows(function() { | |
351 String.prototype.startsWith.call({ | |
352 "toString": function() { return "abc"; } | |
353 }, /./); | |
354 }, TypeError); | |
355 | |
356 assertThrows(function() { | |
357 String.prototype.startsWith.apply(undefined); | |
358 }, TypeError); | |
359 assertThrows(function() { | |
360 String.prototype.startsWith.apply(undefined, ["b"]); | |
361 }, TypeError); | |
362 assertThrows(function() { | |
363 String.prototype.startsWith.apply(undefined, ["b", 4]); | |
364 }, TypeError); | |
365 assertThrows(function() { | |
366 String.prototype.startsWith.apply(null); | |
367 }, TypeError); | |
368 assertThrows(function() { | |
369 String.prototype.startsWith.apply(null, ["b"]); | |
370 }, TypeError); | |
371 assertThrows(function() { | |
372 String.prototype.startsWith.apply(null, ["b", 4]); | |
373 }, TypeError); | |
374 assertEquals(String.prototype.startsWith.apply(42, ["2"]), false); | |
375 assertEquals(String.prototype.startsWith.apply(42, ["4"]), true); | |
376 assertEquals(String.prototype.startsWith.apply(42, ["b", 4]), false); | |
377 assertEquals(String.prototype.startsWith.apply(42, ["2", 1]), true); | |
378 assertEquals(String.prototype.startsWith.apply(42, ["2", 4]), false); | |
379 assertEquals(String.prototype.startsWith.apply({ | |
380 "toString": function() { | |
381 return "abc"; | |
382 } | |
383 }, ["b", 0]), false); | |
384 assertEquals(String.prototype.startsWith.apply({ | |
385 "toString": function() { | |
386 return "abc"; | |
387 } | |
388 }, ["b", 1]), true); | |
389 assertEquals(String.prototype.startsWith.apply({ | |
390 "toString": function() { | |
391 return "abc"; | |
392 } | |
393 }, ["b", 2]), false); | |
394 assertThrows(function() { | |
395 String.prototype.startsWith.apply({ | |
396 "toString": function() { throw RangeError(); } | |
397 }, [/./]); | |
398 }, RangeError); | |
399 assertThrows(function() { | |
400 String.prototype.startsWith.apply({ | |
401 "toString": function() { return "abc"; } | |
402 }, [/./]); | |
403 }, TypeError); | |
OLD | NEW |