2009-11-30 02:20:37 +00:00
|
|
|
// Originally from narwhal.js (http://narwhaljs.org)
|
|
|
|
// Copyright (c) 2009 Thomas Robinson <280north.com>
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
2010-03-15 15:00:19 +00:00
|
|
|
// of this software and associated documentation files (the 'Software'), to
|
2009-11-30 02:20:37 +00:00
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
2010-12-02 00:36:23 +00:00
|
|
|
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
2009-11-30 02:20:37 +00:00
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-04-09 07:55:53 +00:00
|
|
|
const { Object } = primordials;
|
|
|
|
|
2017-12-09 22:20:07 +00:00
|
|
|
const { Buffer } = require('buffer');
|
2018-05-02 14:21:40 +00:00
|
|
|
const { codes: {
|
|
|
|
ERR_AMBIGUOUS_ARGUMENT,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
2018-05-02 22:23:47 +00:00
|
|
|
ERR_INVALID_ARG_VALUE,
|
2019-03-13 18:23:00 +00:00
|
|
|
ERR_INVALID_RETURN_VALUE,
|
|
|
|
ERR_MISSING_ARGS
|
2018-05-02 14:21:40 +00:00
|
|
|
} } = require('internal/errors');
|
2019-02-06 04:46:35 +00:00
|
|
|
const AssertionError = require('internal/assert/assertion_error');
|
2017-12-09 22:20:07 +00:00
|
|
|
const { openSync, closeSync, readSync } = require('fs');
|
2019-03-19 00:05:06 +00:00
|
|
|
const { inspect } = require('internal/util/inspect');
|
|
|
|
const { isPromise, isRegExp } = require('internal/util/types');
|
2018-05-06 21:10:23 +00:00
|
|
|
const { EOL } = require('internal/constants');
|
2018-03-06 14:42:32 +00:00
|
|
|
const { NativeModule } = require('internal/bootstrap/loaders');
|
2017-12-09 22:20:07 +00:00
|
|
|
|
2018-10-07 01:27:54 +00:00
|
|
|
const errorCache = new Map();
|
|
|
|
|
2018-05-07 01:53:02 +00:00
|
|
|
let isDeepEqual;
|
|
|
|
let isDeepStrictEqual;
|
2018-07-02 18:29:57 +00:00
|
|
|
let parseExpressionAt;
|
|
|
|
let findNodeAround;
|
|
|
|
let decoder;
|
2018-05-07 01:53:02 +00:00
|
|
|
|
|
|
|
function lazyLoadComparison() {
|
|
|
|
const comparison = require('internal/util/comparisons');
|
|
|
|
isDeepEqual = comparison.isDeepEqual;
|
|
|
|
isDeepStrictEqual = comparison.isDeepStrictEqual;
|
|
|
|
}
|
|
|
|
|
2017-12-09 22:20:07 +00:00
|
|
|
// Escape control characters but not \n and \t to keep the line breaks and
|
|
|
|
// indentation intact.
|
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
const escapeSequencesRegExp = /[\x00-\x08\x0b\x0c\x0e-\x1f]/g;
|
|
|
|
const 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'
|
|
|
|
];
|
|
|
|
|
|
|
|
const escapeFn = (str) => meta[str.charCodeAt(0)];
|
2017-04-25 20:18:25 +00:00
|
|
|
|
2018-01-28 11:07:18 +00:00
|
|
|
let warned = false;
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The assert module provides functions that throw
|
2009-11-30 02:20:37 +00:00
|
|
|
// AssertionError's when particular conditions are not met. The
|
|
|
|
// assert module must conform to the following interface.
|
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const assert = module.exports = ok;
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2018-01-07 22:17:29 +00:00
|
|
|
const NO_EXCEPTION_SENTINEL = {};
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// All of the following functions must throw an AssertionError
|
2009-11-28 17:26:59 +00:00
|
|
|
// when a corresponding condition is not met, with a message that
|
2017-06-27 04:00:35 +00:00
|
|
|
// may be undefined if not provided. All assertion methods provide
|
2009-11-28 17:26:59 +00:00
|
|
|
// both the actual and expected values to the assertion error for
|
|
|
|
// display purposes.
|
|
|
|
|
2017-12-09 19:15:19 +00:00
|
|
|
function innerFail(obj) {
|
|
|
|
if (obj.message instanceof Error) throw obj.message;
|
2017-09-10 01:36:47 +00:00
|
|
|
|
2018-01-22 23:11:22 +00:00
|
|
|
throw new AssertionError(obj);
|
2017-06-27 04:00:35 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 19:15:19 +00:00
|
|
|
function fail(actual, expected, message, operator, stackStartFn) {
|
2017-07-02 20:48:47 +00:00
|
|
|
const argsLen = arguments.length;
|
|
|
|
|
2019-05-01 20:49:04 +00:00
|
|
|
let internalMessage = false;
|
|
|
|
if (actual == null && argsLen <= 1) {
|
|
|
|
internalMessage = true;
|
|
|
|
message = 'Failed';
|
2017-07-02 20:48:47 +00:00
|
|
|
} else if (argsLen === 1) {
|
2017-04-09 17:36:14 +00:00
|
|
|
message = actual;
|
2017-06-28 23:57:02 +00:00
|
|
|
actual = undefined;
|
2018-01-28 11:07:18 +00:00
|
|
|
} else {
|
|
|
|
if (warned === false) {
|
|
|
|
warned = true;
|
|
|
|
process.emitWarning(
|
|
|
|
'assert.fail() with more than one argument is deprecated. ' +
|
|
|
|
'Please use assert.strictEqual() instead or only pass a message.',
|
|
|
|
'DeprecationWarning',
|
2018-02-04 18:59:53 +00:00
|
|
|
'DEP0094'
|
2018-01-28 11:07:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (argsLen === 2)
|
|
|
|
operator = '!=';
|
2017-06-27 04:00:35 +00:00
|
|
|
}
|
2017-07-02 20:48:47 +00:00
|
|
|
|
2018-09-04 13:18:44 +00:00
|
|
|
if (message instanceof Error) throw message;
|
|
|
|
|
|
|
|
const errArgs = {
|
2017-12-09 19:15:19 +00:00
|
|
|
actual,
|
|
|
|
expected,
|
2018-09-04 13:18:44 +00:00
|
|
|
operator: operator === undefined ? 'fail' : operator,
|
2019-05-01 20:49:04 +00:00
|
|
|
stackStartFn: stackStartFn || fail,
|
|
|
|
message
|
2018-09-04 13:18:44 +00:00
|
|
|
};
|
|
|
|
const err = new AssertionError(errArgs);
|
|
|
|
if (internalMessage) {
|
|
|
|
err.generatedMessage = true;
|
|
|
|
}
|
|
|
|
throw err;
|
2009-11-28 17:26:59 +00:00
|
|
|
}
|
2017-07-02 20:48:47 +00:00
|
|
|
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.fail = fail;
|
|
|
|
|
2017-05-08 20:10:51 +00:00
|
|
|
// The AssertionError is defined in internal/error.
|
2018-01-22 23:11:22 +00:00
|
|
|
assert.AssertionError = AssertionError;
|
2017-05-08 20:10:51 +00:00
|
|
|
|
2018-07-02 18:29:57 +00:00
|
|
|
function findColumn(fd, column, code) {
|
|
|
|
if (code.length > column + 100) {
|
|
|
|
try {
|
|
|
|
return parseCode(code, column);
|
|
|
|
} catch {
|
|
|
|
// End recursion in case no code could be parsed. The expression should
|
|
|
|
// have been found after 2500 characters, so stop trying.
|
|
|
|
if (code.length - column > 2500) {
|
|
|
|
// eslint-disable-next-line no-throw-literal
|
|
|
|
throw null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Read up to 2500 bytes more than necessary in columns. That way we address
|
|
|
|
// multi byte characters and read enough data to parse the code.
|
|
|
|
const bytesToRead = column - code.length + 2500;
|
|
|
|
const buffer = Buffer.allocUnsafe(bytesToRead);
|
|
|
|
const bytesRead = readSync(fd, buffer, 0, bytesToRead);
|
|
|
|
code += decoder.write(buffer.slice(0, bytesRead));
|
|
|
|
// EOF: fast path.
|
|
|
|
if (bytesRead < bytesToRead) {
|
|
|
|
return parseCode(code, column);
|
|
|
|
}
|
|
|
|
// Read potentially missing code.
|
|
|
|
return findColumn(fd, column, code);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCode(fd, line, column) {
|
|
|
|
let bytesRead = 0;
|
|
|
|
if (line === 0) {
|
|
|
|
// Special handle line number one. This is more efficient and simplifies the
|
|
|
|
// rest of the algorithm. Read more than the regular column number in bytes
|
|
|
|
// to prevent multiple reads in case multi byte characters are used.
|
|
|
|
return findColumn(fd, column, '');
|
|
|
|
}
|
2018-05-15 16:44:19 +00:00
|
|
|
let lines = 0;
|
2017-12-09 22:20:07 +00:00
|
|
|
// Prevent blocking the event loop by limiting the maximum amount of
|
|
|
|
// data that may be read.
|
2018-05-15 16:44:19 +00:00
|
|
|
let maxReads = 64; // bytesPerRead * maxReads = 512 kb
|
2017-12-09 22:20:07 +00:00
|
|
|
const bytesPerRead = 8192;
|
2018-07-02 18:29:57 +00:00
|
|
|
// Use a single buffer up front that is reused until the call site is found.
|
|
|
|
let buffer = Buffer.allocUnsafe(bytesPerRead);
|
|
|
|
while (maxReads-- !== 0) {
|
|
|
|
// Only allocate a new buffer in case the needed line is found. All data
|
|
|
|
// before that can be discarded.
|
|
|
|
buffer = lines < line ? buffer : Buffer.allocUnsafe(bytesPerRead);
|
2017-12-09 22:20:07 +00:00
|
|
|
bytesRead = readSync(fd, buffer, 0, bytesPerRead);
|
2018-07-02 18:29:57 +00:00
|
|
|
// Read the buffer until the required code line is found.
|
2017-12-09 22:20:07 +00:00
|
|
|
for (var i = 0; i < bytesRead; i++) {
|
2018-07-02 18:29:57 +00:00
|
|
|
if (buffer[i] === 10 && ++lines === line) {
|
|
|
|
// If the end of file is reached, directly parse the code and return.
|
|
|
|
if (bytesRead < bytesPerRead) {
|
|
|
|
return parseCode(buffer.toString('utf8', i + 1, bytesRead), column);
|
2017-12-09 22:20:07 +00:00
|
|
|
}
|
2018-07-02 18:29:57 +00:00
|
|
|
// Check if the read code is sufficient or read more until the whole
|
|
|
|
// expression is read. Make sure multi byte characters are preserved
|
|
|
|
// properly by using the decoder.
|
|
|
|
const code = decoder.write(buffer.slice(i + 1, bytesRead));
|
|
|
|
return findColumn(fd, column, code);
|
2017-12-09 22:20:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-02 18:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseCode(code, offset) {
|
|
|
|
// Lazy load acorn.
|
|
|
|
if (parseExpressionAt === undefined) {
|
2019-04-24 21:54:42 +00:00
|
|
|
const acorn = require('internal/deps/acorn/acorn/dist/acorn');
|
|
|
|
const privateMethods =
|
|
|
|
require('internal/deps/acorn-plugins/acorn-private-methods/index');
|
|
|
|
const bigInt = require('internal/deps/acorn-plugins/acorn-bigint/index');
|
|
|
|
const classFields =
|
|
|
|
require('internal/deps/acorn-plugins/acorn-class-fields/index');
|
|
|
|
const numericSeparator =
|
|
|
|
require('internal/deps/acorn-plugins/acorn-numeric-separator/index');
|
|
|
|
const staticClassFeatures =
|
|
|
|
require('internal/deps/acorn-plugins/acorn-static-class-features/index');
|
|
|
|
|
2019-01-31 07:36:48 +00:00
|
|
|
({ findNodeAround } = require('internal/deps/acorn/acorn-walk/dist/walk'));
|
2019-04-24 21:54:42 +00:00
|
|
|
|
|
|
|
const Parser = acorn.Parser.extend(
|
|
|
|
privateMethods,
|
|
|
|
bigInt,
|
|
|
|
classFields,
|
|
|
|
numericSeparator,
|
|
|
|
staticClassFeatures
|
|
|
|
);
|
|
|
|
parseExpressionAt = Parser.parseExpressionAt.bind(Parser);
|
2018-07-02 18:29:57 +00:00
|
|
|
}
|
|
|
|
let node;
|
|
|
|
let start = 0;
|
|
|
|
// Parse the read code until the correct expression is found.
|
|
|
|
do {
|
|
|
|
try {
|
|
|
|
node = parseExpressionAt(code, start);
|
|
|
|
start = node.end + 1 || start;
|
|
|
|
// Find the CallExpression in the tree.
|
|
|
|
node = findNodeAround(node, offset, 'CallExpression');
|
|
|
|
} catch (err) {
|
|
|
|
// Unexpected token error and the like.
|
|
|
|
start += err.raisedAt || 1;
|
|
|
|
if (start > offset) {
|
|
|
|
// No matching expression found. This could happen if the assert
|
|
|
|
// expression is bigger than the provided buffer.
|
|
|
|
// eslint-disable-next-line no-throw-literal
|
|
|
|
throw null;
|
|
|
|
}
|
2017-12-09 22:20:07 +00:00
|
|
|
}
|
2018-07-02 18:29:57 +00:00
|
|
|
} while (node === undefined || node.node.end < offset);
|
|
|
|
|
|
|
|
return [
|
|
|
|
node.node.start,
|
|
|
|
code.slice(node.node.start, node.node.end)
|
|
|
|
.replace(escapeSequencesRegExp, escapeFn)
|
|
|
|
];
|
2017-12-09 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 18:29:57 +00:00
|
|
|
function getErrMessage(message, fn) {
|
|
|
|
const tmpLimit = Error.stackTraceLimit;
|
|
|
|
// Make sure the limit is set to 1. Otherwise it could fail (<= 0) or it
|
|
|
|
// does to much work.
|
|
|
|
Error.stackTraceLimit = 1;
|
|
|
|
// We only need the stack trace. To minimize the overhead use an object
|
|
|
|
// instead of an error.
|
|
|
|
const err = {};
|
2019-03-18 02:47:55 +00:00
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2018-07-02 18:29:57 +00:00
|
|
|
Error.captureStackTrace(err, fn);
|
|
|
|
Error.stackTraceLimit = tmpLimit;
|
|
|
|
|
|
|
|
const tmpPrepare = Error.prepareStackTrace;
|
|
|
|
Error.prepareStackTrace = (_, stack) => stack;
|
|
|
|
const call = err.stack[0];
|
|
|
|
Error.prepareStackTrace = tmpPrepare;
|
|
|
|
|
2018-01-23 15:45:28 +00:00
|
|
|
const filename = call.getFileName();
|
2018-07-02 18:29:57 +00:00
|
|
|
|
2018-05-20 12:37:28 +00:00
|
|
|
if (!filename) {
|
2018-07-02 18:29:57 +00:00
|
|
|
return message;
|
2018-05-20 12:37:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 15:45:28 +00:00
|
|
|
const line = call.getLineNumber() - 1;
|
2018-07-02 18:29:57 +00:00
|
|
|
let column = call.getColumnNumber() - 1;
|
|
|
|
|
2018-01-23 15:45:28 +00:00
|
|
|
const identifier = `${filename}${line}${column}`;
|
|
|
|
|
|
|
|
if (errorCache.has(identifier)) {
|
|
|
|
return errorCache.get(identifier);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip Node.js modules!
|
2018-03-03 20:55:45 +00:00
|
|
|
if (filename.endsWith('.js') && NativeModule.exists(filename.slice(0, -3))) {
|
2018-01-23 15:45:28 +00:00
|
|
|
errorCache.set(identifier, undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:29:57 +00:00
|
|
|
let fd;
|
2018-01-23 15:45:28 +00:00
|
|
|
try {
|
2018-07-02 18:29:57 +00:00
|
|
|
// Set the stack trace limit to zero. This makes sure unexpected token
|
|
|
|
// errors are handled faster.
|
|
|
|
Error.stackTraceLimit = 0;
|
|
|
|
|
|
|
|
if (decoder === undefined) {
|
|
|
|
const { StringDecoder } = require('string_decoder');
|
|
|
|
decoder = new StringDecoder('utf8');
|
|
|
|
}
|
|
|
|
|
2018-01-23 15:45:28 +00:00
|
|
|
fd = openSync(filename, 'r', 0o666);
|
2018-07-02 18:29:57 +00:00
|
|
|
// Reset column and message.
|
|
|
|
[column, message] = getCode(fd, line, column);
|
|
|
|
// Flush unfinished multi byte characters.
|
|
|
|
decoder.end();
|
|
|
|
// Always normalize indentation, otherwise the message could look weird.
|
2019-03-18 11:31:07 +00:00
|
|
|
if (message.includes('\n')) {
|
2018-05-15 16:44:19 +00:00
|
|
|
if (EOL === '\r\n') {
|
|
|
|
message = message.replace(/\r\n/g, '\n');
|
|
|
|
}
|
2018-07-02 18:29:57 +00:00
|
|
|
const frames = message.split('\n');
|
|
|
|
message = frames.shift();
|
|
|
|
for (const frame of frames) {
|
|
|
|
let pos = 0;
|
|
|
|
while (pos < column && (frame[pos] === ' ' || frame[pos] === '\t')) {
|
|
|
|
pos++;
|
2018-05-16 16:10:08 +00:00
|
|
|
}
|
2018-07-02 18:29:57 +00:00
|
|
|
message += `\n ${frame.slice(pos)}`;
|
2018-05-16 16:10:08 +00:00
|
|
|
}
|
2018-01-23 15:45:28 +00:00
|
|
|
}
|
2018-07-02 18:29:57 +00:00
|
|
|
message = `The expression evaluated to a falsy value:\n\n ${message}\n`;
|
2018-01-23 15:45:28 +00:00
|
|
|
// Make sure to always set the cache! No matter if the message is
|
|
|
|
// undefined or not
|
|
|
|
errorCache.set(identifier, message);
|
|
|
|
|
|
|
|
return message;
|
2018-11-04 14:59:52 +00:00
|
|
|
} catch {
|
|
|
|
// Invalidate cache to prevent trying to read this part again.
|
2018-01-23 15:45:28 +00:00
|
|
|
errorCache.set(identifier, undefined);
|
|
|
|
} finally {
|
2018-07-02 18:29:57 +00:00
|
|
|
// Reset limit.
|
|
|
|
Error.stackTraceLimit = tmpLimit;
|
2018-01-23 15:45:28 +00:00
|
|
|
if (fd !== undefined)
|
|
|
|
closeSync(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 03:48:01 +00:00
|
|
|
function innerOk(fn, argLen, value, message) {
|
2017-12-09 19:15:19 +00:00
|
|
|
if (!value) {
|
2018-01-31 14:43:09 +00:00
|
|
|
let generatedMessage = false;
|
|
|
|
|
2018-03-12 03:48:01 +00:00
|
|
|
if (argLen === 0) {
|
2018-01-31 14:43:09 +00:00
|
|
|
generatedMessage = true;
|
2018-01-23 13:38:34 +00:00
|
|
|
message = 'No value argument passed to `assert.ok()`';
|
|
|
|
} else if (message == null) {
|
2018-01-31 14:43:09 +00:00
|
|
|
generatedMessage = true;
|
2018-07-02 18:29:57 +00:00
|
|
|
message = getErrMessage(message, fn);
|
2018-01-31 14:43:09 +00:00
|
|
|
} else if (message instanceof Error) {
|
|
|
|
throw message;
|
2017-12-09 22:20:07 +00:00
|
|
|
}
|
2018-01-31 14:43:09 +00:00
|
|
|
|
|
|
|
const err = new AssertionError({
|
2017-12-09 19:15:19 +00:00
|
|
|
actual: value,
|
|
|
|
expected: true,
|
|
|
|
message,
|
|
|
|
operator: '==',
|
2017-12-09 22:20:07 +00:00
|
|
|
stackStartFn: fn
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2018-01-31 14:43:09 +00:00
|
|
|
err.generatedMessage = generatedMessage;
|
|
|
|
throw err;
|
2017-12-09 19:15:19 +00:00
|
|
|
}
|
2011-10-04 22:08:18 +00:00
|
|
|
}
|
2017-12-09 22:20:07 +00:00
|
|
|
|
|
|
|
// Pure assertion tests whether a value is truthy, as determined
|
|
|
|
// by !!value.
|
|
|
|
function ok(...args) {
|
2018-03-12 03:48:01 +00:00
|
|
|
innerOk(ok, args.length, ...args);
|
2017-12-09 22:20:07 +00:00
|
|
|
}
|
2011-10-01 11:42:42 +00:00
|
|
|
assert.ok = ok;
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2017-06-27 04:00:35 +00:00
|
|
|
// The equality assertion tests shallow, coercive equality with ==.
|
2017-01-08 23:23:34 +00:00
|
|
|
/* eslint-disable no-restricted-properties */
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.equal = function equal(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2017-04-16 18:29:35 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2017-12-09 19:15:19 +00:00
|
|
|
if (actual != expected) {
|
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: '==',
|
|
|
|
stackStartFn: equal
|
|
|
|
});
|
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The non-equality assertion tests for whether two objects are not
|
|
|
|
// equal with !=.
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.notEqual = function notEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2017-04-16 18:29:35 +00:00
|
|
|
// eslint-disable-next-line eqeqeq
|
2010-04-11 20:46:24 +00:00
|
|
|
if (actual == expected) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: '!=',
|
|
|
|
stackStartFn: notEqual
|
|
|
|
});
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The equivalence assertion tests a deep equality relation.
|
2009-12-29 19:14:58 +00:00
|
|
|
assert.deepEqual = function deepEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-06 04:27:46 +00:00
|
|
|
if (!isDeepEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'deepEqual',
|
|
|
|
stackStartFn: deepEqual
|
|
|
|
});
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
2015-01-28 16:48:56 +00:00
|
|
|
|
2017-01-02 21:50:04 +00:00
|
|
|
// The non-equivalence assertion tests for any deep inequality.
|
2009-12-29 19:14:58 +00:00
|
|
|
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-06 04:27:46 +00:00
|
|
|
if (isDeepEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'notDeepEqual',
|
|
|
|
stackStartFn: notDeepEqual
|
|
|
|
});
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
2017-10-19 19:39:29 +00:00
|
|
|
/* eslint-enable */
|
|
|
|
|
|
|
|
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-19 19:39:29 +00:00
|
|
|
if (!isDeepStrictEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'deepStrictEqual',
|
2018-04-02 16:42:11 +00:00
|
|
|
stackStartFn: deepStrictEqual
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2017-10-19 19:39:29 +00:00
|
|
|
}
|
|
|
|
};
|
2009-11-28 17:26:59 +00:00
|
|
|
|
2015-01-28 16:48:56 +00:00
|
|
|
assert.notDeepStrictEqual = notDeepStrictEqual;
|
|
|
|
function notDeepStrictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2017-10-06 04:27:46 +00:00
|
|
|
if (isDeepStrictEqual(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'notDeepStrictEqual',
|
2018-04-02 16:42:11 +00:00
|
|
|
stackStartFn: notDeepStrictEqual
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2015-01-28 16:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.strictEqual = function strictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2017-11-13 22:34:16 +00:00
|
|
|
if (!Object.is(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'strictEqual',
|
2018-04-02 16:42:11 +00:00
|
|
|
stackStartFn: strictEqual
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2009-11-30 02:20:37 +00:00
|
|
|
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
2019-03-13 18:23:00 +00:00
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new ERR_MISSING_ARGS('actual', 'expected');
|
|
|
|
}
|
2017-11-13 22:34:16 +00:00
|
|
|
if (Object.is(actual, expected)) {
|
2017-12-09 19:15:19 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
|
|
|
operator: 'notStrictEqual',
|
2018-04-02 16:42:11 +00:00
|
|
|
stackStartFn: notStrictEqual
|
2017-12-09 19:15:19 +00:00
|
|
|
});
|
2009-12-29 19:14:58 +00:00
|
|
|
}
|
2009-11-28 17:26:59 +00:00
|
|
|
};
|
|
|
|
|
2018-03-20 00:49:29 +00:00
|
|
|
class Comparison {
|
2018-05-02 17:26:32 +00:00
|
|
|
constructor(obj, keys, actual) {
|
2018-03-20 00:49:29 +00:00
|
|
|
for (const key of keys) {
|
2018-05-02 17:26:32 +00:00
|
|
|
if (key in obj) {
|
|
|
|
if (actual !== undefined &&
|
|
|
|
typeof actual[key] === 'string' &&
|
|
|
|
isRegExp(obj[key]) &&
|
|
|
|
obj[key].test(actual[key])) {
|
|
|
|
this[key] = actual[key];
|
|
|
|
} else {
|
|
|
|
this[key] = obj[key];
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 00:49:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 01:12:42 +00:00
|
|
|
function compareExceptionKey(actual, expected, key, message, keys, fn) {
|
2018-03-20 00:49:29 +00:00
|
|
|
if (!(key in actual) || !isDeepStrictEqual(actual[key], expected[key])) {
|
|
|
|
if (!message) {
|
|
|
|
// Create placeholder objects to create a nice output.
|
|
|
|
const a = new Comparison(actual, keys);
|
2018-05-02 17:26:32 +00:00
|
|
|
const b = new Comparison(expected, keys, actual);
|
2018-03-20 00:49:29 +00:00
|
|
|
|
|
|
|
const err = new AssertionError({
|
|
|
|
actual: a,
|
|
|
|
expected: b,
|
|
|
|
operator: 'deepStrictEqual',
|
2019-04-02 01:12:42 +00:00
|
|
|
stackStartFn: fn
|
2018-03-20 00:49:29 +00:00
|
|
|
});
|
2018-05-02 14:59:49 +00:00
|
|
|
err.actual = actual;
|
|
|
|
err.expected = expected;
|
2019-04-02 01:12:42 +00:00
|
|
|
err.operator = fn.name;
|
2018-05-02 14:59:49 +00:00
|
|
|
throw err;
|
2018-03-20 00:49:29 +00:00
|
|
|
}
|
2018-02-06 10:08:23 +00:00
|
|
|
innerFail({
|
2018-03-20 00:49:29 +00:00
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message,
|
2019-04-02 01:12:42 +00:00
|
|
|
operator: fn.name,
|
|
|
|
stackStartFn: fn
|
2018-02-06 10:08:23 +00:00
|
|
|
});
|
|
|
|
}
|
2017-12-10 00:54:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 01:12:42 +00:00
|
|
|
function expectedException(actual, expected, msg, fn) {
|
2017-06-28 18:35:16 +00:00
|
|
|
if (typeof expected !== 'function') {
|
2018-05-02 14:59:49 +00:00
|
|
|
if (isRegExp(expected))
|
2017-12-10 00:54:44 +00:00
|
|
|
return expected.test(actual);
|
|
|
|
// assert.doesNotThrow does not accept objects.
|
|
|
|
if (arguments.length === 2) {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'expected', ['Function', 'RegExp'], expected
|
|
|
|
);
|
2017-12-10 00:54:44 +00:00
|
|
|
}
|
2018-05-02 14:59:49 +00:00
|
|
|
|
|
|
|
// Handle primitives properly.
|
|
|
|
if (typeof actual !== 'object' || actual === null) {
|
|
|
|
const err = new AssertionError({
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
message: msg,
|
|
|
|
operator: 'deepStrictEqual',
|
2019-04-02 01:12:42 +00:00
|
|
|
stackStartFn: fn
|
2018-05-02 14:59:49 +00:00
|
|
|
});
|
2019-04-02 01:12:42 +00:00
|
|
|
err.operator = fn.name;
|
2018-05-02 14:59:49 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2018-03-20 00:49:29 +00:00
|
|
|
const keys = Object.keys(expected);
|
|
|
|
// Special handle errors to make sure the name and the message are compared
|
|
|
|
// as well.
|
|
|
|
if (expected instanceof Error) {
|
|
|
|
keys.push('name', 'message');
|
2018-05-02 22:23:47 +00:00
|
|
|
} else if (keys.length === 0) {
|
|
|
|
throw new ERR_INVALID_ARG_VALUE('error',
|
|
|
|
expected, 'may not be an empty object');
|
2017-12-10 00:54:44 +00:00
|
|
|
}
|
2018-05-07 01:53:02 +00:00
|
|
|
if (isDeepEqual === undefined) lazyLoadComparison();
|
2018-03-20 00:49:29 +00:00
|
|
|
for (const key of keys) {
|
2018-05-02 17:26:32 +00:00
|
|
|
if (typeof actual[key] === 'string' &&
|
|
|
|
isRegExp(expected[key]) &&
|
|
|
|
expected[key].test(actual[key])) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-02 01:12:42 +00:00
|
|
|
compareExceptionKey(actual, expected, key, msg, keys, fn);
|
2017-12-10 00:54:44 +00:00
|
|
|
}
|
|
|
|
return true;
|
2010-11-26 23:03:31 +00:00
|
|
|
}
|
2017-06-28 18:35:16 +00:00
|
|
|
// Guard instanceof against arrow functions as they don't have a prototype.
|
|
|
|
if (expected.prototype !== undefined && actual instanceof expected) {
|
|
|
|
return true;
|
2015-10-08 10:25:03 +00:00
|
|
|
}
|
2015-12-06 01:29:28 +00:00
|
|
|
if (Error.isPrototypeOf(expected)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-08 10:25:03 +00:00
|
|
|
return expected.call({}, actual) === true;
|
2010-11-26 23:03:31 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
function getActual(fn) {
|
|
|
|
if (typeof fn !== 'function') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
|
2017-12-09 23:47:49 +00:00
|
|
|
}
|
2015-12-20 07:01:34 +00:00
|
|
|
try {
|
2018-09-08 00:30:07 +00:00
|
|
|
fn();
|
2015-12-20 07:01:34 +00:00
|
|
|
} catch (e) {
|
2017-06-28 18:35:16 +00:00
|
|
|
return e;
|
2015-12-20 07:01:34 +00:00
|
|
|
}
|
2018-01-07 22:17:29 +00:00
|
|
|
return NO_EXCEPTION_SENTINEL;
|
2015-12-20 07:01:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 21:28:30 +00:00
|
|
|
function checkIsPromise(obj) {
|
|
|
|
// Accept native ES6 promises and promises that are implemented in a similar
|
|
|
|
// way. Do not accept thenables that use a function as `obj` and that have no
|
|
|
|
// `catch` handler.
|
2018-11-07 09:27:18 +00:00
|
|
|
|
|
|
|
// TODO: thenables are checked up until they have the correct methods,
|
|
|
|
// but according to documentation, the `then` method should receive
|
|
|
|
// the `fulfill` and `reject` arguments as well or it may be never resolved.
|
|
|
|
|
2018-04-08 21:28:30 +00:00
|
|
|
return isPromise(obj) ||
|
|
|
|
obj !== null && typeof obj === 'object' &&
|
|
|
|
typeof obj.then === 'function' &&
|
|
|
|
typeof obj.catch === 'function';
|
|
|
|
}
|
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
async function waitForActual(promiseFn) {
|
2018-04-08 21:28:30 +00:00
|
|
|
let resultPromise;
|
2018-09-08 00:30:07 +00:00
|
|
|
if (typeof promiseFn === 'function') {
|
|
|
|
// Return a rejected promise if `promiseFn` throws synchronously.
|
|
|
|
resultPromise = promiseFn();
|
2018-04-08 22:38:41 +00:00
|
|
|
// Fail in case no promise is returned.
|
|
|
|
if (!checkIsPromise(resultPromise)) {
|
|
|
|
throw new ERR_INVALID_RETURN_VALUE('instance of Promise',
|
2018-09-08 00:30:07 +00:00
|
|
|
'promiseFn', resultPromise);
|
2018-04-08 22:38:41 +00:00
|
|
|
}
|
2018-09-08 00:30:07 +00:00
|
|
|
} else if (checkIsPromise(promiseFn)) {
|
|
|
|
resultPromise = promiseFn;
|
2018-04-08 21:28:30 +00:00
|
|
|
} else {
|
2018-09-08 00:30:07 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
'promiseFn', ['Function', 'Promise'], promiseFn);
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
2018-03-28 05:21:31 +00:00
|
|
|
|
2018-01-11 23:16:41 +00:00
|
|
|
try {
|
2018-03-28 05:21:31 +00:00
|
|
|
await resultPromise;
|
2018-01-11 23:16:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
return NO_EXCEPTION_SENTINEL;
|
|
|
|
}
|
2010-11-26 23:03:31 +00:00
|
|
|
|
2018-01-11 23:16:41 +00:00
|
|
|
function expectsError(stackStartFn, actual, error, message) {
|
2017-12-09 23:47:49 +00:00
|
|
|
if (typeof error === 'string') {
|
2018-01-11 23:16:41 +00:00
|
|
|
if (arguments.length === 4) {
|
2018-04-07 12:20:43 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('error',
|
|
|
|
['Object', 'Error', 'Function', 'RegExp'],
|
|
|
|
error);
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
2018-04-07 12:22:29 +00:00
|
|
|
if (typeof actual === 'object' && actual !== null) {
|
|
|
|
if (actual.message === error) {
|
|
|
|
throw new ERR_AMBIGUOUS_ARGUMENT(
|
|
|
|
'error/message',
|
|
|
|
`The error message "${actual.message}" is identical to the message.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (actual === error) {
|
|
|
|
throw new ERR_AMBIGUOUS_ARGUMENT(
|
|
|
|
'error/message',
|
|
|
|
`The error "${actual}" is identical to the message.`
|
|
|
|
);
|
|
|
|
}
|
2017-12-09 23:47:49 +00:00
|
|
|
message = error;
|
2018-04-11 01:28:35 +00:00
|
|
|
error = undefined;
|
2018-05-02 22:23:47 +00:00
|
|
|
} else if (error != null &&
|
|
|
|
typeof error !== 'object' &&
|
|
|
|
typeof error !== 'function') {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('error',
|
|
|
|
['Object', 'Error', 'Function', 'RegExp'],
|
|
|
|
error);
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 22:17:29 +00:00
|
|
|
if (actual === NO_EXCEPTION_SENTINEL) {
|
2017-12-09 23:47:49 +00:00
|
|
|
let details = '';
|
|
|
|
if (error && error.name) {
|
|
|
|
details += ` (${error.name})`;
|
2017-06-28 18:35:16 +00:00
|
|
|
}
|
2017-12-09 23:47:49 +00:00
|
|
|
details += message ? `: ${message}` : '.';
|
2018-04-08 21:28:30 +00:00
|
|
|
const fnType = stackStartFn.name === 'rejects' ? 'rejection' : 'exception';
|
2017-12-09 23:47:49 +00:00
|
|
|
innerFail({
|
2018-04-11 01:28:35 +00:00
|
|
|
actual: undefined,
|
2017-12-09 23:47:49 +00:00
|
|
|
expected: error,
|
2018-01-11 23:16:41 +00:00
|
|
|
operator: stackStartFn.name,
|
|
|
|
message: `Missing expected ${fnType}${details}`,
|
|
|
|
stackStartFn
|
2017-12-09 23:47:49 +00:00
|
|
|
});
|
|
|
|
}
|
2019-04-02 01:12:42 +00:00
|
|
|
if (error && !expectedException(actual, error, message, stackStartFn)) {
|
2010-11-26 23:03:31 +00:00
|
|
|
throw actual;
|
2009-12-29 18:37:40 +00:00
|
|
|
}
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
2009-11-30 02:20:37 +00:00
|
|
|
|
2018-01-11 23:16:41 +00:00
|
|
|
function expectsNoError(stackStartFn, actual, error, message) {
|
2018-01-07 22:17:29 +00:00
|
|
|
if (actual === NO_EXCEPTION_SENTINEL)
|
2017-12-09 23:47:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (typeof error === 'string') {
|
|
|
|
message = error;
|
2018-04-11 01:28:35 +00:00
|
|
|
error = undefined;
|
2017-12-09 23:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!error || expectedException(actual, error)) {
|
|
|
|
const details = message ? `: ${message}` : '.';
|
2018-04-08 21:28:30 +00:00
|
|
|
const fnType = stackStartFn.name === 'doesNotReject' ?
|
|
|
|
'rejection' : 'exception';
|
2017-12-09 23:47:49 +00:00
|
|
|
innerFail({
|
|
|
|
actual,
|
|
|
|
expected: error,
|
2018-01-11 23:16:41 +00:00
|
|
|
operator: stackStartFn.name,
|
2018-04-09 01:57:40 +00:00
|
|
|
message: `Got unwanted ${fnType}${details}\n` +
|
|
|
|
`Actual message: "${actual && actual.message}"`,
|
2018-01-11 23:16:41 +00:00
|
|
|
stackStartFn
|
2017-12-09 23:47:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
throw actual;
|
2018-01-11 23:16:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.throws = function throws(promiseFn, ...args) {
|
|
|
|
expectsError(throws, getActual(promiseFn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2018-01-11 23:16:41 +00:00
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.rejects = async function rejects(promiseFn, ...args) {
|
|
|
|
expectsError(rejects, await waitForActual(promiseFn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2018-01-11 23:16:41 +00:00
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.doesNotThrow = function doesNotThrow(fn, ...args) {
|
|
|
|
expectsNoError(doesNotThrow, getActual(fn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2018-01-11 23:16:41 +00:00
|
|
|
|
2018-09-08 00:30:07 +00:00
|
|
|
assert.doesNotReject = async function doesNotReject(fn, ...args) {
|
|
|
|
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
|
2018-04-08 21:28:30 +00:00
|
|
|
};
|
2009-12-29 19:14:58 +00:00
|
|
|
|
2018-01-19 09:35:39 +00:00
|
|
|
assert.ifError = function ifError(err) {
|
2018-01-23 13:07:18 +00:00
|
|
|
if (err !== null && err !== undefined) {
|
2018-01-19 09:35:39 +00:00
|
|
|
let message = 'ifError got unwanted exception: ';
|
|
|
|
if (typeof err === 'object' && typeof err.message === 'string') {
|
|
|
|
if (err.message.length === 0 && err.constructor) {
|
|
|
|
message += err.constructor.name;
|
|
|
|
} else {
|
|
|
|
message += err.message;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
message += inspect(err);
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:11:22 +00:00
|
|
|
const newErr = new AssertionError({
|
2018-01-19 09:35:39 +00:00
|
|
|
actual: err,
|
|
|
|
expected: null,
|
|
|
|
operator: 'ifError',
|
|
|
|
message,
|
|
|
|
stackStartFn: ifError
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure we actually have a stack trace!
|
|
|
|
const origStack = err.stack;
|
|
|
|
|
|
|
|
if (typeof origStack === 'string') {
|
|
|
|
// This will remove any duplicated frames from the error frames taken
|
|
|
|
// from within `ifError` and add the original error frames to the newly
|
|
|
|
// created ones.
|
|
|
|
const tmp2 = origStack.split('\n');
|
|
|
|
tmp2.shift();
|
|
|
|
// Filter all frames existing in err.stack.
|
|
|
|
let tmp1 = newErr.stack.split('\n');
|
|
|
|
for (var i = 0; i < tmp2.length; i++) {
|
|
|
|
// Find the first occurrence of the frame.
|
|
|
|
const pos = tmp1.indexOf(tmp2[i]);
|
|
|
|
if (pos !== -1) {
|
|
|
|
// Only keep new frames.
|
|
|
|
tmp1 = tmp1.slice(0, pos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newErr.stack = `${tmp1.join('\n')}\n${tmp2.join('\n')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw newErr;
|
|
|
|
}
|
|
|
|
};
|
2017-11-13 20:15:24 +00:00
|
|
|
|
|
|
|
// Expose a strict only variant of assert
|
2017-12-09 22:20:07 +00:00
|
|
|
function strict(...args) {
|
2018-03-12 03:48:01 +00:00
|
|
|
innerOk(strict, args.length, ...args);
|
2017-11-13 20:15:24 +00:00
|
|
|
}
|
|
|
|
assert.strict = Object.assign(strict, assert, {
|
|
|
|
equal: assert.strictEqual,
|
|
|
|
deepEqual: assert.deepStrictEqual,
|
|
|
|
notEqual: assert.notStrictEqual,
|
|
|
|
notDeepEqual: assert.notDeepStrictEqual
|
|
|
|
});
|
|
|
|
assert.strict.strict = assert.strict;
|