Home | History | Annotate | Download | only in String
      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-1.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     Author:             christine (at) netscape.com
     46     Date:               2 october 1997
     47 */
     48     var SECTION = "15.5.4.4-2";
     49     var VERSION = "ECMA_1";
     50     startTest();
     51     var TITLE   = "String.prototype.charAt";
     52 
     53     writeHeaderToLog( SECTION + " "+ TITLE);
     54 
     55     var testcases = getTestCases();
     56     test();
     57 
     58 function getTestCases() {
     59     var array = new Array();
     60     var item = 0;
     61     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(0)", "t",    eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(0)") );
     62     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(1)", "r",    eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(1)") );
     63     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(2)", "u",    eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(2)") );
     64     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(3)", "e",    eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(3)") );
     65     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(4)", "",     eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(4)") );
     66     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(-1)", "",    eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(-1)") );
     67 
     68     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(true)", "r",    eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(true)") );
     69     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(false)", "t",    eval("x = new Boolean(true); x.charAt=String.prototype.charAt;x.charAt(false)") );
     70 
     71     array[item++] = new TestCase( SECTION,     "x = new String(); x.charAt(0)",    "",     eval("x=new String();x.charAt(0)") );
     72     array[item++] = new TestCase( SECTION,     "x = new String(); x.charAt(1)",    "",     eval("x=new String();x.charAt(1)") );
     73     array[item++] = new TestCase( SECTION,     "x = new String(); x.charAt(-1)",   "",     eval("x=new String();x.charAt(-1)") );
     74 
     75     array[item++] = new TestCase( SECTION,     "x = new String(); x.charAt(NaN)",  "",     eval("x=new String();x.charAt(Number.NaN)") );
     76     array[item++] = new TestCase( SECTION,     "x = new String(); x.charAt(Number.POSITIVE_INFINITY)",   "",     eval("x=new String();x.charAt(Number.POSITIVE_INFINITY)") );
     77     array[item++] = new TestCase( SECTION,     "x = new String(); x.charAt(Number.NEGATIVE_INFINITY)",   "",     eval("x=new String();x.charAt(Number.NEGATIVE_INFINITY)") );
     78 
     79     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(0)",  "1",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(0)") );
     80     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(1)",  "2",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(1)") );
     81     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(2)",  "3",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(2)") );
     82     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(3)",  "4",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(3)") );
     83     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(4)",  "5",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(4)") );
     84     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(5)",  "6",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(5)") );
     85     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(6)",  "7",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(6)") );
     86     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(7)",  "8",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(7)") );
     87     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(8)",  "9",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(8)") );
     88     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(9)",  "0",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(9)") );
     89     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(10)",  "",       eval("var MYOB = new MyObject(1234567890); MYOB.charAt(10)") );
     90 
     91     array[item++] = new TestCase( SECTION,      "var MYOB = new MyObject(1234567890); MYOB.charAt(Math.PI)",  "4",        eval("var MYOB = new MyObject(1234567890); MYOB.charAt(Math.PI)") );
     92 
     93     // MyOtherObject.toString will return "[object Object]
     94 
     95     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(0)",  "[",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(0)") );
     96     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(1)",  "o",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(1)") );
     97     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(2)",  "b",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(2)") );
     98     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(3)",  "j",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(3)") );
     99     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(4)",  "e",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(4)") );
    100     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(5)",  "c",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(5)") );
    101     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(6)",  "t",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(6)") );
    102     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(7)",  " ",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(7)") );
    103     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(8)",  "O",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(8)") );
    104     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(9)",  "b",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(9)") );
    105     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(10)",  "j",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(10)") );
    106     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(11)",  "e",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(11)") );
    107     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(12)",  "c",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(12)") );
    108     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(13)",  "t",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(13)") );
    109     array[item++] = new TestCase( SECTION,      "var MYOB = new MyOtherObject(1234567890); MYOB.charAt(14)",  "]",        eval("var MYOB = new MyOtherObject(1234567890); MYOB.charAt(14)") );
    110 
    111     return (array );
    112 }
    113 
    114 function test() {
    115         for ( tc = 0; tc < testcases.length; tc++ ) {
    116             testcases[tc].passed = writeTestCaseResult(
    117                     testcases[tc].expect,
    118                     testcases[tc].actual,
    119                     testcases[tc].description +" = "+
    120                     testcases[tc].actual );
    121 
    122             testcases[tc].reason += ( testcases[tc].passed )
    123                                     ? ""
    124                                     : "wrong value "
    125         }
    126 
    127         stopTest();
    128         return ( testcases );
    129 }
    130 
    131 function MyObject( value ) {
    132     this.value      = value;
    133     this.valueOf    = new Function( "return this.value;" );
    134     this.toString   = new Function( "return this.value +''" );
    135     this.charAt     = String.prototype.charAt;
    136 }
    137 function MyOtherObject(value) {
    138     this.value      = value;
    139     this.valueOf    = new Function( "return this.value;" );
    140     this.charAt     = String.prototype.charAt;
    141 }
    142