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:          tostring-1.js
24    Section:            Function.toString
25    Description:
26
27    Since the behavior of Function.toString() is implementation-dependent,
28    toString tests for function are not in the ECMA suite.
29
30    Currently, an attempt to parse the toString output for some functions
31    and verify that the result is something reasonable.
32
33    Author:             christine@netscape.com
34    Date:               12 november 1997
35*/
36
37    var SECTION = "tostring-1";
38    var VERSION = "JS1_2";
39    startTest();
40    var TITLE   = "Function.toString()";
41
42    writeHeaderToLog( SECTION + " "+ TITLE);
43
44    var testcases = new Array();
45
46    var tab = "    ";
47
48    t1 = new TestFunction( "stub", "value", tab + "return value;" );
49
50    t2 = new TestFunction( "ToString", "object", tab+"return object + \"\";" );
51
52    t3 = new TestFunction( "Add", "a, b, c, d, e",  tab +"var s = a + b + c + d + e;\n" +
53                        tab + "return s;" );
54
55    t4 = new TestFunction( "noop", "value" );
56
57    t5 = new TestFunction( "anonymous", "", tab+"return \"hello!\";" );
58
59    var f = new Function( "return \"hello!\"");
60
61    testcases[tc++] = new TestCase( SECTION,
62                                    "stub.toString()",
63                                    t1.valueOf(),
64                                    stub.toString() );
65
66    testcases[tc++] = new TestCase( SECTION,
67                                    "ToString.toString()",
68                                    t2.valueOf(),
69                                    ToString.toString() );
70
71    testcases[tc++] = new TestCase( SECTION,
72                                    "Add.toString()",
73                                    t3.valueOf(),
74                                    Add.toString() );
75
76    testcases[tc++] = new TestCase( SECTION,
77                                    "noop.toString()",
78                                    t4.toString(),
79                                    noop.toString() );
80
81    testcases[tc++] = new TestCase( SECTION,
82                                    "f.toString()",
83                                    t5.toString(),
84                                    f.toString() );
85    test();
86
87function noop( value ) {
88}
89function Add( a, b, c, d, e ) {
90    var s = a + b + c + d + e;
91    return s;
92}
93function stub( value ) {
94    return value;
95}
96function ToString( object ) {
97    return object + "";
98}
99
100function ToBoolean( value ) {
101    if ( value == 0 || value == NaN || value == false ) {
102        return false;
103    } else {
104        return true;
105    }
106}
107
108function test() {
109    for ( tc=0; tc < testcases.length; tc++ ) {
110        testcases[tc].passed = writeTestCaseResult(
111                            testcases[tc].expect,
112                            testcases[tc].actual,
113                            testcases[tc].description +" = "+
114                            testcases[tc].actual );
115
116        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
117    }
118    stopTest();
119    return ( testcases );
120}
121
122function TestFunction( name, args, body ) {
123    if ( name == "anonymous" && version() == 120 ) {
124        name = "";
125    }
126
127    this.name = name;
128    this.arguments = args.toString();
129    this.body = body;
130
131    /* the format of Function.toString() in JavaScript 1.2 is:
132    /n
133    function name ( arguments ) {
134        body
135    }
136    */
137    this.value = "\nfunction " + (name ? name : "" )+
138    "("+args+") {\n"+ (( body ) ? body +"\n" : "") + "}\n";
139
140    this.toString = new Function( "return this.value" );
141    this.valueOf = new Function( "return this.value" );
142    return this;
143}
144