blob: b360425c6189d486f098223b7b6cc0cf52172472 [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;
Kristian Monsen25f61362010-05-21 11:50:48 +010030 // This allows fetching the stack trace using TryCatch::StackTrace.
31 this.stack = new Error("").stack;
Steve Blocka7e24c12009-10-30 11:49:00 +000032}
33
Steve Blocka7e24c12009-10-30 11:49:00 +000034/*
35 * This file is included in all mini jsunit test cases. The test
36 * framework expects lines that signal failed tests to start with
37 * the f-word and ignore all other lines.
38 */
39
Ben Murdoch257744e2011-11-30 15:57:28 +000040
41MjsUnitAssertionError.prototype.toString = function () {
42 return this.message;
43};
44
45
46// Expected and found values the same objects, or the same primitive
47// values.
48// For known primitive values, please use assertEquals.
49var assertSame;
50
51// Expected and found values are identical primitive values or functions
52// or similarly structured objects (checking internal properties
53// of, e.g., Number and Date objects, the elements of arrays
54// and the properties of non-Array objects).
55var assertEquals;
56
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057
58// The difference between expected and found value is within certain tolerance.
59var assertEqualsDelta;
60
Ben Murdoch257744e2011-11-30 15:57:28 +000061// The found object is an Array with the same length and elements
62// as the expected object. The expected object doesn't need to be an Array,
63// as long as it's "array-ish".
64var assertArrayEquals;
65
66// The found object must have the same enumerable properties as the
67// expected object. The type of object isn't checked.
68var assertPropertiesEqual;
69
70// Assert that the string conversion of the found value is equal to
71// the expected string. Only kept for backwards compatability, please
72// check the real structure of the found value.
73var assertToStringEquals;
74
75// Checks that the found value is true. Use with boolean expressions
76// for tests that doesn't have their own assertXXX function.
77var assertTrue;
78
79// Checks that the found value is false.
80var assertFalse;
81
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082// Checks that the found value is null. Kept for historical compatibility,
Ben Murdoch257744e2011-11-30 15:57:28 +000083// please just use assertEquals(null, expected).
84var assertNull;
85
86// Checks that the found value is *not* null.
87var assertNotNull;
88
89// Assert that the passed function or eval code throws an exception.
90// The optional second argument is an exception constructor that the
91// thrown exception is checked against with "instanceof".
92// The optional third argument is a message type string that is compared
93// to the type property on the thrown exception.
94var assertThrows;
95
96// Assert that the passed function or eval code does not throw an exception.
97var assertDoesNotThrow;
98
99// Asserts that the found value is an instance of the constructor passed
100// as the second argument.
101var assertInstanceof;
102
103// Assert that this code is never executed (i.e., always fails if executed).
104var assertUnreachable;
105
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106// Assert that the function code is (not) optimized. If "no sync" is passed
107// as second argument, we do not wait for the concurrent optimization thread to
108// finish when polling for optimization status.
109// Only works with --allow-natives-syntax.
110var assertOptimized;
111var assertUnoptimized;
112
113
Ben Murdoch257744e2011-11-30 15:57:28 +0000114(function () { // Scope for utility functions.
115
116 function classOf(object) {
117 // Argument must not be null or undefined.
118 var string = Object.prototype.toString.call(object);
119 // String has format [object <ClassName>].
120 return string.substring(8, string.length - 1);
121 }
122
123
124 function PrettyPrint(value) {
125 switch (typeof value) {
126 case "string":
127 return JSON.stringify(value);
128 case "number":
129 if (value === 0 && (1 / value) < 0) return "-0";
130 // FALLTHROUGH.
131 case "boolean":
132 case "undefined":
133 case "function":
134 return String(value);
135 case "object":
136 if (value === null) return "null";
137 var objectClass = classOf(value);
138 switch (objectClass) {
Steve Block44f0eee2011-05-26 01:26:41 +0100139 case "Number":
140 case "String":
141 case "Boolean":
142 case "Date":
Ben Murdoch257744e2011-11-30 15:57:28 +0000143 return objectClass + "(" + PrettyPrint(value.valueOf()) + ")";
Steve Block44f0eee2011-05-26 01:26:41 +0100144 case "RegExp":
145 return value.toString();
146 case "Array":
Ben Murdoch257744e2011-11-30 15:57:28 +0000147 return "[" + value.map(PrettyPrintArrayElement).join(",") + "]";
Steve Block44f0eee2011-05-26 01:26:41 +0100148 case "Object":
149 break;
150 default:
Ben Murdoch257744e2011-11-30 15:57:28 +0000151 return objectClass + "()";
152 }
153 // [[Class]] is "Object".
154 var name = value.constructor.name;
155 if (name) return name + "()";
156 return "Object()";
157 default:
158 return "-- unknown value --";
159 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 }
Steve Block44f0eee2011-05-26 01:26:41 +0100161
Ben Murdoch257744e2011-11-30 15:57:28 +0000162
163 function PrettyPrintArrayElement(value, index, array) {
164 if (value === undefined && !(index in array)) return "";
165 return PrettyPrint(value);
166 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000167
168
Ben Murdoch257744e2011-11-30 15:57:28 +0000169 function fail(expectedText, found, name_opt) {
170 var message = "Fail" + "ure";
171 if (name_opt) {
172 // Fix this when we ditch the old test runner.
173 message += " (" + name_opt + ")";
174 }
175
176 message += ": expected <" + expectedText +
177 "> found <" + PrettyPrint(found) + ">";
178 throw new MjsUnitAssertionError(message);
179 }
180
181
182 function deepObjectEquals(a, b) {
183 var aProps = Object.keys(a);
184 aProps.sort();
185 var bProps = Object.keys(b);
186 bProps.sort();
187 if (!deepEquals(aProps, bProps)) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 return false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000189 }
190 for (var i = 0; i < aProps.length; i++) {
191 if (!deepEquals(a[aProps[i]], b[aProps[i]])) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000192 return false;
193 }
194 }
195 return true;
Ben Murdoch257744e2011-11-30 15:57:28 +0000196 }
197
198
199 function deepEquals(a, b) {
200 if (a === b) {
201 // Check for -0.
202 if (a === 0) return (1 / a) === (1 / b);
203 return true;
204 }
205 if (typeof a != typeof b) return false;
206 if (typeof a == "number") return isNaN(a) && isNaN(b);
207 if (typeof a !== "object" && typeof a !== "function") return false;
208 // Neither a nor b is primitive.
209 var objectClass = classOf(a);
210 if (objectClass !== classOf(b)) return false;
211 if (objectClass === "RegExp") {
212 // For RegExp, just compare pattern and flags using its toString.
213 return (a.toString() === b.toString());
214 }
215 // Functions are only identical to themselves.
216 if (objectClass === "Function") return false;
217 if (objectClass === "Array") {
218 var elementCount = 0;
219 if (a.length != b.length) {
220 return false;
221 }
222 for (var i = 0; i < a.length; i++) {
223 if (!deepEquals(a[i], b[i])) return false;
224 }
225 return true;
226 }
227 if (objectClass == "String" || objectClass == "Number" ||
228 objectClass == "Boolean" || objectClass == "Date") {
229 if (a.valueOf() !== b.valueOf()) return false;
230 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 return deepObjectEquals(a, b);
232 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000233
Ben Murdoch257744e2011-11-30 15:57:28 +0000234 assertSame = function assertSame(expected, found, name_opt) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100235 // TODO(mstarzinger): We should think about using Harmony's egal operator
236 // or the function equivalent Object.is() here.
Ben Murdoch257744e2011-11-30 15:57:28 +0000237 if (found === expected) {
238 if (expected !== 0 || (1 / expected) == (1 / found)) return;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239 } else if ((expected !== expected) && (found !== found)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000240 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000241 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000242 fail(PrettyPrint(expected), found, name_opt);
243 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000244
245
Ben Murdoch257744e2011-11-30 15:57:28 +0000246 assertEquals = function assertEquals(expected, found, name_opt) {
247 if (!deepEquals(found, expected)) {
248 fail(PrettyPrint(expected), found, name_opt);
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000250 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
252
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 assertEqualsDelta =
254 function assertEqualsDelta(expected, found, delta, name_opt) {
255 assertTrue(Math.abs(expected - found) <= delta, name_opt);
256 };
257
258
Ben Murdoch257744e2011-11-30 15:57:28 +0000259 assertArrayEquals = function assertArrayEquals(expected, found, name_opt) {
260 var start = "";
261 if (name_opt) {
262 start = name_opt + " - ";
Steve Blocka7e24c12009-10-30 11:49:00 +0000263 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000264 assertEquals(expected.length, found.length, start + "array length");
265 if (expected.length == found.length) {
266 for (var i = 0; i < expected.length; ++i) {
267 assertEquals(expected[i], found[i],
268 start + "array element at index " + i);
269 }
270 }
271 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000272
273
Ben Murdoch257744e2011-11-30 15:57:28 +0000274 assertPropertiesEqual = function assertPropertiesEqual(expected, found,
275 name_opt) {
276 // Check properties only.
277 if (!deepObjectEquals(expected, found)) {
278 fail(expected, found, name_opt);
279 }
280 };
281
282
283 assertToStringEquals = function assertToStringEquals(expected, found,
284 name_opt) {
285 if (expected != String(found)) {
286 fail(expected, found, name_opt);
287 }
288 };
289
290
291 assertTrue = function assertTrue(value, name_opt) {
292 assertEquals(true, value, name_opt);
293 };
294
295
296 assertFalse = function assertFalse(value, name_opt) {
297 assertEquals(false, value, name_opt);
298 };
299
300
301 assertNull = function assertNull(value, name_opt) {
302 if (value !== null) {
303 fail("null", value, name_opt);
304 }
305 };
306
307
308 assertNotNull = function assertNotNull(value, name_opt) {
309 if (value === null) {
310 fail("not null", value, name_opt);
311 }
312 };
313
314
315 assertThrows = function assertThrows(code, type_opt, cause_opt) {
316 var threwException = true;
317 try {
318 if (typeof code == 'function') {
319 code();
320 } else {
321 eval(code);
322 }
323 threwException = false;
324 } catch (e) {
325 if (typeof type_opt == 'function') {
326 assertInstanceof(e, type_opt);
327 }
328 if (arguments.length >= 3) {
329 assertEquals(e.type, cause_opt);
330 }
331 // Success.
332 return;
333 }
334 throw new MjsUnitAssertionError("Did not throw exception");
335 };
336
337
338 assertInstanceof = function assertInstanceof(obj, type) {
339 if (!(obj instanceof type)) {
340 var actualTypeName = null;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 var actualConstructor = Object.getPrototypeOf(obj).constructor;
Ben Murdoch257744e2011-11-30 15:57:28 +0000342 if (typeof actualConstructor == "function") {
343 actualTypeName = actualConstructor.name || String(actualConstructor);
344 }
345 fail("Object <" + PrettyPrint(obj) + "> is not an instance of <" +
346 (type.name || type) + ">" +
347 (actualTypeName ? " but of < " + actualTypeName + ">" : ""));
348 }
349 };
350
351
352 assertDoesNotThrow = function assertDoesNotThrow(code, name_opt) {
353 try {
354 if (typeof code == 'function') {
355 code();
356 } else {
357 eval(code);
358 }
359 } catch (e) {
360 fail("threw an exception: ", e.message || e, name_opt);
361 }
362 };
363
364 assertUnreachable = function assertUnreachable(name_opt) {
365 // Fix this when we ditch the old test runner.
366 var message = "Fail" + "ure: unreachable";
367 if (name_opt) {
368 message += " - " + name_opt;
369 }
370 throw new MjsUnitAssertionError(message);
371 };
372
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000373 var OptimizationStatusImpl = undefined;
Ben Murdoch257744e2011-11-30 15:57:28 +0000374
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375 var OptimizationStatus = function(fun, sync_opt) {
376 if (OptimizationStatusImpl === undefined) {
377 try {
378 OptimizationStatusImpl = new Function(
379 "fun", "sync", "return %GetOptimizationStatus(fun, sync);");
380 } catch (e) {
381 throw new Error("natives syntax not allowed");
382 }
383 }
384 return OptimizationStatusImpl(fun, sync_opt);
385 }
386
387 assertUnoptimized = function assertUnoptimized(fun, sync_opt, name_opt) {
388 if (sync_opt === undefined) sync_opt = "";
389 assertTrue(OptimizationStatus(fun, sync_opt) != 1, name_opt);
390 }
391
392 assertOptimized = function assertOptimized(fun, sync_opt, name_opt) {
393 if (sync_opt === undefined) sync_opt = "";
394 assertTrue(OptimizationStatus(fun, sync_opt) != 2, name_opt);
395 }
396
397})();