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