|
| 1 | +// Copyright 2015 Samsung Electronics Co., Ltd. |
| 2 | +// Copyright 2015 University of Szeged. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +// check properties |
| 17 | +assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').configurable === false); |
| 18 | + |
| 19 | +assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').enumerable === false); |
| 20 | + |
| 21 | +assert(Object.getOwnPropertyDescriptor(String.prototype.substring, 'length').writable === false); |
| 22 | + |
| 23 | +assert(String.prototype.substring.length === 2); |
| 24 | + |
| 25 | +assert(String.prototype.substring.call(new String()) === ""); |
| 26 | + |
| 27 | +assert(String.prototype.substring.call({}) === "[object Object]"); |
| 28 | + |
| 29 | +// check this is undefined |
| 30 | +try { |
| 31 | + String.prototype.substring.call(undefined); |
| 32 | + assert(false); |
| 33 | +} catch(e) { |
| 34 | + assert(e instanceof TypeError); |
| 35 | +} |
| 36 | + |
| 37 | +// check this is null |
| 38 | +try { |
| 39 | + String.prototype.substring.call(null); |
| 40 | + assert(false); |
| 41 | +} catch(e) { |
| 42 | + assert(e instanceof TypeError); |
| 43 | +} |
| 44 | + |
| 45 | +// simple checks |
| 46 | +assert("hello world!".substring(0, 11) === "hello world"); |
| 47 | + |
| 48 | +assert("hello world!".substring(11, 0) === "hello world"); |
| 49 | + |
| 50 | +assert("hello world!".substring(0, 12) === "hello world!"); |
| 51 | + |
| 52 | +assert("hello world!".substring(12, 0) === "hello world!"); |
| 53 | + |
| 54 | +// check NaN |
| 55 | +assert("hello world!".substring(NaN, 12) === "hello world!"); |
| 56 | + |
| 57 | +// check NaN |
| 58 | +assert("hello world!".substring(2, NaN) === "he"); |
| 59 | + |
| 60 | +// check end undefined |
| 61 | +assert("hello world!".substring(2, undefined) === "llo world!"); |
| 62 | + |
| 63 | +// check negative |
| 64 | +assert("hello world!".substring(-1,8) === "hello wo"); |
| 65 | + |
| 66 | +// check negative |
| 67 | +assert("hello\tworld!".substring(5,-8) === "hello"); |
| 68 | + |
| 69 | +// check negative |
| 70 | +assert("hello world!".substring(-1,-8) === ""); |
| 71 | + |
| 72 | +// check ranges |
| 73 | +assert("hello world!".substring(-1,10000) === "hello world!"); |
| 74 | + |
| 75 | +assert("hello world!".substring(10000,1000000) === ""); |
| 76 | + |
| 77 | +assert("hello world!".substring(100000,1) === "ello world!"); |
| 78 | + |
| 79 | +// check both undefined |
| 80 | +assert("hello world!".substring(undefined, undefined) === "hello world!"); |
| 81 | + |
| 82 | +var undef_var; |
| 83 | +assert("hello world!".substring(undef_var, undef_var) === "hello world!"); |
| 84 | + |
| 85 | +// check integer conversion |
| 86 | +assert("hello world!".substring(undefined, 5) === "hello"); |
| 87 | + |
| 88 | +assert("hello world!".substring(undefined, "bar") === ""); |
| 89 | + |
| 90 | +assert("hello world!".substring(2, true) === "e"); |
| 91 | + |
| 92 | +assert("hello world!".substring(2, false) === "he"); |
| 93 | + |
| 94 | +assert("hello world!".substring(5, obj) === " world!"); |
| 95 | + |
| 96 | +// check other objects |
| 97 | +var obj = { substring : String.prototype.substring } |
| 98 | + |
| 99 | +obj.toString = function() { |
| 100 | + return "Iam"; |
| 101 | +} |
| 102 | +assert(obj.substring(100000,1) === "am"); |
| 103 | + |
| 104 | +obj.toString = function() { |
| 105 | + throw new ReferenceError ("foo"); |
| 106 | +}; |
| 107 | + |
| 108 | +try { |
| 109 | + assert(obj.substring(100000,1)); |
| 110 | + assert(false); |
| 111 | +} catch (e) { |
| 112 | + assert(e.message === "foo"); |
| 113 | + assert(e instanceof ReferenceError); |
| 114 | +} |
| 115 | + |
| 116 | +// check coercible - undefined |
| 117 | +try { |
| 118 | + assert(true.substring() === ""); |
| 119 | + assert(false); |
| 120 | +} catch (e) { |
| 121 | + assert(e instanceof TypeError); |
| 122 | +} |
| 123 | + |
| 124 | +// check coercible - null |
| 125 | +try { |
| 126 | + assert(String.prototype.substring.call(null, 0, 1) === ""); |
| 127 | + assert(false); |
| 128 | +} catch (e) { |
| 129 | + assert(e instanceof TypeError); |
| 130 | +} |
| 131 | + |
| 132 | +// check coercible - Boolean |
| 133 | +assert(String.prototype.substring.call(true, 0, 1) === "t"); |
| 134 | + |
| 135 | +// check coercible - Object |
| 136 | +var test_object = {firstName:"John", lastName:"Doe"}; |
| 137 | +assert(String.prototype.substring.call(test_object, 0, 7) === "[object"); |
| 138 | + |
| 139 | +// check coercible - Number |
| 140 | +assert(String.prototype.substring.call(123, 0, 3) === "123"); |
0 commit comments