node/test/parallel/test-validators.js
Denys Otrishko 6914b3798c test: add tests for validateNumber/validateString
PR-URL: https://github.com/nodejs/node/pull/34672
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Pranshu Srivastava <rexagod@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2020-08-09 14:51:08 -07:00

110 lines
2.6 KiB
JavaScript

// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const {
validateArray,
validateBoolean,
validateInteger,
validateNumber,
validateObject,
validateString,
} = require('internal/validators');
const { MAX_SAFE_INTEGER, MIN_SAFE_INTEGER } = Number;
const outOfRangeError = {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
};
const invalidArgTypeError = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
};
const invalidArgValueError = {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
};
{
// validateInteger tests.
// validateInteger() defaults to validating safe integers.
validateInteger(MAX_SAFE_INTEGER, 'foo');
validateInteger(MIN_SAFE_INTEGER, 'foo');
assert.throws(() => {
validateInteger(MAX_SAFE_INTEGER + 1, 'foo');
}, outOfRangeError);
assert.throws(() => {
validateInteger(MIN_SAFE_INTEGER - 1, 'foo');
}, outOfRangeError);
// validateInteger() works with unsafe integers.
validateInteger(MAX_SAFE_INTEGER + 1, 'foo', 0, MAX_SAFE_INTEGER + 1);
validateInteger(MIN_SAFE_INTEGER - 1, 'foo', MIN_SAFE_INTEGER - 1);
}
{
// validateArray tests.
validateArray([], 'foo');
validateArray([1, 2, 3], 'foo');
[undefined, null, true, false, 0, 0.0, 42, '', 'string', {}]
.forEach((val) => {
assert.throws(() => {
validateArray(val, 'foo');
}, invalidArgTypeError);
});
validateArray([1], 'foo', { minLength: 1 });
assert.throws(() => {
validateArray([], 'foo', { minLength: 1 });
}, invalidArgValueError);
}
{
// validateBoolean tests.
validateBoolean(true, 'foo');
validateBoolean(false, 'foo');
[undefined, null, 0, 0.0, 42, '', 'string', {}, []].forEach((val) => {
assert.throws(() => {
validateBoolean(val, 'foo');
}, invalidArgTypeError);
});
}
{
// validateObject tests.
validateObject({}, 'foo');
validateObject({ a: 42, b: 'foo' }, 'foo');
[undefined, null, true, false, 0, 0.0, 42, '', 'string', []]
.forEach((val) => {
assert.throws(() => {
validateObject(val, 'foo');
}, invalidArgTypeError);
});
validateObject(null, 'foo', { nullable: true });
}
{
// validateString type validation.
[
-1, {}, [], false, true,
1, Infinity, -Infinity, NaN,
undefined, null, 1.1
].forEach((i) => assert.throws(() => validateString(i, 'name'), {
code: 'ERR_INVALID_ARG_TYPE'
}));
}
{
// validateNumber type validation.
[
'a', {}, [], false, true,
undefined, null, '', ' ', '0x',
'-0x1', '-0o1', '-0b1', '0o', '0b'
].forEach((i) => assert.throws(() => validateNumber(i, 'name'), {
code: 'ERR_INVALID_ARG_TYPE'
}));
}