blob: e17615267ae327ea25b60db0a67e99d11fbb6a49 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 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
28// Some methods are taken from v8/test/mjsunit/mjsunit.js
29
30/**
31 * Compares two objects for key/value equality.
32 * Returns true if they are equal, false otherwise.
33 */
34function deepObjectEquals(a, b) {
35 var aProps = Object.keys(a);
36 aProps.sort();
37 var bProps = Object.keys(b);
38 bProps.sort();
39 if (!deepEquals(aProps, bProps)) {
40 return false;
41 }
42 for (var i = 0; i < aProps.length; i++) {
43 if (!deepEquals(a[aProps[i]], b[aProps[i]])) {
44 return false;
45 }
46 }
47 return true;
48}
49
50
51/**
52 * Compares two JavaScript values for type and value equality.
53 * It checks internals of arrays and objects.
54 */
55function deepEquals(a, b) {
56 if (a === b) {
57 // Check for -0.
58 if (a === 0) return (1 / a) === (1 / b);
59 return true;
60 }
61 if (typeof a != typeof b) return false;
62 if (typeof a == 'number') return isNaN(a) && isNaN(b);
63 if (typeof a !== 'object' && typeof a !== 'function') return false;
64 // Neither a nor b is primitive.
65 var objectClass = classOf(a);
66 if (objectClass !== classOf(b)) return false;
67 if (objectClass === 'RegExp') {
68 // For RegExp, just compare pattern and flags using its toString.
69 return (a.toString() === b.toString());
70 }
71 // Functions are only identical to themselves.
72 if (objectClass === 'Function') return false;
73 if (objectClass === 'Array') {
74 var elementCount = 0;
75 if (a.length != b.length) {
76 return false;
77 }
78 for (var i = 0; i < a.length; i++) {
79 if (!deepEquals(a[i], b[i])) return false;
80 }
81 return true;
82 }
83 if (objectClass == 'String' || objectClass == 'Number' ||
84 objectClass == 'Boolean' || objectClass == 'Date') {
85 if (a.valueOf() !== b.valueOf()) return false;
86 }
87 return deepObjectEquals(a, b);
88}
89
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090/**
Ben Murdochc5610432016-08-08 18:44:38 +010091 * Throws an exception containing the user_message (if any) and the values.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 */
Ben Murdochc5610432016-08-08 18:44:38 +010093function fail(expected, found, user_message = '') {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 // TODO(cira): Replace String with PrettyPrint for objects and arrays.
Ben Murdochc5610432016-08-08 18:44:38 +010095 var message = 'Failure' + (user_message ? ' (' + user_message + ')' : '') +
96 ': expected <' + String(expected) + '>, found <' + String(found) + '>.';
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 throw new Error(message);
98}
99
100
101/**
102 * Throws if two variables have different types or values.
103 */
Ben Murdochc5610432016-08-08 18:44:38 +0100104function assertEquals(expected, found, user_message = '') {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 if (!deepEquals(expected, found)) {
Ben Murdochc5610432016-08-08 18:44:38 +0100106 fail(expected, found, user_message);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 }
108}
109
110
111/**
112 * Throws if value is false.
113 */
Ben Murdochc5610432016-08-08 18:44:38 +0100114function assertTrue(value, user_message = '') {
115 assertEquals(true, value, user_message);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116}
117
118
119/**
120 * Throws if value is true.
121 */
Ben Murdochc5610432016-08-08 18:44:38 +0100122function assertFalse(value, user_message = '') {
123 assertEquals(false, value, user_message);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124}
125
126
127/**
Ben Murdochc5610432016-08-08 18:44:38 +0100128 * Runs code() and asserts that it throws the specified exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 */
130function assertThrows(code, type_opt, cause_opt) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 try {
132 if (typeof code == 'function') {
133 code();
134 } else {
135 eval(code);
136 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 } catch (e) {
138 if (typeof type_opt == 'function') {
139 assertInstanceof(e, type_opt);
140 }
141 if (arguments.length >= 3) {
Ben Murdochc5610432016-08-08 18:44:38 +0100142 assertEquals(cause_opt, e.type, 'thrown exception type mismatch');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000143 }
144 // Success.
145 return;
146 }
Ben Murdochc5610432016-08-08 18:44:38 +0100147 var expected = arguments.length >= 3 ? cause_opt :
148 typeof type_opt == 'function' ? type_opt : 'any exception';
149 fail(expected, 'no exception', 'expected thrown exception');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150}
151
152
153/**
Ben Murdochc5610432016-08-08 18:44:38 +0100154 * Runs code() and asserts that it does now throw any exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 */
Ben Murdochc5610432016-08-08 18:44:38 +0100156function assertDoesNotThrow(code, user_message = '') {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 try {
158 if (typeof code == 'function') {
159 code();
160 } else {
161 eval(code);
162 }
163 } catch (e) {
Ben Murdochc5610432016-08-08 18:44:38 +0100164 fail("no expection", "exception: " + String(e), user_message);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 }
166}
167
168
169/**
170 * Throws if obj is not of given type.
171 */
172function assertInstanceof(obj, type) {
173 if (!(obj instanceof type)) {
174 var actualTypeName = null;
175 var actualConstructor = Object.prototypeOf(obj).constructor;
176 if (typeof actualConstructor == "function") {
177 actualTypeName = actualConstructor.name || String(actualConstructor);
178 }
179 throw new Error('Object <' + obj + '> is not an instance of <' +
180 (type.name || type) + '>' +
181 (actualTypeName ? ' but of < ' + actualTypeName + '>' : ''));
182 }
183}