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
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.
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.
28 assertEquals(1, String.prototype.startsWith.length);
30 var testString = "Hello World";
31 assertTrue(testString.startsWith(""));
32 assertTrue(testString.startsWith("Hello"));
33 assertFalse(testString.startsWith("hello"));
34 assertFalse(testString.startsWith("Hello World!"));
35 assertFalse(testString.startsWith(null));
36 assertFalse(testString.startsWith(undefined));
38 assertTrue("null".startsWith(null));
39 assertTrue("undefined".startsWith(undefined));
41 var georgianUnicodeString = "\u10D0\u10D1\u10D2\u10D3\u10D4\u10D5\u10D6\u10D7";
42 assertTrue(georgianUnicodeString.startsWith(georgianUnicodeString));
43 assertTrue(georgianUnicodeString.startsWith("\u10D0\u10D1\u10D2"));
44 assertFalse(georgianUnicodeString.startsWith("\u10D8"));
46 assertThrows("String.prototype.startsWith.call(null, 'test')", TypeError);
47 assertThrows("String.prototype.startsWith.call(null, null)", TypeError);
48 assertThrows("String.prototype.startsWith.call(undefined, undefined)", TypeError);
50 assertThrows("String.prototype.startsWith.apply(null, ['test'])", TypeError);
51 assertThrows("String.prototype.startsWith.apply(null, [null])", TypeError);
52 assertThrows("String.prototype.startsWith.apply(undefined, [undefined])", TypeError);
55 msg: "Empty string", val: ""
57 msg: "Number 1234.34", val: 1234.34
59 msg: "Integer number 0", val: 0
61 msg: "Negative number -1", val: -1
63 msg: "Boolean true", val: true
65 msg: "Boolean false", val: false
67 msg: "Empty array []", val: []
69 msg: "Empty object {}", val: {}
71 msg: "Array of size 3", val: new Array(3)
74 function testNonStringValues() {
76 var l = TEST_INPUT.length;
79 var e = TEST_INPUT[i];
82 assertTrue(s.startsWith(v), e.msg);
83 assertTrue(String.prototype.startsWith.call(v, v), e.msg);
84 assertTrue(String.prototype.startsWith.apply(v, [v]), e.msg);
87 testNonStringValues();
89 var CustomType = function(value) {
90 this.startsWith = String.prototype.startsWith;
91 this.toString = function() {
96 function testCutomType() {
98 var l = TEST_INPUT.length;
101 var e = TEST_INPUT[i];
103 var o = new CustomType(v);
104 assertTrue(o.startsWith(v), e.msg);
109 // Test cases found in FF
110 assertTrue("abc".startsWith("abc"));
111 assertTrue("abcd".startsWith("abc"));
112 assertTrue("abc".startsWith("a"));
113 assertFalse("abc".startsWith("abcd"));
114 assertFalse("abc".startsWith("bcde"));
115 assertFalse("abc".startsWith("b"));
116 assertTrue("abc".startsWith("abc", 0));
117 assertFalse("abc".startsWith("bc", 0));
118 assertTrue("abc".startsWith("bc", 1));
119 assertFalse("abc".startsWith("c", 1));
120 assertFalse("abc".startsWith("abc", 1));
121 assertTrue("abc".startsWith("c", 2));
122 assertFalse("abc".startsWith("d", 2));
123 assertFalse("abc".startsWith("dcd", 2));
124 assertFalse("abc".startsWith("a", 42));
125 assertFalse("abc".startsWith("a", Infinity));
126 assertTrue("abc".startsWith("a", NaN));
127 assertFalse("abc".startsWith("b", NaN));
128 assertTrue("abc".startsWith("ab", -43));
129 assertTrue("abc".startsWith("ab", -Infinity));
130 assertFalse("abc".startsWith("bc", -42));
131 assertFalse("abc".startsWith("bc", -Infinity));
133 // Test cases taken from
134 // https://github.com/mathiasbynens/String.prototype.startsWith/blob/master/tests/tests.js
135 Object.prototype[1] = 2; // try to break `arguments[1]`
137 assertEquals(String.prototype.startsWith.length, 1);
138 assertEquals(String.prototype.propertyIsEnumerable("startsWith"), false);
140 assertEquals("undefined".startsWith(), true);
141 assertEquals("undefined".startsWith(undefined), true);
142 assertEquals("undefined".startsWith(null), false);
143 assertEquals("null".startsWith(), false);
144 assertEquals("null".startsWith(undefined), false);
145 assertEquals("null".startsWith(null), true);
147 assertEquals("abc".startsWith(), false);
148 assertEquals("abc".startsWith(""), true);
149 assertEquals("abc".startsWith("\0"), false);
150 assertEquals("abc".startsWith("a"), true);
151 assertEquals("abc".startsWith("b"), false);
152 assertEquals("abc".startsWith("ab"), true);
153 assertEquals("abc".startsWith("bc"), false);
154 assertEquals("abc".startsWith("abc"), true);
155 assertEquals("abc".startsWith("bcd"), false);
156 assertEquals("abc".startsWith("abcd"), false);
157 assertEquals("abc".startsWith("bcde"), false);
159 assertEquals("abc".startsWith("", NaN), true);
160 assertEquals("abc".startsWith("\0", NaN), false);
161 assertEquals("abc".startsWith("a", NaN), true);
162 assertEquals("abc".startsWith("b", NaN), false);
163 assertEquals("abc".startsWith("ab", NaN), true);
164 assertEquals("abc".startsWith("bc", NaN), false);
165 assertEquals("abc".startsWith("abc", NaN), true);
166 assertEquals("abc".startsWith("bcd", NaN), false);
167 assertEquals("abc".startsWith("abcd", NaN), false);
168 assertEquals("abc".startsWith("bcde", NaN), false);
170 assertEquals("abc".startsWith("", false), true);
171 assertEquals("abc".startsWith("\0", false), false);
172 assertEquals("abc".startsWith("a", false), true);
173 assertEquals("abc".startsWith("b", false), false);
174 assertEquals("abc".startsWith("ab", false), true);
175 assertEquals("abc".startsWith("bc", false), false);
176 assertEquals("abc".startsWith("abc", false), true);
177 assertEquals("abc".startsWith("bcd", false), false);
178 assertEquals("abc".startsWith("abcd", false), false);
179 assertEquals("abc".startsWith("bcde", false), false);
181 assertEquals("abc".startsWith("", undefined), true);
182 assertEquals("abc".startsWith("\0", undefined), false);
183 assertEquals("abc".startsWith("a", undefined), true);
184 assertEquals("abc".startsWith("b", undefined), false);
185 assertEquals("abc".startsWith("ab", undefined), true);
186 assertEquals("abc".startsWith("bc", undefined), false);
187 assertEquals("abc".startsWith("abc", undefined), true);
188 assertEquals("abc".startsWith("bcd", undefined), false);
189 assertEquals("abc".startsWith("abcd", undefined), false);
190 assertEquals("abc".startsWith("bcde", undefined), false);
192 assertEquals("abc".startsWith("", null), true);
193 assertEquals("abc".startsWith("\0", null), false);
194 assertEquals("abc".startsWith("a", null), true);
195 assertEquals("abc".startsWith("b", null), false);
196 assertEquals("abc".startsWith("ab", null), true);
197 assertEquals("abc".startsWith("bc", null), false);
198 assertEquals("abc".startsWith("abc", null), true);
199 assertEquals("abc".startsWith("bcd", null), false);
200 assertEquals("abc".startsWith("abcd", null), false);
201 assertEquals("abc".startsWith("bcde", null), false);
203 assertEquals("abc".startsWith("", -Infinity), true);
204 assertEquals("abc".startsWith("\0", -Infinity), false);
205 assertEquals("abc".startsWith("a", -Infinity), true);
206 assertEquals("abc".startsWith("b", -Infinity), false);
207 assertEquals("abc".startsWith("ab", -Infinity), true);
208 assertEquals("abc".startsWith("bc", -Infinity), false);
209 assertEquals("abc".startsWith("abc", -Infinity), true);
210 assertEquals("abc".startsWith("bcd", -Infinity), false);
211 assertEquals("abc".startsWith("abcd", -Infinity), false);
212 assertEquals("abc".startsWith("bcde", -Infinity), false);
214 assertEquals("abc".startsWith("", -1), true);
215 assertEquals("abc".startsWith("\0", -1), false);
216 assertEquals("abc".startsWith("a", -1), true);
217 assertEquals("abc".startsWith("b", -1), false);
218 assertEquals("abc".startsWith("ab", -1), true);
219 assertEquals("abc".startsWith("bc", -1), false);
220 assertEquals("abc".startsWith("abc", -1), true);
221 assertEquals("abc".startsWith("bcd", -1), false);
222 assertEquals("abc".startsWith("abcd", -1), false);
223 assertEquals("abc".startsWith("bcde", -1), false);
225 assertEquals("abc".startsWith("", -0), true);
226 assertEquals("abc".startsWith("\0", -0), false);
227 assertEquals("abc".startsWith("a", -0), true);
228 assertEquals("abc".startsWith("b", -0), false);
229 assertEquals("abc".startsWith("ab", -0), true);
230 assertEquals("abc".startsWith("bc", -0), false);
231 assertEquals("abc".startsWith("abc", -0), true);
232 assertEquals("abc".startsWith("bcd", -0), false);
233 assertEquals("abc".startsWith("abcd", -0), false);
234 assertEquals("abc".startsWith("bcde", -0), false);
236 assertEquals("abc".startsWith("", +0), true);
237 assertEquals("abc".startsWith("\0", +0), false);
238 assertEquals("abc".startsWith("a", +0), true);
239 assertEquals("abc".startsWith("b", +0), false);
240 assertEquals("abc".startsWith("ab", +0), true);
241 assertEquals("abc".startsWith("bc", +0), false);
242 assertEquals("abc".startsWith("abc", +0), true);
243 assertEquals("abc".startsWith("bcd", +0), false);
244 assertEquals("abc".startsWith("abcd", +0), false);
245 assertEquals("abc".startsWith("bcde", +0), false);
247 assertEquals("abc".startsWith("", 1), true);
248 assertEquals("abc".startsWith("\0", 1), false);
249 assertEquals("abc".startsWith("a", 1), false);
250 assertEquals("abc".startsWith("b", 1), true);
251 assertEquals("abc".startsWith("ab", 1), false);
252 assertEquals("abc".startsWith("bc", 1), true);
253 assertEquals("abc".startsWith("abc", 1), false);
254 assertEquals("abc".startsWith("bcd", 1), false);
255 assertEquals("abc".startsWith("abcd", 1), false);
256 assertEquals("abc".startsWith("bcde", 1), false);
258 assertEquals("abc".startsWith("", +Infinity), true);
259 assertEquals("abc".startsWith("\0", +Infinity), false);
260 assertEquals("abc".startsWith("a", +Infinity), false);
261 assertEquals("abc".startsWith("b", +Infinity), false);
262 assertEquals("abc".startsWith("ab", +Infinity), false);
263 assertEquals("abc".startsWith("bc", +Infinity), false);
264 assertEquals("abc".startsWith("abc", +Infinity), false);
265 assertEquals("abc".startsWith("bcd", +Infinity), false);
266 assertEquals("abc".startsWith("abcd", +Infinity), false);
267 assertEquals("abc".startsWith("bcde", +Infinity), false);
269 assertEquals("abc".startsWith("", true), true);
270 assertEquals("abc".startsWith("\0", true), false);
271 assertEquals("abc".startsWith("a", true), false);
272 assertEquals("abc".startsWith("b", true), true);
273 assertEquals("abc".startsWith("ab", true), false);
274 assertEquals("abc".startsWith("bc", true), true);
275 assertEquals("abc".startsWith("abc", true), false);
276 assertEquals("abc".startsWith("bcd", true), false);
277 assertEquals("abc".startsWith("abcd", true), false);
278 assertEquals("abc".startsWith("bcde", true), false);
280 assertEquals("abc".startsWith("", "x"), true);
281 assertEquals("abc".startsWith("\0", "x"), false);
282 assertEquals("abc".startsWith("a", "x"), true);
283 assertEquals("abc".startsWith("b", "x"), false);
284 assertEquals("abc".startsWith("ab", "x"), true);
285 assertEquals("abc".startsWith("bc", "x"), false);
286 assertEquals("abc".startsWith("abc", "x"), true);
287 assertEquals("abc".startsWith("bcd", "x"), false);
288 assertEquals("abc".startsWith("abcd", "x"), false);
289 assertEquals("abc".startsWith("bcde", "x"), false);
291 assertEquals("[a-z]+(bar)?".startsWith("[a-z]+"), true);
292 assertThrows(function() { "[a-z]+(bar)?".startsWith(/[a-z]+/); }, TypeError);
293 assertEquals("[a-z]+(bar)?".startsWith("(bar)?", 6), true);
294 assertThrows(function() { "[a-z]+(bar)?".startsWith(/(bar)?/); }, TypeError);
295 assertThrows(function() { "[a-z]+/(bar)?/".startsWith(/(bar)?/); }, TypeError);
297 // http://mathiasbynens.be/notes/javascript-unicode#poo-test
298 var string = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9";
299 assertEquals(string.startsWith(""), true);
300 assertEquals(string.startsWith("\xF1t\xEBr"), false);
301 assertEquals(string.startsWith("\xF1t\xEBr", 1), true);
302 assertEquals(string.startsWith("\xE0liz\xE6"), false);
303 assertEquals(string.startsWith("\xE0liz\xE6", 11), true);
304 assertEquals(string.startsWith("\xF8n\u2603\uD83D\uDCA9"), false);
305 assertEquals(string.startsWith("\xF8n\u2603\uD83D\uDCA9", 18), true);
306 assertEquals(string.startsWith("\u2603"), false);
307 assertEquals(string.startsWith("\u2603", 20), true);
308 assertEquals(string.startsWith("\uD83D\uDCA9"), false);
309 assertEquals(string.startsWith("\uD83D\uDCA9", 21), true);
311 assertThrows(function() {
312 String.prototype.startsWith.call(undefined);
314 assertThrows(function() {
315 String.prototype.startsWith.call(undefined, "b");
317 assertThrows(function() {
318 String.prototype.startsWith.call(undefined, "b", 4);
320 assertThrows(function() {
321 String.prototype.startsWith.call(null);
323 assertThrows(function() {
324 String.prototype.startsWith.call(null, "b");
326 assertThrows(function() {
327 String.prototype.startsWith.call(null, "b", 4);
329 assertEquals(String.prototype.startsWith.call(42, "2"), false);
330 assertEquals(String.prototype.startsWith.call(42, "4"), true);
331 assertEquals(String.prototype.startsWith.call(42, "b", 4), false);
332 assertEquals(String.prototype.startsWith.call(42, "2", 1), true);
333 assertEquals(String.prototype.startsWith.call(42, "2", 4), false);
334 assertEquals(String.prototype.startsWith.call({
335 "toString": function() { return "abc"; }
337 assertEquals(String.prototype.startsWith.call({
338 "toString": function() { return "abc"; }
340 assertEquals(String.prototype.startsWith.call({
341 "toString": function() { return "abc"; }
343 assertThrows(function() {
344 String.prototype.startsWith.call({
345 "toString": function() { throw RangeError(); }
348 assertThrows(function() {
349 String.prototype.startsWith.call({
350 "toString": function() { return "abc"; }
354 assertThrows(function() {
355 String.prototype.startsWith.apply(undefined);
357 assertThrows(function() {
358 String.prototype.startsWith.apply(undefined, ["b"]);
360 assertThrows(function() {
361 String.prototype.startsWith.apply(undefined, ["b", 4]);
363 assertThrows(function() {
364 String.prototype.startsWith.apply(null);
366 assertThrows(function() {
367 String.prototype.startsWith.apply(null, ["b"]);
369 assertThrows(function() {
370 String.prototype.startsWith.apply(null, ["b", 4]);
372 assertEquals(String.prototype.startsWith.apply(42, ["2"]), false);
373 assertEquals(String.prototype.startsWith.apply(42, ["4"]), true);
374 assertEquals(String.prototype.startsWith.apply(42, ["b", 4]), false);
375 assertEquals(String.prototype.startsWith.apply(42, ["2", 1]), true);
376 assertEquals(String.prototype.startsWith.apply(42, ["2", 4]), false);
377 assertEquals(String.prototype.startsWith.apply({
378 "toString": function() {
381 }, ["b", 0]), false);
382 assertEquals(String.prototype.startsWith.apply({
383 "toString": function() {
387 assertEquals(String.prototype.startsWith.apply({
388 "toString": function() {
391 }, ["b", 2]), false);
392 assertThrows(function() {
393 String.prototype.startsWith.apply({
394 "toString": function() { throw RangeError(); }
397 assertThrows(function() {
398 String.prototype.startsWith.apply({
399 "toString": function() { return "abc"; }