forked from WebKit/WebKit-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.js
186 lines (163 loc) · 6.38 KB
/
assert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (C) 2016-2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
const _fail = (msg, extra) => {
throw new Error(msg + (extra ? ": " + extra : ""));
};
export const isNotA = (v, t, msg) => {
if (typeof v === t)
_fail(`Shouldn't be ${t}`, msg);
};
export const isA = (v, t, msg) => {
if (typeof v !== t)
_fail(`Should be ${t}, got ${typeof(v)}`, msg);
};
export const isNotUndef = (v, msg) => isNotA(v, "undefined", msg);
export const isUndef = (v, msg) => isA(v, "undefined", msg);
export const notObject = (v, msg) => isNotA(v, "object", msg);
export const isObject = (v, msg) => isA(v, "object", msg);
export const notString = (v, msg) => isNotA(v, "string", msg);
export const isString = (v, msg) => isA(v, "string", msg);
export const notNumber = (v, msg) => isNotA(v, "number", msg);
export const isNumber = (v, msg) => isA(v, "number", msg);
export const notFunction = (v, msg) => isNotA(v, "function", msg);
export const isFunction = (v, msg) => isA(v, "function", msg);
export const hasObjectProperty = (o, p, msg) => {
isObject(o, msg);
isNotUndef(o[p], msg, `expected object to have property ${p}`);
};
export const isArray = (v, msg) => {
if (!Array.isArray(v))
_fail(`Expected an array, got ${typeof(v)}`, msg);
};
export const isNotArray = (v, msg) => {
if (Array.isArray(v))
_fail(`Expected to not be an array`, msg);
};
export const truthy = (v, msg) => {
if (!v)
_fail(`Expected truthy`, msg);
};
export const falsy = (v, msg) => {
if (v)
_fail(`Expected falsy`, msg);
};
export const eq = (lhs, rhs, msg) => {
if (typeof lhs !== typeof rhs)
_fail(`Not the same: "${lhs}" and "${rhs}"`, msg);
if (Array.isArray(lhs) && Array.isArray(rhs) && (lhs.length === rhs.length)) {
for (let i = 0; i !== lhs.length; ++i)
eq(lhs[i], rhs[i], msg);
} else if (lhs !== rhs) {
if (typeof lhs === "number" && isNaN(lhs) && isNaN(rhs))
return;
_fail(`Not the same: "${lhs}" and "${rhs}"`, msg);
} else {
if (typeof lhs === "number" && (1.0 / lhs !== 1.0 / rhs)) // Distinguish -0.0 from 0.0.
_fail(`Not the same: "${lhs}" and "${rhs}"`, msg);
}
};
export const matches = (lhs, rhs, msg) => {
if (typeof lhs !== "string" || !(rhs instanceof RegExp))
_fail(`Expected string and regex object, got ${typeof lhs} and ${typeof rhs}`, msg);
if (!rhs.test(lhs))
_fail(`"${msg}" does not match ${rhs}`, msg);
};
const canonicalizeI32 = (number) => {
if (Math.round(number) === number && number >= 2 ** 31)
number = number - 2 ** 32;
return number;
}
export const eqI32 = (lhs, rhs, msg) => {
return eq(canonicalizeI32(lhs), canonicalizeI32(rhs), msg);
};
export const ge = (lhs, rhs, msg) => {
isNotUndef(lhs);
isNotUndef(rhs);
if (!(lhs >= rhs))
_fail(`Expected: "${lhs}" < "${rhs}"`, msg);
};
export const le = (lhs, rhs, msg) => {
isNotUndef(lhs);
isNotUndef(rhs);
if (!(lhs <= rhs))
_fail(`Expected: "${lhs}" > "${rhs}"`, msg);
};
const _throws = (func, type, message, ...args) => {
try {
func(...args);
} catch (e) {
if (e instanceof type && e.message.indexOf(message) >= 0)
return e;
_fail(`Expected to throw a ${type.name} with message "${message}", got ${e.name} with message "${e.message}"`);
}
_fail(`Expected to throw a ${type.name} with message "${message}"`);
};
export async function throwsAsync(promise, type, message) {
try {
await promise;
} catch (e) {
if (e instanceof type) {
if (e.message === message)
return e;
// Ignore source information at the end of the error message if the
// expected message didn't specify that information. Sometimes it
// changes, or it's tricky to get just right.
const evaluatingIndex = e.message.indexOf(" (evaluating '");
if (evaluatingIndex !== -1) {
const cleanMessage = e.message.substring(0, evaluatingIndex);
if (cleanMessage === message)
return e;
}
}
_fail(`Expected to throw a ${type.name} with message "${message}", got ${e.name} with message "${e.message}"`);
}
_fail(`Expected to throw a ${type.name} with message "${message}"`);
}
const _instanceof = (obj, type, msg) => {
if (!(obj instanceof type))
_fail(`Expected a ${typeof(type)}, got ${typeof obj}`);
};
// Use underscore names to avoid clashing with builtin names.
export {
_throws as throws,
_instanceof as instanceof,
};
const asyncTestImpl = (promise, thenFunc, catchFunc) => {
asyncTestStart(1);
promise.then(thenFunc).catch(catchFunc);
};
const printExn = (e) => {
print("Failed: ", e);
print(e.stack);
};
export const asyncTest = (promise) => asyncTestImpl(promise, asyncTestPassed, printExn);
export const asyncTestEq = (promise, expected) => {
const thenCheck = (value) => {
if (value === expected)
return asyncTestPassed();
print("Failed: got ", value, " but expected ", expected);
}
asyncTestImpl(promise, thenCheck, printExn);
};