forked from web-platform-tests/wpt
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassert.js
More file actions
183 lines (155 loc) · 4.75 KB
/
Copy pathassert.js
File metadata and controls
183 lines (155 loc) · 4.75 KB
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
// Copyright (C) 2017 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: |
Collection of assertion functions used throughout test262
defines:
- assert
- formatIdentityFreeValue
- formatSimpleValue
- isNegativeZero
- isPrimitive
---*/
function isNegativeZero(value) {
return value === 0 && 1 / value === -Infinity;
}
function isPrimitive(value) {
return !value || (typeof value !== 'object' && typeof value !== 'function');
}
function formatIdentityFreeValue(value) {
switch (value === null ? 'null' : typeof value) {
case 'string':
return typeof JSON !== "undefined" ? JSON.stringify(value) : '"' + value + '"';
case 'bigint':
return String(value) + "n";
case 'number':
if (isNegativeZero(value)) return '-0';
// falls through
case 'boolean':
case 'undefined':
case 'null':
return String(value);
}
}
function formatSimpleValue(value) {
var basic = formatIdentityFreeValue(value);
if (basic) return basic;
try {
return String(value);
} catch (err) {
if (err.name === 'TypeError') {
return Object.prototype.toString.call(value);
}
throw err;
}
}
function assert(mustBeTrue, message) {
if (mustBeTrue === true) {
return;
}
if (message === undefined) {
message = 'Expected true but got ' + assert._toString(mustBeTrue);
}
throw new Test262Error(message);
}
assert._isSameValue = function (a, b) {
if (a === b) {
// Handle +/-0 vs. -/+0
return a !== 0 || 1 / a === 1 / b;
}
// Handle NaN vs. NaN
return a !== a && b !== b;
};
assert.sameValue = function (actual, expected, message) {
try {
if (assert._isSameValue(actual, expected)) {
return;
}
} catch (error) {
throw new Test262Error(message + ' (_isSameValue operation threw) ' + error);
return;
}
if (message === undefined) {
message = '';
} else {
message += ' ';
}
message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(expected) + '») to be true';
throw new Test262Error(message);
};
assert.notSameValue = function (actual, unexpected, message) {
if (!assert._isSameValue(actual, unexpected)) {
return;
}
if (message === undefined) {
message = '';
} else {
message += ' ';
}
message += 'Expected SameValue(«' + assert._toString(actual) + '», «' + assert._toString(unexpected) + '») to be false';
throw new Test262Error(message);
};
assert.throws = function (expectedErrorConstructor, func, message) {
var expectedName, actualName;
if (typeof func !== "function") {
throw new Test262Error('assert.throws requires two arguments: the error constructor ' +
'and a function to run');
return;
}
if (message === undefined) {
message = '';
} else {
message += ' ';
}
try {
func();
} catch (thrown) {
if (typeof thrown !== 'object' || thrown === null) {
message += 'Thrown value was not an object!';
throw new Test262Error(message);
} else if (thrown.constructor !== expectedErrorConstructor) {
expectedName = expectedErrorConstructor.name;
actualName = thrown.constructor.name;
if (expectedName === actualName) {
message += 'Expected a ' + expectedName + ' but got a different error constructor with the same name';
} else {
message += 'Expected a ' + expectedName + ' but got a ' + actualName;
}
throw new Test262Error(message);
}
return;
}
message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
throw new Test262Error(message);
};
assert.compareArray = function (actual, expected, message) {
message = message === undefined ? '' : message;
if (typeof message === 'symbol') {
message = message.toString();
}
if (isPrimitive(actual)) {
assert(false, "Actual argument [" + actual + "] shouldn't be primitive. " + String(message));
} else if (isPrimitive(expected)) {
assert(false, "Expected argument [" + expected + "] shouldn't be primitive. " + String(message));
}
var result = compareArray(actual, expected);
if (result) return;
var format = compareArray.format;
assert(false, "Actual " + format(actual) + " and expected " + format(expected) + " should have the same contents. " + String(message));
};
function compareArray(a, b) {
if (b.length !== a.length) {
return false;
}
for (var i = 0; i < a.length; i++) {
if (!assert._isSameValue(b[i], a[i])) {
return false;
}
}
return true;
}
compareArray.format = function (arrayLike) {
return "[" + Array.prototype.map.call(arrayLike, String).join(", ") + "]";
};
assert._formatIdentityFreeValue = formatIdentityFreeValue;
assert._toString = formatSimpleValue;