1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// check properties 16 17function length_configurable() 18{ 19 function is_es51() { 20 return (typeof g === "function"); 21 { function g() {} } 22 } 23 return is_es51() ? false : true; 24} 25 26assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').configurable === length_configurable()); 27 28assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').enumerable === false); 29 30assert(Object.getOwnPropertyDescriptor(String.prototype.charAt, 'length').writable === false); 31 32assert(String.prototype.charAt.length === 1); 33 34// check empty string 35assert(String.prototype.charAt.call(new String()) === ""); 36 37// check NaN 38assert("hello world!".charAt(NaN) === "h"); 39 40// check Object 41assert(String.prototype.charAt.call({}) === "["); 42 43// simple checks 44assert("hello world!".charAt(0) === "h"); 45 46assert("hello world!".charAt(1) === "e"); 47 48// check +-Inf 49assert("hello world!".charAt(-Infinity) === ""); 50 51assert("hello world!".charAt(Infinity) === ""); 52 53assert("hello world!".charAt(11) === "!"); 54 55assert("hello world!".charAt(12) === ""); 56 57// check unicode 58assert("hello\u000B\u000C\u0020\u00A0world!".charAt(8) === "\u00A0"); 59 60assert("hello\uD834\uDF06world!".charAt(6) === "\uDF06"); 61 62assert("hell\u006F\u006F w\u006F\u006Frld!".charAt(8) === "\u006F"); 63 64assert("\u00A9\u006F".charAt(2) === ""); 65 66// check negative 67assert("hello world!".charAt(-1) === ""); 68 69assert("hello world!".charAt(-9999999) === ""); 70 71assert("hello world!".charAt(-0) === "h"); 72 73// check undefined 74assert("hello world!".charAt(undefined) === "h"); 75 76// check booleans 77assert("hello world!".charAt(true) === "e"); 78 79assert("hello world!".charAt(false) === "h"); 80 81// check this is undefined 82try { 83 String.prototype.charAt.call(undefined); 84 assert(false); 85} catch(e) { 86 assert(e instanceof TypeError); 87} 88 89// check this is null 90try { 91 String.prototype.charAt.call(null); 92 assert(false); 93} catch(e) { 94 assert(e instanceof TypeError); 95} 96 97// check coercible - undefined 98try { 99 assert(true.charAt() === ""); 100 assert(false); 101} catch (e) { 102 assert(e instanceof TypeError); 103} 104 105// check coercible - null 106try { 107 assert(String.prototype.charAt.call(null, 0) === ""); 108 assert(false); 109} catch (e) { 110 assert(e instanceof TypeError); 111} 112 113// check coercible - Boolean 114assert(String.prototype.charAt.call(true, 1) === "r"); 115 116// check coercible - Object 117var test_object = {firstName:"John", lastName:"Doe"}; 118assert(String.prototype.charAt.call(test_object, 1) === "o"); 119 120// check coercible - Number 121assert(String.prototype.charAt.call(123, 2) === "3"); 122