]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* |
2 | * The contents of this file are subject to the Netscape Public | |
3 | * License Version 1.1 (the "License"); you may not use this file | |
4 | * except in compliance with the License. You may obtain a copy of | |
5 | * the License at http://www.mozilla.org/NPL/ | |
6 | * | |
7 | * Software distributed under the License is distributed on an "AS | |
8 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
9 | * implied. See the License for the specific language governing | |
10 | * rights and limitations under the License. | |
11 | * | |
12 | * The Original Code is mozilla.org code. | |
13 | * | |
14 | * The Initial Developer of the Original Code is Netscape | |
15 | * Communications Corporation. Portions created by Netscape are | |
16 | * Copyright (C) 1998 Netscape Communications Corporation. All | |
17 | * Rights Reserved. | |
18 | * | |
19 | * Contributor(s): | |
20 | */ | |
21 | ||
22 | var completed = false; | |
23 | var testcases; | |
24 | var tc = 0; | |
25 | ||
26 | SECTION = ""; | |
27 | VERSION = ""; | |
28 | BUGNUMBER = ""; | |
29 | ||
30 | var GLOBAL = "[object global]"; | |
31 | var PASSED = " PASSED!" | |
32 | var FAILED = " FAILED! expected: "; | |
33 | ||
34 | function test() { | |
35 | for ( tc=0; tc < testcases.length; tc++ ) { | |
36 | testcases[tc].passed = writeTestCaseResult( | |
37 | testcases[tc].expect, | |
38 | testcases[tc].actual, | |
39 | testcases[tc].description +" = "+ | |
40 | testcases[tc].actual ); | |
41 | ||
42 | testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; | |
43 | } | |
44 | stopTest(); | |
45 | return ( testcases ); | |
46 | } | |
47 | /* wrapper for test cas constructor that doesn't require the SECTION | |
48 | * argument. | |
49 | */ | |
50 | ||
51 | function AddTestCase( description, expect, actual ) { | |
52 | testcases[tc++] = new TestCase( SECTION, description, expect, actual ); | |
53 | } | |
54 | ||
55 | function TestCase( n, d, e, a ) { | |
56 | this.name = n; | |
57 | this.description = d; | |
58 | this.expect = e; | |
59 | this.actual = a; | |
60 | this.passed = true; | |
61 | this.reason = ""; | |
62 | ||
63 | this.passed = getTestCaseResult( this.expect, this.actual ); | |
64 | } | |
65 | function startTest() { | |
66 | version(110); | |
67 | ||
68 | if ( BUGNUMBER ) { | |
69 | writeLineToLog ("BUGNUMBER: " + BUGNUMBER ); | |
70 | } | |
71 | ||
72 | testcases = new Array(); | |
73 | tc = 0; | |
74 | } | |
75 | function getTestCaseResult( expect, actual ) { | |
76 | // because ( NaN == NaN ) always returns false, need to do | |
77 | // a special compare to see if we got the right result. | |
78 | if ( actual != actual ) { | |
79 | if ( typeof actual == "object" ) { | |
80 | actual = "NaN object"; | |
81 | } else { | |
82 | actual = "NaN number"; | |
83 | } | |
84 | } | |
85 | if ( expect != expect ) { | |
86 | if ( typeof expect == "object" ) { | |
87 | expect = "NaN object"; | |
88 | } else { | |
89 | expect = "NaN number"; | |
90 | } | |
91 | } | |
92 | ||
93 | var passed = ( expect == actual ) ? true : false; | |
94 | ||
95 | // if both objects are numbers, give a little leeway for rounding. | |
96 | if ( !passed | |
97 | && typeof(actual) == "number" | |
98 | && typeof(expect) == "number" | |
99 | ) { | |
100 | if ( Math.abs(actual-expect) < 0.0000001 ) { | |
101 | passed = true; | |
102 | } | |
103 | } | |
104 | ||
105 | // verify type is the same | |
106 | if ( typeof(expect) != typeof(actual) ) { | |
107 | passed = false; | |
108 | } | |
109 | ||
110 | return passed; | |
111 | } | |
112 | /* | |
113 | * Begin printing functions. These functions use the shell's | |
114 | * print function. When running tests in the browser, these | |
115 | * functions, override these functions with functions that use | |
116 | * document.write. | |
117 | */ | |
118 | ||
119 | function writeTestCaseResult( expect, actual, string ) { | |
120 | var passed = getTestCaseResult( expect, actual ); | |
121 | writeFormattedResult( expect, actual, string, passed ); | |
122 | return passed; | |
123 | } | |
124 | function writeFormattedResult( expect, actual, string, passed ) { | |
125 | var s = string ; | |
126 | s += ( passed ) ? PASSED : FAILED + expect; | |
127 | writeLineToLog( s); | |
128 | return passed; | |
129 | } | |
130 | function writeLineToLog( string ) { | |
131 | print( string ); | |
132 | } | |
133 | function writeHeaderToLog( string ) { | |
134 | print( string ); | |
135 | } | |
136 | /* end of print functions */ | |
137 | ||
138 | function stopTest() { | |
139 | var gc; | |
140 | if ( gc != undefined ) { | |
141 | gc(); | |
142 | } | |
143 | } |