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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
module.metadata = {
"stability": "unstable"
};
const { isFunction, isNull, isObject, isString,
isRegExp, isArray, isDate, isPrimitive,
isUndefined, instanceOf, source } = require("../lang/type");
/**
* The `AssertionError` is defined in assert.
* @extends Error
* @example
* new assert.AssertionError({
* message: message,
* actual: actual,
* expected: expected
* })
*/
function AssertionError(options) {
let assertionError = Object.create(AssertionError.prototype);
if (isString(options))
options = { message: options };
if ("actual" in options)
assertionError.actual = options.actual;
if ("expected" in options)
assertionError.expected = options.expected;
if ("operator" in options)
assertionError.operator = options.operator;
assertionError.message = options.message;
assertionError.stack = new Error().stack;
return assertionError;
}
AssertionError.prototype = Object.create(Error.prototype, {
constructor: { value: AssertionError },
name: { value: "AssertionError", enumerable: true },
toString: { value: function toString() {
let value;
if (this.message) {
value = this.name + " : " + this.message;
}
else {
value = [
this.name + " : ",
source(this.expected),
this.operator,
source(this.actual)
].join(" ");
}
return value;
}}
});
exports.AssertionError = AssertionError;
function Assert(logger) {
let assert = Object.create(Assert.prototype, { _log: { value: logger }});
assert.fail = assert.fail.bind(assert);
assert.pass = assert.pass.bind(assert);
return assert;
}
Assert.prototype = {
fail: function fail(e) {
if (!e || typeof(e) !== 'object') {
this._log.fail(e);
return;
}
let message = e.message;
try {
if ('operator' in e) {
message += [
" -",
source(e.actual),
e.operator,
source(e.expected)
].join(" ");
}
}
catch(e) {}
this._log.fail(message);
},
pass: function pass(message) {
this._log.pass(message);
return true;
},
error: function error(e) {
this._log.exception(e);
},
ok: function ok(value, message) {
if (!!!value) {
this.fail({
actual: value,
expected: true,
message: message,
operator: "=="
});
return false;
}
this.pass(message);
return true;
},
/**
* The equality assertion tests shallow, coercive equality with `==`.
* @example
* assert.equal(1, 1, "one is one");
*/
equal: function equal(actual, expected, message) {
if (actual == expected) {
this.pass(message);
return true;
}
this.fail({
actual: actual,
expected: expected,
message: message,
operator: "=="
});
return false;
},
/**
* The non-equality assertion tests for whether two objects are not equal
* with `!=`.
* @example
* assert.notEqual(1, 2, "one is not two");
*/
notEqual: function notEqual(actual, expected, message) {
if (actual != expected) {
this.pass(message);
return true;
}
this.fail({
actual: actual,
expected: expected,
message: message,
operator: "!=",
});
return false;
},
/**
* The equivalence assertion tests a deep (with `===`) equality relation.
* @example
* assert.deepEqual({ a: "foo" }, { a: "foo" }, "equivalent objects")
*/
deepEqual: function deepEqual(actual, expected, message) {
if (isDeepEqual(actual, expected)) {
this.pass(message);
return true;
}
this.fail({
actual: actual,
expected: expected,
message: message,
operator: "deepEqual"
});
return false;
},
/**
* The non-equivalence assertion tests for any deep (with `===`) inequality.
* @example
* assert.notDeepEqual({ a: "foo" }, Object.create({ a: "foo" }),
* "object's inherit from different prototypes");
*/
notDeepEqual: function notDeepEqual(actual, expected, message) {
if (!isDeepEqual(actual, expected)) {
this.pass(message);
return true;
}
this.fail({
actual: actual,
expected: expected,
message: message,
operator: "notDeepEqual"
});
return false;
},
/**
* The strict equality assertion tests strict equality, as determined by
* `===`.
* @example
* assert.strictEqual(null, null, "`null` is `null`")
*/
strictEqual: function strictEqual(actual, expected, message) {
if (actual === expected) {
this.pass(message);
return true;
}
this.fail({
actual: actual,
expected: expected,
message: message,
operator: "==="
});
return false;
},
/**
* The strict non-equality assertion tests for strict inequality, as
* determined by `!==`.
* @example
* assert.notStrictEqual(null, undefined, "`null` is not `undefined`");
*/
notStrictEqual: function notStrictEqual(actual, expected, message) {
if (actual !== expected) {
this.pass(message);
return true;
}
this.fail({
actual: actual,
expected: expected,
message: message,
operator: "!=="
});
return false;
},
/**
* The assertion whether or not given `block` throws an exception. If optional
* `Error` argument is provided and it's type of function thrown error is
* asserted to be an instance of it, if type of `Error` is string then message
* of throw exception is asserted to contain it.
* @param {Function} block
* Function that is expected to throw.
* @param {Error|RegExp} [Error]
* Error constructor that is expected to be thrown or a string that
* must be contained by a message of the thrown exception, or a RegExp
* matching a message of the thrown exception.
* @param {String} message
* Description message
*
* @examples
*
* assert.throws(function block() {
* doSomething(4)
* }, "Object is expected", "Incorrect argument is passed");
*
* assert.throws(function block() {
* Object.create(5)
* }, TypeError, "TypeError is thrown");
*/
throws: function throws(block, Error, message) {
let threw = false;
let exception = null;
// If third argument is not provided and second argument is a string it
// means that optional `Error` argument was not passed, so we shift
// arguments.
if (isString(Error) && isUndefined(message)) {
message = Error;
Error = undefined;
}
// Executing given `block`.
try {
block();
}
catch (e) {
threw = true;
exception = e;
}
// If exception was thrown and `Error` argument was not passed assert is
// passed.
if (threw && (isUndefined(Error) ||
// If passed `Error` is RegExp using it's test method to
// assert thrown exception message.
(isRegExp(Error) && (Error.test(exception.message) || Error.test(exception.toString()))) ||
// If passed `Error` is a constructor function testing if
// thrown exception is an instance of it.
(isFunction(Error) && instanceOf(exception, Error))))
{
this.pass(message);
return true;
}
// Otherwise we report assertion failure.
let failure = {
message: message,
operator: "matches"
};
if (exception) {
failure.actual = exception.message || exception.toString();
}
if (Error) {
failure.expected = Error.toString();
}
this.fail(failure);
return false;
}
};
exports.Assert = Assert;
function isDeepEqual(actual, expected) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
}
// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
else if (isDate(actual) && isDate(expected)) {
return actual.getTime() === expected.getTime();
}
// XXX specification bug: this should be specified
else if (isPrimitive(actual) || isPrimitive(expected)) {
return expected === actual;
}
// 7.3. Other pairs that do not both pass typeof value == "object",
// equivalence is determined by ==.
else if (!isObject(actual) && !isObject(expected)) {
return actual == expected;
}
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
else {
return actual.prototype === expected.prototype &&
isEquivalent(actual, expected);
}
}
function isEquivalent(a, b, stack) {
let aKeys = Object.keys(a);
let bKeys = Object.keys(b);
return aKeys.length === bKeys.length &&
isArrayEquivalent(aKeys.sort(), bKeys.sort()) &&
aKeys.every(function(key) {
return isDeepEqual(a[key], b[key], stack)
});
}
function isArrayEquivalent(a, b, stack) {
return isArray(a) && isArray(b) &&
a.every(function(value, index) {
return isDeepEqual(value, b[index]);
});
}
|