|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 var completed = false; |
|
5 var testcases; |
|
6 var tc = 0; |
|
7 |
|
8 SECTION = ""; |
|
9 VERSION = ""; |
|
10 BUGNUMBER = ""; |
|
11 EXCLUDE = ""; |
|
12 |
|
13 /* |
|
14 * constant strings |
|
15 */ |
|
16 var GLOBAL = "[object global]"; |
|
17 var PASSED = " PASSED!" |
|
18 var FAILED = " FAILED! expected: "; |
|
19 |
|
20 var DEBUG = false; |
|
21 |
|
22 version("120"); |
|
23 /* |
|
24 * change this for date tests if you're not in PST |
|
25 */ |
|
26 |
|
27 TZ_DIFF = -8; |
|
28 /* wrapper for test cas constructor that doesn't require the SECTION |
|
29 * argument. |
|
30 */ |
|
31 |
|
32 function AddTestCase( description, expect, actual ) { |
|
33 testcases[tc++] = new TestCase( SECTION, description, expect, actual ); |
|
34 } |
|
35 function TestCase( n, d, e, a ) { |
|
36 this.name = n; |
|
37 this.description = d; |
|
38 this.expect = e; |
|
39 this.actual = a; |
|
40 this.passed = true; |
|
41 this.reason = ""; |
|
42 this.bugnumber = BUGNUMBER; |
|
43 |
|
44 this.passed = getTestCaseResult( this.expect, this.actual ); |
|
45 } |
|
46 function startTest() { |
|
47 version(120); |
|
48 |
|
49 // for ecma version 2.0, we will leave the javascript version to |
|
50 // the default ( for now ). |
|
51 // print out bugnumber |
|
52 |
|
53 if ( BUGNUMBER ) { |
|
54 print ("BUGNUMBER: " + BUGNUMBER ); |
|
55 } |
|
56 |
|
57 testcases = new Array(); |
|
58 tc = 0; |
|
59 |
|
60 } |
|
61 function test() { |
|
62 for ( tc=0; tc < testcases.length; tc++ ) { |
|
63 testcases[tc].passed = writeTestCaseResult( |
|
64 testcases[tc].expect, |
|
65 testcases[tc].actual, |
|
66 testcases[tc].description +" = "+ |
|
67 testcases[tc].actual ); |
|
68 |
|
69 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; |
|
70 } |
|
71 stopTest(); |
|
72 return ( testcases ); |
|
73 } |
|
74 function getTestCaseResult( expect, actual ) { |
|
75 // because ( NaN == NaN ) always returns false, need to do |
|
76 // a special compare to see if we got the right result. |
|
77 if ( actual != actual ) { |
|
78 if ( typeof actual == "object" ) { |
|
79 actual = "NaN object"; |
|
80 } else { |
|
81 actual = "NaN number"; |
|
82 } |
|
83 } |
|
84 if ( expect != expect ) { |
|
85 if ( typeof expect == "object" ) { |
|
86 expect = "NaN object"; |
|
87 } else { |
|
88 expect = "NaN number"; |
|
89 } |
|
90 } |
|
91 |
|
92 var passed = ( expect == actual ) ? true : false; |
|
93 |
|
94 // if both objects are numbers, give a little leeway for rounding. |
|
95 if ( !passed |
|
96 && typeof(actual) == "number" |
|
97 && typeof(expect) == "number" |
|
98 ) { |
|
99 if ( Math.abs(actual-expect) < 0.0000001 ) { |
|
100 passed = true; |
|
101 } |
|
102 } |
|
103 |
|
104 // verify type is the same |
|
105 if ( typeof(expect) != typeof(actual) ) { |
|
106 passed = false; |
|
107 } |
|
108 |
|
109 return passed; |
|
110 } |
|
111 /* |
|
112 * Begin printing functions. These functions use the shell's |
|
113 * print function. When running tests in the browser, these |
|
114 * functions, override these functions with functions that use |
|
115 * document.write. |
|
116 */ |
|
117 |
|
118 function writeTestCaseResult( expect, actual, string ) { |
|
119 var passed = getTestCaseResult( expect, actual ); |
|
120 writeFormattedResult( expect, actual, string, passed ); |
|
121 return passed; |
|
122 } |
|
123 function writeFormattedResult( expect, actual, string, passed ) { |
|
124 var s = string ; |
|
125 s += ( passed ) ? PASSED : FAILED + expect; |
|
126 print( s); |
|
127 return passed; |
|
128 } |
|
129 |
|
130 function writeHeaderToLog( string ) { |
|
131 print( string ); |
|
132 } |
|
133 /* end of print functions */ |
|
134 |
|
135 |
|
136 function stopTest() { |
|
137 var sizeTag = "<#TEST CASES SIZE>"; |
|
138 var doneTag = "<#TEST CASES DONE>"; |
|
139 var beginTag = "<#TEST CASE "; |
|
140 var endTag = ">"; |
|
141 |
|
142 print(sizeTag); |
|
143 print(testcases.length); |
|
144 for (tc = 0; tc < testcases.length; tc++) |
|
145 { |
|
146 print(beginTag + 'PASSED' + endTag); |
|
147 print(testcases[tc].passed); |
|
148 print(beginTag + 'NAME' + endTag); |
|
149 print(testcases[tc].name); |
|
150 print(beginTag + 'EXPECTED' + endTag); |
|
151 print(testcases[tc].expect); |
|
152 print(beginTag + 'ACTUAL' + endTag); |
|
153 print(testcases[tc].actual); |
|
154 print(beginTag + 'DESCRIPTION' + endTag); |
|
155 print(testcases[tc].description); |
|
156 print(beginTag + 'REASON' + endTag); |
|
157 print(( testcases[tc].passed ) ? "" : "wrong value "); |
|
158 print(beginTag + 'BUGNUMBER' + endTag); |
|
159 print( BUGNUMBER ); |
|
160 } |
|
161 print(doneTag); |
|
162 gc(); |
|
163 } |
|
164 |
|
165 function getFailedCases() { |
|
166 for ( var i = 0; i < testcases.length; i++ ) { |
|
167 if ( ! testcases[i].passed ) { |
|
168 print( testcases[i].description +" = " +testcases[i].actual +" expected: "+ testcases[i].expect ); |
|
169 } |
|
170 } |
|
171 } |
|
172 function err( msg, page, line ) { |
|
173 testcases[tc].actual = "error"; |
|
174 testcases[tc].reason = msg; |
|
175 writeTestCaseResult( testcases[tc].expect, |
|
176 testcases[tc].actual, |
|
177 testcases[tc].description +" = "+ testcases[tc].actual + |
|
178 ": " + testcases[tc].reason ); |
|
179 stopTest(); |
|
180 return true; |
|
181 } |
|
182 function Enumerate ( o ) { |
|
183 var p; |
|
184 for ( p in o ) { |
|
185 print( p +": " + o[p] ); |
|
186 } |
|
187 } |
|
188 function GetContext() { |
|
189 return Packages.com.netscape.javascript.Context.getCurrentContext(); |
|
190 } |
|
191 function OptLevel( i ) { |
|
192 i = Number(i); |
|
193 var cx = GetContext(); |
|
194 cx.setOptimizationLevel(i); |
|
195 } |