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    This verifies
34    http://scopus.mcom.com/bugsplat/show_bug.cgi?id=99212
35
36    Author:             christine@netscape.com
37    Date:               12 november 1997
38*/
39
40// These test cases should not be testing for a particular
41// whitespace formatting (this is implementation defined).
42// Strip out whitespace, or in the case of whitespace
43// abutting a word character reduce to a single space.
44function simplify(str)
45{
46    return str.replace(/\s+/g, " ").replace(/ (\W)/g, "$1").replace(/(\W) /g, "$1").trim();
47}
48
49    var SECTION = "tostring-2";
50    var VERSION = "JS1_2";
51    startTest();
52    var TITLE   = "Function.toString()";
53    var BUGNUMBER="123444";
54
55    writeHeaderToLog( SECTION + " "+ TITLE);
56
57    var testcases = new Array();
58
59    var tab = "    ";
60
61
62var equals = new TestFunction( "Equals", "a, b", tab+ "return a == b;" );
63function Equals (a, b) {
64    return a == b;
65}
66
67var reallyequals = new TestFunction( "ReallyEquals", "a, b",
68    ( version() <= 120 )  ? tab +"return a == b;" : tab +"return a === b;" );
69function ReallyEquals( a, b ) {
70    return a === b;
71}
72
73var doesntequal = new TestFunction( "DoesntEqual", "a, b", tab + "return a != b;" );
74function DoesntEqual( a, b ) {
75    return a != b;
76}
77
78var reallydoesntequal = new TestFunction( "ReallyDoesntEqual", "a, b",
79    ( version() <= 120 ) ? tab +"return a != b;"  : tab +"return a !== b;" );
80function ReallyDoesntEqual( a, b ) {
81    return a !== b;
82}
83
84// Modified to match expected results; JSC won't automatically insert redundant braces into the result.
85var testor = new TestFunction( "TestOr", "a",  tab+"if (a == null || a == void 0) {\n"+
86    tab +tab+"return 0;\n"+tab+"} else {\n"+tab+tab+"return a;\n"+tab+"}" );
87function TestOr( a ) {
88 if ( a == null || a == void 0 ) {
89    return 0;
90 } else {
91    return a;
92 }
93}
94
95// Modified to match expected results; JSC won't automatically insert redundant braces into the result.
96var testand = new TestFunction( "TestAnd", "a", tab+"if (a != null && a != void 0) {\n"+
97    tab+tab+"return a;\n" + tab+ "} else {\n"+tab+tab+"return 0;\n"+tab+"}" );
98function TestAnd( a ) {
99 if ( a != null && a != void 0 ) {
100    return a;
101 } else {
102    return 0;
103 }
104}
105
106var or = new TestFunction( "Or", "a, b", tab + "return a | b;" );
107function Or( a, b ) {
108    return a | b;
109}
110
111var and = new TestFunction( "And", "a, b", tab + "return a & b;" );
112function And( a, b ) {
113    return a & b;
114}
115
116var xor = new TestFunction( "XOr", "a, b", tab + "return a ^ b;" );
117function XOr( a, b ) {
118    return a ^ b;
119}
120
121    testcases[testcases.length] = new TestCase( SECTION,
122        "Equals.toString()",
123        simplify(equals.valueOf()),
124        simplify(Equals.toString()) );
125
126    testcases[testcases.length] = new TestCase( SECTION,
127        "ReallyEquals.toString()",
128        simplify(reallyequals.valueOf()),
129        simplify(ReallyEquals.toString()) );
130
131    testcases[testcases.length] = new TestCase( SECTION,
132        "DoesntEqual.toString()",
133        simplify(doesntequal.valueOf()),
134        simplify(DoesntEqual.toString()) );
135
136    testcases[testcases.length] = new TestCase( SECTION,
137        "ReallyDoesntEqual.toString()",
138        simplify(reallydoesntequal.valueOf()),
139        simplify(ReallyDoesntEqual.toString()) );
140
141    testcases[testcases.length] = new TestCase( SECTION,
142        "TestOr.toString()",
143        simplify(testor.valueOf()),
144        simplify(TestOr.toString()) );
145
146    testcases[testcases.length] = new TestCase( SECTION,
147        "TestAnd.toString()",
148        simplify(testand.valueOf()),
149        simplify(TestAnd.toString()) );
150
151    testcases[testcases.length] = new TestCase( SECTION,
152        "Or.toString()",
153        simplify(or.valueOf()),
154        simplify(Or.toString()) );
155
156    testcases[testcases.length] = new TestCase( SECTION,
157        "And.toString()",
158        simplify(and.valueOf()),
159        simplify(And.toString()) );
160
161    testcases[testcases.length] = new TestCase( SECTION,
162        "XOr.toString()",
163        simplify(xor.valueOf()),
164        simplify(XOr.toString()) );
165
166    test();
167
168function test() {
169    for ( tc=0; tc < testcases.length; tc++ ) {
170        testcases[tc].passed = writeTestCaseResult(
171                            testcases[tc].expect,
172                            testcases[tc].actual,
173                            testcases[tc].description +" = "+
174                            testcases[tc].actual );
175
176        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
177    }
178    stopTest();
179    return ( testcases );
180}
181function TestFunction( name, args, body ) {
182    this.name = name;
183    this.arguments = args.toString();
184    this.body = body;
185
186    /* the format of Function.toString() in JavaScript 1.2 is:
187    /n
188    function name ( arguments ) {
189        body
190    }
191    */
192    this.value = "\nfunction " + (name ? name : "anonymous" )+
193    "("+args+") {\n"+ (( body ) ? body +"\n" : "") + "}\n";
194
195    this.toString = new Function( "return this.value" );
196    this.valueOf = new Function( "return this.value" );
197    return this;
198}
199