|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 15.5.4.4-3.js |
|
9 ECMA Section: 15.5.4.4 String.prototype.charAt(pos) |
|
10 Description: Returns a string containing the character at position |
|
11 pos in the string. If there is no character at that |
|
12 string, the result is the empty string. The result is |
|
13 a string value, not a String object. |
|
14 |
|
15 When the charAt method is called with one argument, |
|
16 pos, the following steps are taken: |
|
17 1. Call ToString, with this value as its argument |
|
18 2. Call ToInteger pos |
|
19 3. Compute the number of characters in Result(1) |
|
20 4. If Result(2) is less than 0 is or not less than |
|
21 Result(3), return the empty string |
|
22 5. Return a string of length 1 containing one character |
|
23 from result (1), the character at position Result(2). |
|
24 |
|
25 Note that the charAt function is intentionally generic; |
|
26 it does not require that its this value be a String |
|
27 object. Therefore it can be transferred to other kinds |
|
28 of objects for use as a method. |
|
29 |
|
30 This tests assiging charAt to a user-defined function. |
|
31 |
|
32 Author: christine@netscape.com |
|
33 Date: 2 october 1997 |
|
34 */ |
|
35 var SECTION = "15.5.4.4-3"; |
|
36 var VERSION = "ECMA_1"; |
|
37 startTest(); |
|
38 var TITLE = "String.prototype.charAt"; |
|
39 |
|
40 writeHeaderToLog( SECTION + " "+ TITLE); |
|
41 |
|
42 var foo = new MyObject('hello'); |
|
43 |
|
44 |
|
45 new TestCase( SECTION, "var foo = new MyObject('hello'); ", "h", foo.charAt(0) ); |
|
46 new TestCase( SECTION, "var foo = new MyObject('hello'); ", "e", foo.charAt(1) ); |
|
47 new TestCase( SECTION, "var foo = new MyObject('hello'); ", "l", foo.charAt(2) ); |
|
48 new TestCase( SECTION, "var foo = new MyObject('hello'); ", "l", foo.charAt(3) ); |
|
49 new TestCase( SECTION, "var foo = new MyObject('hello'); ", "o", foo.charAt(4) ); |
|
50 new TestCase( SECTION, "var foo = new MyObject('hello'); ", "", foo.charAt(-1) ); |
|
51 new TestCase( SECTION, "var foo = new MyObject('hello'); ", "", foo.charAt(5) ); |
|
52 |
|
53 var boo = new MyObject(true); |
|
54 |
|
55 new TestCase( SECTION, "var boo = new MyObject(true); ", "t", boo.charAt(0) ); |
|
56 new TestCase( SECTION, "var boo = new MyObject(true); ", "r", boo.charAt(1) ); |
|
57 new TestCase( SECTION, "var boo = new MyObject(true); ", "u", boo.charAt(2) ); |
|
58 new TestCase( SECTION, "var boo = new MyObject(true); ", "e", boo.charAt(3) ); |
|
59 |
|
60 var noo = new MyObject( Math.PI ); |
|
61 |
|
62 new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "3", noo.charAt(0) ); |
|
63 new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", ".", noo.charAt(1) ); |
|
64 new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "1", noo.charAt(2) ); |
|
65 new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "4", noo.charAt(3) ); |
|
66 new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "1", noo.charAt(4) ); |
|
67 new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "5", noo.charAt(5) ); |
|
68 new TestCase( SECTION, "var noo = new MyObject(Math.PI); ", "9", noo.charAt(6) ); |
|
69 |
|
70 test(); |
|
71 |
|
72 function MyObject (v) { |
|
73 this.value = v; |
|
74 this.toString = new Function( "return this.value +'';" ); |
|
75 this.valueOf = new Function( "return this.value" ); |
|
76 this.charAt = String.prototype.charAt; |
|
77 } |
|
78 |