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