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