OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 msg: "Number 1234.34", val: 1234.34 | 59 msg: "Number 1234.34", val: 1234.34 |
60 }, { | 60 }, { |
61 msg: "Integer number 0", val: 0 | 61 msg: "Integer number 0", val: 0 |
62 }, { | 62 }, { |
63 msg: "Negative number -1", val: -1 | 63 msg: "Negative number -1", val: -1 |
64 }, { | 64 }, { |
65 msg: "Boolean true", val: true | 65 msg: "Boolean true", val: true |
66 }, { | 66 }, { |
67 msg: "Boolean false", val: false | 67 msg: "Boolean false", val: false |
68 }, { | 68 }, { |
69 msg: "Regular expression /\d+/", val: /\d+/ | |
70 }, { | |
71 msg: "Empty array []", val: [] | 69 msg: "Empty array []", val: [] |
72 }, { | 70 }, { |
73 msg: "Empty object {}", val: {} | 71 msg: "Empty object {}", val: {} |
74 }, { | 72 }, { |
75 msg: "Array of size 3", val: new Array(3) | 73 msg: "Array of size 3", val: new Array(3) |
76 }]; | 74 }]; |
77 | 75 |
78 function testNonStringValues() { | 76 function testNonStringValues() { |
79 var i = 0; | 77 var i = 0; |
80 var l = TEST_INPUT.length; | 78 var l = TEST_INPUT.length; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 assertFalse("abc".startsWith("d", 2)); | 124 assertFalse("abc".startsWith("d", 2)); |
127 assertFalse("abc".startsWith("dcd", 2)); | 125 assertFalse("abc".startsWith("dcd", 2)); |
128 assertFalse("abc".startsWith("a", 42)); | 126 assertFalse("abc".startsWith("a", 42)); |
129 assertFalse("abc".startsWith("a", Infinity)); | 127 assertFalse("abc".startsWith("a", Infinity)); |
130 assertTrue("abc".startsWith("a", NaN)); | 128 assertTrue("abc".startsWith("a", NaN)); |
131 assertFalse("abc".startsWith("b", NaN)); | 129 assertFalse("abc".startsWith("b", NaN)); |
132 assertTrue("abc".startsWith("ab", -43)); | 130 assertTrue("abc".startsWith("ab", -43)); |
133 assertTrue("abc".startsWith("ab", -Infinity)); | 131 assertTrue("abc".startsWith("ab", -Infinity)); |
134 assertFalse("abc".startsWith("bc", -42)); | 132 assertFalse("abc".startsWith("bc", -42)); |
135 assertFalse("abc".startsWith("bc", -Infinity)); | 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 |