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 |
| 28 assertEquals(1, String.prototype.endsWith.length); |
| 29 |
| 30 var testString = "Hello World"; |
| 31 assertTrue(testString.endsWith("")); |
| 32 assertTrue(testString.endsWith("World")); |
| 33 assertFalse(testString.endsWith("world")); |
| 34 assertFalse(testString.endsWith("Hello World!")); |
| 35 assertFalse(testString.endsWith(null)); |
| 36 assertFalse(testString.endsWith(undefined)); |
| 37 |
| 38 assertTrue("null".endsWith(null)); |
| 39 assertTrue("undefined".endsWith(undefined)); |
| 40 |
| 41 var georgianUnicodeString = "\u10D0\u10D1\u10D2\u10D3\u10D4\u10D5\u10D6\u10D7"; |
| 42 assertTrue(georgianUnicodeString.endsWith(georgianUnicodeString)); |
| 43 assertTrue(georgianUnicodeString.endsWith("\u10D4\u10D5\u10D6\u10D7")); |
| 44 assertFalse(georgianUnicodeString.endsWith("\u10D0")); |
| 45 |
| 46 assertThrows("String.prototype.endsWith.call(null, 'test')"); |
| 47 assertThrows("String.prototype.endsWith.call(null, null)"); |
| 48 assertThrows("String.prototype.endsWith.call(undefined, undefined)"); |
| 49 |
| 50 assertThrows("String.prototype.endsWith.apply(null, ['test'])"); |
| 51 assertThrows("String.prototype.endsWith.apply(null, [null])"); |
| 52 assertThrows("String.prototype.endsWith.apply(undefined, [undefined])"); |
| 53 |
| 54 var TEST_INPUT = [{ |
| 55 msg: "Empty string", val: "" |
| 56 }, { |
| 57 msg: "Number 1234.34", val: 1234.34 |
| 58 }, { |
| 59 msg: "Integer number 0", val: 0 |
| 60 }, { |
| 61 msg: "Negative number -1", val: -1 |
| 62 }, { |
| 63 msg: "Boolean true", val: true |
| 64 }, { |
| 65 msg: "Boolean false", val: false |
| 66 }, { |
| 67 msg: "Regular expression /\d+/", val: /\d+/ |
| 68 }, { |
| 69 msg: "Empty array []", val: [] |
| 70 }, { |
| 71 msg: "Empty object {}", val: {} |
| 72 }, { |
| 73 msg: "Array of size 3", val: new Array(3) |
| 74 }]; |
| 75 |
| 76 function testNonStringValues() { |
| 77 var i = 0; |
| 78 var l = TEST_INPUT.length; |
| 79 |
| 80 for (; i < l; i++) { |
| 81 var e = TEST_INPUT[i]; |
| 82 var v = e.val; |
| 83 var s = String(v); |
| 84 assertTrue(s.endsWith(v), e.msg); |
| 85 assertTrue(String.prototype.endsWith.call(v, v), e.msg); |
| 86 assertTrue(String.prototype.endsWith.apply(v, [v]), e.msg); |
| 87 } |
| 88 } |
| 89 testNonStringValues(); |
| 90 |
| 91 var CustomType = function(value) { |
| 92 this.endsWith = String.prototype.endsWith; |
| 93 this.toString = function() { |
| 94 return String(value); |
| 95 } |
| 96 }; |
| 97 |
| 98 function testCutomType() { |
| 99 var i = 0; |
| 100 var l = TEST_INPUT.length; |
| 101 |
| 102 for (; i < l; i++) { |
| 103 var e = TEST_INPUT[i]; |
| 104 var v = e.val; |
| 105 var o = new CustomType(v); |
| 106 assertTrue(o.endsWith(v), e.msg); |
| 107 } |
| 108 } |
| 109 testCutomType(); |
| 110 |
| 111 |
| 112 // Test cases found in FF |
| 113 assertTrue("abc".endsWith("abc")); |
| 114 assertTrue("abcd".endsWith("bcd")); |
| 115 assertTrue("abc".endsWith("c")); |
| 116 assertFalse("abc".endsWith("abcd")); |
| 117 assertFalse("abc".endsWith("bbc")); |
| 118 assertFalse("abc".endsWith("b")); |
| 119 assertTrue("abc".endsWith("abc", 3)); |
| 120 assertTrue("abc".endsWith("bc", 3)); |
| 121 assertFalse("abc".endsWith("a", 3)); |
| 122 assertTrue("abc".endsWith("bc", 3)); |
| 123 assertTrue("abc".endsWith("a", 1)); |
| 124 assertFalse("abc".endsWith("abc", 1)); |
| 125 assertTrue("abc".endsWith("b", 2)); |
| 126 assertFalse("abc".endsWith("d", 2)); |
| 127 assertFalse("abc".endsWith("dcd", 2)); |
| 128 assertFalse("abc".endsWith("a", 42)); |
| 129 assertTrue("abc".endsWith("bc", Infinity)); |
| 130 assertFalse("abc".endsWith("a", Infinity)); |
| 131 assertTrue("abc".endsWith("bc", undefined)); |
| 132 assertFalse("abc".endsWith("bc", -43)); |
| 133 assertFalse("abc".endsWith("bc", -Infinity)); |
| 134 assertTrue("abc".endsWith("bc", NaN)); |
OLD | NEW |