|
| 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.charAt, 'length').configurable === false); |
| 18 | + |
| 19 | +assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').enumerable === false); |
| 20 | + |
| 21 | +assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').writable === false); |
| 22 | + |
| 23 | +assert(String.prototype.charAt.length === 1); |
| 24 | + |
| 25 | +// check empty string |
| 26 | +assert(String.prototype.charAt.call(new String()) === ""); |
| 27 | + |
| 28 | +// check NaN |
| 29 | +assert("hello world!".charAt(NaN) === "h"); |
| 30 | + |
| 31 | +// check Object |
| 32 | +assert(String.prototype.charAt.call({}) === "["); |
| 33 | + |
| 34 | +// simple checks |
| 35 | +assert("hello world!".charAt(0) === "h"); |
| 36 | + |
| 37 | +assert("hello world!".charAt(1) === "e"); |
| 38 | + |
| 39 | +// check +-Inf |
| 40 | +assert("hello world!".charAt(-Infinity) === ""); |
| 41 | + |
| 42 | +assert("hello world!".charAt(Infinity) === ""); |
| 43 | + |
| 44 | +assert("hello world!".charAt(11) === "!"); |
| 45 | + |
| 46 | +assert("hello world!".charAt(12) === ""); |
| 47 | + |
| 48 | +// check negative |
| 49 | +assert("hello world!".charAt(-1) === ""); |
| 50 | + |
| 51 | +assert("hello world!".charAt(-9999999) === ""); |
| 52 | + |
| 53 | +assert("hello world!".charAt(-0) === "h"); |
| 54 | + |
| 55 | +// check undefined |
| 56 | +assert("hello world!".charAt(undefined) === "h"); |
| 57 | + |
| 58 | +// check booleans |
| 59 | +assert("hello world!".charAt(true) === "e"); |
| 60 | + |
| 61 | +assert("hello world!".charAt(false) === "h"); |
| 62 | + |
| 63 | +// check this is undefined |
| 64 | +try { |
| 65 | + String.prototype.charAt.call(undefined); |
| 66 | + assert(false); |
| 67 | +} catch(e) { |
| 68 | + assert(e instanceof TypeError); |
| 69 | +} |
| 70 | + |
| 71 | +// check this is null |
| 72 | +try { |
| 73 | + String.prototype.charAt.call(null); |
| 74 | + assert(false); |
| 75 | +} catch(e) { |
| 76 | + assert(e instanceof TypeError); |
| 77 | +} |
| 78 | + |
| 79 | +// check coercible - undefined |
| 80 | +try { |
| 81 | + assert(true.charAt() === ""); |
| 82 | + assert(false); |
| 83 | +} catch (e) { |
| 84 | + assert(e instanceof TypeError); |
| 85 | +} |
| 86 | + |
| 87 | +// check coercible - null |
| 88 | +try { |
| 89 | + assert(String.prototype.charAt.call(null, 0) === ""); |
| 90 | + assert(false); |
| 91 | +} catch (e) { |
| 92 | + assert(e instanceof TypeError); |
| 93 | +} |
| 94 | + |
| 95 | +// check coercible - Boolean |
| 96 | +assert(String.prototype.charAt.call(true, 1) === "r"); |
| 97 | + |
| 98 | +// check coercible - Object |
| 99 | +var test_object = {firstName:"John", lastName:"Doe"}; |
| 100 | +assert(String.prototype.charAt.call(test_object, 1) === "o"); |
| 101 | + |
| 102 | +// check coercible - Number |
| 103 | +assert(String.prototype.charAt.call(123, 2) === "3"); |
0 commit comments