Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/assert/build/assert.js
1126 views
1
// Currently in sync with Node.js lib/assert.js
2
// https://github.com/nodejs/node/commit/2a51ae424a513ec9a6aa3466baa0cc1d55dd4f3b
3
// Originally from narwhal.js (http://narwhaljs.org)
4
// Copyright (c) 2009 Thomas Robinson <280north.com>
5
//
6
// Permission is hereby granted, free of charge, to any person obtaining a copy
7
// of this software and associated documentation files (the 'Software'), to
8
// deal in the Software without restriction, including without limitation the
9
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
// sell copies of the Software, and to permit persons to whom the Software is
11
// furnished to do so, subject to the following conditions:
12
//
13
// The above copyright notice and this permission notice shall be included in
14
// all copies or substantial portions of the Software.
15
//
16
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
'use strict';
23
24
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
25
26
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27
28
var _require = require('./internal/errors'),
29
_require$codes = _require.codes,
30
ERR_AMBIGUOUS_ARGUMENT = _require$codes.ERR_AMBIGUOUS_ARGUMENT,
31
ERR_INVALID_ARG_TYPE = _require$codes.ERR_INVALID_ARG_TYPE,
32
ERR_INVALID_ARG_VALUE = _require$codes.ERR_INVALID_ARG_VALUE,
33
ERR_INVALID_RETURN_VALUE = _require$codes.ERR_INVALID_RETURN_VALUE,
34
ERR_MISSING_ARGS = _require$codes.ERR_MISSING_ARGS;
35
36
var AssertionError = require('./internal/assert/assertion_error');
37
38
var _require2 = require('util/'),
39
inspect = _require2.inspect;
40
41
var _require$types = require('util/').types,
42
isPromise = _require$types.isPromise,
43
isRegExp = _require$types.isRegExp;
44
45
var objectAssign = Object.assign ? Object.assign : require('es6-object-assign').assign;
46
var objectIs = Object.is ? Object.is : require('object-is');
47
var errorCache = new Map();
48
var isDeepEqual;
49
var isDeepStrictEqual;
50
var parseExpressionAt;
51
var findNodeAround;
52
var decoder;
53
54
function lazyLoadComparison() {
55
var comparison = require('./internal/util/comparisons');
56
57
isDeepEqual = comparison.isDeepEqual;
58
isDeepStrictEqual = comparison.isDeepStrictEqual;
59
} // Escape control characters but not \n and \t to keep the line breaks and
60
// indentation intact.
61
// eslint-disable-next-line no-control-regex
62
63
64
var escapeSequencesRegExp = /[\x00-\x08\x0b\x0c\x0e-\x1f]/g;
65
var meta = ["\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007", '\\b', '', '', "\\u000b", '\\f', '', "\\u000e", "\\u000f", "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018", "\\u0019", "\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f"];
66
67
var escapeFn = function escapeFn(str) {
68
return meta[str.charCodeAt(0)];
69
};
70
71
var warned = false; // The assert module provides functions that throw
72
// AssertionError's when particular conditions are not met. The
73
// assert module must conform to the following interface.
74
75
var assert = module.exports = ok;
76
var NO_EXCEPTION_SENTINEL = {}; // All of the following functions must throw an AssertionError
77
// when a corresponding condition is not met, with a message that
78
// may be undefined if not provided. All assertion methods provide
79
// both the actual and expected values to the assertion error for
80
// display purposes.
81
82
function innerFail(obj) {
83
if (obj.message instanceof Error) throw obj.message;
84
throw new AssertionError(obj);
85
}
86
87
function fail(actual, expected, message, operator, stackStartFn) {
88
var argsLen = arguments.length;
89
var internalMessage;
90
91
if (argsLen === 0) {
92
internalMessage = 'Failed';
93
} else if (argsLen === 1) {
94
message = actual;
95
actual = undefined;
96
} else {
97
if (warned === false) {
98
warned = true;
99
var warn = process.emitWarning ? process.emitWarning : console.warn.bind(console);
100
warn('assert.fail() with more than one argument is deprecated. ' + 'Please use assert.strictEqual() instead or only pass a message.', 'DeprecationWarning', 'DEP0094');
101
}
102
103
if (argsLen === 2) operator = '!=';
104
}
105
106
if (message instanceof Error) throw message;
107
var errArgs = {
108
actual: actual,
109
expected: expected,
110
operator: operator === undefined ? 'fail' : operator,
111
stackStartFn: stackStartFn || fail
112
};
113
114
if (message !== undefined) {
115
errArgs.message = message;
116
}
117
118
var err = new AssertionError(errArgs);
119
120
if (internalMessage) {
121
err.message = internalMessage;
122
err.generatedMessage = true;
123
}
124
125
throw err;
126
}
127
128
assert.fail = fail; // The AssertionError is defined in internal/error.
129
130
assert.AssertionError = AssertionError;
131
132
function innerOk(fn, argLen, value, message) {
133
if (!value) {
134
var generatedMessage = false;
135
136
if (argLen === 0) {
137
generatedMessage = true;
138
message = 'No value argument passed to `assert.ok()`';
139
} else if (message instanceof Error) {
140
throw message;
141
}
142
143
var err = new AssertionError({
144
actual: value,
145
expected: true,
146
message: message,
147
operator: '==',
148
stackStartFn: fn
149
});
150
err.generatedMessage = generatedMessage;
151
throw err;
152
}
153
} // Pure assertion tests whether a value is truthy, as determined
154
// by !!value.
155
156
157
function ok() {
158
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
159
args[_key] = arguments[_key];
160
}
161
162
innerOk.apply(void 0, [ok, args.length].concat(args));
163
}
164
165
assert.ok = ok; // The equality assertion tests shallow, coercive equality with ==.
166
167
/* eslint-disable no-restricted-properties */
168
169
assert.equal = function equal(actual, expected, message) {
170
if (arguments.length < 2) {
171
throw new ERR_MISSING_ARGS('actual', 'expected');
172
} // eslint-disable-next-line eqeqeq
173
174
175
if (actual != expected) {
176
innerFail({
177
actual: actual,
178
expected: expected,
179
message: message,
180
operator: '==',
181
stackStartFn: equal
182
});
183
}
184
}; // The non-equality assertion tests for whether two objects are not
185
// equal with !=.
186
187
188
assert.notEqual = function notEqual(actual, expected, message) {
189
if (arguments.length < 2) {
190
throw new ERR_MISSING_ARGS('actual', 'expected');
191
} // eslint-disable-next-line eqeqeq
192
193
194
if (actual == expected) {
195
innerFail({
196
actual: actual,
197
expected: expected,
198
message: message,
199
operator: '!=',
200
stackStartFn: notEqual
201
});
202
}
203
}; // The equivalence assertion tests a deep equality relation.
204
205
206
assert.deepEqual = function deepEqual(actual, expected, message) {
207
if (arguments.length < 2) {
208
throw new ERR_MISSING_ARGS('actual', 'expected');
209
}
210
211
if (isDeepEqual === undefined) lazyLoadComparison();
212
213
if (!isDeepEqual(actual, expected)) {
214
innerFail({
215
actual: actual,
216
expected: expected,
217
message: message,
218
operator: 'deepEqual',
219
stackStartFn: deepEqual
220
});
221
}
222
}; // The non-equivalence assertion tests for any deep inequality.
223
224
225
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
226
if (arguments.length < 2) {
227
throw new ERR_MISSING_ARGS('actual', 'expected');
228
}
229
230
if (isDeepEqual === undefined) lazyLoadComparison();
231
232
if (isDeepEqual(actual, expected)) {
233
innerFail({
234
actual: actual,
235
expected: expected,
236
message: message,
237
operator: 'notDeepEqual',
238
stackStartFn: notDeepEqual
239
});
240
}
241
};
242
/* eslint-enable */
243
244
245
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
246
if (arguments.length < 2) {
247
throw new ERR_MISSING_ARGS('actual', 'expected');
248
}
249
250
if (isDeepEqual === undefined) lazyLoadComparison();
251
252
if (!isDeepStrictEqual(actual, expected)) {
253
innerFail({
254
actual: actual,
255
expected: expected,
256
message: message,
257
operator: 'deepStrictEqual',
258
stackStartFn: deepStrictEqual
259
});
260
}
261
};
262
263
assert.notDeepStrictEqual = notDeepStrictEqual;
264
265
function notDeepStrictEqual(actual, expected, message) {
266
if (arguments.length < 2) {
267
throw new ERR_MISSING_ARGS('actual', 'expected');
268
}
269
270
if (isDeepEqual === undefined) lazyLoadComparison();
271
272
if (isDeepStrictEqual(actual, expected)) {
273
innerFail({
274
actual: actual,
275
expected: expected,
276
message: message,
277
operator: 'notDeepStrictEqual',
278
stackStartFn: notDeepStrictEqual
279
});
280
}
281
}
282
283
assert.strictEqual = function strictEqual(actual, expected, message) {
284
if (arguments.length < 2) {
285
throw new ERR_MISSING_ARGS('actual', 'expected');
286
}
287
288
if (!objectIs(actual, expected)) {
289
innerFail({
290
actual: actual,
291
expected: expected,
292
message: message,
293
operator: 'strictEqual',
294
stackStartFn: strictEqual
295
});
296
}
297
};
298
299
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
300
if (arguments.length < 2) {
301
throw new ERR_MISSING_ARGS('actual', 'expected');
302
}
303
304
if (objectIs(actual, expected)) {
305
innerFail({
306
actual: actual,
307
expected: expected,
308
message: message,
309
operator: 'notStrictEqual',
310
stackStartFn: notStrictEqual
311
});
312
}
313
};
314
315
var Comparison = function Comparison(obj, keys, actual) {
316
var _this = this;
317
318
_classCallCheck(this, Comparison);
319
320
keys.forEach(function (key) {
321
if (key in obj) {
322
if (actual !== undefined && typeof actual[key] === 'string' && isRegExp(obj[key]) && obj[key].test(actual[key])) {
323
_this[key] = actual[key];
324
} else {
325
_this[key] = obj[key];
326
}
327
}
328
});
329
};
330
331
function compareExceptionKey(actual, expected, key, message, keys, fn) {
332
if (!(key in actual) || !isDeepStrictEqual(actual[key], expected[key])) {
333
if (!message) {
334
// Create placeholder objects to create a nice output.
335
var a = new Comparison(actual, keys);
336
var b = new Comparison(expected, keys, actual);
337
var err = new AssertionError({
338
actual: a,
339
expected: b,
340
operator: 'deepStrictEqual',
341
stackStartFn: fn
342
});
343
err.actual = actual;
344
err.expected = expected;
345
err.operator = fn.name;
346
throw err;
347
}
348
349
innerFail({
350
actual: actual,
351
expected: expected,
352
message: message,
353
operator: fn.name,
354
stackStartFn: fn
355
});
356
}
357
}
358
359
function expectedException(actual, expected, msg, fn) {
360
if (typeof expected !== 'function') {
361
if (isRegExp(expected)) return expected.test(actual); // assert.doesNotThrow does not accept objects.
362
363
if (arguments.length === 2) {
364
throw new ERR_INVALID_ARG_TYPE('expected', ['Function', 'RegExp'], expected);
365
} // Handle primitives properly.
366
367
368
if (_typeof(actual) !== 'object' || actual === null) {
369
var err = new AssertionError({
370
actual: actual,
371
expected: expected,
372
message: msg,
373
operator: 'deepStrictEqual',
374
stackStartFn: fn
375
});
376
err.operator = fn.name;
377
throw err;
378
}
379
380
var keys = Object.keys(expected); // Special handle errors to make sure the name and the message are compared
381
// as well.
382
383
if (expected instanceof Error) {
384
keys.push('name', 'message');
385
} else if (keys.length === 0) {
386
throw new ERR_INVALID_ARG_VALUE('error', expected, 'may not be an empty object');
387
}
388
389
if (isDeepEqual === undefined) lazyLoadComparison();
390
keys.forEach(function (key) {
391
if (typeof actual[key] === 'string' && isRegExp(expected[key]) && expected[key].test(actual[key])) {
392
return;
393
}
394
395
compareExceptionKey(actual, expected, key, msg, keys, fn);
396
});
397
return true;
398
} // Guard instanceof against arrow functions as they don't have a prototype.
399
400
401
if (expected.prototype !== undefined && actual instanceof expected) {
402
return true;
403
}
404
405
if (Error.isPrototypeOf(expected)) {
406
return false;
407
}
408
409
return expected.call({}, actual) === true;
410
}
411
412
function getActual(fn) {
413
if (typeof fn !== 'function') {
414
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
415
}
416
417
try {
418
fn();
419
} catch (e) {
420
return e;
421
}
422
423
return NO_EXCEPTION_SENTINEL;
424
}
425
426
function checkIsPromise(obj) {
427
// Accept native ES6 promises and promises that are implemented in a similar
428
// way. Do not accept thenables that use a function as `obj` and that have no
429
// `catch` handler.
430
// TODO: thenables are checked up until they have the correct methods,
431
// but according to documentation, the `then` method should receive
432
// the `fulfill` and `reject` arguments as well or it may be never resolved.
433
return isPromise(obj) || obj !== null && _typeof(obj) === 'object' && typeof obj.then === 'function' && typeof obj.catch === 'function';
434
}
435
436
function waitForActual(promiseFn) {
437
return Promise.resolve().then(function () {
438
var resultPromise;
439
440
if (typeof promiseFn === 'function') {
441
// Return a rejected promise if `promiseFn` throws synchronously.
442
resultPromise = promiseFn(); // Fail in case no promise is returned.
443
444
if (!checkIsPromise(resultPromise)) {
445
throw new ERR_INVALID_RETURN_VALUE('instance of Promise', 'promiseFn', resultPromise);
446
}
447
} else if (checkIsPromise(promiseFn)) {
448
resultPromise = promiseFn;
449
} else {
450
throw new ERR_INVALID_ARG_TYPE('promiseFn', ['Function', 'Promise'], promiseFn);
451
}
452
453
return Promise.resolve().then(function () {
454
return resultPromise;
455
}).then(function () {
456
return NO_EXCEPTION_SENTINEL;
457
}).catch(function (e) {
458
return e;
459
});
460
});
461
}
462
463
function expectsError(stackStartFn, actual, error, message) {
464
if (typeof error === 'string') {
465
if (arguments.length === 4) {
466
throw new ERR_INVALID_ARG_TYPE('error', ['Object', 'Error', 'Function', 'RegExp'], error);
467
}
468
469
if (_typeof(actual) === 'object' && actual !== null) {
470
if (actual.message === error) {
471
throw new ERR_AMBIGUOUS_ARGUMENT('error/message', "The error message \"".concat(actual.message, "\" is identical to the message."));
472
}
473
} else if (actual === error) {
474
throw new ERR_AMBIGUOUS_ARGUMENT('error/message', "The error \"".concat(actual, "\" is identical to the message."));
475
}
476
477
message = error;
478
error = undefined;
479
} else if (error != null && _typeof(error) !== 'object' && typeof error !== 'function') {
480
throw new ERR_INVALID_ARG_TYPE('error', ['Object', 'Error', 'Function', 'RegExp'], error);
481
}
482
483
if (actual === NO_EXCEPTION_SENTINEL) {
484
var details = '';
485
486
if (error && error.name) {
487
details += " (".concat(error.name, ")");
488
}
489
490
details += message ? ": ".concat(message) : '.';
491
var fnType = stackStartFn.name === 'rejects' ? 'rejection' : 'exception';
492
innerFail({
493
actual: undefined,
494
expected: error,
495
operator: stackStartFn.name,
496
message: "Missing expected ".concat(fnType).concat(details),
497
stackStartFn: stackStartFn
498
});
499
}
500
501
if (error && !expectedException(actual, error, message, stackStartFn)) {
502
throw actual;
503
}
504
}
505
506
function expectsNoError(stackStartFn, actual, error, message) {
507
if (actual === NO_EXCEPTION_SENTINEL) return;
508
509
if (typeof error === 'string') {
510
message = error;
511
error = undefined;
512
}
513
514
if (!error || expectedException(actual, error)) {
515
var details = message ? ": ".concat(message) : '.';
516
var fnType = stackStartFn.name === 'doesNotReject' ? 'rejection' : 'exception';
517
innerFail({
518
actual: actual,
519
expected: error,
520
operator: stackStartFn.name,
521
message: "Got unwanted ".concat(fnType).concat(details, "\n") + "Actual message: \"".concat(actual && actual.message, "\""),
522
stackStartFn: stackStartFn
523
});
524
}
525
526
throw actual;
527
}
528
529
assert.throws = function throws(promiseFn) {
530
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
531
args[_key2 - 1] = arguments[_key2];
532
}
533
534
expectsError.apply(void 0, [throws, getActual(promiseFn)].concat(args));
535
};
536
537
assert.rejects = function rejects(promiseFn) {
538
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
539
args[_key3 - 1] = arguments[_key3];
540
}
541
542
return waitForActual(promiseFn).then(function (result) {
543
return expectsError.apply(void 0, [rejects, result].concat(args));
544
});
545
};
546
547
assert.doesNotThrow = function doesNotThrow(fn) {
548
for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) {
549
args[_key4 - 1] = arguments[_key4];
550
}
551
552
expectsNoError.apply(void 0, [doesNotThrow, getActual(fn)].concat(args));
553
};
554
555
assert.doesNotReject = function doesNotReject(fn) {
556
for (var _len5 = arguments.length, args = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
557
args[_key5 - 1] = arguments[_key5];
558
}
559
560
return waitForActual(fn).then(function (result) {
561
return expectsNoError.apply(void 0, [doesNotReject, result].concat(args));
562
});
563
};
564
565
assert.ifError = function ifError(err) {
566
if (err !== null && err !== undefined) {
567
var message = 'ifError got unwanted exception: ';
568
569
if (_typeof(err) === 'object' && typeof err.message === 'string') {
570
if (err.message.length === 0 && err.constructor) {
571
message += err.constructor.name;
572
} else {
573
message += err.message;
574
}
575
} else {
576
message += inspect(err);
577
}
578
579
var newErr = new AssertionError({
580
actual: err,
581
expected: null,
582
operator: 'ifError',
583
message: message,
584
stackStartFn: ifError
585
}); // Make sure we actually have a stack trace!
586
587
var origStack = err.stack;
588
589
if (typeof origStack === 'string') {
590
// This will remove any duplicated frames from the error frames taken
591
// from within `ifError` and add the original error frames to the newly
592
// created ones.
593
var tmp2 = origStack.split('\n');
594
tmp2.shift(); // Filter all frames existing in err.stack.
595
596
var tmp1 = newErr.stack.split('\n');
597
598
for (var i = 0; i < tmp2.length; i++) {
599
// Find the first occurrence of the frame.
600
var pos = tmp1.indexOf(tmp2[i]);
601
602
if (pos !== -1) {
603
// Only keep new frames.
604
tmp1 = tmp1.slice(0, pos);
605
break;
606
}
607
}
608
609
newErr.stack = "".concat(tmp1.join('\n'), "\n").concat(tmp2.join('\n'));
610
}
611
612
throw newErr;
613
}
614
}; // Expose a strict only variant of assert
615
616
617
function strict() {
618
for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
619
args[_key6] = arguments[_key6];
620
}
621
622
innerOk.apply(void 0, [strict, args.length].concat(args));
623
}
624
625
assert.strict = objectAssign(strict, assert, {
626
equal: assert.strictEqual,
627
deepEqual: assert.deepStrictEqual,
628
notEqual: assert.notStrictEqual,
629
notDeepEqual: assert.notDeepStrictEqual
630
});
631
assert.strict.strict = assert.strict;
632