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