]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
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 | ||
93a37866 A |
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. | |
44 | function simplify(str) | |
45 | { | |
46 | return str.replace(/\s+/g, " ").replace(/ (\W)/g, "$1").replace(/(\W) /g, "$1").trim(); | |
47 | } | |
48 | ||
b37bf2e1 A |
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 | ||
62 | var equals = new TestFunction( "Equals", "a, b", tab+ "return a == b;" ); | |
63 | function Equals (a, b) { | |
64 | return a == b; | |
65 | } | |
66 | ||
67 | var reallyequals = new TestFunction( "ReallyEquals", "a, b", | |
68 | ( version() <= 120 ) ? tab +"return a == b;" : tab +"return a === b;" ); | |
69 | function ReallyEquals( a, b ) { | |
70 | return a === b; | |
71 | } | |
72 | ||
73 | var doesntequal = new TestFunction( "DoesntEqual", "a, b", tab + "return a != b;" ); | |
74 | function DoesntEqual( a, b ) { | |
75 | return a != b; | |
76 | } | |
77 | ||
78 | var reallydoesntequal = new TestFunction( "ReallyDoesntEqual", "a, b", | |
79 | ( version() <= 120 ) ? tab +"return a != b;" : tab +"return a !== b;" ); | |
80 | function ReallyDoesntEqual( a, b ) { | |
81 | return a !== b; | |
82 | } | |
83 | ||
93a37866 | 84 | // Modified to match expected results; JSC won't automatically insert redundant braces into the result. |
b37bf2e1 A |
85 | var 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+"}" ); | |
87 | function TestOr( a ) { | |
93a37866 | 88 | if ( a == null || a == void 0 ) { |
b37bf2e1 | 89 | return 0; |
93a37866 | 90 | } else { |
b37bf2e1 | 91 | return a; |
93a37866 | 92 | } |
b37bf2e1 A |
93 | } |
94 | ||
93a37866 | 95 | // Modified to match expected results; JSC won't automatically insert redundant braces into the result. |
b37bf2e1 A |
96 | var 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+"}" ); | |
98 | function TestAnd( a ) { | |
93a37866 | 99 | if ( a != null && a != void 0 ) { |
b37bf2e1 | 100 | return a; |
93a37866 | 101 | } else { |
b37bf2e1 | 102 | return 0; |
93a37866 | 103 | } |
b37bf2e1 A |
104 | } |
105 | ||
106 | var or = new TestFunction( "Or", "a, b", tab + "return a | b;" ); | |
107 | function Or( a, b ) { | |
108 | return a | b; | |
109 | } | |
110 | ||
111 | var and = new TestFunction( "And", "a, b", tab + "return a & b;" ); | |
112 | function And( a, b ) { | |
113 | return a & b; | |
114 | } | |
115 | ||
116 | var xor = new TestFunction( "XOr", "a, b", tab + "return a ^ b;" ); | |
117 | function XOr( a, b ) { | |
118 | return a ^ b; | |
119 | } | |
120 | ||
121 | testcases[testcases.length] = new TestCase( SECTION, | |
122 | "Equals.toString()", | |
93a37866 A |
123 | simplify(equals.valueOf()), |
124 | simplify(Equals.toString()) ); | |
b37bf2e1 A |
125 | |
126 | testcases[testcases.length] = new TestCase( SECTION, | |
127 | "ReallyEquals.toString()", | |
93a37866 A |
128 | simplify(reallyequals.valueOf()), |
129 | simplify(ReallyEquals.toString()) ); | |
b37bf2e1 A |
130 | |
131 | testcases[testcases.length] = new TestCase( SECTION, | |
132 | "DoesntEqual.toString()", | |
93a37866 A |
133 | simplify(doesntequal.valueOf()), |
134 | simplify(DoesntEqual.toString()) ); | |
b37bf2e1 A |
135 | |
136 | testcases[testcases.length] = new TestCase( SECTION, | |
137 | "ReallyDoesntEqual.toString()", | |
93a37866 A |
138 | simplify(reallydoesntequal.valueOf()), |
139 | simplify(ReallyDoesntEqual.toString()) ); | |
b37bf2e1 A |
140 | |
141 | testcases[testcases.length] = new TestCase( SECTION, | |
142 | "TestOr.toString()", | |
93a37866 A |
143 | simplify(testor.valueOf()), |
144 | simplify(TestOr.toString()) ); | |
b37bf2e1 A |
145 | |
146 | testcases[testcases.length] = new TestCase( SECTION, | |
147 | "TestAnd.toString()", | |
93a37866 A |
148 | simplify(testand.valueOf()), |
149 | simplify(TestAnd.toString()) ); | |
b37bf2e1 A |
150 | |
151 | testcases[testcases.length] = new TestCase( SECTION, | |
152 | "Or.toString()", | |
93a37866 A |
153 | simplify(or.valueOf()), |
154 | simplify(Or.toString()) ); | |
b37bf2e1 A |
155 | |
156 | testcases[testcases.length] = new TestCase( SECTION, | |
157 | "And.toString()", | |
93a37866 A |
158 | simplify(and.valueOf()), |
159 | simplify(And.toString()) ); | |
b37bf2e1 A |
160 | |
161 | testcases[testcases.length] = new TestCase( SECTION, | |
162 | "XOr.toString()", | |
93a37866 A |
163 | simplify(xor.valueOf()), |
164 | simplify(XOr.toString()) ); | |
b37bf2e1 A |
165 | |
166 | test(); | |
167 | ||
168 | function 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 | } | |
181 | function 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 | } |