Make String.prototype.{starts,ends}With
throw when passing a regular expression
Contributed by Mathias Bynens <mathiasb@opera.com>. TEST=mjsunit/harmony BUG=v8:3070 LOG=Y R=arv@chromium.org, ishell@chromium.org Review URL: https://codereview.chromium.org/120683002 Patch from Mathias Bynens <mathiasb@opera.com>. git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18870 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
9e462504fb
commit
1776dffa56
@ -1,4 +1,4 @@
|
||||
// Copyright 2013 the V8 project authors. All rights reserved.
|
||||
// Copyright 2014 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
@ -34,12 +34,9 @@
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// ES6 draft 07-15-13, section 15.5.3.21
|
||||
// ES6 draft 01-20-14, section 21.1.3.13
|
||||
function StringRepeat(count) {
|
||||
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
||||
throw MakeTypeError("called_on_null_or_undefined",
|
||||
["String.prototype.repeat"]);
|
||||
}
|
||||
CHECK_OBJECT_COERCIBLE(this, "String.prototype.repeat");
|
||||
|
||||
var s = TO_STRING_INLINE(this);
|
||||
var n = ToInteger(count);
|
||||
@ -56,14 +53,17 @@ function StringRepeat(count) {
|
||||
}
|
||||
|
||||
|
||||
// ES6 draft 07-15-13, section 15.5.3.22
|
||||
// ES6 draft 01-20-14, section 21.1.3.18
|
||||
function StringStartsWith(searchString /* position */) { // length == 1
|
||||
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
||||
throw MakeTypeError("called_on_null_or_undefined",
|
||||
CHECK_OBJECT_COERCIBLE(this, "String.prototype.startsWith");
|
||||
|
||||
var s = TO_STRING_INLINE(this);
|
||||
|
||||
if (IS_REGEXP(searchString)) {
|
||||
throw MakeTypeError("first_argument_not_regexp",
|
||||
["String.prototype.startsWith"]);
|
||||
}
|
||||
|
||||
var s = TO_STRING_INLINE(this);
|
||||
var ss = TO_STRING_INLINE(searchString);
|
||||
var pos = 0;
|
||||
if (%_ArgumentsLength() > 1) {
|
||||
@ -82,14 +82,17 @@ function StringStartsWith(searchString /* position */) { // length == 1
|
||||
}
|
||||
|
||||
|
||||
// ES6 draft 07-15-13, section 15.5.3.23
|
||||
// ES6 draft 01-20-14, section 21.1.3.7
|
||||
function StringEndsWith(searchString /* position */) { // length == 1
|
||||
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
||||
throw MakeTypeError("called_on_null_or_undefined",
|
||||
CHECK_OBJECT_COERCIBLE(this, "String.prototype.endsWith");
|
||||
|
||||
var s = TO_STRING_INLINE(this);
|
||||
|
||||
if (IS_REGEXP(searchString)) {
|
||||
throw MakeTypeError("first_argument_not_regexp",
|
||||
["String.prototype.endsWith"]);
|
||||
}
|
||||
|
||||
var s = TO_STRING_INLINE(this);
|
||||
var ss = TO_STRING_INLINE(searchString);
|
||||
var s_len = s.length;
|
||||
var pos = s_len;
|
||||
@ -111,12 +114,9 @@ function StringEndsWith(searchString /* position */) { // length == 1
|
||||
}
|
||||
|
||||
|
||||
// ES6 draft 07-15-13, section 15.5.3.24
|
||||
// ES6 draft 01-20-14, section 21.1.3.6
|
||||
function StringContains(searchString /* position */) { // length == 1
|
||||
if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) {
|
||||
throw MakeTypeError("called_on_null_or_undefined",
|
||||
["String.prototype.contains"]);
|
||||
}
|
||||
CHECK_OBJECT_COERCIBLE(this, "String.prototype.contains");
|
||||
|
||||
var s = TO_STRING_INLINE(this);
|
||||
var ss = TO_STRING_INLINE(searchString);
|
||||
|
@ -114,6 +114,7 @@ var kMessages = {
|
||||
promise_cyclic: ["Chaining cycle detected for promise ", "%0"],
|
||||
array_functions_on_frozen: ["Cannot modify frozen array elements"],
|
||||
array_functions_change_sealed: ["Cannot add/remove sealed array elements"],
|
||||
first_argument_not_regexp: ["First argument to ", "%0", " must not be a regular expression"],
|
||||
// RangeError
|
||||
invalid_array_length: ["Invalid array length"],
|
||||
invalid_array_buffer_length: ["Invalid array buffer length"],
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2013 the V8 project authors. All rights reserved.
|
||||
// Copyright 2014 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
@ -65,8 +65,6 @@ var TEST_INPUT = [{
|
||||
msg: "Boolean true", val: true
|
||||
}, {
|
||||
msg: "Boolean false", val: false
|
||||
}, {
|
||||
msg: "Regular expression /\d+/", val: /\d+/
|
||||
}, {
|
||||
msg: "Empty array []", val: []
|
||||
}, {
|
||||
@ -134,3 +132,281 @@ assertTrue("abc".endsWith("bc", undefined));
|
||||
assertFalse("abc".endsWith("bc", -43));
|
||||
assertFalse("abc".endsWith("bc", -Infinity));
|
||||
assertFalse("abc".endsWith("bc", NaN));
|
||||
|
||||
// Test cases taken from
|
||||
// https://github.com/mathiasbynens/String.prototype.endsWith/blob/master/tests/tests.js
|
||||
Object.prototype[1] = 2; // try to break `arguments[1]`
|
||||
|
||||
assertEquals(String.prototype.endsWith.length, 1);
|
||||
assertEquals(String.prototype.propertyIsEnumerable("endsWith"), false);
|
||||
|
||||
assertEquals("undefined".endsWith(), true);
|
||||
assertEquals("undefined".endsWith(undefined), true);
|
||||
assertEquals("undefined".endsWith(null), false);
|
||||
assertEquals("null".endsWith(), false);
|
||||
assertEquals("null".endsWith(undefined), false);
|
||||
assertEquals("null".endsWith(null), true);
|
||||
|
||||
assertEquals("abc".endsWith(), false);
|
||||
assertEquals("abc".endsWith(""), true);
|
||||
assertEquals("abc".endsWith("\0"), false);
|
||||
assertEquals("abc".endsWith("c"), true);
|
||||
assertEquals("abc".endsWith("b"), false);
|
||||
assertEquals("abc".endsWith("ab"), false);
|
||||
assertEquals("abc".endsWith("bc"), true);
|
||||
assertEquals("abc".endsWith("abc"), true);
|
||||
assertEquals("abc".endsWith("bcd"), false);
|
||||
assertEquals("abc".endsWith("abcd"), false);
|
||||
assertEquals("abc".endsWith("bcde"), false);
|
||||
|
||||
assertEquals("abc".endsWith("", NaN), true);
|
||||
assertEquals("abc".endsWith("\0", NaN), false);
|
||||
assertEquals("abc".endsWith("c", NaN), false);
|
||||
assertEquals("abc".endsWith("b", NaN), false);
|
||||
assertEquals("abc".endsWith("ab", NaN), false);
|
||||
assertEquals("abc".endsWith("bc", NaN), false);
|
||||
assertEquals("abc".endsWith("abc", NaN), false);
|
||||
assertEquals("abc".endsWith("bcd", NaN), false);
|
||||
assertEquals("abc".endsWith("abcd", NaN), false);
|
||||
assertEquals("abc".endsWith("bcde", NaN), false);
|
||||
|
||||
assertEquals("abc".endsWith("", false), true);
|
||||
assertEquals("abc".endsWith("\0", false), false);
|
||||
assertEquals("abc".endsWith("c", false), false);
|
||||
assertEquals("abc".endsWith("b", false), false);
|
||||
assertEquals("abc".endsWith("ab", false), false);
|
||||
assertEquals("abc".endsWith("bc", false), false);
|
||||
assertEquals("abc".endsWith("abc", false), false);
|
||||
assertEquals("abc".endsWith("bcd", false), false);
|
||||
assertEquals("abc".endsWith("abcd", false), false);
|
||||
assertEquals("abc".endsWith("bcde", false), false);
|
||||
|
||||
assertEquals("abc".endsWith("", undefined), true);
|
||||
assertEquals("abc".endsWith("\0", undefined), false);
|
||||
assertEquals("abc".endsWith("c", undefined), true);
|
||||
assertEquals("abc".endsWith("b", undefined), false);
|
||||
assertEquals("abc".endsWith("ab", undefined), false);
|
||||
assertEquals("abc".endsWith("bc", undefined), true);
|
||||
assertEquals("abc".endsWith("abc", undefined), true);
|
||||
assertEquals("abc".endsWith("bcd", undefined), false);
|
||||
assertEquals("abc".endsWith("abcd", undefined), false);
|
||||
assertEquals("abc".endsWith("bcde", undefined), false);
|
||||
|
||||
assertEquals("abc".endsWith("", null), true);
|
||||
assertEquals("abc".endsWith("\0", null), false);
|
||||
assertEquals("abc".endsWith("c", null), false);
|
||||
assertEquals("abc".endsWith("b", null), false);
|
||||
assertEquals("abc".endsWith("ab", null), false);
|
||||
assertEquals("abc".endsWith("bc", null), false);
|
||||
assertEquals("abc".endsWith("abc", null), false);
|
||||
assertEquals("abc".endsWith("bcd", null), false);
|
||||
assertEquals("abc".endsWith("abcd", null), false);
|
||||
assertEquals("abc".endsWith("bcde", null), false);
|
||||
|
||||
assertEquals("abc".endsWith("", -Infinity), true);
|
||||
assertEquals("abc".endsWith("\0", -Infinity), false);
|
||||
assertEquals("abc".endsWith("c", -Infinity), false);
|
||||
assertEquals("abc".endsWith("b", -Infinity), false);
|
||||
assertEquals("abc".endsWith("ab", -Infinity), false);
|
||||
assertEquals("abc".endsWith("bc", -Infinity), false);
|
||||
assertEquals("abc".endsWith("abc", -Infinity), false);
|
||||
assertEquals("abc".endsWith("bcd", -Infinity), false);
|
||||
assertEquals("abc".endsWith("abcd", -Infinity), false);
|
||||
assertEquals("abc".endsWith("bcde", -Infinity), false);
|
||||
|
||||
assertEquals("abc".endsWith("", -1), true);
|
||||
assertEquals("abc".endsWith("\0", -1), false);
|
||||
assertEquals("abc".endsWith("c", -1), false);
|
||||
assertEquals("abc".endsWith("b", -1), false);
|
||||
assertEquals("abc".endsWith("ab", -1), false);
|
||||
assertEquals("abc".endsWith("bc", -1), false);
|
||||
assertEquals("abc".endsWith("abc", -1), false);
|
||||
assertEquals("abc".endsWith("bcd", -1), false);
|
||||
assertEquals("abc".endsWith("abcd", -1), false);
|
||||
assertEquals("abc".endsWith("bcde", -1), false);
|
||||
|
||||
assertEquals("abc".endsWith("", -0), true);
|
||||
assertEquals("abc".endsWith("\0", -0), false);
|
||||
assertEquals("abc".endsWith("c", -0), false);
|
||||
assertEquals("abc".endsWith("b", -0), false);
|
||||
assertEquals("abc".endsWith("ab", -0), false);
|
||||
assertEquals("abc".endsWith("bc", -0), false);
|
||||
assertEquals("abc".endsWith("abc", -0), false);
|
||||
assertEquals("abc".endsWith("bcd", -0), false);
|
||||
assertEquals("abc".endsWith("abcd", -0), false);
|
||||
assertEquals("abc".endsWith("bcde", -0), false);
|
||||
|
||||
assertEquals("abc".endsWith("", +0), true);
|
||||
assertEquals("abc".endsWith("\0", +0), false);
|
||||
assertEquals("abc".endsWith("c", +0), false);
|
||||
assertEquals("abc".endsWith("b", +0), false);
|
||||
assertEquals("abc".endsWith("ab", +0), false);
|
||||
assertEquals("abc".endsWith("bc", +0), false);
|
||||
assertEquals("abc".endsWith("abc", +0), false);
|
||||
assertEquals("abc".endsWith("bcd", +0), false);
|
||||
assertEquals("abc".endsWith("abcd", +0), false);
|
||||
assertEquals("abc".endsWith("bcde", +0), false);
|
||||
|
||||
assertEquals("abc".endsWith("", 1), true);
|
||||
assertEquals("abc".endsWith("\0", 1), false);
|
||||
assertEquals("abc".endsWith("c", 1), false);
|
||||
assertEquals("abc".endsWith("b", 1), false);
|
||||
assertEquals("abc".endsWith("ab", 1), false);
|
||||
assertEquals("abc".endsWith("bc", 1), false);
|
||||
assertEquals("abc".endsWith("abc", 1), false);
|
||||
assertEquals("abc".endsWith("bcd", 1), false);
|
||||
assertEquals("abc".endsWith("abcd", 1), false);
|
||||
assertEquals("abc".endsWith("bcde", 1), false);
|
||||
|
||||
assertEquals("abc".endsWith("", 2), true);
|
||||
assertEquals("abc".endsWith("\0", 2), false);
|
||||
assertEquals("abc".endsWith("c", 2), false);
|
||||
assertEquals("abc".endsWith("b", 2), true);
|
||||
assertEquals("abc".endsWith("ab", 2), true);
|
||||
assertEquals("abc".endsWith("bc", 2), false);
|
||||
assertEquals("abc".endsWith("abc", 2), false);
|
||||
assertEquals("abc".endsWith("bcd", 2), false);
|
||||
assertEquals("abc".endsWith("abcd", 2), false);
|
||||
assertEquals("abc".endsWith("bcde", 2), false);
|
||||
|
||||
assertEquals("abc".endsWith("", +Infinity), true);
|
||||
assertEquals("abc".endsWith("\0", +Infinity), false);
|
||||
assertEquals("abc".endsWith("c", +Infinity), true);
|
||||
assertEquals("abc".endsWith("b", +Infinity), false);
|
||||
assertEquals("abc".endsWith("ab", +Infinity), false);
|
||||
assertEquals("abc".endsWith("bc", +Infinity), true);
|
||||
assertEquals("abc".endsWith("abc", +Infinity), true);
|
||||
assertEquals("abc".endsWith("bcd", +Infinity), false);
|
||||
assertEquals("abc".endsWith("abcd", +Infinity), false);
|
||||
assertEquals("abc".endsWith("bcde", +Infinity), false);
|
||||
|
||||
assertEquals("abc".endsWith("", true), true);
|
||||
assertEquals("abc".endsWith("\0", true), false);
|
||||
assertEquals("abc".endsWith("c", true), false);
|
||||
assertEquals("abc".endsWith("b", true), false);
|
||||
assertEquals("abc".endsWith("ab", true), false);
|
||||
assertEquals("abc".endsWith("bc", true), false);
|
||||
assertEquals("abc".endsWith("abc", true), false);
|
||||
assertEquals("abc".endsWith("bcd", true), false);
|
||||
assertEquals("abc".endsWith("abcd", true), false);
|
||||
assertEquals("abc".endsWith("bcde", true), false);
|
||||
|
||||
assertEquals("abc".endsWith("", "x"), true);
|
||||
assertEquals("abc".endsWith("\0", "x"), false);
|
||||
assertEquals("abc".endsWith("c", "x"), false);
|
||||
assertEquals("abc".endsWith("b", "x"), false);
|
||||
assertEquals("abc".endsWith("ab", "x"), false);
|
||||
assertEquals("abc".endsWith("bc", "x"), false);
|
||||
assertEquals("abc".endsWith("abc", "x"), false);
|
||||
assertEquals("abc".endsWith("bcd", "x"), false);
|
||||
assertEquals("abc".endsWith("abcd", "x"), false);
|
||||
assertEquals("abc".endsWith("bcde", "x"), false);
|
||||
|
||||
assertEquals("[a-z]+(bar)?".endsWith("(bar)?"), true);
|
||||
assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/);
|
||||
}, TypeError);
|
||||
assertEquals("[a-z]+(bar)?".endsWith("[a-z]+", 6), true);
|
||||
assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/);
|
||||
}, TypeError);
|
||||
assertThrows(function() { "[a-z]+/(bar)?/".endsWith(/(bar)?/);
|
||||
}, TypeError);
|
||||
|
||||
// http://mathiasbynens.be/notes/javascript-unicode#poo-test
|
||||
var string = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9";
|
||||
assertEquals(string.endsWith(""), true);
|
||||
assertEquals(string.endsWith("\xF1t\xEBr"), false);
|
||||
assertEquals(string.endsWith("\xF1t\xEBr", 5), true);
|
||||
assertEquals(string.endsWith("\xE0liz\xE6"), false);
|
||||
assertEquals(string.endsWith("\xE0liz\xE6", 16), true);
|
||||
assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9"), true);
|
||||
assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9", 23), true);
|
||||
assertEquals(string.endsWith("\u2603"), false);
|
||||
assertEquals(string.endsWith("\u2603", 21), true);
|
||||
assertEquals(string.endsWith("\uD83D\uDCA9"), true);
|
||||
assertEquals(string.endsWith("\uD83D\uDCA9", 23), true);
|
||||
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call(undefined);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call(undefined, "b");
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call(undefined, "b", 4);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call(null);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call(null, "b");
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call(null, "b", 4);
|
||||
}, TypeError);
|
||||
assertEquals(String.prototype.endsWith.call(42, "2"), true);
|
||||
assertEquals(String.prototype.endsWith.call(42, "4"), false);
|
||||
assertEquals(String.prototype.endsWith.call(42, "b", 4), false);
|
||||
assertEquals(String.prototype.endsWith.call(42, "2", 1), false);
|
||||
assertEquals(String.prototype.endsWith.call(42, "2", 4), true);
|
||||
assertEquals(String.prototype.endsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, "b", 0), false);
|
||||
assertEquals(String.prototype.endsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, "b", 1), false);
|
||||
assertEquals(String.prototype.endsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, "b", 2), true);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call({
|
||||
"toString": function() { throw RangeError(); }
|
||||
}, /./);
|
||||
}, RangeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, /./);
|
||||
}, TypeError);
|
||||
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply(undefined);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply(undefined, ["b"]); },
|
||||
TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply(undefined, ["b", 4]);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply(null);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply(null, ["b"]);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply(null, ["b", 4]);
|
||||
}, TypeError);
|
||||
assertEquals(String.prototype.endsWith.apply(42, ["2"]), true);
|
||||
assertEquals(String.prototype.endsWith.apply(42, ["4"]), false);
|
||||
assertEquals(String.prototype.endsWith.apply(42, ["b", 4]), false);
|
||||
assertEquals(String.prototype.endsWith.apply(42, ["2", 1]), false);
|
||||
assertEquals(String.prototype.endsWith.apply(42, ["2", 4]), true);
|
||||
assertEquals(String.prototype.endsWith.apply({
|
||||
"toString": function() { return "abc"; }
|
||||
}, ["b", 0]), false);
|
||||
assertEquals(String.prototype.endsWith.apply({
|
||||
"toString": function() { return "abc"; }
|
||||
}, ["b", 1]), false);
|
||||
assertEquals(String.prototype.endsWith.apply({
|
||||
"toString": function() { return "abc"; }
|
||||
}, ["b", 2]), true);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply({
|
||||
"toString": function() { throw RangeError(); }
|
||||
}, [/./]);
|
||||
}, RangeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.endsWith.apply({
|
||||
"toString": function() { return "abc"; }
|
||||
}, [/./]);
|
||||
}, TypeError);
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright 2013 the V8 project authors. All rights reserved.
|
||||
// Copyright 2014 the V8 project authors. All rights reserved.
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
@ -65,8 +65,6 @@ var TEST_INPUT = [{
|
||||
msg: "Boolean true", val: true
|
||||
}, {
|
||||
msg: "Boolean false", val: false
|
||||
}, {
|
||||
msg: "Regular expression /\d+/", val: /\d+/
|
||||
}, {
|
||||
msg: "Empty array []", val: []
|
||||
}, {
|
||||
@ -133,3 +131,273 @@ assertTrue("abc".startsWith("ab", -43));
|
||||
assertTrue("abc".startsWith("ab", -Infinity));
|
||||
assertFalse("abc".startsWith("bc", -42));
|
||||
assertFalse("abc".startsWith("bc", -Infinity));
|
||||
|
||||
// Test cases taken from
|
||||
// https://github.com/mathiasbynens/String.prototype.startsWith/blob/master/tests/tests.js
|
||||
Object.prototype[1] = 2; // try to break `arguments[1]`
|
||||
|
||||
assertEquals(String.prototype.startsWith.length, 1);
|
||||
assertEquals(String.prototype.propertyIsEnumerable("startsWith"), false);
|
||||
|
||||
assertEquals("undefined".startsWith(), true);
|
||||
assertEquals("undefined".startsWith(undefined), true);
|
||||
assertEquals("undefined".startsWith(null), false);
|
||||
assertEquals("null".startsWith(), false);
|
||||
assertEquals("null".startsWith(undefined), false);
|
||||
assertEquals("null".startsWith(null), true);
|
||||
|
||||
assertEquals("abc".startsWith(), false);
|
||||
assertEquals("abc".startsWith(""), true);
|
||||
assertEquals("abc".startsWith("\0"), false);
|
||||
assertEquals("abc".startsWith("a"), true);
|
||||
assertEquals("abc".startsWith("b"), false);
|
||||
assertEquals("abc".startsWith("ab"), true);
|
||||
assertEquals("abc".startsWith("bc"), false);
|
||||
assertEquals("abc".startsWith("abc"), true);
|
||||
assertEquals("abc".startsWith("bcd"), false);
|
||||
assertEquals("abc".startsWith("abcd"), false);
|
||||
assertEquals("abc".startsWith("bcde"), false);
|
||||
|
||||
assertEquals("abc".startsWith("", NaN), true);
|
||||
assertEquals("abc".startsWith("\0", NaN), false);
|
||||
assertEquals("abc".startsWith("a", NaN), true);
|
||||
assertEquals("abc".startsWith("b", NaN), false);
|
||||
assertEquals("abc".startsWith("ab", NaN), true);
|
||||
assertEquals("abc".startsWith("bc", NaN), false);
|
||||
assertEquals("abc".startsWith("abc", NaN), true);
|
||||
assertEquals("abc".startsWith("bcd", NaN), false);
|
||||
assertEquals("abc".startsWith("abcd", NaN), false);
|
||||
assertEquals("abc".startsWith("bcde", NaN), false);
|
||||
|
||||
assertEquals("abc".startsWith("", false), true);
|
||||
assertEquals("abc".startsWith("\0", false), false);
|
||||
assertEquals("abc".startsWith("a", false), true);
|
||||
assertEquals("abc".startsWith("b", false), false);
|
||||
assertEquals("abc".startsWith("ab", false), true);
|
||||
assertEquals("abc".startsWith("bc", false), false);
|
||||
assertEquals("abc".startsWith("abc", false), true);
|
||||
assertEquals("abc".startsWith("bcd", false), false);
|
||||
assertEquals("abc".startsWith("abcd", false), false);
|
||||
assertEquals("abc".startsWith("bcde", false), false);
|
||||
|
||||
assertEquals("abc".startsWith("", undefined), true);
|
||||
assertEquals("abc".startsWith("\0", undefined), false);
|
||||
assertEquals("abc".startsWith("a", undefined), true);
|
||||
assertEquals("abc".startsWith("b", undefined), false);
|
||||
assertEquals("abc".startsWith("ab", undefined), true);
|
||||
assertEquals("abc".startsWith("bc", undefined), false);
|
||||
assertEquals("abc".startsWith("abc", undefined), true);
|
||||
assertEquals("abc".startsWith("bcd", undefined), false);
|
||||
assertEquals("abc".startsWith("abcd", undefined), false);
|
||||
assertEquals("abc".startsWith("bcde", undefined), false);
|
||||
|
||||
assertEquals("abc".startsWith("", null), true);
|
||||
assertEquals("abc".startsWith("\0", null), false);
|
||||
assertEquals("abc".startsWith("a", null), true);
|
||||
assertEquals("abc".startsWith("b", null), false);
|
||||
assertEquals("abc".startsWith("ab", null), true);
|
||||
assertEquals("abc".startsWith("bc", null), false);
|
||||
assertEquals("abc".startsWith("abc", null), true);
|
||||
assertEquals("abc".startsWith("bcd", null), false);
|
||||
assertEquals("abc".startsWith("abcd", null), false);
|
||||
assertEquals("abc".startsWith("bcde", null), false);
|
||||
|
||||
assertEquals("abc".startsWith("", -Infinity), true);
|
||||
assertEquals("abc".startsWith("\0", -Infinity), false);
|
||||
assertEquals("abc".startsWith("a", -Infinity), true);
|
||||
assertEquals("abc".startsWith("b", -Infinity), false);
|
||||
assertEquals("abc".startsWith("ab", -Infinity), true);
|
||||
assertEquals("abc".startsWith("bc", -Infinity), false);
|
||||
assertEquals("abc".startsWith("abc", -Infinity), true);
|
||||
assertEquals("abc".startsWith("bcd", -Infinity), false);
|
||||
assertEquals("abc".startsWith("abcd", -Infinity), false);
|
||||
assertEquals("abc".startsWith("bcde", -Infinity), false);
|
||||
|
||||
assertEquals("abc".startsWith("", -1), true);
|
||||
assertEquals("abc".startsWith("\0", -1), false);
|
||||
assertEquals("abc".startsWith("a", -1), true);
|
||||
assertEquals("abc".startsWith("b", -1), false);
|
||||
assertEquals("abc".startsWith("ab", -1), true);
|
||||
assertEquals("abc".startsWith("bc", -1), false);
|
||||
assertEquals("abc".startsWith("abc", -1), true);
|
||||
assertEquals("abc".startsWith("bcd", -1), false);
|
||||
assertEquals("abc".startsWith("abcd", -1), false);
|
||||
assertEquals("abc".startsWith("bcde", -1), false);
|
||||
|
||||
assertEquals("abc".startsWith("", -0), true);
|
||||
assertEquals("abc".startsWith("\0", -0), false);
|
||||
assertEquals("abc".startsWith("a", -0), true);
|
||||
assertEquals("abc".startsWith("b", -0), false);
|
||||
assertEquals("abc".startsWith("ab", -0), true);
|
||||
assertEquals("abc".startsWith("bc", -0), false);
|
||||
assertEquals("abc".startsWith("abc", -0), true);
|
||||
assertEquals("abc".startsWith("bcd", -0), false);
|
||||
assertEquals("abc".startsWith("abcd", -0), false);
|
||||
assertEquals("abc".startsWith("bcde", -0), false);
|
||||
|
||||
assertEquals("abc".startsWith("", +0), true);
|
||||
assertEquals("abc".startsWith("\0", +0), false);
|
||||
assertEquals("abc".startsWith("a", +0), true);
|
||||
assertEquals("abc".startsWith("b", +0), false);
|
||||
assertEquals("abc".startsWith("ab", +0), true);
|
||||
assertEquals("abc".startsWith("bc", +0), false);
|
||||
assertEquals("abc".startsWith("abc", +0), true);
|
||||
assertEquals("abc".startsWith("bcd", +0), false);
|
||||
assertEquals("abc".startsWith("abcd", +0), false);
|
||||
assertEquals("abc".startsWith("bcde", +0), false);
|
||||
|
||||
assertEquals("abc".startsWith("", 1), true);
|
||||
assertEquals("abc".startsWith("\0", 1), false);
|
||||
assertEquals("abc".startsWith("a", 1), false);
|
||||
assertEquals("abc".startsWith("b", 1), true);
|
||||
assertEquals("abc".startsWith("ab", 1), false);
|
||||
assertEquals("abc".startsWith("bc", 1), true);
|
||||
assertEquals("abc".startsWith("abc", 1), false);
|
||||
assertEquals("abc".startsWith("bcd", 1), false);
|
||||
assertEquals("abc".startsWith("abcd", 1), false);
|
||||
assertEquals("abc".startsWith("bcde", 1), false);
|
||||
|
||||
assertEquals("abc".startsWith("", +Infinity), true);
|
||||
assertEquals("abc".startsWith("\0", +Infinity), false);
|
||||
assertEquals("abc".startsWith("a", +Infinity), false);
|
||||
assertEquals("abc".startsWith("b", +Infinity), false);
|
||||
assertEquals("abc".startsWith("ab", +Infinity), false);
|
||||
assertEquals("abc".startsWith("bc", +Infinity), false);
|
||||
assertEquals("abc".startsWith("abc", +Infinity), false);
|
||||
assertEquals("abc".startsWith("bcd", +Infinity), false);
|
||||
assertEquals("abc".startsWith("abcd", +Infinity), false);
|
||||
assertEquals("abc".startsWith("bcde", +Infinity), false);
|
||||
|
||||
assertEquals("abc".startsWith("", true), true);
|
||||
assertEquals("abc".startsWith("\0", true), false);
|
||||
assertEquals("abc".startsWith("a", true), false);
|
||||
assertEquals("abc".startsWith("b", true), true);
|
||||
assertEquals("abc".startsWith("ab", true), false);
|
||||
assertEquals("abc".startsWith("bc", true), true);
|
||||
assertEquals("abc".startsWith("abc", true), false);
|
||||
assertEquals("abc".startsWith("bcd", true), false);
|
||||
assertEquals("abc".startsWith("abcd", true), false);
|
||||
assertEquals("abc".startsWith("bcde", true), false);
|
||||
|
||||
assertEquals("abc".startsWith("", "x"), true);
|
||||
assertEquals("abc".startsWith("\0", "x"), false);
|
||||
assertEquals("abc".startsWith("a", "x"), true);
|
||||
assertEquals("abc".startsWith("b", "x"), false);
|
||||
assertEquals("abc".startsWith("ab", "x"), true);
|
||||
assertEquals("abc".startsWith("bc", "x"), false);
|
||||
assertEquals("abc".startsWith("abc", "x"), true);
|
||||
assertEquals("abc".startsWith("bcd", "x"), false);
|
||||
assertEquals("abc".startsWith("abcd", "x"), false);
|
||||
assertEquals("abc".startsWith("bcde", "x"), false);
|
||||
|
||||
assertEquals("[a-z]+(bar)?".startsWith("[a-z]+"), true);
|
||||
assertThrows(function() { "[a-z]+(bar)?".startsWith(/[a-z]+/); }, TypeError);
|
||||
assertEquals("[a-z]+(bar)?".startsWith("(bar)?", 6), true);
|
||||
assertThrows(function() { "[a-z]+(bar)?".startsWith(/(bar)?/); }, TypeError);
|
||||
assertThrows(function() { "[a-z]+/(bar)?/".startsWith(/(bar)?/); }, TypeError);
|
||||
|
||||
// http://mathiasbynens.be/notes/javascript-unicode#poo-test
|
||||
var string = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9";
|
||||
assertEquals(string.startsWith(""), true);
|
||||
assertEquals(string.startsWith("\xF1t\xEBr"), false);
|
||||
assertEquals(string.startsWith("\xF1t\xEBr", 1), true);
|
||||
assertEquals(string.startsWith("\xE0liz\xE6"), false);
|
||||
assertEquals(string.startsWith("\xE0liz\xE6", 11), true);
|
||||
assertEquals(string.startsWith("\xF8n\u2603\uD83D\uDCA9"), false);
|
||||
assertEquals(string.startsWith("\xF8n\u2603\uD83D\uDCA9", 18), true);
|
||||
assertEquals(string.startsWith("\u2603"), false);
|
||||
assertEquals(string.startsWith("\u2603", 20), true);
|
||||
assertEquals(string.startsWith("\uD83D\uDCA9"), false);
|
||||
assertEquals(string.startsWith("\uD83D\uDCA9", 21), true);
|
||||
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call(undefined);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call(undefined, "b");
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call(undefined, "b", 4);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call(null);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call(null, "b");
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call(null, "b", 4);
|
||||
}, TypeError);
|
||||
assertEquals(String.prototype.startsWith.call(42, "2"), false);
|
||||
assertEquals(String.prototype.startsWith.call(42, "4"), true);
|
||||
assertEquals(String.prototype.startsWith.call(42, "b", 4), false);
|
||||
assertEquals(String.prototype.startsWith.call(42, "2", 1), true);
|
||||
assertEquals(String.prototype.startsWith.call(42, "2", 4), false);
|
||||
assertEquals(String.prototype.startsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, "b", 0), false);
|
||||
assertEquals(String.prototype.startsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, "b", 1), true);
|
||||
assertEquals(String.prototype.startsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, "b", 2), false);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call({
|
||||
"toString": function() { throw RangeError(); }
|
||||
}, /./);
|
||||
}, RangeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.call({
|
||||
"toString": function() { return "abc"; }
|
||||
}, /./);
|
||||
}, TypeError);
|
||||
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply(undefined);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply(undefined, ["b"]);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply(undefined, ["b", 4]);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply(null);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply(null, ["b"]);
|
||||
}, TypeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply(null, ["b", 4]);
|
||||
}, TypeError);
|
||||
assertEquals(String.prototype.startsWith.apply(42, ["2"]), false);
|
||||
assertEquals(String.prototype.startsWith.apply(42, ["4"]), true);
|
||||
assertEquals(String.prototype.startsWith.apply(42, ["b", 4]), false);
|
||||
assertEquals(String.prototype.startsWith.apply(42, ["2", 1]), true);
|
||||
assertEquals(String.prototype.startsWith.apply(42, ["2", 4]), false);
|
||||
assertEquals(String.prototype.startsWith.apply({
|
||||
"toString": function() {
|
||||
return "abc";
|
||||
}
|
||||
}, ["b", 0]), false);
|
||||
assertEquals(String.prototype.startsWith.apply({
|
||||
"toString": function() {
|
||||
return "abc";
|
||||
}
|
||||
}, ["b", 1]), true);
|
||||
assertEquals(String.prototype.startsWith.apply({
|
||||
"toString": function() {
|
||||
return "abc";
|
||||
}
|
||||
}, ["b", 2]), false);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply({
|
||||
"toString": function() { throw RangeError(); }
|
||||
}, [/./]);
|
||||
}, RangeError);
|
||||
assertThrows(function() {
|
||||
String.prototype.startsWith.apply({
|
||||
"toString": function() { return "abc"; }
|
||||
}, [/./]);
|
||||
}, TypeError);
|
||||
|
Loading…
Reference in New Issue
Block a user