blob: 8ced0119fecff9583f7a7e019fcd21ead1c2a763 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28function MjsUnitAssertionError(message) {
29 this.message = message;
30}
31
32MjsUnitAssertionError.prototype.toString = function () {
33 return this.message;
34}
35
36/*
37 * This file is included in all mini jsunit test cases. The test
38 * framework expects lines that signal failed tests to start with
39 * the f-word and ignore all other lines.
40 */
41
42function fail(expected, found, name_opt) {
43 var start;
44 if (name_opt) {
45 // Fix this when we ditch the old test runner.
46 start = "Fail" + "ure (" + name_opt + "): ";
47 } else {
48 start = "Fail" + "ure:";
49 }
50 throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">");
51}
52
53
54function deepObjectEquals(a, b) {
55 var aProps = [];
56 for (var key in a)
57 aProps.push(key);
58 var bProps = [];
59 for (var key in b)
60 bProps.push(key);
61 aProps.sort();
62 bProps.sort();
63 if (!deepEquals(aProps, bProps))
64 return false;
65 for (var i = 0; i < aProps.length; i++) {
66 if (!deepEquals(a[aProps[i]], b[aProps[i]]))
67 return false;
68 }
69 return true;
70}
71
72
73function deepEquals(a, b) {
74 if (a == b) return true;
75 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) {
76 return true;
77 }
Steve Blockd0582a62009-12-15 09:54:21 +000078 if (a.constructor === RegExp || b.constructor === RegExp) {
79 return (a.constructor === b.constructor) && (a.toString === b.toString);
80 }
Steve Blocka7e24c12009-10-30 11:49:00 +000081 if ((typeof a) !== 'object' || (typeof b) !== 'object' ||
82 (a === null) || (b === null))
83 return false;
84 if (a.constructor === Array) {
85 if (b.constructor !== Array)
86 return false;
87 if (a.length != b.length)
88 return false;
89 for (var i = 0; i < a.length; i++) {
90 if (i in a) {
91 if (!(i in b) || !(deepEquals(a[i], b[i])))
92 return false;
93 } else if (i in b) {
94 return false;
95 }
96 }
97 return true;
98 } else {
99 return deepObjectEquals(a, b);
100 }
101}
102
103
104function assertEquals(expected, found, name_opt) {
105 if (!deepEquals(found, expected)) {
106 fail(expected, found, name_opt);
107 }
108}
109
110
111function assertArrayEquals(expected, found, name_opt) {
112 var start = "";
113 if (name_opt) {
114 start = name_opt + " - ";
115 }
116 assertEquals(expected.length, found.length, start + "array length");
117 if (expected.length == found.length) {
118 for (var i = 0; i < expected.length; ++i) {
119 assertEquals(expected[i], found[i], start + "array element at index " + i);
120 }
121 }
122}
123
124
125function assertTrue(value, name_opt) {
126 assertEquals(true, value, name_opt);
127}
128
129
130function assertFalse(value, name_opt) {
131 assertEquals(false, value, name_opt);
132}
133
134
135function assertNaN(value, name_opt) {
136 if (!isNaN(value)) {
137 fail("NaN", value, name_opt);
138 }
139}
140
141
142function assertNull(value, name_opt) {
143 if (value !== null) {
144 fail("null", value, name_opt);
145 }
146}
147
148
149function assertNotNull(value, name_opt) {
150 if (value === null) {
151 fail("not null", value, name_opt);
152 }
153}
154
155
156function assertThrows(code, type_opt, cause_opt) {
157 var threwException = true;
158 try {
159 if (typeof code == 'function') {
160 code();
161 } else {
162 eval(code);
163 }
164 threwException = false;
165 } catch (e) {
166 if (typeof type_opt == 'function')
167 assertInstanceof(e, type_opt);
168 if (arguments.length >= 3)
169 assertEquals(e.type, cause_opt);
170 // Do nothing.
171 }
172 if (!threwException) assertTrue(false, "did not throw exception");
173}
174
175
176function assertInstanceof(obj, type) {
177 if (!(obj instanceof type)) {
178 assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + ">");
179 }
180}
181
182
183function assertDoesNotThrow(code) {
184 try {
185 if (typeof code == 'function') {
186 code();
187 } else {
188 eval(code);
189 }
190 } catch (e) {
191 assertTrue(false, "threw an exception: " + (e.message || e));
192 }
193}
194
195
196function assertUnreachable(name_opt) {
197 // Fix this when we ditch the old test runner.
198 var message = "Fail" + "ure: unreachable"
199 if (name_opt) {
200 message += " - " + name_opt;
201 }
202 throw new MjsUnitAssertionError(message);
203}