1// Copyright 2013 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: "Regular expression /\d+/", val: /\d+/
70}, {
71  msg: "Empty array []", val: []
72}, {
73  msg: "Empty object {}", val: {}
74}, {
75  msg: "Array of size 3", val: new Array(3)
76}];
77
78function testNonStringValues() {
79  var i = 0;
80  var l = TEST_INPUT.length;
81
82  for (; i < l; i++) {
83    var e = TEST_INPUT[i];
84    var v = e.val;
85    var s = String(v);
86    assertTrue(s.endsWith(v), e.msg);
87    assertTrue(String.prototype.endsWith.call(v, v), e.msg);
88    assertTrue(String.prototype.endsWith.apply(v, [v]), e.msg);
89  }
90}
91testNonStringValues();
92
93var CustomType = function(value) {
94  this.endsWith = String.prototype.endsWith;
95  this.toString = function() {
96    return String(value);
97  }
98};
99
100function testCutomType() {
101  var i = 0;
102  var l = TEST_INPUT.length;
103
104  for (; i < l; i++) {
105    var e = TEST_INPUT[i];
106    var v = e.val;
107    var o = new CustomType(v);
108    assertTrue(o.endsWith(v), e.msg);
109  }
110}
111testCutomType();
112
113
114// Test cases found in FF
115assertTrue("abc".endsWith("abc"));
116assertTrue("abcd".endsWith("bcd"));
117assertTrue("abc".endsWith("c"));
118assertFalse("abc".endsWith("abcd"));
119assertFalse("abc".endsWith("bbc"));
120assertFalse("abc".endsWith("b"));
121assertTrue("abc".endsWith("abc", 3));
122assertTrue("abc".endsWith("bc", 3));
123assertFalse("abc".endsWith("a", 3));
124assertTrue("abc".endsWith("bc", 3));
125assertTrue("abc".endsWith("a", 1));
126assertFalse("abc".endsWith("abc", 1));
127assertTrue("abc".endsWith("b", 2));
128assertFalse("abc".endsWith("d", 2));
129assertFalse("abc".endsWith("dcd", 2));
130assertFalse("abc".endsWith("a", 42));
131assertTrue("abc".endsWith("bc", Infinity));
132assertFalse("abc".endsWith("a", Infinity));
133assertTrue("abc".endsWith("bc", undefined));
134assertFalse("abc".endsWith("bc", -43));
135assertFalse("abc".endsWith("bc", -Infinity));
136assertFalse("abc".endsWith("bc", NaN));
137