1 /* The contents of this file are subject to the Netscape Public 2 * License Version 1.1 (the "License"); you may not use this file 3 * except in compliance with the License. You may obtain a copy of 4 * the License at http://www.mozilla.org/NPL/ 5 * 6 * Software distributed under the License is distributed on an "AS 7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 8 * implied. See the License for the specific language governing 9 * rights and limitations under the License. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 14 * The Initial Developer of the Original Code is Netscape Communications 15 * Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22 /** 23 File Name: 15.5.4.4-3.js 24 ECMA Section: 15.5.4.4 String.prototype.charAt(pos) 25 Description: Returns a string containing the character at position 26 pos in the string. If there is no character at that 27 string, the result is the empty string. The result is 28 a string value, not a String object. 29 30 When the charAt method is called with one argument, 31 pos, the following steps are taken: 32 1. Call ToString, with this value as its argument 33 2. Call ToInteger pos 34 3. Compute the number of characters in Result(1) 35 4. If Result(2) is less than 0 is or not less than 36 Result(3), return the empty string 37 5. Return a string of length 1 containing one character 38 from result (1), the character at position Result(2). 39 40 Note that the charAt function is intentionally generic; 41 it does not require that its this value be a String 42 object. Therefore it can be transferred to other kinds 43 of objects for use as a method. 44 45 This tests assiging charAt to a user-defined function. 46 47 Author: christine (at) netscape.com 48 Date: 2 october 1997 49 */ 50 var SECTION = "15.5.4.4-3"; 51 var VERSION = "ECMA_1"; 52 startTest(); 53 var TITLE = "String.prototype.charAt"; 54 55 writeHeaderToLog( SECTION + " "+ TITLE); 56 57 var testcases = getTestCases(); 58 test(); 59 60 function MyObject (v) { 61 this.value = v; 62 this.toString = new Function( "return this.value +'';" ); 63 this.valueOf = new Function( "return this.value" ); 64 this.charAt = String.prototype.charAt; 65 } 66 function getTestCases() { 67 var array = new Array(); 68 var item = 0; 69 70 var foo = new MyObject('hello'); 71 72 73 array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello'); ", "h", foo.charAt(0) ); 74 array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello'); ", "e", foo.charAt(1) ); 75 array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello'); ", "l", foo.charAt(2) ); 76 array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello'); ", "l", foo.charAt(3) ); 77 array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello'); ", "o", foo.charAt(4) ); 78 array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello'); ", "", foo.charAt(-1) ); 79 array[item++] = new TestCase( SECTION, "var foo = new MyObject('hello'); ", "", foo.charAt(5) ); 80 81 var boo = new MyObject(true); 82 83 array[item++] = new TestCase( SECTION, "var boo = new MyObject(true); ", "t", boo.charAt(0) ); 84 array[item++] = new TestCase( SECTION, "var boo = new MyObject(true); ", "r", boo.charAt(1) ); 85 array[item++] = new TestCase( SECTION, "var boo = new MyObject(true); ", "u", boo.charAt(2) ); 86 array[item++] = new TestCase( SECTION, "var boo = new MyObject(true); ", "e", boo.charAt(3) ); 87 88 var noo = new MyObject( Math.PI ); 89 90 array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "3", noo.charAt(0) ); 91 array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", ".", noo.charAt(1) ); 92 array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "1", noo.charAt(2) ); 93 array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "4", noo.charAt(3) ); 94 array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "1", noo.charAt(4) ); 95 array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "5", noo.charAt(5) ); 96 array[item++] = new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "9", noo.charAt(6) ); 97 98 return array; 99 } 100 101 function test() { 102 for ( tc = 0; tc < testcases.length; tc++ ) { 103 testcases[tc].passed = writeTestCaseResult( 104 testcases[tc].expect, 105 testcases[tc].actual, 106 testcases[tc].description +" = "+ 107 testcases[tc].actual ); 108 109 testcases[tc].reason += ( testcases[tc].passed ) 110 ? "" 111 : "wrong value " 112 } 113 stopTest(); 114 115 // all tests must return a boolean value 116 return ( testcases ); 117 } 118