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.indexOf, 'length').configurable === length_configurable()); 27 28assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').enumerable === false); 29 30assert(Object.getOwnPropertyDescriptor(String.prototype.indexOf, 'length').writable === false); 31 32assert(String.prototype.indexOf.length === 1); 33 34assert("Hello world, welcome to the universe.".indexOf("welcome") === 13); 35 36assert("Hello world, welcome to the universe.".indexOf("Hello world, welcome to the universe.") === 0); 37 38assert("Hello world, welcome to the universe.".indexOf("welcome",10) == 13); 39 40assert("Hello world, welcome to the universe.".indexOf("welcome",-100) == 13); 41 42assert("Hello world, welcome to the universe.".indexOf("welcome", 15) === -1); 43 44assert("Hello world, welcome to the universe.".indexOf("o", 15) === 17); 45 46// check utf8 strings 47assert("\uFFA2".indexOf("\uFFA2") === 0); 48 49assert("\uFFA2".indexOf("A") === -1); 50 51assert("w2\uFFA2A".indexOf("A") === 3); 52 53assert("w2\u1D306A".indexOf("A") === 4); 54 55assert("\uD834\uDF06".indexOf("\uDF06") === 1); 56 57assert("\uD834\uDF06w2\u1D306D".indexOf("D") === 6); 58 59assert("\ud800\dc00".indexOf("\dc00") === 1); 60 61assert("\u8000\u0700\u8000\u8000A".indexOf("A", 3) === 4); 62 63// check prefix search 64assert("aaaabaaa".indexOf("aaaba") === 1); 65 66// check empty string 67assert(String.prototype.indexOf.call(new String()) === -1); 68 69assert(String.prototype.indexOf.call("","") === 0); 70 71// check NaN 72assert("Hello world, welcome to the universe.".indexOf(NaN) === -1); 73 74assert("Hello world, welcome to the universe.".indexOf("welcome",NaN) === 13); 75 76// check Object 77assert(String.prototype.indexOf.call({}) === -1); 78 79// check +-Inf 80assert("hello world!".indexOf("world", -Infinity) === 6); 81 82assert("hello world!".indexOf("world", Infinity) === -1); 83 84// check numbers 85assert("hello world!".indexOf(-1) === -1); 86 87assert("hello 0 world!".indexOf(-0) === 6); 88 89// check undefined 90assert("hello world!".indexOf(undefined) === -1); 91 92var undefined_var; 93assert("Hello world, welcome to the universe.".indexOf("welcome", undefined_var) === 13); 94 95// check booleans 96assert("true".indexOf(true, false) === 0); 97 98// check coercible - undefined 99try { 100 String.prototype.indexOf.call(undefined); 101 assert(false); 102} catch(e) { 103 assert(e instanceof TypeError); 104} 105 106// check coercible - null 107try { 108 String.prototype.indexOf.call(null); 109 assert(false); 110} catch(e) { 111 assert(e instanceof TypeError); 112} 113 114// check coercible - Boolean 115assert(String.prototype.indexOf.call(true, "e") === 3); 116assert(String.prototype.indexOf.call(false, "e") === 4); 117 118// check coercible - Object 119var test_object = {firstName:"John", lastName:"Doe"}; 120assert(String.prototype.indexOf.call(test_object, "Obj") === 8); 121 122// check coercible - Number 123assert(String.prototype.indexOf.call(123, "2") === 1); 124