itns-sidechain/test/util/assert.js

216 lines
4.8 KiB
JavaScript
Raw Normal View History

2017-08-10 11:17:10 -07:00
'use strict';
const _assert = require('assert');
const util = require('util');
const assert = function assert(value, message) {
if (!value) {
throw new assert.AssertionError({
message,
actual: value,
expected: true,
operator: '==',
stackStartFunction: assert
});
}
2017-08-10 11:17:10 -07:00
};
Object.setPrototypeOf(assert, _assert);
2017-08-11 18:25:54 -07:00
assert.typeOf = function typeOf(value, expected, message) {
_isString(expected, '`expected` must be a string.', typeOf);
const actual = _typeOf(value);
if (actual !== expected) {
2017-08-11 18:25:54 -07:00
throw new assert.AssertionError({
message,
actual,
expected,
operator: 'typeof ==',
2017-08-11 18:25:54 -07:00
stackStartFunction: typeOf
});
}
};
assert.notTypeOf = function notTypeOf(value, expected, message) {
_isString(expected, '`expected` must be a string.', notTypeOf);
const actual = _typeOf(value);
if (actual === expected) {
2017-08-11 18:25:54 -07:00
throw new assert.AssertionError({
message,
actual,
expected,
operator: 'typeof !=',
2017-08-11 18:25:54 -07:00
stackStartFunction: notTypeOf
});
}
};
assert.instanceOf = function instanceOf(object, parent, message) {
_isFunction(parent, '`parent` must be a constructor.', instanceOf);
2017-08-11 18:25:54 -07:00
if (!(object instanceof parent)) {
throw new assert.AssertionError({
message,
actual: _getConstructorName(object),
expected: _getFunctionName(parent),
operator: 'instanceof',
2017-08-11 18:25:54 -07:00
stackStartFunction: instanceOf
});
}
};
assert.notInstanceOf = function notInstanceOf(object, parent, message) {
_isFunction(parent, '`parent` must be a constructor.', notInstanceOf);
2017-08-11 18:25:54 -07:00
if (object instanceof parent) {
throw new assert.AssertionError({
message,
actual: _getConstructorName(object),
expected: _getFunctionName(parent),
operator: 'not instanceof',
2017-08-11 18:25:54 -07:00
stackStartFunction: notInstanceOf
});
}
};
2017-08-10 11:17:10 -07:00
assert.bufferEqual = function bufferEqual(actual, expected, message) {
_isBuffer(actual, '`actual` must be a buffer.', bufferEqual);
_isBuffer(expected, '`expected` must be a buffer.', bufferEqual);
2017-08-11 18:25:54 -07:00
if (actual !== expected && !actual.equals(expected)) {
2017-08-10 11:17:10 -07:00
throw new assert.AssertionError({
message,
2017-08-10 11:17:10 -07:00
actual: actual.toString('hex'),
expected: expected.toString('hex'),
operator: '===',
stackStartFunction: bufferEqual
});
}
};
assert.notBufferEqual = function notBufferEqual(actual, expected, message) {
_isBuffer(actual, '`actual` must be a buffer.', notBufferEqual);
_isBuffer(expected, '`expected` must be a buffer.', notBufferEqual);
2017-08-10 11:17:10 -07:00
if (actual === expected || actual.equals(expected)) {
2017-08-10 11:17:10 -07:00
throw new assert.AssertionError({
message,
2017-08-10 11:17:10 -07:00
actual: actual.toString('hex'),
expected: expected.toString('hex'),
operator: '!==',
stackStartFunction: notBufferEqual
});
}
};
function _isString(value, message, stackStartFunction) {
if (typeof value !== 'string') {
throw new assert.AssertionError({
message,
actual: _typeOf(value),
expected: 'string',
operator: 'typeof ==',
stackStartFunction
});
}
}
function _isFunction(value, message, stackStartFunction) {
if (typeof value !== 'function') {
throw new assert.AssertionError({
message,
actual: _typeOf(value),
expected: 'function',
operator: 'typeof ==',
stackStartFunction
});
}
}
function _isBuffer(value, message, stackStartFunction) {
if (!Buffer.isBuffer(value)) {
throw new assert.AssertionError({
message,
actual: _typeOf(value),
expected: 'buffer',
operator: 'typeof ==',
stackStartFunction
});
}
}
2017-08-11 18:25:54 -07:00
function _typeOf(value) {
const type = typeof value;
switch (type) {
case 'object':
if (value === null)
return 'null';
2017-08-10 11:17:10 -07:00
if (Array.isArray(value))
return 'array';
2017-08-10 11:17:10 -07:00
if (Buffer.isBuffer(value))
return 'buffer';
2017-08-10 11:17:10 -07:00
if (ArrayBuffer.isView(value))
return 'arraybuffer';
2017-08-10 11:17:10 -07:00
if (util.isError(value))
return 'error';
2017-08-10 11:17:10 -07:00
if (util.isDate(value))
return 'date';
if (util.isRegExp(value))
return 'regexp';
break;
case 'number':
if (!isFinite(value))
return 'nan';
break;
}
2017-08-10 11:17:10 -07:00
return type;
2017-08-10 11:17:10 -07:00
}
function _getConstructorName(object) {
if (object === undefined)
return 'undefined';
2017-08-11 18:25:54 -07:00
if (object === null)
return 'null';
const proto = Object.getPrototypeOf(object);
2017-08-11 18:25:54 -07:00
// Should never happen.
if (proto === undefined)
throw new Error('Bad prototype.');
2017-08-11 18:25:54 -07:00
// Inherited from `null`.
if (proto === null)
2017-08-11 18:25:54 -07:00
return 'Null';
// Someone overwrote their
// constructor property?
if (!proto.constructor)
return 'Object';
// Non-named constructor function.
if (!proto.constructor.name)
return 'Unknown';
return proto.constructor.name;
}
2017-08-11 18:25:54 -07:00
function _getFunctionName(func) {
return func.name || 'Unknown';
2017-08-11 18:25:54 -07:00
}
2017-08-10 11:17:10 -07:00
module.exports = assert;