]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_2/function/tostring-2.js
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/
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.
11 * The Original Code is Mozilla Communicator client code, released March
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
23 File Name: tostring-1.js
24 Section: Function.toString
27 Since the behavior of Function.toString() is implementation-dependent,
28 toString tests for function are not in the ECMA suite.
30 Currently, an attempt to parse the toString output for some functions
31 and verify that the result is something reasonable.
34 http://scopus.mcom.com/bugsplat/show_bug.cgi?id=99212
36 Author: christine@netscape.com
37 Date: 12 november 1997
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
)
46 return str
.replace(/\s+/g, " ").replace(/ (\W
)/g
, "$1").replace(/(\W) /g, "$1").trim();
49 var SECTION
= "tostring-2";
50 var VERSION
= "JS1_2";
52 var TITLE
= "Function.toString()";
53 var BUGNUMBER
="123444";
55 writeHeaderToLog( SECTION
+ " "+ TITLE
);
57 var testcases
= new Array();
62 var equals
= new TestFunction( "Equals", "a, b", tab
+ "return a == b;" );
63 function Equals (a
, b
) {
67 var reallyequals
= new TestFunction( "ReallyEquals", "a, b",
68 ( version() <= 120 ) ? tab
+"return a == b;" : tab
+"return a === b;" );
69 function ReallyEquals( a
, b
) {
73 var doesntequal
= new TestFunction( "DoesntEqual", "a, b", tab
+ "return a != b;" );
74 function DoesntEqual( a
, b
) {
78 var reallydoesntequal
= new TestFunction( "ReallyDoesntEqual", "a, b",
79 ( version() <= 120 ) ? tab
+"return a != b;" : tab
+"return a !== b;" );
80 function ReallyDoesntEqual( a
, b
) {
84 // Modified to match expected results; JSC won't automatically insert redundant braces into the result.
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
) {
88 if ( a
== null || a
== void 0 ) {
95 // Modified to match expected results; JSC won't automatically insert redundant braces into the result.
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
) {
99 if ( a
!= null && a
!= void 0 ) {
106 var or
= new TestFunction( "Or", "a, b", tab
+ "return a | b;" );
107 function Or( a
, b
) {
111 var and
= new TestFunction( "And", "a, b", tab
+ "return a & b;" );
112 function And( a
, b
) {
116 var xor
= new TestFunction( "XOr", "a, b", tab
+ "return a ^ b;" );
117 function XOr( a
, b
) {
121 testcases
[testcases
.length
] = new TestCase( SECTION
,
123 simplify(equals
.valueOf()),
124 simplify(Equals
.toString()) );
126 testcases
[testcases
.length
] = new TestCase( SECTION
,
127 "ReallyEquals.toString()",
128 simplify(reallyequals
.valueOf()),
129 simplify(ReallyEquals
.toString()) );
131 testcases
[testcases
.length
] = new TestCase( SECTION
,
132 "DoesntEqual.toString()",
133 simplify(doesntequal
.valueOf()),
134 simplify(DoesntEqual
.toString()) );
136 testcases
[testcases
.length
] = new TestCase( SECTION
,
137 "ReallyDoesntEqual.toString()",
138 simplify(reallydoesntequal
.valueOf()),
139 simplify(ReallyDoesntEqual
.toString()) );
141 testcases
[testcases
.length
] = new TestCase( SECTION
,
143 simplify(testor
.valueOf()),
144 simplify(TestOr
.toString()) );
146 testcases
[testcases
.length
] = new TestCase( SECTION
,
147 "TestAnd.toString()",
148 simplify(testand
.valueOf()),
149 simplify(TestAnd
.toString()) );
151 testcases
[testcases
.length
] = new TestCase( SECTION
,
153 simplify(or
.valueOf()),
154 simplify(Or
.toString()) );
156 testcases
[testcases
.length
] = new TestCase( SECTION
,
158 simplify(and
.valueOf()),
159 simplify(And
.toString()) );
161 testcases
[testcases
.length
] = new TestCase( SECTION
,
163 simplify(xor
.valueOf()),
164 simplify(XOr
.toString()) );
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
);
176 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
179 return ( testcases
);
181 function TestFunction( name
, args
, body
) {
183 this.arguments
= args
.toString();
186 /* the format of Function.toString() in JavaScript 1.2 is:
188 function name ( arguments ) {
192 this.value
= "\nfunction " + (name
? name : "anonymous" )+
193 "("+args
+") {\n"+ (( body
) ? body
+"\n" : "") + "}\n";
195 this.toString
= new Function( "return this.value" );
196 this.valueOf
= new Function( "return this.value" );