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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
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.
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
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).
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.
45 This tests assiging charAt to a user-defined function.
47 Author: christine@netscape.com
50 var SECTION = "15.5.4.4-3";
51 var VERSION = "ECMA_1";
53 var TITLE = "String.prototype.charAt";
55 writeHeaderToLog( SECTION + " "+ TITLE);
57 var testcases = getTestCases();
60 function MyObject (v) {
62 this.toString = new Function( "return this.value +'';" );
63 this.valueOf = new Function( "return this.value" );
64 this.charAt = String.prototype.charAt;
66 function getTestCases() {
67 var array = new Array();
70 var foo = new MyObject('hello');
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) );
81 var boo = new MyObject(true);
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) );
88 var noo = new MyObject( Math.PI );
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) );
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 );
109 testcases[tc].reason += ( testcases[tc].passed )
115 // all tests must return a boolean value
116 return ( testcases );