Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 msg: "Number 1234.34", val: 1234.34 | 59 msg: "Number 1234.34", val: 1234.34 |
| 60 }, { | 60 }, { |
| 61 msg: "Integer number 0", val: 0 | 61 msg: "Integer number 0", val: 0 |
| 62 }, { | 62 }, { |
| 63 msg: "Negative number -1", val: -1 | 63 msg: "Negative number -1", val: -1 |
| 64 }, { | 64 }, { |
| 65 msg: "Boolean true", val: true | 65 msg: "Boolean true", val: true |
| 66 }, { | 66 }, { |
| 67 msg: "Boolean false", val: false | 67 msg: "Boolean false", val: false |
| 68 }, { | 68 }, { |
| 69 msg: "Regular expression /\d+/", val: /\d+/ | |
| 70 }, { | |
| 71 msg: "Empty array []", val: [] | 69 msg: "Empty array []", val: [] |
| 72 }, { | 70 }, { |
| 73 msg: "Empty object {}", val: {} | 71 msg: "Empty object {}", val: {} |
| 74 }, { | 72 }, { |
| 75 msg: "Array of size 3", val: new Array(3) | 73 msg: "Array of size 3", val: new Array(3) |
| 76 }]; | 74 }]; |
| 77 | 75 |
| 78 function testNonStringValues() { | 76 function testNonStringValues() { |
| 79 var i = 0; | 77 var i = 0; |
| 80 var l = TEST_INPUT.length; | 78 var l = TEST_INPUT.length; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 assertTrue("abc".endsWith("b", 2)); | 125 assertTrue("abc".endsWith("b", 2)); |
| 128 assertFalse("abc".endsWith("d", 2)); | 126 assertFalse("abc".endsWith("d", 2)); |
| 129 assertFalse("abc".endsWith("dcd", 2)); | 127 assertFalse("abc".endsWith("dcd", 2)); |
| 130 assertFalse("abc".endsWith("a", 42)); | 128 assertFalse("abc".endsWith("a", 42)); |
| 131 assertTrue("abc".endsWith("bc", Infinity)); | 129 assertTrue("abc".endsWith("bc", Infinity)); |
| 132 assertFalse("abc".endsWith("a", Infinity)); | 130 assertFalse("abc".endsWith("a", Infinity)); |
| 133 assertTrue("abc".endsWith("bc", undefined)); | 131 assertTrue("abc".endsWith("bc", undefined)); |
| 134 assertFalse("abc".endsWith("bc", -43)); | 132 assertFalse("abc".endsWith("bc", -43)); |
| 135 assertFalse("abc".endsWith("bc", -Infinity)); | 133 assertFalse("abc".endsWith("bc", -Infinity)); |
| 136 assertFalse("abc".endsWith("bc", NaN)); | 134 assertFalse("abc".endsWith("bc", NaN)); |
| 135 | |
| 136 // Test cases taken from https://github.com/mathiasbynens/String.prototype.endsW ith/blob/master/tests/tests.js | |
| 137 Object.prototype[1] = 2; // try to break `arguments[1]` | |
| 138 | |
| 139 assertEquals(String.prototype.endsWith.length, 1); | |
| 140 assertEquals(String.prototype.propertyIsEnumerable("endsWith"), false); | |
| 141 | |
| 142 assertEquals("undefined".endsWith(), true); | |
| 143 assertEquals("undefined".endsWith(undefined), true); | |
| 144 assertEquals("undefined".endsWith(null), false); | |
| 145 assertEquals("null".endsWith(), false); | |
| 146 assertEquals("null".endsWith(undefined), false); | |
| 147 assertEquals("null".endsWith(null), true); | |
| 148 | |
| 149 assertEquals("abc".endsWith(), false); | |
| 150 assertEquals("abc".endsWith(""), true); | |
| 151 assertEquals("abc".endsWith("\0"), false); | |
| 152 assertEquals("abc".endsWith("c"), true); | |
| 153 assertEquals("abc".endsWith("b"), false); | |
| 154 assertEquals("abc".endsWith("ab"), false); | |
| 155 assertEquals("abc".endsWith("bc"), true); | |
| 156 assertEquals("abc".endsWith("abc"), true); | |
| 157 assertEquals("abc".endsWith("bcd"), false); | |
| 158 assertEquals("abc".endsWith("abcd"), false); | |
| 159 assertEquals("abc".endsWith("bcde"), false); | |
| 160 | |
| 161 assertEquals("abc".endsWith("", NaN), true); | |
| 162 assertEquals("abc".endsWith("\0", NaN), false); | |
| 163 assertEquals("abc".endsWith("c", NaN), false); | |
| 164 assertEquals("abc".endsWith("b", NaN), false); | |
| 165 assertEquals("abc".endsWith("ab", NaN), false); | |
| 166 assertEquals("abc".endsWith("bc", NaN), false); | |
| 167 assertEquals("abc".endsWith("abc", NaN), false); | |
| 168 assertEquals("abc".endsWith("bcd", NaN), false); | |
| 169 assertEquals("abc".endsWith("abcd", NaN), false); | |
| 170 assertEquals("abc".endsWith("bcde", NaN), false); | |
| 171 | |
| 172 assertEquals("abc".endsWith("", false), true); | |
| 173 assertEquals("abc".endsWith("\0", false), false); | |
| 174 assertEquals("abc".endsWith("c", false), false); | |
| 175 assertEquals("abc".endsWith("b", false), false); | |
| 176 assertEquals("abc".endsWith("ab", false), false); | |
| 177 assertEquals("abc".endsWith("bc", false), false); | |
| 178 assertEquals("abc".endsWith("abc", false), false); | |
| 179 assertEquals("abc".endsWith("bcd", false), false); | |
| 180 assertEquals("abc".endsWith("abcd", false), false); | |
| 181 assertEquals("abc".endsWith("bcde", false), false); | |
| 182 | |
| 183 assertEquals("abc".endsWith("", undefined), true); | |
| 184 assertEquals("abc".endsWith("\0", undefined), false); | |
| 185 assertEquals("abc".endsWith("c", undefined), true); | |
| 186 assertEquals("abc".endsWith("b", undefined), false); | |
| 187 assertEquals("abc".endsWith("ab", undefined), false); | |
| 188 assertEquals("abc".endsWith("bc", undefined), true); | |
| 189 assertEquals("abc".endsWith("abc", undefined), true); | |
| 190 assertEquals("abc".endsWith("bcd", undefined), false); | |
| 191 assertEquals("abc".endsWith("abcd", undefined), false); | |
| 192 assertEquals("abc".endsWith("bcde", undefined), false); | |
| 193 | |
| 194 assertEquals("abc".endsWith("", null), true); | |
| 195 assertEquals("abc".endsWith("\0", null), false); | |
| 196 assertEquals("abc".endsWith("c", null), false); | |
| 197 assertEquals("abc".endsWith("b", null), false); | |
| 198 assertEquals("abc".endsWith("ab", null), false); | |
| 199 assertEquals("abc".endsWith("bc", null), false); | |
| 200 assertEquals("abc".endsWith("abc", null), false); | |
| 201 assertEquals("abc".endsWith("bcd", null), false); | |
| 202 assertEquals("abc".endsWith("abcd", null), false); | |
| 203 assertEquals("abc".endsWith("bcde", null), false); | |
| 204 | |
| 205 assertEquals("abc".endsWith("", -Infinity), true); | |
| 206 assertEquals("abc".endsWith("\0", -Infinity), false); | |
| 207 assertEquals("abc".endsWith("c", -Infinity), false); | |
| 208 assertEquals("abc".endsWith("b", -Infinity), false); | |
| 209 assertEquals("abc".endsWith("ab", -Infinity), false); | |
| 210 assertEquals("abc".endsWith("bc", -Infinity), false); | |
| 211 assertEquals("abc".endsWith("abc", -Infinity), false); | |
| 212 assertEquals("abc".endsWith("bcd", -Infinity), false); | |
| 213 assertEquals("abc".endsWith("abcd", -Infinity), false); | |
| 214 assertEquals("abc".endsWith("bcde", -Infinity), false); | |
| 215 | |
| 216 assertEquals("abc".endsWith("", -1), true); | |
| 217 assertEquals("abc".endsWith("\0", -1), false); | |
| 218 assertEquals("abc".endsWith("c", -1), false); | |
| 219 assertEquals("abc".endsWith("b", -1), false); | |
| 220 assertEquals("abc".endsWith("ab", -1), false); | |
| 221 assertEquals("abc".endsWith("bc", -1), false); | |
| 222 assertEquals("abc".endsWith("abc", -1), false); | |
| 223 assertEquals("abc".endsWith("bcd", -1), false); | |
| 224 assertEquals("abc".endsWith("abcd", -1), false); | |
| 225 assertEquals("abc".endsWith("bcde", -1), false); | |
| 226 | |
| 227 assertEquals("abc".endsWith("", -0), true); | |
| 228 assertEquals("abc".endsWith("\0", -0), false); | |
| 229 assertEquals("abc".endsWith("c", -0), false); | |
| 230 assertEquals("abc".endsWith("b", -0), false); | |
| 231 assertEquals("abc".endsWith("ab", -0), false); | |
| 232 assertEquals("abc".endsWith("bc", -0), false); | |
| 233 assertEquals("abc".endsWith("abc", -0), false); | |
| 234 assertEquals("abc".endsWith("bcd", -0), false); | |
| 235 assertEquals("abc".endsWith("abcd", -0), false); | |
| 236 assertEquals("abc".endsWith("bcde", -0), false); | |
| 237 | |
| 238 assertEquals("abc".endsWith("", +0), true); | |
| 239 assertEquals("abc".endsWith("\0", +0), false); | |
| 240 assertEquals("abc".endsWith("c", +0), false); | |
| 241 assertEquals("abc".endsWith("b", +0), false); | |
| 242 assertEquals("abc".endsWith("ab", +0), false); | |
| 243 assertEquals("abc".endsWith("bc", +0), false); | |
| 244 assertEquals("abc".endsWith("abc", +0), false); | |
| 245 assertEquals("abc".endsWith("bcd", +0), false); | |
| 246 assertEquals("abc".endsWith("abcd", +0), false); | |
| 247 assertEquals("abc".endsWith("bcde", +0), false); | |
| 248 | |
| 249 assertEquals("abc".endsWith("", 1), true); | |
| 250 assertEquals("abc".endsWith("\0", 1), false); | |
| 251 assertEquals("abc".endsWith("c", 1), false); | |
| 252 assertEquals("abc".endsWith("b", 1), false); | |
| 253 assertEquals("abc".endsWith("ab", 1), false); | |
| 254 assertEquals("abc".endsWith("bc", 1), false); | |
| 255 assertEquals("abc".endsWith("abc", 1), false); | |
| 256 assertEquals("abc".endsWith("bcd", 1), false); | |
| 257 assertEquals("abc".endsWith("abcd", 1), false); | |
| 258 assertEquals("abc".endsWith("bcde", 1), false); | |
| 259 | |
| 260 assertEquals("abc".endsWith("", 2), true); | |
| 261 assertEquals("abc".endsWith("\0", 2), false); | |
| 262 assertEquals("abc".endsWith("c", 2), false); | |
| 263 assertEquals("abc".endsWith("b", 2), true); | |
| 264 assertEquals("abc".endsWith("ab", 2), true); | |
| 265 assertEquals("abc".endsWith("bc", 2), false); | |
| 266 assertEquals("abc".endsWith("abc", 2), false); | |
| 267 assertEquals("abc".endsWith("bcd", 2), false); | |
| 268 assertEquals("abc".endsWith("abcd", 2), false); | |
| 269 assertEquals("abc".endsWith("bcde", 2), false); | |
| 270 | |
| 271 assertEquals("abc".endsWith("", +Infinity), true); | |
| 272 assertEquals("abc".endsWith("\0", +Infinity), false); | |
| 273 assertEquals("abc".endsWith("c", +Infinity), true); | |
| 274 assertEquals("abc".endsWith("b", +Infinity), false); | |
| 275 assertEquals("abc".endsWith("ab", +Infinity), false); | |
| 276 assertEquals("abc".endsWith("bc", +Infinity), true); | |
| 277 assertEquals("abc".endsWith("abc", +Infinity), true); | |
| 278 assertEquals("abc".endsWith("bcd", +Infinity), false); | |
| 279 assertEquals("abc".endsWith("abcd", +Infinity), false); | |
| 280 assertEquals("abc".endsWith("bcde", +Infinity), false); | |
| 281 | |
| 282 assertEquals("abc".endsWith("", true), true); | |
| 283 assertEquals("abc".endsWith("\0", true), false); | |
| 284 assertEquals("abc".endsWith("c", true), false); | |
| 285 assertEquals("abc".endsWith("b", true), false); | |
| 286 assertEquals("abc".endsWith("ab", true), false); | |
| 287 assertEquals("abc".endsWith("bc", true), false); | |
| 288 assertEquals("abc".endsWith("abc", true), false); | |
| 289 assertEquals("abc".endsWith("bcd", true), false); | |
| 290 assertEquals("abc".endsWith("abcd", true), false); | |
| 291 assertEquals("abc".endsWith("bcde", true), false); | |
| 292 | |
| 293 assertEquals("abc".endsWith("", "x"), true); | |
| 294 assertEquals("abc".endsWith("\0", "x"), false); | |
| 295 assertEquals("abc".endsWith("c", "x"), false); | |
| 296 assertEquals("abc".endsWith("b", "x"), false); | |
| 297 assertEquals("abc".endsWith("ab", "x"), false); | |
| 298 assertEquals("abc".endsWith("bc", "x"), false); | |
| 299 assertEquals("abc".endsWith("abc", "x"), false); | |
| 300 assertEquals("abc".endsWith("bcd", "x"), false); | |
| 301 assertEquals("abc".endsWith("abcd", "x"), false); | |
| 302 assertEquals("abc".endsWith("bcde", "x"), false); | |
| 303 | |
| 304 assertEquals("[a-z]+(bar)?".endsWith("(bar)?"), true); | |
| 305 assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/); }, TypeError); | |
| 306 assertEquals("[a-z]+(bar)?".endsWith("[a-z]+", 6), true); | |
| 307 assertThrows(function() { "[a-z]+(bar)?".endsWith(/(bar)?/); }, TypeError); | |
| 308 assertThrows(function() { "[a-z]+/(bar)?/".endsWith(/(bar)?/); }, TypeError); | |
| 309 | |
| 310 // http://mathiasbynens.be/notes/javascript-unicode#poo-test | |
| 311 var string = "I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9"; | |
| 312 assertEquals(string.endsWith(""), true); | |
| 313 assertEquals(string.endsWith("\xF1t\xEBr"), false); | |
| 314 assertEquals(string.endsWith("\xF1t\xEBr", 5), true); | |
| 315 assertEquals(string.endsWith("\xE0liz\xE6"), false); | |
| 316 assertEquals(string.endsWith("\xE0liz\xE6", 16), true); | |
| 317 assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9"), true); | |
| 318 assertEquals(string.endsWith("\xF8n\u2603\uD83D\uDCA9", 23), true); | |
| 319 assertEquals(string.endsWith("\u2603"), false); | |
| 320 assertEquals(string.endsWith("\u2603", 21), true); | |
| 321 assertEquals(string.endsWith("\uD83D\uDCA9"), true); | |
| 322 assertEquals(string.endsWith("\uD83D\uDCA9", 23), true); | |
| 323 | |
| 324 assertThrows(function() { String.prototype.endsWith.call(undefined); }, TypeErro r); | |
|
Yang
2013/12/23 09:56:07
We try to adhere to a 80-character-per-line limit
| |
| 325 assertThrows(function() { String.prototype.endsWith.call(undefined, "b"); }, Typ eError); | |
| 326 assertThrows(function() { String.prototype.endsWith.call(undefined, "b", 4); }, TypeError); | |
| 327 assertThrows(function() { String.prototype.endsWith.call(null); }, TypeError); | |
| 328 assertThrows(function() { String.prototype.endsWith.call(null, "b"); }, TypeErro r); | |
| 329 assertThrows(function() { String.prototype.endsWith.call(null, "b", 4); }, TypeE rror); | |
| 330 assertEquals(String.prototype.endsWith.call(42, "2"), true); | |
| 331 assertEquals(String.prototype.endsWith.call(42, "4"), false); | |
| 332 assertEquals(String.prototype.endsWith.call(42, "b", 4), false); | |
| 333 assertEquals(String.prototype.endsWith.call(42, "2", 1), false); | |
| 334 assertEquals(String.prototype.endsWith.call(42, "2", 4), true); | |
| 335 assertEquals(String.prototype.endsWith.call({ "toString": function() { return "a bc"; } }, "b", 0), false); | |
| 336 assertEquals(String.prototype.endsWith.call({ "toString": function() { return "a bc"; } }, "b", 1), false); | |
| 337 assertEquals(String.prototype.endsWith.call({ "toString": function() { return "a bc"; } }, "b", 2), true); | |
| 338 assertThrows(function() { String.prototype.endsWith.call({ "toString": function( ) { throw RangeError(); } }, /./); }, RangeError); | |
| 339 assertThrows(function() { String.prototype.endsWith.call({ "toString": function( ) { return "abc" } }, /./); }, TypeError); | |
| 340 | |
| 341 assertThrows(function() { String.prototype.endsWith.apply(undefined); }, TypeErr or); | |
| 342 assertThrows(function() { String.prototype.endsWith.apply(undefined, ["b"]); }, TypeError); | |
| 343 assertThrows(function() { String.prototype.endsWith.apply(undefined, ["b", 4]); }, TypeError); | |
| 344 assertThrows(function() { String.prototype.endsWith.apply(null); }, TypeError); | |
| 345 assertThrows(function() { String.prototype.endsWith.apply(null, ["b"]); }, TypeE rror); | |
| 346 assertThrows(function() { String.prototype.endsWith.apply(null, ["b", 4]); }, Ty peError); | |
| 347 assertEquals(String.prototype.endsWith.apply(42, ["2"]), true); | |
| 348 assertEquals(String.prototype.endsWith.apply(42, ["4"]), false); | |
| 349 assertEquals(String.prototype.endsWith.apply(42, ["b", 4]), false); | |
| 350 assertEquals(String.prototype.endsWith.apply(42, ["2", 1]), false); | |
| 351 assertEquals(String.prototype.endsWith.apply(42, ["2", 4]), true); | |
| 352 assertEquals(String.prototype.endsWith.apply({ "toString": function() { return " abc"; } }, ["b", 0]), false); | |
| 353 assertEquals(String.prototype.endsWith.apply({ "toString": function() { return " abc"; } }, ["b", 1]), false); | |
| 354 assertEquals(String.prototype.endsWith.apply({ "toString": function() { return " abc"; } }, ["b", 2]), true); | |
| 355 assertThrows(function() { String.prototype.endsWith.apply({ "toString": function () { throw RangeError(); } }, [/./]); }, RangeError); | |
| 356 assertThrows(function() { String.prototype.endsWith.apply({ "toString": function () { return "abc" } }, [/./]); }, TypeError); | |
| OLD | NEW |