• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
28assertEquals(1, String.prototype.includes.length);
29
30var reString = "asdf[a-z]+(asdf)?";
31assertTrue(reString.includes("[a-z]+"));
32assertTrue(reString.includes("(asdf)?"));
33
34// Random greek letters
35var twoByteString = "\u039a\u0391\u03a3\u03a3\u0395";
36
37// Test single char pattern
38assertTrue(twoByteString.includes("\u039a"), "Lamda");
39assertTrue(twoByteString.includes("\u0391"), "Alpha");
40assertTrue(twoByteString.includes("\u03a3"), "First Sigma");
41assertTrue(twoByteString.includes("\u03a3",3), "Second Sigma");
42assertTrue(twoByteString.includes("\u0395"), "Epsilon");
43assertFalse(twoByteString.includes("\u0392"), "Not beta");
44
45// Test multi-char pattern
46assertTrue(twoByteString.includes("\u039a\u0391"), "lambda Alpha");
47assertTrue(twoByteString.includes("\u0391\u03a3"), "Alpha Sigma");
48assertTrue(twoByteString.includes("\u03a3\u03a3"), "Sigma Sigma");
49assertTrue(twoByteString.includes("\u03a3\u0395"), "Sigma Epsilon");
50
51assertFalse(twoByteString.includes("\u0391\u03a3\u0395"),
52    "Not Alpha Sigma Epsilon");
53
54//single char pattern
55assertTrue(twoByteString.includes("\u0395"));
56
57assertThrows("String.prototype.includes.call(null, 'test')", TypeError);
58assertThrows("String.prototype.includes.call(null, null)", TypeError);
59assertThrows("String.prototype.includes.call(undefined, undefined)", TypeError);
60
61assertThrows("String.prototype.includes.apply(null, ['test'])", TypeError);
62assertThrows("String.prototype.includes.apply(null, [null])", TypeError);
63assertThrows("String.prototype.includes.apply(undefined, [undefined])", TypeError);
64
65var TEST_INPUT = [{
66  msg: "Empty string", val: ""
67}, {
68  msg: "Number 1234.34", val: 1234.34
69}, {
70  msg: "Integer number 0", val: 0
71}, {
72  msg: "Negative number -1", val: -1
73}, {
74  msg: "Boolean true", val: true
75}, {
76  msg: "Boolean false", val: false
77}, {
78  msg: "Empty array []", val: []
79}, {
80  msg: "Empty object {}", val: {}
81}, {
82  msg: "Array of size 3", val: new Array(3)
83}];
84
85var i = 0;
86var l = TEST_INPUT.length;
87
88for (; i < l; i++) {
89  var e = TEST_INPUT[i];
90  var v = e.val;
91  var s = String(v);
92  assertTrue(s.includes(v), e.msg);
93  assertTrue(String.prototype.includes.call(v, v), e.msg);
94  assertTrue(String.prototype.includes.apply(v, [v]), e.msg);
95}
96
97// Test cases found in FF
98assertTrue("abc".includes("a"));
99assertTrue("abc".includes("b"));
100assertTrue("abc".includes("abc"));
101assertTrue("abc".includes("bc"));
102assertFalse("abc".includes("d"));
103assertFalse("abc".includes("abcd"));
104assertFalse("abc".includes("ac"));
105assertTrue("abc".includes("abc", 0));
106assertTrue("abc".includes("bc", 0));
107assertFalse("abc".includes("de", 0));
108assertTrue("abc".includes("bc", 1));
109assertTrue("abc".includes("c", 1));
110assertFalse("abc".includes("a", 1));
111assertFalse("abc".includes("abc", 1));
112assertTrue("abc".includes("c", 2));
113assertFalse("abc".includes("d", 2));
114assertFalse("abc".includes("dcd", 2));
115assertFalse("abc".includes("a", 42));
116assertFalse("abc".includes("a", Infinity));
117assertTrue("abc".includes("ab", -43));
118assertFalse("abc".includes("cd", -42));
119assertTrue("abc".includes("ab", -Infinity));
120assertFalse("abc".includes("cd", -Infinity));
121assertTrue("abc".includes("ab", NaN));
122assertFalse("abc".includes("cd", NaN));
123assertFalse("xyzzy".includes("zy\0", 2));
124
125var dots = Array(10000).join(".");
126assertFalse(dots.includes("\x01", 10000));
127assertFalse(dots.includes("\0", 10000));
128
129var myobj = {
130  toString: function () {
131    return "abc";
132  },
133  includes: String.prototype.includes
134};
135assertTrue(myobj.includes("abc"));
136assertFalse(myobj.includes("cd"));
137
138var gotStr = false;
139var gotPos = false;
140myobj = {
141  toString: function () {
142    assertFalse(gotPos);
143    gotStr = true;
144    return "xyz";
145  },
146  includes: String.prototype.includes
147};
148
149assertEquals("foo[a-z]+(bar)?".includes("[a-z]+"), true);
150assertThrows("'foo[a-z]+(bar)?'.includes(/[a-z]+/)", TypeError);
151assertThrows("'foo/[a-z]+/(bar)?'.includes(/[a-z]+/)", TypeError);
152assertEquals("foo[a-z]+(bar)?".includes("(bar)?"), true);
153assertThrows("'foo[a-z]+(bar)?'.includes(/(bar)?/)", TypeError);
154assertThrows("'foo[a-z]+/(bar)?/'.includes(/(bar)?/)", TypeError);
155
156assertThrows("String.prototype.includes.call({ 'toString': function() { " +
157  "throw RangeError(); } }, /./)", RangeError);
158assertThrows("String.prototype.includes.call({ 'toString': function() { " +
159  "return 'abc'; } }, /./)", TypeError);
160
161assertThrows("String.prototype.includes.apply({ 'toString': function() { " +
162  "throw RangeError(); } }, [/./])", RangeError);
163assertThrows("String.prototype.includes.apply({ 'toString': function() { " +
164  "return 'abc'; } }, [/./])", TypeError);
165