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
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.
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.
28 // Flags: --harmony-strings
30 assertEquals(1, String.prototype.includes.length);
32 var reString = "asdf[a-z]+(asdf)?";
33 assertTrue(reString.includes("[a-z]+"));
34 assertTrue(reString.includes("(asdf)?"));
36 // Random greek letters
37 var twoByteString = "\u039a\u0391\u03a3\u03a3\u0395";
39 // Test single char pattern
40 assertTrue(twoByteString.includes("\u039a"), "Lamda");
41 assertTrue(twoByteString.includes("\u0391"), "Alpha");
42 assertTrue(twoByteString.includes("\u03a3"), "First Sigma");
43 assertTrue(twoByteString.includes("\u03a3",3), "Second Sigma");
44 assertTrue(twoByteString.includes("\u0395"), "Epsilon");
45 assertFalse(twoByteString.includes("\u0392"), "Not beta");
47 // Test multi-char pattern
48 assertTrue(twoByteString.includes("\u039a\u0391"), "lambda Alpha");
49 assertTrue(twoByteString.includes("\u0391\u03a3"), "Alpha Sigma");
50 assertTrue(twoByteString.includes("\u03a3\u03a3"), "Sigma Sigma");
51 assertTrue(twoByteString.includes("\u03a3\u0395"), "Sigma Epsilon");
53 assertFalse(twoByteString.includes("\u0391\u03a3\u0395"),
54 "Not Alpha Sigma Epsilon");
57 assertTrue(twoByteString.includes("\u0395"));
59 assertThrows("String.prototype.includes.call(null, 'test')", TypeError);
60 assertThrows("String.prototype.includes.call(null, null)", TypeError);
61 assertThrows("String.prototype.includes.call(undefined, undefined)", TypeError);
63 assertThrows("String.prototype.includes.apply(null, ['test'])", TypeError);
64 assertThrows("String.prototype.includes.apply(null, [null])", TypeError);
65 assertThrows("String.prototype.includes.apply(undefined, [undefined])", TypeError);
68 msg: "Empty string", val: ""
70 msg: "Number 1234.34", val: 1234.34
72 msg: "Integer number 0", val: 0
74 msg: "Negative number -1", val: -1
76 msg: "Boolean true", val: true
78 msg: "Boolean false", val: false
80 msg: "Empty array []", val: []
82 msg: "Empty object {}", val: {}
84 msg: "Array of size 3", val: new Array(3)
88 var l = TEST_INPUT.length;
91 var e = TEST_INPUT[i];
94 assertTrue(s.includes(v), e.msg);
95 assertTrue(String.prototype.includes.call(v, v), e.msg);
96 assertTrue(String.prototype.includes.apply(v, [v]), e.msg);
99 // Test cases found in FF
100 assertTrue("abc".includes("a"));
101 assertTrue("abc".includes("b"));
102 assertTrue("abc".includes("abc"));
103 assertTrue("abc".includes("bc"));
104 assertFalse("abc".includes("d"));
105 assertFalse("abc".includes("abcd"));
106 assertFalse("abc".includes("ac"));
107 assertTrue("abc".includes("abc", 0));
108 assertTrue("abc".includes("bc", 0));
109 assertFalse("abc".includes("de", 0));
110 assertTrue("abc".includes("bc", 1));
111 assertTrue("abc".includes("c", 1));
112 assertFalse("abc".includes("a", 1));
113 assertFalse("abc".includes("abc", 1));
114 assertTrue("abc".includes("c", 2));
115 assertFalse("abc".includes("d", 2));
116 assertFalse("abc".includes("dcd", 2));
117 assertFalse("abc".includes("a", 42));
118 assertFalse("abc".includes("a", Infinity));
119 assertTrue("abc".includes("ab", -43));
120 assertFalse("abc".includes("cd", -42));
121 assertTrue("abc".includes("ab", -Infinity));
122 assertFalse("abc".includes("cd", -Infinity));
123 assertTrue("abc".includes("ab", NaN));
124 assertFalse("abc".includes("cd", NaN));
125 assertFalse("xyzzy".includes("zy\0", 2));
127 var dots = Array(10000).join(".");
128 assertFalse(dots.includes("\x01", 10000));
129 assertFalse(dots.includes("\0", 10000));
132 toString: function () {
135 includes: String.prototype.includes
137 assertTrue(myobj.includes("abc"));
138 assertFalse(myobj.includes("cd"));
143 toString: function () {
148 includes: String.prototype.includes
151 assertEquals("foo[a-z]+(bar)?".includes("[a-z]+"), true);
152 assertThrows("'foo[a-z]+(bar)?'.includes(/[a-z]+/)", TypeError);
153 assertThrows("'foo/[a-z]+/(bar)?'.includes(/[a-z]+/)", TypeError);
154 assertEquals("foo[a-z]+(bar)?".includes("(bar)?"), true);
155 assertThrows("'foo[a-z]+(bar)?'.includes(/(bar)?/)", TypeError);
156 assertThrows("'foo[a-z]+/(bar)?/'.includes(/(bar)?/)", TypeError);
158 assertThrows("String.prototype.includes.call({ 'toString': function() { " +
159 "throw RangeError(); } }, /./)", RangeError);
160 assertThrows("String.prototype.includes.call({ 'toString': function() { " +
161 "return 'abc'; } }, /./)", TypeError);
163 assertThrows("String.prototype.includes.apply({ 'toString': function() { " +
164 "throw RangeError(); } }, [/./])", RangeError);
165 assertThrows("String.prototype.includes.apply({ 'toString': function() { " +
166 "return 'abc'; } }, [/./])", TypeError);