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 30 assertEquals(1, String.prototype.contains.length); 31 32 var reString = "asdf[a-z]+(asdf)?"; 33 assertTrue(reString.contains("[a-z]+")); 34 assertTrue(reString.contains("(asdf)?")); 35 36 // Random greek letters 37 var twoByteString = "\u039a\u0391\u03a3\u03a3\u0395"; 38 39 // Test single char pattern 40 assertTrue(twoByteString.contains("\u039a"), "Lamda"); 41 assertTrue(twoByteString.contains("\u0391"), "Alpha"); 42 assertTrue(twoByteString.contains("\u03a3"), "First Sigma"); 43 assertTrue(twoByteString.contains("\u03a3",3), "Second Sigma"); 44 assertTrue(twoByteString.contains("\u0395"), "Epsilon"); 45 assertFalse(twoByteString.contains("\u0392"), "Not beta"); 46 47 // Test multi-char pattern 48 assertTrue(twoByteString.contains("\u039a\u0391"), "lambda Alpha"); 49 assertTrue(twoByteString.contains("\u0391\u03a3"), "Alpha Sigma"); 50 assertTrue(twoByteString.contains("\u03a3\u03a3"), "Sigma Sigma"); 51 assertTrue(twoByteString.contains("\u03a3\u0395"), "Sigma Epsilon"); 52 53 assertFalse(twoByteString.contains("\u0391\u03a3\u0395"), 54 "Not Alpha Sigma Epsilon"); 55 56 //single char pattern 57 assertTrue(twoByteString.contains("\u0395")); 58 59 assertThrows("String.prototype.contains.call(null, 'test')", TypeError); 60 assertThrows("String.prototype.contains.call(null, null)", TypeError); 61 assertThrows("String.prototype.contains.call(undefined, undefined)", TypeError); 62 63 assertThrows("String.prototype.contains.apply(null, ['test'])", TypeError); 64 assertThrows("String.prototype.contains.apply(null, [null])", TypeError); 65 assertThrows("String.prototype.contains.apply(undefined, [undefined])", TypeError); 66 67 var 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 89 var i = 0; 90 var l = TEST_INPUT.length; 91 92 for (; 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 102 assertTrue("abc".contains("a")); 103 assertTrue("abc".contains("b")); 104 assertTrue("abc".contains("abc")); 105 assertTrue("abc".contains("bc")); 106 assertFalse("abc".contains("d")); 107 assertFalse("abc".contains("abcd")); 108 assertFalse("abc".contains("ac")); 109 assertTrue("abc".contains("abc", 0)); 110 assertTrue("abc".contains("bc", 0)); 111 assertFalse("abc".contains("de", 0)); 112 assertTrue("abc".contains("bc", 1)); 113 assertTrue("abc".contains("c", 1)); 114 assertFalse("abc".contains("a", 1)); 115 assertFalse("abc".contains("abc", 1)); 116 assertTrue("abc".contains("c", 2)); 117 assertFalse("abc".contains("d", 2)); 118 assertFalse("abc".contains("dcd", 2)); 119 assertFalse("abc".contains("a", 42)); 120 assertFalse("abc".contains("a", Infinity)); 121 assertTrue("abc".contains("ab", -43)); 122 assertFalse("abc".contains("cd", -42)); 123 assertTrue("abc".contains("ab", -Infinity)); 124 assertFalse("abc".contains("cd", -Infinity)); 125 assertTrue("abc".contains("ab", NaN)); 126 assertFalse("abc".contains("cd", NaN)); 127 assertFalse("xyzzy".contains("zy\0", 2)); 128 129 var dots = Array(10000).join('.'); 130 assertFalse(dots.contains("\x01", 10000)); 131 assertFalse(dots.contains("\0", 10000)); 132 133 var myobj = { 134 toString: function () { 135 return "abc"; 136 }, 137 contains: String.prototype.contains 138 }; 139 assertTrue(myobj.contains("abc")); 140 assertFalse(myobj.contains("cd")); 141 142 var gotStr = false; 143 var gotPos = false; 144 myobj = { 145 toString: function () { 146 assertFalse(gotPos); 147 gotStr = true; 148 return "xyz"; 149 }, 150 contains: String.prototype.contains 151 }; 152