]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_2/function/tostring-1.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / mozilla / js1_2 / function / tostring-1.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/
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 // These test cases should not be testing for a particular
38 // whitespace formatting (this is implementation defined).
39 // Strip out whitespace, or in the case of whitespace
40 // abutting a word character reduce to a single space.
41 function simplify(str)
42 {
43 return str.replace(/\s+/g, " ").replace(/ (\W)/g, "$1").replace(/(\W) /g, "$1").trim();
44 }
45
46 var SECTION = "tostring-1";
47 var VERSION = "JS1_2";
48 startTest();
49 var TITLE = "Function.toString()";
50
51 writeHeaderToLog( SECTION + " "+ TITLE);
52
53 var testcases = new Array();
54
55 var tab = " ";
56
57 t1 = new TestFunction( "stub", "value", tab + "return value;" );
58
59 t2 = new TestFunction( "ToString", "object", tab+"return object + \"\";" );
60
61 t3 = new TestFunction( "Add", "a, b, c, d, e", tab +"var s = a + b + c + d + e;\n" +
62 tab + "return s;" );
63
64 t4 = new TestFunction( "noop", "value" );
65
66 t5 = new TestFunction( "anonymous", "", tab+"return \"hello!\";" );
67
68 var f = new Function( "return \"hello!\";");
69
70 testcases[tc++] = new TestCase( SECTION,
71 "stub.toString()",
72 simplify(t1.valueOf()),
73 simplify(stub.toString()) );
74
75 testcases[tc++] = new TestCase( SECTION,
76 "ToString.toString()",
77 simplify(t2.valueOf()),
78 simplify(ToString.toString()) );
79
80 testcases[tc++] = new TestCase( SECTION,
81 "Add.toString()",
82 simplify(t3.valueOf()),
83 simplify(Add.toString()) );
84
85 testcases[tc++] = new TestCase( SECTION,
86 "noop.toString()",
87 simplify(t4.toString()),
88 simplify(noop.toString()) );
89
90 testcases[tc++] = new TestCase( SECTION,
91 "f.toString()",
92 simplify(t5.toString()),
93 simplify(f.toString()) );
94 test();
95
96 function noop( value ) {
97 }
98 function Add( a, b, c, d, e ) {
99 var s = a + b + c + d + e;
100 return s;
101 }
102 function stub( value ) {
103 return value;
104 }
105 function ToString( object ) {
106 return object + "";
107 }
108
109 function ToBoolean( value ) {
110 if ( value == 0 || value == NaN || value == false ) {
111 return false;
112 } else {
113 return true;
114 }
115 }
116
117 function test() {
118 for ( tc=0; tc < testcases.length; tc++ ) {
119 testcases[tc].passed = writeTestCaseResult(
120 testcases[tc].expect,
121 testcases[tc].actual,
122 testcases[tc].description +" = "+
123 testcases[tc].actual );
124
125 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
126 }
127 stopTest();
128 return ( testcases );
129 }
130
131 function TestFunction( name, args, body ) {
132 if ( name == "anonymous" && version() == 120 ) {
133 name = "";
134 }
135
136 this.name = name;
137 this.arguments = args.toString();
138 this.body = body;
139
140 /* the format of Function.toString() in JavaScript 1.2 is:
141 /n
142 function name ( arguments ) {
143 body
144 }
145 */
146 this.value = "\nfunction " + (name ? name : "" )+
147 "("+args+") {\n"+ (( body ) ? body +"\n" : "") + "}\n";
148
149 this.toString = new Function( "return this.value" );
150 this.valueOf = new Function( "return this.value" );
151 return this;
152 }