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 30assertEquals(1, String.prototype.endsWith.length); 31 32var testString = "Hello World"; 33assertTrue(testString.endsWith("")); 34assertTrue(testString.endsWith("World")); 35assertFalse(testString.endsWith("world")); 36assertFalse(testString.endsWith("Hello World!")); 37assertFalse(testString.endsWith(null)); 38assertFalse(testString.endsWith(undefined)); 39 40assertTrue("null".endsWith(null)); 41assertTrue("undefined".endsWith(undefined)); 42 43var georgianUnicodeString = "\u10D0\u10D1\u10D2\u10D3\u10D4\u10D5\u10D6\u10D7"; 44assertTrue(georgianUnicodeString.endsWith(georgianUnicodeString)); 45assertTrue(georgianUnicodeString.endsWith("\u10D4\u10D5\u10D6\u10D7")); 46assertFalse(georgianUnicodeString.endsWith("\u10D0")); 47 48assertThrows("String.prototype.endsWith.call(null, 'test')", TypeError); 49assertThrows("String.prototype.endsWith.call(null, null)", TypeError); 50assertThrows("String.prototype.endsWith.call(undefined, undefined)", TypeError); 51 52assertThrows("String.prototype.endsWith.apply(null, ['test'])", TypeError); 53assertThrows("String.prototype.endsWith.apply(null, [null])", TypeError); 54assertThrows("String.prototype.endsWith.apply(undefined, [undefined])", TypeError); 55 56var 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 76function 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.endsWith(v), e.msg); 85 assertTrue(String.prototype.endsWith.call(v, v), e.msg); 86 assertTrue(String.prototype.endsWith.apply(v, [v]), e.msg); 87 } 88} 89testNonStringValues(); 90 91var CustomType = function(value) { 92 this.endsWith = String.prototype.endsWith; 93 this.toString = function() { 94 return String(value); 95 } 96}; 97 98function 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.endsWith(v), e.msg); 107 } 108} 109testCutomType(); 110 111 112// Test cases found in FF 113assertTrue("abc".endsWith("abc")); 114assertTrue("abcd".endsWith("bcd")); 115assertTrue("abc".endsWith("c")); 116assertFalse("abc".endsWith("abcd")); 117assertFalse("abc".endsWith("bbc")); 118assertFalse("abc".endsWith("b")); 119assertTrue("abc".endsWith("abc", 3)); 120assertTrue("abc".endsWith("bc", 3)); 121assertFalse("abc".endsWith("a", 3)); 122assertTrue("abc".endsWith("bc", 3)); 123assertTrue("abc".endsWith("a", 1)); 124assertFalse("abc".endsWith("abc", 1)); 125assertTrue("abc".endsWith("b", 2)); 126assertFalse("abc".endsWith("d", 2)); 127assertFalse("abc".endsWith("dcd", 2)); 128assertFalse("abc".endsWith("a", 42)); 129assertTrue("abc".endsWith("bc", Infinity)); 130assertFalse("abc".endsWith("a", Infinity)); 131assertTrue("abc".endsWith("bc", undefined)); 132assertFalse("abc".endsWith("bc", -43)); 133assertFalse("abc".endsWith("bc", -Infinity)); 134assertFalse("abc".endsWith("bc", NaN)); 135 136// Test cases taken from 137// https://github.com/mathiasbynens/String.prototype.endsWith/blob/master/tests/tests.js 138Object.prototype[1] = 2; // try to break `arguments[1]` 139 140assertEquals(String.prototype.endsWith.length, 1); 141assertEquals(String.prototype.propertyIsEnumerable("endsWith"), false); 142 143assertEquals("undefined".endsWith(), true); 144assertEquals("undefined".endsWith(undefined), true); 145assertEquals("undefined".endsWith(null), false); 146assertEquals("null".endsWith(), false); 147assertEquals("null".endsWith(undefined), false); 148assertEquals("null".endsWith(null), true); 149 150assertEquals("abc".endsWith(), false); 151assertEquals("abc".endsWith(""), true); 152assertEquals("abc".endsWith("\0"), false); 153assertEquals("abc".endsWith("c"), true); 154assertEquals("abc".endsWith("b"), false); 155assertEquals("abc".endsWith("ab"), false); 156assertEquals("abc".endsWith("bc"), true); 157assertEquals("abc".endsWith("abc"), true); 158assertEquals("abc".endsWith("bcd"), false); 159assertEquals("abc".endsWith("abcd"), false); 160assertEquals("abc".endsWith("bcde"), false); 161 162assertEquals("abc".endsWith("", NaN), true); 163assertEquals("abc".endsWith("\0", NaN), false); 164assertEquals("abc".endsWith("c", NaN), false); 165assertEquals("abc".endsWith("b", NaN), false); 166assertEquals("abc".endsWith("ab", NaN), false); 167assertEquals("abc".endsWith("bc", NaN), false); 168assertEquals("abc".endsWith("abc", NaN), false); 169assertEquals("abc".endsWith("bcd", NaN), false); 170assertEquals("abc".endsWith("abcd", NaN), false); 171assertEquals("abc".endsWith("bcde", NaN), false); 172 173assertEquals("abc".endsWith("", false), true); 174assertEquals("abc".endsWith("\0", false), false); 175assertEquals("abc".endsWith("c", false), false); 176assertEquals("abc".endsWith("b", false), false); 177assertEquals("abc".endsWith("ab", false), false); 178assertEquals("abc".endsWith("bc", false), false); 179assertEquals("abc".endsWith("abc", false), false); 180assertEquals("abc".endsWith("bcd", false), false); 181assertEquals("abc".endsWith("abcd", false), false); 182assertEquals("abc".endsWith("bcde", false), false); 183 184assertEquals("abc".endsWith("", undefined), true); 185assertEquals("abc".endsWith("\0", undefined), false); 186assertEquals("abc".endsWith("c", undefined), true); 187assertEquals("abc".endsWith("b", undefined), false); 188assertEquals("abc".endsWith("ab", undefined), false); 189assertEquals("abc".endsWith("bc", undefined), true); 190assertEquals("abc".endsWith("abc", undefined), true); 191assertEquals("abc".endsWith("bcd", undefined), false); 192assertEquals("abc".endsWith("abcd", undefined), false); 193assertEquals("abc".endsWith("bcde", undefined), false); 194 195assertEquals("abc".endsWith("", null), true); 196assertEquals("abc".endsWith("\0", null), false); 197assertEquals("abc".endsWith("c", null), false); 198assertEquals("abc".endsWith("b", null), false); 199assertEquals("abc".endsWith("ab", null), false); 200assertEquals("abc".endsWith("bc", null), false); 201assertEquals("abc".endsWith("abc", null), false); 202assertEquals("abc".endsWith("bcd", null), false); 203assertEquals("abc".endsWith("abcd", null), false); 204assertEquals("abc".endsWith("bcde", null), false); 205 206assertEquals("abc".endsWith("", -Infinity), true); 207assertEquals("abc".endsWith("\0", -Infinity), false); 208assertEquals("abc".endsWith("c", -Infinity), false); 209assertEquals("abc".endsWith("b", -Infinity), false); 210assertEquals("abc".endsWith("ab", -Infinity), false); 211assertEquals("abc".endsWith("bc", -Infinity), false); 212assertEquals("abc".endsWith("abc", -Infinity), false); 213assertEquals("abc".endsWith("bcd", -Infinity), false); 214assertEquals("abc".endsWith("abcd", -Infinity), false); 215assertEquals("abc".endsWith("bcde", -Infinity), false); 216 217assertEquals("abc".endsWith("", -1), true); 218assertEquals("abc".endsWith("\0", -1), false); 219assertEquals("abc".endsWith("c", -1), false); 220assertEquals("abc".endsWith("b", -1), false); 221assertEquals("abc".endsWith("ab", -1), false); 222assertEquals("abc".endsWith("bc", -1), false); 223assertEquals("abc".endsWith("abc", -1), false); 224assertEquals("abc".endsWith("bcd", -1), false); 225assertEquals("abc".endsWith("abcd", -1), false); 226assertEquals("abc".endsWith("bcde", -1), false); 227 228assertEquals("abc".endsWith("", -0), true); 229assertEquals("abc".endsWith("\0", -0), false); 230assertEquals("abc".endsWith("c", -0), false); 231assertEquals("abc".endsWith("b", -0), false); 232assertEquals("abc".endsWith("ab", -0), false); 233assertEquals("abc".endsWith("bc", -0), false); 234assertEquals("abc".endsWith("abc", -0), false); 235assertEquals("abc".endsWith("bcd", -0), false); 236assertEquals("abc".endsWith("abcd", -0), false); 237assertEquals("abc".endsWith("bcde", -0), false); 238 239assertEquals("abc".endsWith("", +0), true); 240assertEquals("abc".endsWith("\0", +0), false); 241assertEquals("abc".endsWith("c", +0), false); 242assertEquals("abc".endsWith("b", +0), false); 243assertEquals("abc".endsWith("ab", +0), false); 244assertEquals("abc".endsWith("bc", +0), false); 245assertEquals("abc".endsWith("abc", +0), false); 246assertEquals("abc".endsWith("bcd", +0), false); 247assertEquals("abc".endsWith("abcd", +0), false); 248assertEquals("abc".endsWith("bcde", +0), false); 249 250assertEquals("abc".endsWith("", 1), true); 251assertEquals("abc".endsWith("\0", 1), false); 252assertEquals("abc".endsWith("c", 1), false); 253assertEquals("abc".endsWith("b", 1), false); 254assertEquals("abc".endsWith("ab", 1), false); 255assertEquals("abc".endsWith("bc", 1), false); 256assertEquals("abc".endsWith("abc", 1), false); 257assertEquals("abc".endsWith("bcd", 1), false); 258assertEquals("abc".endsWith("abcd", 1), false); 259assertEquals("abc".endsWith("bcde", 1), false); 260 261assertEquals("abc".endsWith("", 2), true); 262assertEquals("abc".endsWith("\0", 2), false); 263assertEquals("abc".endsWith("c", 2), false); 264assertEquals("abc".endsWith("b", 2), true); 265assertEquals("abc".endsWith("ab", 2), true); 266assertEquals("abc".endsWith("bc", 2), false); 267assertEquals("abc".endsWith("abc", 2), false); 268assertEquals("abc".endsWith("bcd", 2), false); 269assertEquals("abc".endsWith("abcd", 2), false); 270assertEquals("abc".endsWith("bcde", 2), false); 271 272assertEquals("abc".endsWith("", +Infinity), true); 273assertEquals("abc".endsWith("\0", +Infinity), false); 274assertEquals("abc".endsWith("c", +Infinity), true); 275assertEquals("abc".endsWith("b", +Infinity), false); 276assertEquals("abc".endsWith("ab", +Infinity), false); 277assertEquals("abc".endsWith("bc", +Infinity), true); 278assertEquals("abc".endsWith("abc", +Infinity), true); 279assertEquals("abc".endsWith("bcd", +Infinity), false); 280assertEquals("abc".endsWith("abcd", +Infinity), false); 281assertEquals("abc".endsWith("bcde", +Infinity), false); 282 283assertEquals("abc".endsWith("", true), true); 284assertEquals("abc".endsWith("\0", true), false); 285assertEquals("abc".endsWith("c", true), false); 286assertEquals("abc".endsWith("b", true), false); 287assertEquals("abc".endsWith("ab", true), false); 288assertEquals("abc".endsWith("bc", true), false); 289assertEquals("abc".endsWith("abc", true), false); 290assertEquals("abc".endsWith("bcd", true), false); 291assertEquals("abc".endsWith("abcd", true), false); 292assertEquals("abc".endsWith("bcde", true), false); 293 294assertEquals("abc".endsWith("", "x"), true); 295assertEquals("abc".endsWith("\0", "x"), false); 296assertEquals("abc".endsWith("c", "x"), false); 297assertEquals("abc".endsWith("b", "x"), false); 298assertEquals("abc".endsWith("ab", "x"), false); 299assertEquals("abc".endsWith("bc", "x"), false); 300assertEquals("abc".endsWith("abc", "x"), false); 301assertEquals("abc".endsWith("bcd", "x"), false); 302assertEquals("abc".endsWith("abcd", "x"), false); 303assertEquals("abc".endsWith("bcde", "x"), false); 304 305assertEquals("[a-z]+(bar)?".endsWith("(bar)?"), true); 306assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/); 307}, TypeError); 308assertEquals("[a-z]+(bar)?".endsWith("[a-z]+", 6), true); 309assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/); 310}, TypeError); 311assertThrows(function() { "[a-z]+/(bar)?/".endsWith(/(bar)?/); 312}, TypeError); 313 314// http://mathiasbynens.be/notes/javascript-unicode#poo-test 315var string = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9"; 316assertEquals(string.endsWith(""), true); 317assertEquals(string.endsWith("\xF1t\xEBr"), false); 318assertEquals(string.endsWith("\xF1t\xEBr", 5), true); 319assertEquals(string.endsWith("\xE0liz\xE6"), false); 320assertEquals(string.endsWith("\xE0liz\xE6", 16), true); 321assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9"), true); 322assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9", 23), true); 323assertEquals(string.endsWith("\u2603"), false); 324assertEquals(string.endsWith("\u2603", 21), true); 325assertEquals(string.endsWith("\uD83D\uDCA9"), true); 326assertEquals(string.endsWith("\uD83D\uDCA9", 23), true); 327 328assertThrows(function() { 329 String.prototype.endsWith.call(undefined); 330}, TypeError); 331assertThrows(function() { 332 String.prototype.endsWith.call(undefined, "b"); 333}, TypeError); 334assertThrows(function() { 335 String.prototype.endsWith.call(undefined, "b", 4); 336}, TypeError); 337assertThrows(function() { 338 String.prototype.endsWith.call(null); 339}, TypeError); 340assertThrows(function() { 341 String.prototype.endsWith.call(null, "b"); 342}, TypeError); 343assertThrows(function() { 344 String.prototype.endsWith.call(null, "b", 4); 345}, TypeError); 346assertEquals(String.prototype.endsWith.call(42, "2"), true); 347assertEquals(String.prototype.endsWith.call(42, "4"), false); 348assertEquals(String.prototype.endsWith.call(42, "b", 4), false); 349assertEquals(String.prototype.endsWith.call(42, "2", 1), false); 350assertEquals(String.prototype.endsWith.call(42, "2", 4), true); 351assertEquals(String.prototype.endsWith.call({ 352 "toString": function() { return "abc"; } 353}, "b", 0), false); 354assertEquals(String.prototype.endsWith.call({ 355 "toString": function() { return "abc"; } 356}, "b", 1), false); 357assertEquals(String.prototype.endsWith.call({ 358 "toString": function() { return "abc"; } 359}, "b", 2), true); 360assertThrows(function() { 361 String.prototype.endsWith.call({ 362 "toString": function() { throw RangeError(); } 363 }, /./); 364}, RangeError); 365assertThrows(function() { 366 String.prototype.endsWith.call({ 367 "toString": function() { return "abc"; } 368 }, /./); 369}, TypeError); 370 371assertThrows(function() { 372 String.prototype.endsWith.apply(undefined); 373}, TypeError); 374assertThrows(function() { 375 String.prototype.endsWith.apply(undefined, ["b"]); }, 376TypeError); 377assertThrows(function() { 378 String.prototype.endsWith.apply(undefined, ["b", 4]); 379}, TypeError); 380assertThrows(function() { 381 String.prototype.endsWith.apply(null); 382}, TypeError); 383assertThrows(function() { 384 String.prototype.endsWith.apply(null, ["b"]); 385}, TypeError); 386assertThrows(function() { 387 String.prototype.endsWith.apply(null, ["b", 4]); 388}, TypeError); 389assertEquals(String.prototype.endsWith.apply(42, ["2"]), true); 390assertEquals(String.prototype.endsWith.apply(42, ["4"]), false); 391assertEquals(String.prototype.endsWith.apply(42, ["b", 4]), false); 392assertEquals(String.prototype.endsWith.apply(42, ["2", 1]), false); 393assertEquals(String.prototype.endsWith.apply(42, ["2", 4]), true); 394assertEquals(String.prototype.endsWith.apply({ 395 "toString": function() { return "abc"; } 396}, ["b", 0]), false); 397assertEquals(String.prototype.endsWith.apply({ 398 "toString": function() { return "abc"; } 399}, ["b", 1]), false); 400assertEquals(String.prototype.endsWith.apply({ 401 "toString": function() { return "abc"; } 402}, ["b", 2]), true); 403assertThrows(function() { 404 String.prototype.endsWith.apply({ 405 "toString": function() { throw RangeError(); } 406 }, [/./]); 407}, RangeError); 408assertThrows(function() { 409 String.prototype.endsWith.apply({ 410 "toString": function() { return "abc"; } 411 }, [/./]); 412}, TypeError); 413