Home | History | Annotate | Download | only in FunctionObjects
      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.3.2.1.js
     24     ECMA Section:       15.3.2.1 The Function Constructor
     25                         new Function(p1, p2, ..., pn, body )
     26 
     27     Description:        The last argument specifies the body (executable code)
     28                         of a function; any preceeding arguments sepcify formal
     29                         parameters.
     30 
     31                         See the text for description of this section.
     32 
     33                         This test examples from the specification.
     34 
     35     Author:             christine (at) netscape.com
     36     Date:               28 october 1997
     37 
     38 */
     39     var SECTION = "15.3.2.1";
     40     var VERSION = "ECMA_1";
     41     startTest();
     42     var TITLE   = "The Function Constructor";
     43 
     44     writeHeaderToLog( SECTION + " "+ TITLE);
     45 
     46     var MyObject = new Function( "value", "this.value = value; this.valueOf = new Function( 'return this.value' ); this.toString = new Function( 'return String(this.value);' )" );
     47 
     48     var testcases = getTestCases();
     49     test();
     50 
     51 function getTestCases() {
     52     var array = new Array();
     53     var item = 0;
     54 
     55     var myfunc = new Function();
     56 
     57 //    not going to test toString here since it is implementation dependent.
     58 //    array[item++] = new TestCase( SECTION,  "myfunc.toString()",     "function anonymous() { }",    myfunc.toString() );
     59 
     60      myfunc.toString = Object.prototype.toString;
     61 
     62     array[item++] = new TestCase( SECTION,  "myfunc = new Function(); myfunc.toString = Object.prototype.toString; myfunc.toString()",
     63                                             "[object Function]",
     64                                             myfunc.toString() );
     65     array[item++] = new TestCase( SECTION,  "myfunc.length",                            0,                      myfunc.length );
     66     array[item++] = new TestCase( SECTION,  "myfunc.prototype.toString()",              "[object Object]",      myfunc.prototype.toString() );
     67 
     68     array[item++] = new TestCase( SECTION,  "myfunc.prototype.constructor",             myfunc,                 myfunc.prototype.constructor );
     69     array[item++] = new TestCase( SECTION,  "myfunc.arguments",                         null,                   myfunc.arguments );
     70 
     71     array[item++] = new TestCase( SECTION,  "var OBJ = new MyObject(true); OBJ.valueOf()",    true,             eval("var OBJ = new MyObject(true); OBJ.valueOf()") );
     72     array[item++] = new TestCase( SECTION,  "OBJ.toString()",                           "true",                 OBJ.toString() );
     73     array[item++] = new TestCase( SECTION,  "OBJ.toString = Object.prototype.toString; OBJ.toString()", "[object Object]",  eval("OBJ.toString = Object.prototype.toString; OBJ.toString()") );
     74     array[item++] = new TestCase( SECTION,  "MyObject.toString = Object.prototype.toString; MyObject.toString()",    "[object Function]",   eval("MyObject.toString = Object.prototype.toString; MyObject.toString()") );
     75 
     76     array[item++] = new TestCase( SECTION,  "MyObject.__proto__ == Function.prototype",     true,   MyObject.__proto__ == Function.prototype );
     77     array[item++] = new TestCase( SECTION,  "MyObject.length",                              1,      MyObject.length );
     78     array[item++] = new TestCase( SECTION,  "MyObject.prototype.constructor",               MyObject,   MyObject.prototype.constructor );
     79     array[item++] = new TestCase( SECTION,  "MyObject.arguments",                           null,   MyObject.arguments );
     80 
     81     return ( array );
     82 }
     83 function test() {
     84     for ( tc=0; tc < testcases.length; tc++ ) {
     85         testcases[tc].passed = writeTestCaseResult(
     86                             testcases[tc].expect,
     87                             testcases[tc].actual,
     88                             testcases[tc].description +" = "+ testcases[tc].actual );
     89 
     90         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     91     }
     92     stopTest();
     93     return ( testcases );
     94 }
     95