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 assertEquals(1, String.prototype.includes.length); 29 30 var reString = "asdf[a-z]+(asdf)?"; 31 assertTrue(reString.includes("[a-z]+")); 32 assertTrue(reString.includes("(asdf)?")); 33 34 // Random greek letters 35 var twoByteString = "\u039a\u0391\u03a3\u03a3\u0395"; 36 37 // Test single char pattern 38 assertTrue(twoByteString.includes("\u039a"), "Lamda"); 39 assertTrue(twoByteString.includes("\u0391"), "Alpha"); 40 assertTrue(twoByteString.includes("\u03a3"), "First Sigma"); 41 assertTrue(twoByteString.includes("\u03a3",3), "Second Sigma"); 42 assertTrue(twoByteString.includes("\u0395"), "Epsilon"); 43 assertFalse(twoByteString.includes("\u0392"), "Not beta"); 44 45 // Test multi-char pattern 46 assertTrue(twoByteString.includes("\u039a\u0391"), "lambda Alpha"); 47 assertTrue(twoByteString.includes("\u0391\u03a3"), "Alpha Sigma"); 48 assertTrue(twoByteString.includes("\u03a3\u03a3"), "Sigma Sigma"); 49 assertTrue(twoByteString.includes("\u03a3\u0395"), "Sigma Epsilon"); 50 51 assertFalse(twoByteString.includes("\u0391\u03a3\u0395"), 52 "Not Alpha Sigma Epsilon"); 53 54 //single char pattern 55 assertTrue(twoByteString.includes("\u0395")); 56 57 assertThrows("String.prototype.includes.call(null, 'test')", TypeError); 58 assertThrows("String.prototype.includes.call(null, null)", TypeError); 59 assertThrows("String.prototype.includes.call(undefined, undefined)", TypeError); 60 61 assertThrows("String.prototype.includes.apply(null, ['test'])", TypeError); 62 assertThrows("String.prototype.includes.apply(null, [null])", TypeError); 63 assertThrows("String.prototype.includes.apply(undefined, [undefined])", TypeError); 64 65 var 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 85 var i = 0; 86 var l = TEST_INPUT.length; 87 88 for (; 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 98 assertTrue("abc".includes("a")); 99 assertTrue("abc".includes("b")); 100 assertTrue("abc".includes("abc")); 101 assertTrue("abc".includes("bc")); 102 assertFalse("abc".includes("d")); 103 assertFalse("abc".includes("abcd")); 104 assertFalse("abc".includes("ac")); 105 assertTrue("abc".includes("abc", 0)); 106 assertTrue("abc".includes("bc", 0)); 107 assertFalse("abc".includes("de", 0)); 108 assertTrue("abc".includes("bc", 1)); 109 assertTrue("abc".includes("c", 1)); 110 assertFalse("abc".includes("a", 1)); 111 assertFalse("abc".includes("abc", 1)); 112 assertTrue("abc".includes("c", 2)); 113 assertFalse("abc".includes("d", 2)); 114 assertFalse("abc".includes("dcd", 2)); 115 assertFalse("abc".includes("a", 42)); 116 assertFalse("abc".includes("a", Infinity)); 117 assertTrue("abc".includes("ab", -43)); 118 assertFalse("abc".includes("cd", -42)); 119 assertTrue("abc".includes("ab", -Infinity)); 120 assertFalse("abc".includes("cd", -Infinity)); 121 assertTrue("abc".includes("ab", NaN)); 122 assertFalse("abc".includes("cd", NaN)); 123 assertFalse("xyzzy".includes("zy\0", 2)); 124 125 var dots = Array(10000).join("."); 126 assertFalse(dots.includes("\x01", 10000)); 127 assertFalse(dots.includes("\0", 10000)); 128 129 var myobj = { 130 toString: function () { 131 return "abc"; 132 }, 133 includes: String.prototype.includes 134 }; 135 assertTrue(myobj.includes("abc")); 136 assertFalse(myobj.includes("cd")); 137 138 var gotStr = false; 139 var gotPos = false; 140 myobj = { 141 toString: function () { 142 assertFalse(gotPos); 143 gotStr = true; 144 return "xyz"; 145 }, 146 includes: String.prototype.includes 147 }; 148 149 assertEquals("foo[a-z]+(bar)?".includes("[a-z]+"), true); 150 assertThrows("'foo[a-z]+(bar)?'.includes(/[a-z]+/)", TypeError); 151 assertThrows("'foo/[a-z]+/(bar)?'.includes(/[a-z]+/)", TypeError); 152 assertEquals("foo[a-z]+(bar)?".includes("(bar)?"), true); 153 assertThrows("'foo[a-z]+(bar)?'.includes(/(bar)?/)", TypeError); 154 assertThrows("'foo[a-z]+/(bar)?/'.includes(/(bar)?/)", TypeError); 155 156 assertThrows("String.prototype.includes.call({ 'toString': function() { " + 157 "throw RangeError(); } }, /./)", RangeError); 158 assertThrows("String.prototype.includes.call({ 'toString': function() { " + 159 "return 'abc'; } }, /./)", TypeError); 160 161 assertThrows("String.prototype.includes.apply({ 'toString': function() { " + 162 "throw RangeError(); } }, [/./])", RangeError); 163 assertThrows("String.prototype.includes.apply({ 'toString': function() { " + 164 "return 'abc'; } }, [/./])", TypeError); 165 166 // includes does its brand checks with Symbol.match 167 var re = /./; 168 assertThrows(function() { 169 "".includes(re); 170 }, TypeError); 171 re[Symbol.match] = false; 172 assertEquals(false, "".includes(re)); 173