2022-10-17 18:02:01 +07:00
|
|
|
|
/* eslint-disable @typescript-eslint/no-empty-function */
|
2022-06-11 17:44:01 +07:00
|
|
|
|
import {Buffer} from 'node:buffer';
|
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
|
import net from 'node:net';
|
|
|
|
|
|
import Stream from 'node:stream';
|
|
|
|
|
|
import {inspect} from 'node:util';
|
2022-10-17 18:02:01 +07:00
|
|
|
|
import test, {type ExecutionContext} from 'ava';
|
2018-03-28 01:24:25 +07:00
|
|
|
|
import {JSDOM} from 'jsdom';
|
2018-05-03 05:22:32 +02:00
|
|
|
|
import {Subject, Observable} from 'rxjs';
|
2022-06-11 17:44:01 +07:00
|
|
|
|
import {temporaryFile} from 'tempy';
|
2023-07-23 15:35:23 +03:00
|
|
|
|
import {expectTypeOf} from 'expect-type';
|
2022-06-11 17:44:01 +07:00
|
|
|
|
import ZenObservable from 'zen-observable';
|
2022-10-17 18:02:01 +07:00
|
|
|
|
import is, {
|
|
|
|
|
|
assert,
|
2023-07-30 19:32:34 +08:00
|
|
|
|
type AssertionTypeDescription,
|
2025-06-06 19:58:46 +08:00
|
|
|
|
type Predicate,
|
2022-10-17 18:02:01 +07:00
|
|
|
|
type Primitive,
|
|
|
|
|
|
type TypedArray,
|
|
|
|
|
|
type TypeName,
|
2025-09-12 03:58:08 +07:00
|
|
|
|
type UrlString,
|
2022-10-17 18:02:01 +07:00
|
|
|
|
} from '../source/index.js';
|
2025-06-06 19:58:46 +08:00
|
|
|
|
import {keysOf} from '../source/utilities.js';
|
2017-09-22 00:44:27 +07:00
|
|
|
|
|
2018-03-28 01:24:25 +07:00
|
|
|
|
class PromiseSubclassFixture<T> extends Promise<T> {}
|
2017-11-06 16:26:59 +01:00
|
|
|
|
class ErrorSubclassFixture extends Error {}
|
2017-09-22 00:44:27 +07:00
|
|
|
|
|
2018-03-28 01:24:25 +07:00
|
|
|
|
const {window} = new JSDOM();
|
|
|
|
|
|
const {document} = window;
|
2017-10-07 09:19:11 -07:00
|
|
|
|
|
2023-06-10 11:43:41 +03:00
|
|
|
|
const structuredClone = globalThis.structuredClone ?? (x => x);
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
type Test = Readonly<{
|
2018-11-02 10:43:51 +00:00
|
|
|
|
fixtures: unknown[];
|
2019-04-28 03:15:11 -05:00
|
|
|
|
typename?: TypeName;
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription?: AssertionTypeDescription;
|
2025-06-06 19:58:46 +08:00
|
|
|
|
}>;
|
|
|
|
|
|
|
|
|
|
|
|
// Every entry should be unique and belongs in the most specific type for that entry
|
|
|
|
|
|
const reusableFixtures = {
|
|
|
|
|
|
asyncFunction: [async function () {}, async () => {}],
|
|
|
|
|
|
asyncGeneratorFunction: [
|
|
|
|
|
|
async function * () {},
|
|
|
|
|
|
async function * () {
|
|
|
|
|
|
yield 4;
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
boundFunction: [() => {}, function () {}.bind(null)], // eslint-disable-line no-extra-bind
|
|
|
|
|
|
buffer: [Buffer.from('🦄')],
|
|
|
|
|
|
emptyArray: [[], new Array()], // eslint-disable-line @typescript-eslint/no-array-constructor
|
|
|
|
|
|
emptyMap: [new Map()],
|
|
|
|
|
|
emptySet: [new Set()],
|
|
|
|
|
|
emptyString: ['', String()],
|
|
|
|
|
|
function: [
|
|
|
|
|
|
function foo() {}, // eslint-disable-line func-names
|
|
|
|
|
|
function () {},
|
|
|
|
|
|
],
|
|
|
|
|
|
generatorFunction: [
|
|
|
|
|
|
function * () {},
|
|
|
|
|
|
function * () {
|
|
|
|
|
|
yield 4;
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
infinite: [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
|
|
|
|
|
|
integer: [0, -0, 6],
|
|
|
|
|
|
nativePromise: [Promise.resolve(), PromiseSubclassFixture.resolve()],
|
|
|
|
|
|
number: [1.4],
|
|
|
|
|
|
numericString: ['5', '-3.2', 'Infinity', '0x56'],
|
|
|
|
|
|
plainObject: [
|
|
|
|
|
|
{x: 1},
|
|
|
|
|
|
Object.create(null),
|
|
|
|
|
|
new Object(), // eslint-disable-line no-object-constructor
|
|
|
|
|
|
structuredClone({x: 1}),
|
|
|
|
|
|
structuredClone(Object.create(null)),
|
|
|
|
|
|
structuredClone(new Object()), // eslint-disable-line no-object-constructor
|
|
|
|
|
|
],
|
|
|
|
|
|
promise: [Object.create({then() {}, catch() {}})], // eslint-disable-line unicorn/no-thenable
|
|
|
|
|
|
safeInteger: [(2 ** 53) - 1, -(2 ** 53) + 1],
|
|
|
|
|
|
} as const satisfies Partial<{[K in keyof typeof is]: unknown[]}>;
|
|
|
|
|
|
|
|
|
|
|
|
const primitiveTypes = {
|
|
|
|
|
|
undefined: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
undefined,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'undefined',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
null: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
null,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'null',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
string: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
'🦄',
|
|
|
|
|
|
'hello world',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.emptyString,
|
|
|
|
|
|
...reusableFixtures.numericString,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'string',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
emptyString: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.emptyString],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'string',
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'empty string',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
number: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.number,
|
|
|
|
|
|
...reusableFixtures.infinite,
|
|
|
|
|
|
...reusableFixtures.integer,
|
|
|
|
|
|
...reusableFixtures.safeInteger,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'number',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
bigint: {
|
2019-11-07 15:56:02 +07:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
1n,
|
|
|
|
|
|
0n,
|
|
|
|
|
|
-0n,
|
2022-06-11 17:44:01 +07:00
|
|
|
|
BigInt('1234'),
|
2019-11-07 15:56:02 +07:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'bigint',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
boolean: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
true, false,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'boolean',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
numericString: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.numericString],
|
|
|
|
|
|
typename: 'string',
|
|
|
|
|
|
typeDescription: 'string with a number',
|
|
|
|
|
|
},
|
|
|
|
|
|
nan: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
NaN, // eslint-disable-line unicorn/prefer-number-properties
|
|
|
|
|
|
Number.NaN,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2025-06-06 19:58:46 +08:00
|
|
|
|
typename: 'NaN',
|
|
|
|
|
|
typeDescription: 'NaN',
|
|
|
|
|
|
},
|
|
|
|
|
|
nullOrUndefined: {
|
2018-11-01 13:21:49 +02:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
null,
|
|
|
|
|
|
undefined,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2025-06-06 19:58:46 +08:00
|
|
|
|
typeDescription: 'null or undefined',
|
|
|
|
|
|
},
|
|
|
|
|
|
integer: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.integer, ...reusableFixtures.safeInteger],
|
|
|
|
|
|
typename: 'number',
|
|
|
|
|
|
typeDescription: 'integer',
|
|
|
|
|
|
},
|
|
|
|
|
|
safeInteger: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.integer, ...reusableFixtures.safeInteger],
|
|
|
|
|
|
typename: 'number',
|
|
|
|
|
|
typeDescription: 'integer',
|
|
|
|
|
|
},
|
|
|
|
|
|
infinite: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.infinite],
|
|
|
|
|
|
typename: 'number',
|
|
|
|
|
|
typeDescription: 'infinite number',
|
|
|
|
|
|
},
|
|
|
|
|
|
} as const satisfies Partial<{[K in keyof typeof is]: Test}>;
|
|
|
|
|
|
|
|
|
|
|
|
const objectTypes = {
|
|
|
|
|
|
symbol: {
|
|
|
|
|
|
fixtures: [
|
|
|
|
|
|
Symbol('🦄'),
|
|
|
|
|
|
],
|
|
|
|
|
|
typename: 'symbol',
|
|
|
|
|
|
},
|
|
|
|
|
|
array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
[1, 2],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
Array.from({length: 2}),
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.emptyArray,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
emptyArray: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.emptyArray],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Array',
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'empty array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
function: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.asyncFunction,
|
|
|
|
|
|
...reusableFixtures.asyncGeneratorFunction,
|
|
|
|
|
|
...reusableFixtures.boundFunction,
|
|
|
|
|
|
...reusableFixtures.function,
|
|
|
|
|
|
...reusableFixtures.generatorFunction,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Function',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
buffer: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.buffer],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Buffer',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
blob: {
|
2022-02-27 15:16:25 +08:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new window.Blob(),
|
2022-02-27 15:16:25 +08:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Blob',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
object: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
Object.create({x: 1}),
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.plainObject,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Object',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
regExp: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
/\w/,
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new RegExp('\\w'), // eslint-disable-line prefer-regex-literals
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'RegExp',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
date: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Date(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Date',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
error: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
new Error('🦄'),
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new ErrorSubclassFixture(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Error',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
nativePromise: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.nativePromise],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Promise',
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'native Promise',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
promise: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.nativePromise,
|
|
|
|
|
|
...reusableFixtures.promise,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2025-06-06 19:58:46 +08:00
|
|
|
|
typename: 'Promise',
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typeDescription: 'Promise',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
generator: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2017-11-07 10:23:00 +07:00
|
|
|
|
(function * () {
|
|
|
|
|
|
yield 4;
|
2022-06-11 17:44:01 +07:00
|
|
|
|
})(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Generator',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
asyncGenerator: {
|
2020-01-29 18:35:58 +01:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
(async function * () {
|
|
|
|
|
|
yield 4;
|
2022-06-11 17:44:01 +07:00
|
|
|
|
})(),
|
2020-01-29 18:35:58 +01:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'AsyncGenerator',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
generatorFunction: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.generatorFunction],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Function',
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typeDescription: 'GeneratorFunction',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
asyncGeneratorFunction: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.asyncGeneratorFunction],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Function',
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typeDescription: 'AsyncGeneratorFunction',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
asyncFunction: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.asyncFunction],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Function',
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typeDescription: 'AsyncFunction',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
boundFunction: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.boundFunction, ...reusableFixtures.asyncFunction],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Function',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
map: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Map([['one', '1']]),
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.emptyMap,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Map',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
emptyMap: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.emptyMap],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Map',
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'empty map',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
set: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Set(['one']),
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.emptySet,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Set',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
emptySet: {
|
|
|
|
|
|
fixtures: [...reusableFixtures.emptySet],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Set',
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'empty set',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
weakSet: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new WeakSet(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'WeakSet',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
weakRef: {
|
|
|
|
|
|
fixtures: [
|
|
|
|
|
|
new window.WeakRef({}),
|
|
|
|
|
|
],
|
2022-06-13 15:05:26 +08:00
|
|
|
|
typename: 'WeakRef',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
weakMap: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new WeakMap(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'WeakMap',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
int8Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Int8Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Int8Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
uint8Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Uint8Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Uint8Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
uint8ClampedArray: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Uint8ClampedArray(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Uint8ClampedArray',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
int16Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Int16Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Int16Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
uint16Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Uint16Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Uint16Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
int32Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Int32Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Int32Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
uint32Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Uint32Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Uint32Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
float32Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Float32Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Float32Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
float64Array: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Float64Array(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Float64Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
bigInt64Array: {
|
2019-11-07 15:56:02 +07:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new BigInt64Array(),
|
2019-11-07 15:56:02 +07:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'BigInt64Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
bigUint64Array: {
|
2019-11-07 15:56:02 +07:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new BigUint64Array(),
|
2019-11-07 15:56:02 +07:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'BigUint64Array',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
arrayBuffer: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new ArrayBuffer(10),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'ArrayBuffer',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
dataView: {
|
2017-12-11 21:38:53 +01:00
|
|
|
|
fixtures: [
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new DataView(new ArrayBuffer(10)),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'DataView',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
plainObject: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
...reusableFixtures.plainObject,
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Object',
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'plain object',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
htmlElement: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
'div',
|
|
|
|
|
|
'input',
|
|
|
|
|
|
'span',
|
|
|
|
|
|
'img',
|
|
|
|
|
|
'canvas',
|
2022-06-11 17:44:01 +07:00
|
|
|
|
'script',
|
|
|
|
|
|
]
|
2023-08-10 22:06:46 +08:00
|
|
|
|
.map(fixture => document.createElement(fixture)),
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'HTMLElement',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
observable: {
|
2018-12-13 16:52:55 +01:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
new Observable(),
|
|
|
|
|
|
new Subject(),
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new ZenObservable(() => {}),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2022-06-11 17:44:01 +07:00
|
|
|
|
typename: 'Observable',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
nodeStream: {
|
2017-11-19 15:16:06 -05:00
|
|
|
|
fixtures: [
|
|
|
|
|
|
fs.createReadStream('readme.md'),
|
2022-06-11 17:44:01 +07:00
|
|
|
|
fs.createWriteStream(temporaryFile()),
|
2017-11-19 15:16:06 -05:00
|
|
|
|
new net.Socket(),
|
|
|
|
|
|
new Stream.Duplex(),
|
|
|
|
|
|
new Stream.PassThrough(),
|
|
|
|
|
|
new Stream.Readable(),
|
|
|
|
|
|
new Stream.Transform(),
|
|
|
|
|
|
new Stream.Stream(),
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new Stream.Writable(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2020-06-20 16:28:47 +09:00
|
|
|
|
typename: 'Object',
|
2023-07-30 19:32:34 +08:00
|
|
|
|
typeDescription: 'Node.js Stream',
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
formData: {
|
2017-11-06 16:26:59 +01:00
|
|
|
|
fixtures: [
|
2025-06-06 19:58:46 +08:00
|
|
|
|
new window.FormData(),
|
2019-04-28 03:15:11 -05:00
|
|
|
|
],
|
2025-06-06 19:58:46 +08:00
|
|
|
|
typename: 'FormData',
|
|
|
|
|
|
},
|
|
|
|
|
|
} as const satisfies Partial<{[K in keyof typeof is]: Test}>;
|
|
|
|
|
|
|
|
|
|
|
|
const types = {
|
|
|
|
|
|
...objectTypes,
|
|
|
|
|
|
...primitiveTypes,
|
|
|
|
|
|
} as const satisfies Partial<{[K in keyof typeof is]: Test}>;
|
|
|
|
|
|
|
|
|
|
|
|
type TypeNameWithFixture = keyof typeof types;
|
|
|
|
|
|
|
|
|
|
|
|
const subClasses = new Map<TypeNameWithFixture, TypeNameWithFixture[]>([
|
|
|
|
|
|
['uint8Array', ['buffer']], // It's too hard to differentiate the two
|
|
|
|
|
|
['object', keysOf(objectTypes)],
|
2017-09-22 00:44:27 +07:00
|
|
|
|
]);
|
|
|
|
|
|
|
2017-11-06 16:26:59 +01:00
|
|
|
|
// This ensures a certain method matches only the types it's supposed to and none of the other methods' types
|
2025-06-06 19:58:46 +08:00
|
|
|
|
const exclusivelyTyped = test.macro({
|
|
|
|
|
|
exec(t: ExecutionContext, type: TypeNameWithFixture) {
|
|
|
|
|
|
const {fixtures, typeDescription, typename} = types[type] as Test;
|
|
|
|
|
|
const valueType = typeDescription ?? typename ?? 'unspecified';
|
2017-11-06 16:26:59 +01:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
const testAssert: (value: unknown) => never | void = assert[type];
|
|
|
|
|
|
const testIs: Predicate = is[type];
|
2017-11-06 16:26:59 +01:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
for (const fixture of fixtures) {
|
|
|
|
|
|
t.true(testIs(fixture), `Value: ${inspect(fixture)}`);
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
testAssert(fixture);
|
|
|
|
|
|
});
|
2017-11-06 16:26:59 +01:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
if (typename) {
|
|
|
|
|
|
t.is<TypeName, TypeName>(is(fixture), typename);
|
|
|
|
|
|
}
|
2017-09-22 00:44:27 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
for (const key of keysOf(types).filter(key => key !== type)) {
|
|
|
|
|
|
if (subClasses.has(type) && subClasses.get(type)?.includes(key)) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2017-09-22 00:44:27 +07:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
for (let i = 0; i < types[key].fixtures.length; i += 1) {
|
|
|
|
|
|
const fixture: unknown = types[key].fixtures[i];
|
2020-02-22 02:01:58 +07:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
if (fixtures.includes(fixture)) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
t.false(testIs(fixture), `${key}.fixture[${i}]: ${inspect(fixture)} should not be ${type}`);
|
2020-02-22 02:01:58 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
testAssert(fixture);
|
|
|
|
|
|
}, {
|
2023-07-30 19:32:34 +08:00
|
|
|
|
message: `Expected value which is \`${valueType}\`, received value of type \`${is(fixture)}\`.`,
|
2020-02-22 02:01:58 +07:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2017-09-22 00:44:27 +07:00
|
|
|
|
}
|
2025-06-06 19:58:46 +08:00
|
|
|
|
},
|
|
|
|
|
|
title(_, type: TypeNameWithFixture) {
|
|
|
|
|
|
return `is.${type}`;
|
|
|
|
|
|
},
|
2017-09-22 00:44:27 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
for (const type of keysOf(types)) {
|
|
|
|
|
|
test(exclusivelyTyped, type);
|
|
|
|
|
|
}
|
2017-09-22 00:44:27 +07:00
|
|
|
|
|
2023-07-14 23:26:30 +01:00
|
|
|
|
test('is.positiveNumber', t => {
|
|
|
|
|
|
t.true(is.positiveNumber(6));
|
|
|
|
|
|
t.true(is.positiveNumber(1.4));
|
|
|
|
|
|
t.true(is.positiveNumber(Number.POSITIVE_INFINITY));
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.positiveNumber(6);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.positiveNumber(1.4);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.positiveNumber(Number.POSITIVE_INFINITY);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.false(is.positiveNumber(0));
|
|
|
|
|
|
t.false(is.positiveNumber(-0));
|
|
|
|
|
|
t.false(is.positiveNumber(-6));
|
|
|
|
|
|
t.false(is.positiveNumber(-1.4));
|
|
|
|
|
|
t.false(is.positiveNumber(Number.NEGATIVE_INFINITY));
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.positiveNumber(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.positiveNumber(-0);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.positiveNumber(-6);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.positiveNumber(-1.4);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.positiveNumber(Number.NEGATIVE_INFINITY);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('is.negativeNumber', t => {
|
|
|
|
|
|
t.true(is.negativeNumber(-6));
|
|
|
|
|
|
t.true(is.negativeNumber(-1.4));
|
|
|
|
|
|
t.true(is.negativeNumber(Number.NEGATIVE_INFINITY));
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.negativeNumber(-6);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.negativeNumber(-1.4);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.negativeNumber(Number.NEGATIVE_INFINITY);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.false(is.negativeNumber(0));
|
|
|
|
|
|
t.false(is.negativeNumber(-0));
|
|
|
|
|
|
t.false(is.negativeNumber(6));
|
|
|
|
|
|
t.false(is.negativeNumber(1.4));
|
|
|
|
|
|
t.false(is.negativeNumber(Number.POSITIVE_INFINITY));
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.negativeNumber(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.negativeNumber(-0);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.negativeNumber(6);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.negativeNumber(1.4);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.negativeNumber(Number.POSITIVE_INFINITY);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.numericString supplemental', t => {
|
2018-11-02 10:43:51 +00:00
|
|
|
|
t.false(is.numericString(''));
|
2020-04-18 03:25:02 +05:30
|
|
|
|
t.false(is.numericString(' '));
|
|
|
|
|
|
t.false(is.numericString(' \t\t\n'));
|
2018-11-02 10:43:51 +00:00
|
|
|
|
t.false(is.numericString(1));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.numericString('');
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.numericString(1);
|
|
|
|
|
|
});
|
2018-11-01 13:21:49 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.array supplemental', t => {
|
2020-07-25 10:11:59 +03:00
|
|
|
|
t.true(is.array([1, 2, 3], is.number));
|
|
|
|
|
|
t.false(is.array([1, '2', 3], is.number));
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.array([1, 2], assert.number);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.array([1, '2'], assert.number);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
const x: unknown[] = [1, 2, 3];
|
|
|
|
|
|
assert.array(x, assert.number);
|
2024-06-25 08:08:48 +08:00
|
|
|
|
x[0]?.toFixed(0);
|
2020-07-25 10:11:59 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
const x: unknown[] = [1, 2, 3];
|
|
|
|
|
|
if (is.array<number>(x, is.number)) {
|
2024-06-25 08:08:48 +08:00
|
|
|
|
x[0]?.toFixed(0);
|
2020-07-25 10:11:59 +03:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2024-09-06 17:52:56 +02:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.array([1, '2'], assert.number, 'Expected numbers');
|
|
|
|
|
|
}, {message: /Expected numbers/});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.boundFunction supplemental', t => {
|
2019-03-31 20:41:19 +07:00
|
|
|
|
t.false(is.boundFunction(function () {})); // eslint-disable-line prefer-arrow-callback
|
2020-01-22 18:47:01 +07:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.boundFunction(function () {}); // eslint-disable-line prefer-arrow-callback
|
|
|
|
|
|
});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.asyncFunction supplemental', t => {
|
2019-11-07 16:28:20 +07:00
|
|
|
|
const fixture = async () => {};
|
|
|
|
|
|
if (is.asyncFunction(fixture)) {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
t.true(is.function(fixture().then));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
2024-06-26 20:30:00 +08:00
|
|
|
|
assert.function(fixture().then);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2019-11-07 16:28:20 +07:00
|
|
|
|
}
|
2019-03-31 21:50:37 +07:00
|
|
|
|
});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.asyncGenerator supplemental', t => {
|
2020-01-29 18:35:58 +01:00
|
|
|
|
const fixture = (async function * () {
|
|
|
|
|
|
yield 4;
|
|
|
|
|
|
})();
|
|
|
|
|
|
if (is.asyncGenerator(fixture)) {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
t.true(is.function(fixture.next));
|
2020-01-29 18:35:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.asyncGeneratorFunction supplemental', t => {
|
2020-01-29 18:35:58 +01:00
|
|
|
|
const fixture = async function * () {
|
|
|
|
|
|
yield 4;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (is.asyncGeneratorFunction(fixture)) {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
t.true(is.function(fixture().next));
|
2020-01-29 18:35:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-01-17 00:55:40 -05:00
|
|
|
|
test('is.enumCase', t => {
|
|
|
|
|
|
enum NonNumericalEnum {
|
|
|
|
|
|
Key1 = 'key1',
|
|
|
|
|
|
Key2 = 'key2',
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
t.true(is.enumCase('key1', NonNumericalEnum));
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.enumCase('key1', NonNumericalEnum);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.false(is.enumCase('invalid', NonNumericalEnum));
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.enumCase('invalid', NonNumericalEnum);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-12-09 11:55:08 -05:00
|
|
|
|
test('is.directInstanceOf', t => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
const error = new Error('fixture');
|
2017-12-09 11:55:08 -05:00
|
|
|
|
const errorSubclass = new ErrorSubclassFixture();
|
|
|
|
|
|
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.directInstanceOf(error, Error));
|
|
|
|
|
|
t.true(is.directInstanceOf(errorSubclass, ErrorSubclassFixture));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.directInstanceOf(error, Error);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.directInstanceOf(errorSubclass, ErrorSubclassFixture);
|
|
|
|
|
|
});
|
2017-12-09 11:55:08 -05:00
|
|
|
|
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.directInstanceOf(error, ErrorSubclassFixture));
|
|
|
|
|
|
t.false(is.directInstanceOf(errorSubclass, Error));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.directInstanceOf(error, ErrorSubclassFixture);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.directInstanceOf(errorSubclass, Error);
|
|
|
|
|
|
});
|
2023-10-15 16:01:29 +07:00
|
|
|
|
|
|
|
|
|
|
t.false(is.directInstanceOf(undefined, Error));
|
|
|
|
|
|
t.false(is.directInstanceOf(null, Error));
|
2017-12-09 11:55:08 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
2018-07-10 12:04:20 +03:00
|
|
|
|
test('is.urlInstance', t => {
|
2019-11-07 15:56:02 +07:00
|
|
|
|
const url = new URL('https://example.com');
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.urlInstance(url));
|
|
|
|
|
|
t.false(is.urlInstance({}));
|
|
|
|
|
|
t.false(is.urlInstance(undefined));
|
|
|
|
|
|
t.false(is.urlInstance(null));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.urlInstance(url);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlInstance({});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlInstance(undefined);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlInstance(null);
|
|
|
|
|
|
});
|
2018-07-10 12:04:20 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
2018-12-13 17:52:21 +02:00
|
|
|
|
test('is.urlString', t => {
|
|
|
|
|
|
const url = 'https://example.com';
|
|
|
|
|
|
t.true(is.urlString(url));
|
2019-11-07 15:56:02 +07:00
|
|
|
|
t.false(is.urlString(new URL(url)));
|
2018-12-13 17:52:21 +02:00
|
|
|
|
t.false(is.urlString({}));
|
|
|
|
|
|
t.false(is.urlString(undefined));
|
|
|
|
|
|
t.false(is.urlString(null));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.urlString(url);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlString(new URL(url));
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlString({});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlString(undefined);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlString(null);
|
|
|
|
|
|
});
|
2018-12-13 17:52:21 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-12 03:58:08 +07:00
|
|
|
|
// Type test for urlString narrowing fix (issue #212)
|
|
|
|
|
|
// This test demonstrates that the fix allows proper type narrowing in both branches
|
|
|
|
|
|
(() => {
|
|
|
|
|
|
const value: unknown = 'test';
|
|
|
|
|
|
|
|
|
|
|
|
if (is.urlString(value)) {
|
|
|
|
|
|
// ✅ In true branch: value is narrowed to UrlString
|
|
|
|
|
|
expectTypeOf(value).toEqualTypeOf<UrlString>();
|
|
|
|
|
|
expectTypeOf(value).toMatchTypeOf<string>();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// ✅ In false branch: value remains unknown (not incorrectly narrowed)
|
|
|
|
|
|
expectTypeOf(value).toEqualTypeOf<unknown>();
|
|
|
|
|
|
|
|
|
|
|
|
// ✅ Manual narrowing to string still works
|
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
|
expectTypeOf(value).toEqualTypeOf<string>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
2017-10-20 14:49:52 +00:00
|
|
|
|
test('is.truthy', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.truthy('unicorn'));
|
|
|
|
|
|
t.true(is.truthy('🦄'));
|
|
|
|
|
|
t.true(is.truthy(new Set()));
|
|
|
|
|
|
t.true(is.truthy(Symbol('🦄')));
|
|
|
|
|
|
t.true(is.truthy(true));
|
2019-05-04 12:05:23 +03:00
|
|
|
|
t.true(is.truthy(1));
|
2025-06-06 19:58:46 +08:00
|
|
|
|
t.true(is.truthy(1n));
|
2019-11-07 15:56:02 +07:00
|
|
|
|
t.true(is.truthy(BigInt(1)));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy('unicorn');
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy('🦄');
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy(new Set());
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy(Symbol('🦄'));
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy(true);
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy(1);
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2022-06-11 17:44:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy(1n);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.truthy(BigInt(1));
|
|
|
|
|
|
});
|
2023-07-16 22:19:31 +08:00
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.truthy` narrow downs boolean type to `true`.
|
|
|
|
|
|
{
|
|
|
|
|
|
const booleans = [true, false];
|
|
|
|
|
|
const function_ = (value: true) => value;
|
|
|
|
|
|
assert.truthy(booleans[0]);
|
|
|
|
|
|
function_(booleans[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.truthy` excludes zero value from number type.
|
|
|
|
|
|
{
|
|
|
|
|
|
const bits: Array<0 | 1> = [1, 0, -0];
|
|
|
|
|
|
const function_ = (value: 1) => value;
|
|
|
|
|
|
assert.truthy(bits[0]);
|
|
|
|
|
|
function_(bits[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.truthy` excludes zero value from bigint type.
|
|
|
|
|
|
{
|
|
|
|
|
|
const bits: Array<0n | 1n> = [1n, 0n, -0n];
|
|
|
|
|
|
const function_ = (value: 1n) => value;
|
|
|
|
|
|
assert.truthy(bits[0]);
|
|
|
|
|
|
function_(bits[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.truthy` excludes empty string from string type.
|
|
|
|
|
|
{
|
|
|
|
|
|
const strings: Array<'nonEmpty' | ''> = ['nonEmpty', ''];
|
|
|
|
|
|
const function_ = (value: 'nonEmpty') => value;
|
|
|
|
|
|
assert.truthy(strings[0]);
|
|
|
|
|
|
function_(strings[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.truthy` excludes undefined from mixed type.
|
|
|
|
|
|
{
|
|
|
|
|
|
const maybeUndefineds = ['🦄', undefined];
|
|
|
|
|
|
const function_ = (value: string) => value;
|
|
|
|
|
|
assert.truthy(maybeUndefineds[0]);
|
|
|
|
|
|
function_(maybeUndefineds[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.truthy` excludes null from mixed type.
|
|
|
|
|
|
{
|
|
|
|
|
|
const maybeNulls = ['🦄', null];
|
|
|
|
|
|
const function_ = (value: string) => value;
|
|
|
|
|
|
assert.truthy(maybeNulls[0]);
|
|
|
|
|
|
function_(maybeNulls[0]);
|
|
|
|
|
|
}
|
2017-10-20 14:49:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('is.falsy', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.falsy(false));
|
|
|
|
|
|
t.true(is.falsy(0));
|
|
|
|
|
|
t.true(is.falsy(''));
|
|
|
|
|
|
t.true(is.falsy(null));
|
|
|
|
|
|
t.true(is.falsy(undefined));
|
2022-06-11 17:44:01 +07:00
|
|
|
|
t.true(is.falsy(Number.NaN));
|
|
|
|
|
|
t.true(is.falsy(0n));
|
2019-11-07 15:56:02 +07:00
|
|
|
|
t.true(is.falsy(BigInt(0)));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.falsy(false);
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.falsy(0);
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.falsy('');
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.falsy(null);
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.falsy(undefined);
|
|
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.falsy(Number.NaN);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2022-06-11 17:44:01 +07:00
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.falsy(0n);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.falsy(BigInt(0));
|
|
|
|
|
|
});
|
2023-07-16 22:19:31 +08:00
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.falsy` narrow downs boolean type to `false`.
|
|
|
|
|
|
{
|
|
|
|
|
|
const booleans = [false, true];
|
2024-06-25 08:08:48 +08:00
|
|
|
|
const function_ = (value?: false) => value;
|
2023-07-16 22:19:31 +08:00
|
|
|
|
assert.falsy(booleans[0]);
|
|
|
|
|
|
function_(booleans[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.falsy` narrow downs number type to `0`.
|
|
|
|
|
|
{
|
|
|
|
|
|
const bits = [0, -0, 1];
|
2024-06-25 08:08:48 +08:00
|
|
|
|
const function_ = (value?: 0) => value;
|
2023-07-16 22:19:31 +08:00
|
|
|
|
assert.falsy(bits[0]);
|
|
|
|
|
|
function_(bits[0]);
|
|
|
|
|
|
assert.falsy(bits[1]);
|
|
|
|
|
|
function_(bits[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.falsy` narrow downs bigint type to `0n`.
|
|
|
|
|
|
{
|
|
|
|
|
|
const bits = [0n, -0n, 1n];
|
2024-06-25 08:08:48 +08:00
|
|
|
|
const function_ = (value?: 0n) => value;
|
2023-07-16 22:19:31 +08:00
|
|
|
|
assert.falsy(bits[0]);
|
|
|
|
|
|
function_(bits[0]);
|
|
|
|
|
|
assert.falsy(bits[1]);
|
|
|
|
|
|
function_(bits[1]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.falsy` narrow downs string type to empty string.
|
|
|
|
|
|
{
|
|
|
|
|
|
const strings = ['', 'nonEmpty'];
|
2024-06-25 08:08:48 +08:00
|
|
|
|
const function_ = (value?: '') => value;
|
2023-07-16 22:19:31 +08:00
|
|
|
|
assert.falsy(strings[0]);
|
|
|
|
|
|
function_(strings[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.falsy` can narrow down mixed type to undefined.
|
|
|
|
|
|
{
|
|
|
|
|
|
const maybeUndefineds = [undefined, Symbol('🦄')];
|
|
|
|
|
|
const function_ = (value: undefined) => value;
|
|
|
|
|
|
assert.falsy(maybeUndefineds[0]);
|
|
|
|
|
|
function_(maybeUndefineds[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Checks that `assert.falsy` can narrow down mixed type to null.
|
|
|
|
|
|
{
|
|
|
|
|
|
const maybeNulls = [null, Symbol('🦄')];
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
2024-06-25 08:08:48 +08:00
|
|
|
|
const function_ = (value?: null) => value;
|
2023-07-16 22:19:31 +08:00
|
|
|
|
assert.falsy(maybeNulls[0]);
|
|
|
|
|
|
function_(maybeNulls[0]);
|
|
|
|
|
|
}
|
2017-10-20 14:49:52 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2017-09-22 00:44:27 +07:00
|
|
|
|
test('is.primitive', t => {
|
2020-06-20 16:28:47 +09:00
|
|
|
|
const primitives: Primitive[] = [
|
2017-09-22 00:44:27 +07:00
|
|
|
|
undefined,
|
|
|
|
|
|
null,
|
|
|
|
|
|
'🦄',
|
|
|
|
|
|
6,
|
2022-06-11 17:44:01 +07:00
|
|
|
|
Number.POSITIVE_INFINITY,
|
|
|
|
|
|
Number.NEGATIVE_INFINITY,
|
2017-09-22 00:44:27 +07:00
|
|
|
|
true,
|
|
|
|
|
|
false,
|
2022-06-11 17:44:01 +07:00
|
|
|
|
Symbol('🦄'),
|
2025-06-06 19:58:46 +08:00
|
|
|
|
6n,
|
2017-09-22 00:44:27 +07:00
|
|
|
|
];
|
|
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
for (const element of primitives) {
|
|
|
|
|
|
t.true(is.primitive(element));
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.primitive(element);
|
|
|
|
|
|
});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.integer supplemental', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.integer(1.4));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.integer(1.4);
|
|
|
|
|
|
});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.safeInteger supplemental', t => {
|
2019-03-31 20:41:19 +07:00
|
|
|
|
t.false(is.safeInteger(2 ** 53));
|
|
|
|
|
|
t.false(is.safeInteger(-(2 ** 53)));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.safeInteger(2 ** 53);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.safeInteger(-(2 ** 53));
|
|
|
|
|
|
});
|
2017-10-17 11:19:17 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2017-09-22 00:44:27 +07:00
|
|
|
|
test('is.iterable', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.iterable(''));
|
|
|
|
|
|
t.true(is.iterable([]));
|
|
|
|
|
|
t.true(is.iterable(new Map()));
|
|
|
|
|
|
t.false(is.iterable(null));
|
|
|
|
|
|
t.false(is.iterable(undefined));
|
|
|
|
|
|
t.false(is.iterable(0));
|
2022-06-11 17:44:01 +07:00
|
|
|
|
t.false(is.iterable(Number.NaN));
|
|
|
|
|
|
t.false(is.iterable(Number.POSITIVE_INFINITY));
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.iterable({}));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.iterable('');
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.iterable([]);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.iterable(new Map());
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.iterable(null);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.iterable(undefined);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.iterable(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.iterable(Number.NaN);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.iterable(Number.POSITIVE_INFINITY);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.iterable({});
|
|
|
|
|
|
});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
2019-11-07 15:56:02 +07:00
|
|
|
|
test('is.asyncIterable', t => {
|
|
|
|
|
|
t.true(is.asyncIterable({
|
2022-06-11 17:44:01 +07:00
|
|
|
|
[Symbol.asyncIterator]() {},
|
2019-11-07 15:56:02 +07:00
|
|
|
|
}));
|
2018-07-09 20:35:16 +03:00
|
|
|
|
|
2019-11-07 15:56:02 +07:00
|
|
|
|
t.false(is.asyncIterable(null));
|
|
|
|
|
|
t.false(is.asyncIterable(undefined));
|
|
|
|
|
|
t.false(is.asyncIterable(0));
|
2022-06-11 17:44:01 +07:00
|
|
|
|
t.false(is.asyncIterable(Number.NaN));
|
|
|
|
|
|
t.false(is.asyncIterable(Number.POSITIVE_INFINITY));
|
2019-11-07 15:56:02 +07:00
|
|
|
|
t.false(is.asyncIterable({}));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.asyncIterable({
|
2022-06-11 17:44:01 +07:00
|
|
|
|
[Symbol.asyncIterator]() {},
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncIterable(null);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncIterable(undefined);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncIterable(0);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.asyncIterable(Number.NaN);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.asyncIterable(Number.POSITIVE_INFINITY);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncIterable({});
|
|
|
|
|
|
});
|
2019-11-07 15:56:02 +07:00
|
|
|
|
});
|
2018-07-09 20:35:16 +03:00
|
|
|
|
|
2017-09-24 22:26:15 +03:00
|
|
|
|
test('is.class', t => {
|
2019-03-31 20:41:19 +07:00
|
|
|
|
class Foo {} // eslint-disable-line @typescript-eslint/no-extraneous-class
|
|
|
|
|
|
|
2017-09-24 22:26:15 +03:00
|
|
|
|
const classDeclarations = [
|
|
|
|
|
|
Foo,
|
2022-06-11 17:44:01 +07:00
|
|
|
|
class Bar extends Foo {},
|
2017-09-24 22:26:15 +03:00
|
|
|
|
];
|
|
|
|
|
|
|
2019-03-31 20:41:19 +07:00
|
|
|
|
for (const classDeclaration of classDeclarations) {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
t.true(is.class(classDeclaration));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
2024-06-26 20:30:00 +08:00
|
|
|
|
assert.class(classDeclaration);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2017-09-24 22:26:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-09-22 00:44:27 +07:00
|
|
|
|
test('is.typedArray', t => {
|
2020-06-20 16:28:47 +09:00
|
|
|
|
const typedArrays: TypedArray[] = [
|
2019-03-31 21:50:37 +07:00
|
|
|
|
new Int8Array(),
|
|
|
|
|
|
new Uint8Array(),
|
|
|
|
|
|
new Uint8ClampedArray(),
|
|
|
|
|
|
new Uint16Array(),
|
|
|
|
|
|
new Int32Array(),
|
|
|
|
|
|
new Uint32Array(),
|
|
|
|
|
|
new Float32Array(),
|
2019-11-07 15:56:02 +07:00
|
|
|
|
new Float64Array(),
|
|
|
|
|
|
new BigInt64Array(),
|
2022-06-11 17:44:01 +07:00
|
|
|
|
new BigUint64Array(),
|
2017-09-22 00:44:27 +07:00
|
|
|
|
];
|
|
|
|
|
|
|
2018-10-30 23:19:19 +07:00
|
|
|
|
for (const item of typedArrays) {
|
|
|
|
|
|
t.true(is.typedArray(item));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.typedArray(item);
|
|
|
|
|
|
});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.typedArray(new ArrayBuffer(1)));
|
|
|
|
|
|
t.false(is.typedArray([]));
|
|
|
|
|
|
t.false(is.typedArray({}));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.typedArray(new ArrayBuffer(1));
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.typedArray([]);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.typedArray({});
|
|
|
|
|
|
});
|
2017-09-22 00:44:27 +07:00
|
|
|
|
});
|
2017-09-25 20:20:06 +01:00
|
|
|
|
|
2017-10-26 09:30:17 -04:00
|
|
|
|
test('is.arrayLike', t => {
|
2019-03-31 20:41:19 +07:00
|
|
|
|
(function () {
|
|
|
|
|
|
t.true(is.arrayLike(arguments)); // eslint-disable-line prefer-rest-params
|
2017-10-26 09:30:17 -04:00
|
|
|
|
})();
|
2019-03-31 20:41:19 +07:00
|
|
|
|
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.arrayLike([]));
|
|
|
|
|
|
t.true(is.arrayLike('unicorn'));
|
2017-10-26 09:30:17 -04:00
|
|
|
|
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.arrayLike({}));
|
2019-03-31 20:41:19 +07:00
|
|
|
|
t.false(is.arrayLike(() => {}));
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.arrayLike(new Map()));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
|
|
|
|
|
(function () {
|
2022-06-13 15:05:26 +08:00
|
|
|
|
t.notThrows(function () {
|
2020-01-22 18:47:01 +07:00
|
|
|
|
assert.arrayLike(arguments); // eslint-disable-line prefer-rest-params
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
})();
|
|
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.arrayLike([]);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.arrayLike('unicorn');
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.arrayLike({});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.arrayLike(() => {});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.arrayLike(new Map());
|
|
|
|
|
|
});
|
2017-10-26 09:30:17 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2023-07-23 15:35:23 +03:00
|
|
|
|
test('is.tupleLike', t => {
|
|
|
|
|
|
(function () {
|
|
|
|
|
|
t.false(is.tupleLike(arguments, [])); // eslint-disable-line prefer-rest-params
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
t.true(is.tupleLike([], []));
|
|
|
|
|
|
t.true(is.tupleLike([1, '2', true, {}, [], undefined, null], [is.number, is.string, is.boolean, is.object, is.array, is.undefined, is.nullOrUndefined]));
|
|
|
|
|
|
t.false(is.tupleLike('unicorn', [is.string]));
|
|
|
|
|
|
|
|
|
|
|
|
t.false(is.tupleLike({}, []));
|
2023-08-07 08:50:03 +08:00
|
|
|
|
t.false(is.tupleLike(() => {}, [is.function]));
|
2023-07-23 15:35:23 +03:00
|
|
|
|
t.false(is.tupleLike(new Map(), [is.map]));
|
|
|
|
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
|
|
t.throws(function () {
|
|
|
|
|
|
assert.tupleLike(arguments, []); // eslint-disable-line prefer-rest-params
|
|
|
|
|
|
});
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.tupleLike([], []);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.tupleLike('unicorn', [is.string]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.tupleLike({}, [is.object]);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
assert.tupleLike(() => {}, [is.function]);
|
2023-07-23 15:35:23 +03:00
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.tupleLike(new Map(), [is.map]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
const tuple = [[false, 'unicorn'], 'string', true];
|
|
|
|
|
|
|
|
|
|
|
|
if (is.tupleLike(tuple, [is.array, is.string, is.boolean])) {
|
|
|
|
|
|
if (is.tupleLike(tuple[0], [is.boolean, is.string])) { // eslint-disable-line unicorn/no-lonely-if
|
|
|
|
|
|
const value = tuple[0][1];
|
|
|
|
|
|
expectTypeOf(value).toEqualTypeOf<string>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
const tuple = [{isTest: true}, '1', true, null];
|
|
|
|
|
|
|
2023-08-07 08:50:03 +08:00
|
|
|
|
if (is.tupleLike(tuple, [is.nonEmptyObject, is.string, is.boolean, is.null])) {
|
2023-07-23 15:35:23 +03:00
|
|
|
|
const value = tuple[0];
|
|
|
|
|
|
expectTypeOf(value).toEqualTypeOf<Record<string | number | symbol, unknown>>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
const tuple = [1, '1', true, null, undefined];
|
|
|
|
|
|
|
2023-08-07 08:50:03 +08:00
|
|
|
|
if (is.tupleLike(tuple, [is.number, is.string, is.boolean, is.undefined, is.null])) {
|
2023-07-23 15:35:23 +03:00
|
|
|
|
const numericValue = tuple[0];
|
|
|
|
|
|
const stringValue = tuple[1];
|
|
|
|
|
|
const booleanValue = tuple[2];
|
|
|
|
|
|
const undefinedValue = tuple[3];
|
|
|
|
|
|
const nullValue = tuple[4];
|
|
|
|
|
|
expectTypeOf(numericValue).toEqualTypeOf<number>();
|
|
|
|
|
|
expectTypeOf(stringValue).toEqualTypeOf<string>();
|
|
|
|
|
|
expectTypeOf(booleanValue).toEqualTypeOf<boolean>();
|
|
|
|
|
|
expectTypeOf(undefinedValue).toEqualTypeOf<undefined>();
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
|
|
expectTypeOf(nullValue).toEqualTypeOf<null>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-09-25 20:20:06 +01:00
|
|
|
|
test('is.inRange', t => {
|
|
|
|
|
|
const x = 3;
|
|
|
|
|
|
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.inRange(x, [0, 5]));
|
|
|
|
|
|
t.true(is.inRange(x, [5, 0]));
|
|
|
|
|
|
t.true(is.inRange(x, [-5, 5]));
|
|
|
|
|
|
t.true(is.inRange(x, [5, -5]));
|
|
|
|
|
|
t.false(is.inRange(x, [4, 8]));
|
|
|
|
|
|
t.true(is.inRange(-7, [-5, -10]));
|
|
|
|
|
|
t.true(is.inRange(-5, [-5, -10]));
|
|
|
|
|
|
t.true(is.inRange(-10, [-5, -10]));
|
|
|
|
|
|
|
|
|
|
|
|
t.true(is.inRange(x, 10));
|
|
|
|
|
|
t.true(is.inRange(0, 0));
|
|
|
|
|
|
t.true(is.inRange(-2, -3));
|
|
|
|
|
|
t.false(is.inRange(x, 2));
|
|
|
|
|
|
t.false(is.inRange(-3, -2));
|
2017-09-28 06:41:16 +03:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
// @ts-expect-error invalid argument
|
2018-10-30 23:19:19 +07:00
|
|
|
|
is.inRange(0, []);
|
2017-09-28 06:41:16 +03:00
|
|
|
|
});
|
2017-09-26 02:32:58 +07:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
// @ts-expect-error invalid argument
|
2018-10-30 23:19:19 +07:00
|
|
|
|
is.inRange(0, [5]);
|
2017-09-26 02:32:58 +07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
// @ts-expect-error invalid argument
|
2018-10-30 23:19:19 +07:00
|
|
|
|
is.inRange(0, [1, 2, 3]);
|
2017-09-26 02:32:58 +07:00
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(x, [0, 5]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(x, [5, 0]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(x, [-5, 5]);
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(x, [5, -5]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.inRange(x, [4, 8]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(-7, [-5, -10]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(-5, [-5, -10]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(-10, [-5, -10]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(x, 10);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(0, 0);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.inRange(-2, -3);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.inRange(x, 2);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.inRange(-3, -2);
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
// @ts-expect-error invalid argument
|
2020-01-22 12:08:35 +01:00
|
|
|
|
assert.inRange(0, []);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
// @ts-expect-error invalid argument
|
2020-01-22 12:08:35 +01:00
|
|
|
|
assert.inRange(0, [5]);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-08-07 08:50:03 +08:00
|
|
|
|
// @ts-expect-error invalid argument
|
2020-01-22 12:08:35 +01:00
|
|
|
|
assert.inRange(0, [1, 2, 3]);
|
|
|
|
|
|
});
|
2017-09-25 20:20:06 +01:00
|
|
|
|
});
|
2017-10-05 00:18:25 -07:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.htmlElement supplemental', t => {
|
2023-08-10 22:06:46 +08:00
|
|
|
|
t.false(is.htmlElement({nodeType: 1, nodeName: 'div'}));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
2023-08-10 22:06:46 +08:00
|
|
|
|
assert.htmlElement({nodeType: 1, nodeName: 'div'});
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2019-04-28 03:15:11 -05:00
|
|
|
|
|
2020-06-28 05:20:36 +09:00
|
|
|
|
const tagNames = [
|
|
|
|
|
|
'div',
|
|
|
|
|
|
'input',
|
|
|
|
|
|
'span',
|
|
|
|
|
|
'img',
|
|
|
|
|
|
'canvas',
|
2022-06-11 17:44:01 +07:00
|
|
|
|
'script',
|
2023-08-10 22:06:46 +08:00
|
|
|
|
] as const;
|
2019-04-28 03:15:11 -05:00
|
|
|
|
|
2020-06-28 05:20:36 +09:00
|
|
|
|
for (const tagName of tagNames) {
|
2023-08-10 22:06:46 +08:00
|
|
|
|
const element = document.createElement(tagName);
|
|
|
|
|
|
t.is(is(element), 'HTMLElement');
|
2019-04-28 03:15:11 -05:00
|
|
|
|
}
|
2017-10-07 09:19:11 -07:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
const nonHtmlElements = [
|
|
|
|
|
|
document.createTextNode('data'),
|
|
|
|
|
|
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
|
|
|
|
|
|
document.createComment('This is a comment'),
|
|
|
|
|
|
document,
|
|
|
|
|
|
document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'),
|
|
|
|
|
|
document.createDocumentFragment(),
|
|
|
|
|
|
] as const;
|
2017-11-19 15:16:06 -05:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
for (const element of nonHtmlElements) {
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.htmlElement(element);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2017-10-05 00:18:25 -07:00
|
|
|
|
});
|
2017-10-06 02:39:36 -05:00
|
|
|
|
|
2019-02-02 00:22:23 -06:00
|
|
|
|
test('is.evenInteger', t => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
for (const element of [-6, 2, 4]) {
|
|
|
|
|
|
t.true(is.evenInteger(element));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.evenInteger(element);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2017-10-17 11:36:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-11 17:44:01 +07:00
|
|
|
|
for (const element of [-3, 1, 5]) {
|
|
|
|
|
|
t.false(is.evenInteger(element));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.evenInteger(element);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2017-10-17 11:36:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2019-02-02 00:22:23 -06:00
|
|
|
|
test('is.oddInteger', t => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
for (const element of [-5, 7, 13]) {
|
|
|
|
|
|
t.true(is.oddInteger(element));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.oddInteger(element);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2017-10-17 11:36:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-11 17:44:01 +07:00
|
|
|
|
for (const element of [-8, 8, 10]) {
|
|
|
|
|
|
t.false(is.oddInteger(element));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.oddInteger(element);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2017-10-17 11:36:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2018-09-28 11:54:35 +05:30
|
|
|
|
test('is.nonEmptyArray', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.nonEmptyArray([1, 2, 3]));
|
|
|
|
|
|
t.false(is.nonEmptyArray([]));
|
2019-03-31 20:41:19 +07:00
|
|
|
|
t.false(is.nonEmptyArray(new Array())); // eslint-disable-line @typescript-eslint/no-array-constructor
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.nonEmptyArray([1, 2, 3]);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyArray([]);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyArray(new Array()); // eslint-disable-line @typescript-eslint/no-array-constructor
|
|
|
|
|
|
});
|
2022-10-17 17:51:49 +07:00
|
|
|
|
|
2023-07-14 23:24:56 +01:00
|
|
|
|
{
|
2023-07-17 10:42:04 +01:00
|
|
|
|
const strings = ['🦄', 'unicorn'] as string[] | undefined;
|
2023-07-14 23:24:56 +01:00
|
|
|
|
const function_ = (value: string) => value;
|
|
|
|
|
|
|
|
|
|
|
|
if (is.nonEmptyArray(strings)) {
|
|
|
|
|
|
const value = strings[0];
|
|
|
|
|
|
function_(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
const mixed = ['🦄', 'unicorn', 1, 2];
|
|
|
|
|
|
const function_ = (value: string | number) => value;
|
|
|
|
|
|
|
|
|
|
|
|
if (is.nonEmptyArray(mixed)) {
|
|
|
|
|
|
const value = mixed[0];
|
|
|
|
|
|
function_(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
const arrays = [['🦄'], ['unicorn']];
|
|
|
|
|
|
const function_ = (value: string[]) => value;
|
|
|
|
|
|
|
|
|
|
|
|
if (is.nonEmptyArray(arrays)) {
|
|
|
|
|
|
const value = arrays[0];
|
|
|
|
|
|
function_(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
2023-07-17 10:42:04 +01:00
|
|
|
|
const strings = ['🦄', 'unicorn'] as string[] | undefined;
|
2023-07-14 23:24:56 +01:00
|
|
|
|
const function_ = (value: string) => value;
|
|
|
|
|
|
|
|
|
|
|
|
assert.nonEmptyArray(strings);
|
|
|
|
|
|
|
|
|
|
|
|
const value = strings[0];
|
|
|
|
|
|
function_(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
const mixed = ['🦄', 'unicorn', 1, 2];
|
|
|
|
|
|
const function_ = (value: string | number) => value;
|
|
|
|
|
|
|
|
|
|
|
|
assert.nonEmptyArray(mixed);
|
|
|
|
|
|
|
|
|
|
|
|
const value = mixed[0];
|
|
|
|
|
|
function_(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
const arrays = [['🦄'], ['unicorn']];
|
|
|
|
|
|
const function_ = (value: string[]) => value;
|
|
|
|
|
|
|
|
|
|
|
|
assert.nonEmptyArray(arrays);
|
|
|
|
|
|
|
|
|
|
|
|
const value = arrays[0];
|
|
|
|
|
|
function_(value);
|
|
|
|
|
|
}
|
2018-09-28 11:54:35 +05:30
|
|
|
|
});
|
2017-10-06 02:39:36 -05:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.emptyString supplemental', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.emptyString('🦄'));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyString('🦄');
|
|
|
|
|
|
});
|
2018-09-28 11:54:35 +05:30
|
|
|
|
});
|
2017-10-06 02:39:36 -05:00
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.emptyStringOrWhitespace supplemental', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.emptyStringOrWhitespace(' '));
|
|
|
|
|
|
t.false(is.emptyStringOrWhitespace('🦄'));
|
|
|
|
|
|
t.false(is.emptyStringOrWhitespace('unicorn'));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.emptyStringOrWhitespace(' ');
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyStringOrWhitespace('🦄');
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyStringOrWhitespace('unicorn');
|
|
|
|
|
|
});
|
2024-06-26 14:31:56 +02:00
|
|
|
|
|
|
|
|
|
|
let value = 'test'; // eslint-disable-line prefer-const -- can't use `const` here because then it will be inferred as `never` in the `if` block
|
|
|
|
|
|
if (is.emptyStringOrWhitespace(value)) {
|
|
|
|
|
|
value.charAt(0); // Should be inferred as `'' | Whitespace` and not `never`
|
|
|
|
|
|
} else {
|
|
|
|
|
|
value.charAt(0); // Should be inferred as `string` and not `never`
|
|
|
|
|
|
}
|
2022-02-25 09:10:57 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-25 16:19:29 +07:00
|
|
|
|
test('is.nonEmptyString', t => {
|
|
|
|
|
|
t.false(is.nonEmptyString(''));
|
|
|
|
|
|
t.false(is.nonEmptyString(String()));
|
|
|
|
|
|
t.true(is.nonEmptyString('🦄'));
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyString('');
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyString(String());
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.nonEmptyString('🦄');
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2022-02-25 09:10:57 +00:00
|
|
|
|
test('is.nonEmptyStringAndNotWhitespace', t => {
|
|
|
|
|
|
t.false(is.nonEmptyStringAndNotWhitespace(' '));
|
|
|
|
|
|
t.true(is.nonEmptyStringAndNotWhitespace('🦄'));
|
|
|
|
|
|
|
2022-06-11 17:44:01 +07:00
|
|
|
|
for (const value of [null, undefined, 5, Number.NaN, {}, []]) {
|
2022-02-25 09:10:57 +00:00
|
|
|
|
t.false(is.nonEmptyStringAndNotWhitespace(value));
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyStringAndNotWhitespace(value);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyStringAndNotWhitespace('');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.nonEmptyStringAndNotWhitespace('🦄');
|
|
|
|
|
|
});
|
2018-09-28 11:54:35 +05:30
|
|
|
|
});
|
2017-10-06 02:39:36 -05:00
|
|
|
|
|
2018-09-28 11:54:35 +05:30
|
|
|
|
test('is.emptyObject', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.emptyObject({}));
|
2024-07-10 15:41:12 +02:00
|
|
|
|
t.true(is.emptyObject(new Object())); // eslint-disable-line no-object-constructor
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.false(is.emptyObject({unicorn: '🦄'}));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.emptyObject({});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
2024-07-10 15:41:12 +02:00
|
|
|
|
assert.emptyObject(new Object()); // eslint-disable-line no-object-constructor
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyObject({unicorn: '🦄'});
|
|
|
|
|
|
});
|
2018-09-28 11:54:35 +05:30
|
|
|
|
});
|
2017-10-06 02:39:36 -05:00
|
|
|
|
|
2018-09-28 11:54:35 +05:30
|
|
|
|
test('is.nonEmptyObject', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
const foo = {};
|
|
|
|
|
|
is.nonEmptyObject(foo);
|
|
|
|
|
|
|
|
|
|
|
|
t.false(is.nonEmptyObject({}));
|
2024-07-10 15:41:12 +02:00
|
|
|
|
t.false(is.nonEmptyObject(new Object())); // eslint-disable-line no-object-constructor
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.nonEmptyObject({unicorn: '🦄'}));
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyObject({});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
2024-07-10 15:41:12 +02:00
|
|
|
|
assert.nonEmptyObject(new Object()); // eslint-disable-line no-object-constructor
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.nonEmptyObject({unicorn: '🦄'});
|
|
|
|
|
|
});
|
2018-09-28 11:54:35 +05:30
|
|
|
|
});
|
2017-10-06 02:39:36 -05:00
|
|
|
|
|
2018-09-28 11:54:35 +05:30
|
|
|
|
test('is.nonEmptySet', t => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
const temporarySet = new Set();
|
|
|
|
|
|
t.false(is.nonEmptySet(temporarySet));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.nonEmptySet(temporarySet);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2017-10-06 02:39:36 -05:00
|
|
|
|
|
2022-06-11 17:44:01 +07:00
|
|
|
|
temporarySet.add(1);
|
|
|
|
|
|
t.true(is.nonEmptySet(temporarySet));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.nonEmptySet(temporarySet);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2018-09-28 11:54:35 +05:30
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('is.nonEmptyMap', t => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
const temporaryMap = new Map();
|
|
|
|
|
|
t.false(is.nonEmptyMap(temporaryMap));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.throws(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.nonEmptyMap(temporaryMap);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2018-09-28 11:54:35 +05:30
|
|
|
|
|
2022-06-11 17:44:01 +07:00
|
|
|
|
temporaryMap.set('unicorn', '🦄');
|
|
|
|
|
|
t.true(is.nonEmptyMap(temporaryMap));
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.nonEmptyMap(temporaryMap);
|
2020-01-22 18:47:01 +07:00
|
|
|
|
});
|
2017-10-17 16:46:58 -04:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-09-10 10:26:13 +02:00
|
|
|
|
test('is.propertyKey', t => {
|
|
|
|
|
|
t.true(is.propertyKey('key'));
|
|
|
|
|
|
t.true(is.propertyKey(42));
|
|
|
|
|
|
t.true(is.propertyKey(Symbol('')));
|
|
|
|
|
|
|
|
|
|
|
|
t.false(is.propertyKey(null));
|
|
|
|
|
|
t.false(is.propertyKey(undefined));
|
|
|
|
|
|
t.false(is.propertyKey(true));
|
|
|
|
|
|
t.false(is.propertyKey({}));
|
|
|
|
|
|
t.false(is.propertyKey([]));
|
|
|
|
|
|
t.false(is.propertyKey(new Map()));
|
|
|
|
|
|
t.false(is.propertyKey(new Set()));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2017-10-11 04:26:45 -05:00
|
|
|
|
test('is.any', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.any(is.string, {}, true, '🦄'));
|
|
|
|
|
|
t.true(is.any(is.object, false, {}, 'unicorns'));
|
|
|
|
|
|
t.false(is.any(is.boolean, '🦄', [], 3));
|
|
|
|
|
|
t.false(is.any(is.integer, true, 'lol', {}));
|
2020-02-16 19:06:19 -06:00
|
|
|
|
t.true(is.any([is.string, is.number], {}, true, '🦄'));
|
|
|
|
|
|
t.false(is.any([is.boolean, is.number], 'unicorns', [], new Map()));
|
2017-10-11 04:26:45 -05:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-05-30 21:13:40 +07:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2019-02-04 02:13:23 +07:00
|
|
|
|
is.any(null as any, true);
|
2017-10-11 04:26:45 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
is.any(is.string);
|
2017-10-11 04:26:45 -05:00
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.any(is.string, {}, true, '🦄');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.any(is.object, false, {}, 'unicorns');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any(is.boolean, '🦄', [], 3);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any(is.integer, true, 'lol', {});
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-05-30 21:13:40 +07:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2020-01-22 12:08:35 +01:00
|
|
|
|
assert.any(null as any, true);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any(is.string);
|
|
|
|
|
|
});
|
2021-04-22 04:00:08 -05:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any(is.string, 1, 2, 3);
|
|
|
|
|
|
}, {
|
2023-08-09 19:49:06 +08:00
|
|
|
|
// Includes expected type and removes duplicates from received types:
|
|
|
|
|
|
message: /Expected values which are `string`. Received values of type `number`./,
|
2021-04-22 04:00:08 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any(is.string, 1, [4]);
|
|
|
|
|
|
}, {
|
2023-08-09 19:49:06 +08:00
|
|
|
|
// Includes expected type and lists all received types:
|
|
|
|
|
|
message: /Expected values which are `string`. Received values of types `number` and `Array`./,
|
2021-04-22 04:00:08 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any([is.string, is.nullOrUndefined], 1);
|
|
|
|
|
|
}, {
|
|
|
|
|
|
// Handles array as first argument:
|
2023-08-09 19:49:06 +08:00
|
|
|
|
message: /Expected values which are `string` or `null or undefined`. Received values of type `number`./,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any([is.string, is.number, is.boolean], null, undefined, Number.NaN);
|
|
|
|
|
|
}, {
|
|
|
|
|
|
// Handles more than 2 expected and received types:
|
|
|
|
|
|
message: /Expected values which are `string`, `number`, or `boolean`. Received values of types `null`, `undefined`, and `NaN`./,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.any(() => false, 1);
|
|
|
|
|
|
}, {
|
|
|
|
|
|
// Default type assertion message
|
|
|
|
|
|
message: /Expected values which are `predicate returns truthy for any value`./,
|
2021-04-22 04:00:08 -05:00
|
|
|
|
});
|
2017-10-11 04:26:45 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('is.all', t => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
t.true(is.all(is.object, {}, new Set(), new Map()));
|
|
|
|
|
|
t.true(is.all(is.boolean, true, false));
|
|
|
|
|
|
t.false(is.all(is.string, '🦄', []));
|
|
|
|
|
|
t.false(is.all(is.set, new Map(), {}));
|
2017-10-11 04:26:45 -05:00
|
|
|
|
|
2022-06-11 17:44:01 +07:00
|
|
|
|
t.true(is.all(is.array, ['1'], ['2']));
|
2020-08-21 15:18:34 -07:00
|
|
|
|
|
2017-10-11 04:26:45 -05:00
|
|
|
|
t.throws(() => {
|
2023-05-30 21:13:40 +07:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2019-02-04 02:13:23 +07:00
|
|
|
|
is.all(null as any, true);
|
2017-10-11 04:26:45 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2018-10-30 23:19:19 +07:00
|
|
|
|
is.all(is.string);
|
2017-10-11 04:26:45 -05:00
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.all(is.object, {}, new Set(), new Map());
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.all(is.boolean, true, false);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.all(is.string, '🦄', []);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.all(is.set, new Map(), {});
|
|
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
2023-05-30 21:13:40 +07:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2020-01-22 12:08:35 +01:00
|
|
|
|
assert.all(null as any, true);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.all(is.string);
|
|
|
|
|
|
});
|
2021-04-22 04:00:08 -05:00
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.all(is.string, 1, 2, 3);
|
|
|
|
|
|
}, {
|
2023-08-09 19:49:06 +08:00
|
|
|
|
// Includes expected type and removes duplicates from received types:
|
|
|
|
|
|
message: /Expected values which are `string`. Received values of type `number`./,
|
2021-04-22 04:00:08 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.all(is.string, 1, [4]);
|
|
|
|
|
|
}, {
|
2023-08-09 19:49:06 +08:00
|
|
|
|
// Includes expected type and lists all received types:
|
|
|
|
|
|
message: /Expected values which are `string`. Received values of types `number` and `Array`./,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.all(() => false, 1);
|
|
|
|
|
|
}, {
|
|
|
|
|
|
// Default type assertion message
|
|
|
|
|
|
message: /Expected values which are `predicate returns truthy for all values`./,
|
2021-04-22 04:00:08 -05:00
|
|
|
|
});
|
2020-01-22 12:08:35 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-06 19:58:46 +08:00
|
|
|
|
test('is.formData supplemental', t => {
|
2021-09-13 22:25:44 +08:00
|
|
|
|
const data = new window.FormData();
|
|
|
|
|
|
t.true(is.formData(data));
|
|
|
|
|
|
t.false(is.formData({}));
|
|
|
|
|
|
t.false(is.formData(undefined));
|
|
|
|
|
|
t.false(is.formData(null));
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.formData(data);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.formData({});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.formData(undefined);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.formData(null);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('is.urlSearchParams', t => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
const searchParameters = new URLSearchParams();
|
|
|
|
|
|
t.true(is.urlSearchParams(searchParameters));
|
2021-09-13 22:25:44 +08:00
|
|
|
|
t.false(is.urlSearchParams({}));
|
|
|
|
|
|
t.false(is.urlSearchParams(undefined));
|
|
|
|
|
|
t.false(is.urlSearchParams(null));
|
|
|
|
|
|
|
|
|
|
|
|
t.notThrows(() => {
|
2022-06-11 17:44:01 +07:00
|
|
|
|
assert.urlSearchParams(searchParameters);
|
2021-09-13 22:25:44 +08:00
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlSearchParams({});
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlSearchParams(undefined);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlSearchParams(null);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2024-02-29 08:23:30 +01:00
|
|
|
|
test('is.validDate', t => {
|
|
|
|
|
|
t.true(is.validDate(new Date()));
|
|
|
|
|
|
t.false(is.validDate(new Date('x')));
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.validDate(new Date());
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.validDate(new Date('x'));
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2023-08-07 08:50:03 +08:00
|
|
|
|
test('is.validLength', t => {
|
|
|
|
|
|
t.true(is.validLength(1));
|
|
|
|
|
|
t.true(is.validLength(0));
|
|
|
|
|
|
t.false(is.validLength(-1));
|
|
|
|
|
|
t.false(is.validLength(0.1));
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
|
|
assert.validLength(1);
|
|
|
|
|
|
});
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.validLength(-1);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('is.whitespaceString', t => {
|
|
|
|
|
|
t.true(is.whitespaceString(' '));
|
|
|
|
|
|
t.true(is.whitespaceString(' '));
|
|
|
|
|
|
t.true(is.whitespaceString(' '));
|
|
|
|
|
|
t.true(is.whitespaceString('\u3000'));
|
|
|
|
|
|
t.true(is.whitespaceString(' '));
|
|
|
|
|
|
t.false(is.whitespaceString(''));
|
|
|
|
|
|
t.false(is.whitespaceString('-'));
|
|
|
|
|
|
t.false(is.whitespaceString(' hi '));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2020-01-22 12:08:35 +01:00
|
|
|
|
test('assert', t => {
|
2020-01-22 18:47:01 +07:00
|
|
|
|
// Contrived test showing that TypeScript acknowledges the type assertion in `assert.number()`.
|
|
|
|
|
|
// Real--world usage includes asserting user input, but here we use a random number/string generator.
|
2020-01-22 12:08:35 +01:00
|
|
|
|
t.plan(2);
|
|
|
|
|
|
|
|
|
|
|
|
const getNumberOrStringRandomly = (): number | string => {
|
2020-01-22 18:47:01 +07:00
|
|
|
|
const random = Math.random();
|
2020-01-22 12:08:35 +01:00
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
if (random < 0.5) {
|
2020-01-22 12:08:35 +01:00
|
|
|
|
return 'sometimes this function returns text';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-22 18:47:01 +07:00
|
|
|
|
return random;
|
2020-01-22 12:08:35 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const canUseOnlyNumber = (badlyTypedArgument: any): number => {
|
|
|
|
|
|
// Narrow the type to number, or throw an error at runtime for non-numbers.
|
|
|
|
|
|
assert.number(badlyTypedArgument);
|
|
|
|
|
|
|
|
|
|
|
|
// Both the type and runtime value is number.
|
|
|
|
|
|
return 1000 * badlyTypedArgument;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const badlyTypedVariable: any = getNumberOrStringRandomly();
|
|
|
|
|
|
|
|
|
|
|
|
t.true(is.number(badlyTypedVariable) || is.string(badlyTypedVariable));
|
|
|
|
|
|
|
|
|
|
|
|
// Using try/catch for test purposes only.
|
|
|
|
|
|
try {
|
|
|
|
|
|
const result = canUseOnlyNumber(badlyTypedVariable);
|
|
|
|
|
|
|
|
|
|
|
|
// Got lucky, the input was a number yielding a good result.
|
|
|
|
|
|
t.true(is.number(result));
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// Assertion was tripped.
|
|
|
|
|
|
t.true(is.string(badlyTypedVariable));
|
|
|
|
|
|
}
|
2017-10-11 04:26:45 -05:00
|
|
|
|
});
|
2024-04-23 07:58:28 +02:00
|
|
|
|
|
|
|
|
|
|
test('custom assertion message', t => {
|
|
|
|
|
|
const message = 'Custom error message';
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.array(undefined, undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.arrayBuffer(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.arrayLike(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncFunction(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncGenerator(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncGeneratorFunction(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.asyncIterable(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.bigInt64Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.bigUint64Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.bigint(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.blob(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.boolean(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.boundFunction(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.buffer(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.class(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.dataView(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.date(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.directInstanceOf(undefined, Error, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyArray(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyMap(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyObject(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptySet(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyString(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.emptyStringOrWhitespace(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
enum Enum {}
|
|
|
|
|
|
assert.enumCase('invalid', Enum, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.error(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.evenInteger(33, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.falsy(true, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.float32Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.float64Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.formData(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.function(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.generator(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.generatorFunction(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.htmlElement(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.inRange(5, [1, 2], message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.infinite(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.int16Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.int32Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.int8Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.integer(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.iterable(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.map(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nan(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nativePromise(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.negativeNumber(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nodeStream(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyArray(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyMap(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyObject(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptySet(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyString(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nonEmptyStringAndNotWhitespace(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.null(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.nullOrUndefined(false, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.number(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.numericString(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.object(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.observable(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.oddInteger(42, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.plainObject(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.positiveNumber(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.primitive([], message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.promise(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.propertyKey(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.regExp(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.safeInteger(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.set(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.sharedArrayBuffer(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.string(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.symbol(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.truthy(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.tupleLike(undefined, [], message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.typedArray(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.uint16Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.uint32Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.uint8Array(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.uint8ClampedArray(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.undefined(false, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlInstance(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlSearchParams(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.urlString(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.validDate(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.validLength(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.weakMap(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.weakRef(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.weakSet(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
|
|
|
|
|
|
t.throws(() => {
|
|
|
|
|
|
assert.whitespaceString(undefined, message);
|
|
|
|
|
|
}, {instanceOf: TypeError, message});
|
|
|
|
|
|
});
|