is/test/test.ts

1587 lines
31 KiB
TypeScript
Raw Normal View History

2020-01-22 18:47:01 +07:00
import fs = require('fs');
import net = require('net');
import Stream = require('stream');
2019-06-15 02:12:31 +07:00
import {inspect} from 'util';
import test, {ExecutionContext} from 'ava';
import {JSDOM} from 'jsdom';
2018-05-03 05:22:32 +02:00
import {Subject, Observable} from 'rxjs';
2020-01-22 12:08:35 +01:00
import tempy = require('tempy');
2019-06-15 02:12:31 +07:00
import ZenObservable = require('zen-observable');
import is, {assert, AssertionTypeDescription, Primitive, TypedArray, TypeName} from '../source';
2017-09-22 00:44:27 +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
const {window} = new JSDOM();
const {document} = window;
2018-10-30 23:19:19 +07:00
const createDomElement = (element: string) => document.createElement(element);
2017-10-07 09:19:11 -07:00
2017-11-06 16:26:59 +01:00
interface Test {
2020-01-22 12:08:35 +01:00
assert: (...args: any[]) => void | never;
fixtures: unknown[];
2019-04-28 03:15:11 -05:00
typename?: TypeName;
2020-01-22 12:08:35 +01:00
typeDescription?: AssertionTypeDescription | TypeName;
2018-11-30 14:28:54 +07:00
is(value: unknown): boolean;
2017-11-06 16:26:59 +01:00
}
2020-01-22 12:08:35 +01:00
const invertAssertThrow = (description: string, fn: () => void | never, value: unknown): void | never => {
2020-01-22 18:47:01 +07:00
const expectedAssertErrorMessage = `Expected value which is \`${description}\`, received value of type \`${is(value)}\`.`;
2020-01-22 12:08:35 +01:00
try {
fn();
} catch (error) {
if (error instanceof TypeError && error.message.includes(expectedAssertErrorMessage)) {
return;
}
throw error;
}
throw new Error(`Function did not throw any error, expected: ${expectedAssertErrorMessage}`);
};
2017-11-06 16:26:59 +01:00
const types = new Map<string, Test>([
['undefined', {
2018-10-30 23:19:19 +07:00
is: is.undefined,
2020-01-22 12:08:35 +01:00
assert: assert.undefined,
2017-11-06 16:26:59 +01:00
fixtures: [
undefined
2019-04-28 03:15:11 -05:00
],
typename: 'undefined'
2017-11-06 16:26:59 +01:00
}],
['null', {
2018-10-30 23:19:19 +07:00
is: is.null_,
2020-01-22 12:08:35 +01:00
assert: assert.null_,
2017-11-06 16:26:59 +01:00
fixtures: [
null
2019-04-28 03:15:11 -05:00
],
typename: 'null'
2017-11-06 16:26:59 +01:00
}],
['string', {
2018-10-30 23:19:19 +07:00
is: is.string,
2020-01-22 12:08:35 +01:00
assert: assert.string,
2017-11-06 16:26:59 +01:00
fixtures: [
'🦄',
'hello world',
''
2019-04-28 03:15:11 -05:00
],
typename: 'string'
2017-11-06 16:26:59 +01:00
}],
2018-09-28 11:54:35 +05:30
['emptyString', {
2018-10-30 23:19:19 +07:00
is: is.emptyString,
2020-01-22 12:08:35 +01:00
assert: assert.emptyString,
2018-09-28 11:54:35 +05:30
fixtures: [
'',
String()
2019-04-28 03:15:11 -05:00
],
typename: 'string',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.emptyString
2018-09-28 11:54:35 +05:30
}],
2017-11-06 16:26:59 +01:00
['number', {
2018-10-30 23:19:19 +07:00
is: is.number,
2020-01-22 12:08:35 +01:00
assert: assert.number,
2017-11-06 16:26:59 +01:00
fixtures: [
6,
1.4,
0,
-0,
Infinity,
-Infinity
2019-04-28 03:15:11 -05:00
],
typename: 'number'
2017-11-06 16:26:59 +01:00
}],
2019-11-07 15:56:02 +07:00
['bigint', {
is: is.bigint,
2020-01-22 12:08:35 +01:00
assert: assert.bigint,
2019-11-07 15:56:02 +07:00
fixtures: [
// Disabled until TS supports it for an ESnnnn target.
// 1n,
// 0n,
// -0n,
BigInt('1234')
],
typename: 'bigint'
2019-11-07 15:56:02 +07:00
}],
2017-11-06 16:26:59 +01:00
['boolean', {
2018-10-30 23:19:19 +07:00
is: is.boolean,
2020-01-22 12:08:35 +01:00
assert: assert.boolean,
2017-11-06 16:26:59 +01:00
fixtures: [
true, false
2019-04-28 03:15:11 -05:00
],
typename: 'boolean'
2017-11-06 16:26:59 +01:00
}],
['symbol', {
2018-10-30 23:19:19 +07:00
is: is.symbol,
2020-01-22 12:08:35 +01:00
assert: assert.symbol,
2017-11-06 16:26:59 +01:00
fixtures: [
Symbol('🦄')
2019-04-28 03:15:11 -05:00
],
typename: 'symbol'
2017-11-06 16:26:59 +01:00
}],
2018-11-01 13:21:49 +02:00
['numericString', {
is: is.numericString,
2020-01-22 12:08:35 +01:00
assert: assert.numericString,
2018-11-01 13:21:49 +02:00
fixtures: [
'5',
'-3.2',
'Infinity',
'0x56'
2019-04-28 03:15:11 -05:00
],
typename: 'string',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.numericString
2018-11-01 13:21:49 +02:00
}],
2017-11-06 16:26:59 +01:00
['array', {
2018-10-30 23:19:19 +07:00
is: is.array,
2020-01-22 12:08:35 +01:00
assert: assert.array,
2017-11-06 16:26:59 +01:00
fixtures: [
[1, 2],
2019-03-31 20:41:19 +07:00
new Array(2)
2019-04-28 03:15:11 -05:00
],
typename: 'Array'
2017-11-06 16:26:59 +01:00
}],
2018-09-28 11:54:35 +05:30
['emptyArray', {
2018-10-30 23:19:19 +07:00
is: is.emptyArray,
2020-01-22 12:08:35 +01:00
assert: assert.emptyArray,
2018-09-28 11:54:35 +05:30
fixtures: [
[],
2019-03-31 20:41:19 +07:00
new Array() // eslint-disable-line @typescript-eslint/no-array-constructor
2019-04-28 03:15:11 -05:00
],
typename: 'Array',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.emptyArray
2018-09-28 11:54:35 +05:30
}],
2017-11-06 16:26:59 +01:00
['function', {
2018-10-30 23:19:19 +07:00
is: is.function_,
2020-01-22 12:08:35 +01:00
assert: assert.function_,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 20:41:19 +07:00
function foo() {}, // eslint-disable-line func-names
2017-11-07 10:23:00 +07:00
function () {},
2017-11-06 16:26:59 +01:00
() => {},
2017-11-07 10:23:00 +07:00
async function () {},
function * (): unknown {},
async function * (): unknown {}
2019-04-28 03:15:11 -05:00
],
typename: 'Function'
2017-11-06 16:26:59 +01:00
}],
['buffer', {
2018-10-30 23:19:19 +07:00
is: is.buffer,
2020-01-22 12:08:35 +01:00
assert: assert.buffer,
2017-11-06 16:26:59 +01:00
fixtures: [
Buffer.from('🦄')
2019-04-28 03:15:11 -05:00
],
typename: 'Buffer'
2017-11-06 16:26:59 +01:00
}],
['object', {
2018-10-30 23:19:19 +07:00
is: is.object,
2020-01-22 12:08:35 +01:00
assert: assert.object,
2017-11-06 16:26:59 +01:00
fixtures: [
{x: 1},
Object.create({x: 1})
2019-04-28 03:15:11 -05:00
],
typename: 'Object'
2017-11-06 16:26:59 +01:00
}],
['regExp', {
2018-10-30 23:19:19 +07:00
is: is.regExp,
2020-01-22 12:08:35 +01:00
assert: assert.regExp,
2017-11-06 16:26:59 +01:00
fixtures: [
/\w/,
2019-11-07 15:56:02 +07:00
new RegExp('\\w') // eslint-disable-line prefer-regex-literals
2019-04-28 03:15:11 -05:00
],
typename: 'RegExp'
2017-11-06 16:26:59 +01:00
}],
['date', {
2018-10-30 23:19:19 +07:00
is: is.date,
2020-01-22 12:08:35 +01:00
assert: assert.date,
2017-11-06 16:26:59 +01:00
fixtures: [
new Date()
2019-04-28 03:15:11 -05:00
],
typename: 'Date'
2017-11-06 16:26:59 +01:00
}],
['error', {
2018-10-30 23:19:19 +07:00
is: is.error,
2020-01-22 12:08:35 +01:00
assert: assert.error,
2017-11-06 16:26:59 +01:00
fixtures: [
new Error('🦄'),
new ErrorSubclassFixture()
2019-04-28 03:15:11 -05:00
],
typename: 'Error'
2017-11-06 16:26:59 +01:00
}],
['nativePromise', {
2018-10-30 23:19:19 +07:00
is: is.nativePromise,
2020-01-22 12:08:35 +01:00
assert: assert.nativePromise,
2017-11-06 16:26:59 +01:00
fixtures: [
Promise.resolve(),
PromiseSubclassFixture.resolve()
2019-04-28 03:15:11 -05:00
],
typename: 'Promise',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.nativePromise
2017-11-06 16:26:59 +01:00
}],
['promise', {
2018-10-30 23:19:19 +07:00
is: is.promise,
2020-01-22 12:08:35 +01:00
assert: assert.promise,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 20:41:19 +07:00
{then() {}, catch() {}}
2019-04-28 03:15:11 -05:00
],
typename: 'Object',
typeDescription: 'Promise'
2017-11-06 16:26:59 +01:00
}],
['generator', {
2018-10-30 23:19:19 +07:00
is: is.generator,
2020-01-22 12:08:35 +01:00
assert: assert.generator,
2017-11-06 16:26:59 +01:00
fixtures: [
2017-11-07 10:23:00 +07:00
(function * () {
yield 4;
})()
2019-04-28 03:15:11 -05:00
],
typename: 'Generator'
2017-11-06 16:26:59 +01:00
}],
['asyncGenerator', {
is: is.asyncGenerator,
assert: assert.asyncGenerator,
fixtures: [
(async function * () {
yield 4;
})()
],
typename: 'AsyncGenerator'
}],
2017-11-06 16:26:59 +01:00
['generatorFunction', {
2018-10-30 23:19:19 +07:00
is: is.generatorFunction,
2020-01-22 12:08:35 +01:00
assert: assert.generatorFunction,
2017-11-06 16:26:59 +01:00
fixtures: [
2017-11-07 10:23:00 +07:00
function * () {
yield 4;
}
2019-04-28 03:15:11 -05:00
],
typename: 'Function',
typeDescription: 'GeneratorFunction'
2017-11-06 16:26:59 +01:00
}],
['asyncGeneratorFunction', {
is: is.asyncGeneratorFunction,
assert: assert.asyncGeneratorFunction,
fixtures: [
async function * () {
yield 4;
}
],
typename: 'Function',
typeDescription: 'AsyncGeneratorFunction'
}],
2017-11-06 16:26:59 +01:00
['asyncFunction', {
2018-10-30 23:19:19 +07:00
is: is.asyncFunction,
2020-01-22 12:08:35 +01:00
assert: assert.asyncFunction,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 20:41:19 +07:00
async function () {},
async () => {}
2019-04-28 03:15:11 -05:00
],
typename: 'Function',
typeDescription: 'AsyncFunction'
2017-11-06 16:26:59 +01:00
}],
2017-11-10 19:11:35 +01:00
['boundFunction', {
2018-10-30 23:19:19 +07:00
is: is.boundFunction,
2020-01-22 12:08:35 +01:00
assert: assert.boundFunction,
2017-11-10 19:11:35 +01:00
fixtures: [
2019-03-31 20:41:19 +07:00
() => {},
function () {}.bind(null) // eslint-disable-line no-extra-bind
2019-04-28 03:15:11 -05:00
],
typename: 'Function'
2017-11-10 19:11:35 +01:00
}],
2017-11-06 16:26:59 +01:00
['map', {
2018-10-30 23:19:19 +07:00
is: is.map,
2020-01-22 12:08:35 +01:00
assert: assert.map,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 20:41:19 +07:00
new Map([['one', '1']])
2019-04-28 03:15:11 -05:00
],
typename: 'Map'
2018-09-28 11:54:35 +05:30
}],
['emptyMap', {
2018-10-30 23:19:19 +07:00
is: is.emptyMap,
2020-01-22 12:08:35 +01:00
assert: assert.emptyMap,
2018-09-28 11:54:35 +05:30
fixtures: [
2019-03-31 20:41:19 +07:00
new Map()
2019-04-28 03:15:11 -05:00
],
typename: 'Map',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.emptyMap
2017-11-06 16:26:59 +01:00
}],
['set', {
2018-10-30 23:19:19 +07:00
is: is.set,
2020-01-22 12:08:35 +01:00
assert: assert.set,
2017-11-06 16:26:59 +01:00
fixtures: [
2018-09-28 11:54:35 +05:30
new Set(['one'])
2019-04-28 03:15:11 -05:00
],
typename: 'Set'
2018-09-28 11:54:35 +05:30
}],
['emptySet', {
2018-10-30 23:19:19 +07:00
is: is.emptySet,
2020-01-22 12:08:35 +01:00
assert: assert.emptySet,
2018-09-28 11:54:35 +05:30
fixtures: [
2019-03-31 20:41:19 +07:00
new Set()
2019-04-28 03:15:11 -05:00
],
typename: 'Set',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.emptySet
2017-11-06 16:26:59 +01:00
}],
['weakSet', {
2018-10-30 23:19:19 +07:00
is: is.weakSet,
2020-01-22 12:08:35 +01:00
assert: assert.weakSet,
2017-11-06 16:26:59 +01:00
fixtures: [
new WeakSet()
2019-04-28 03:15:11 -05:00
],
typename: 'WeakSet'
2017-11-06 16:26:59 +01:00
}],
['weakMap', {
2018-10-30 23:19:19 +07:00
is: is.weakMap,
2020-01-22 12:08:35 +01:00
assert: assert.weakMap,
2017-11-06 16:26:59 +01:00
fixtures: [
new WeakMap()
2019-04-28 03:15:11 -05:00
],
typename: 'WeakMap'
2017-11-06 16:26:59 +01:00
}],
['int8Array', {
2018-10-30 23:19:19 +07:00
is: is.int8Array,
2020-01-22 12:08:35 +01:00
assert: assert.int8Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Int8Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Int8Array'
2017-11-06 16:26:59 +01:00
}],
['uint8Array', {
2018-10-30 23:19:19 +07:00
is: is.uint8Array,
2020-01-22 12:08:35 +01:00
assert: assert.uint8Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Uint8Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Uint8Array'
2017-11-06 16:26:59 +01:00
}],
['uint8ClampedArray', {
2018-10-30 23:19:19 +07:00
is: is.uint8ClampedArray,
2020-01-22 12:08:35 +01:00
assert: assert.uint8ClampedArray,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Uint8ClampedArray()
2019-04-28 03:15:11 -05:00
],
typename: 'Uint8ClampedArray'
2017-11-06 16:26:59 +01:00
}],
['int16Array', {
2018-10-30 23:19:19 +07:00
is: is.int16Array,
2020-01-22 12:08:35 +01:00
assert: assert.int16Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Int16Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Int16Array'
2017-11-06 16:26:59 +01:00
}],
['uint16Array', {
2018-10-30 23:19:19 +07:00
is: is.uint16Array,
2020-01-22 12:08:35 +01:00
assert: assert.uint16Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Uint16Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Uint16Array'
2017-11-06 16:26:59 +01:00
}],
['int32Array', {
2018-10-30 23:19:19 +07:00
is: is.int32Array,
2020-01-22 12:08:35 +01:00
assert: assert.int32Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Int32Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Int32Array'
2017-11-06 16:26:59 +01:00
}],
['uint32Array', {
2018-10-30 23:19:19 +07:00
is: is.uint32Array,
2020-01-22 12:08:35 +01:00
assert: assert.uint32Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Uint32Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Uint32Array'
2017-11-06 16:26:59 +01:00
}],
['float32Array', {
2018-10-30 23:19:19 +07:00
is: is.float32Array,
2020-01-22 12:08:35 +01:00
assert: assert.float32Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Float32Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Float32Array'
2017-11-06 16:26:59 +01:00
}],
['float64Array', {
2018-10-30 23:19:19 +07:00
is: is.float64Array,
2020-01-22 12:08:35 +01:00
assert: assert.float64Array,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 21:50:37 +07:00
new Float64Array()
2019-04-28 03:15:11 -05:00
],
typename: 'Float64Array'
2017-11-06 16:26:59 +01:00
}],
2019-11-07 15:56:02 +07:00
['bigInt64Array', {
is: is.bigInt64Array,
2020-01-22 12:08:35 +01:00
assert: assert.bigInt64Array,
2019-11-07 15:56:02 +07:00
fixtures: [
new BigInt64Array()
],
typename: 'BigInt64Array'
2019-11-07 15:56:02 +07:00
}],
['bigUint64Array', {
is: is.bigUint64Array,
2020-01-22 12:08:35 +01:00
assert: assert.bigUint64Array,
2019-11-07 15:56:02 +07:00
fixtures: [
new BigUint64Array()
],
typename: 'BigUint64Array'
2019-11-07 15:56:02 +07:00
}],
2017-11-06 16:26:59 +01:00
['arrayBuffer', {
2018-10-30 23:19:19 +07:00
is: is.arrayBuffer,
2020-01-22 12:08:35 +01:00
assert: assert.arrayBuffer,
2017-11-06 16:26:59 +01:00
fixtures: [
new ArrayBuffer(10)
2019-04-28 03:15:11 -05:00
],
typename: 'ArrayBuffer'
2017-11-06 16:26:59 +01:00
}],
['dataView', {
2018-10-30 23:19:19 +07:00
is: is.dataView,
2020-01-22 12:08:35 +01:00
assert: assert.dataView,
fixtures: [
new DataView(new ArrayBuffer(10))
2019-04-28 03:15:11 -05:00
],
typename: 'DataView'
}],
2017-11-06 16:26:59 +01:00
['nan', {
2018-10-30 23:19:19 +07:00
is: is.nan,
2020-01-22 12:08:35 +01:00
assert: assert.nan,
2017-11-06 16:26:59 +01:00
fixtures: [
NaN,
Number.NaN
2019-04-28 03:15:11 -05:00
],
typename: 'number',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.nan
2017-11-06 16:26:59 +01:00
}],
['nullOrUndefined', {
2018-10-30 23:19:19 +07:00
is: is.nullOrUndefined,
2020-01-22 12:08:35 +01:00
assert: assert.nullOrUndefined,
2017-11-06 16:26:59 +01:00
fixtures: [
null,
undefined
2020-01-22 12:08:35 +01:00
],
typeDescription: AssertionTypeDescription.nullOrUndefined
2017-11-06 16:26:59 +01:00
}],
['plainObject', {
2018-10-30 23:19:19 +07:00
is: is.plainObject,
2020-01-22 12:08:35 +01:00
assert: assert.plainObject,
2017-11-06 16:26:59 +01:00
fixtures: [
{x: 1},
Object.create(null),
2019-03-31 20:41:19 +07:00
new Object() // eslint-disable-line no-new-object
2019-04-28 03:15:11 -05:00
],
typename: 'Object',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.plainObject
2017-11-06 16:26:59 +01:00
}],
['integer', {
2018-10-30 23:19:19 +07:00
is: is.integer,
2020-01-22 12:08:35 +01:00
assert: assert.integer,
2017-11-06 16:26:59 +01:00
fixtures: [
6
2019-04-28 03:15:11 -05:00
],
typename: 'number',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.integer
2017-11-06 16:26:59 +01:00
}],
['safeInteger', {
2018-10-30 23:19:19 +07:00
is: is.safeInteger,
2020-01-22 12:08:35 +01:00
assert: assert.safeInteger,
2017-11-06 16:26:59 +01:00
fixtures: [
2019-03-31 20:41:19 +07:00
(2 ** 53) - 1,
-(2 ** 53) + 1
2019-04-28 03:15:11 -05:00
],
typename: 'number',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.safeInteger
2017-11-06 16:26:59 +01:00
}],
['domElement', {
2018-10-30 23:19:19 +07:00
is: is.domElement,
2020-01-22 12:08:35 +01:00
assert: assert.domElement,
2017-11-06 16:26:59 +01:00
fixtures: [
'div',
'input',
'span',
'img',
'canvas',
'script'
2020-01-22 12:08:35 +01:00
].map(createDomElement),
typeDescription: AssertionTypeDescription.domElement
2019-03-31 20:41:19 +07:00
}],
2017-11-19 15:16:06 -05:00
['non-domElements', {
2018-10-30 23:19:19 +07:00
is: value => !is.domElement(value),
2020-01-22 12:08:35 +01:00
assert: (value: unknown) => invertAssertThrow(AssertionTypeDescription.domElement, () => assert.domElement(value), value),
2017-11-06 16:26:59 +01:00
fixtures: [
document.createTextNode('data'),
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
document.createComment('This is a comment'),
document,
2017-11-07 10:23:00 +07:00
document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'),
2017-11-06 16:26:59 +01:00
document.createDocumentFragment()
]
}],
['observable', {
is: is.observable,
2020-01-22 12:08:35 +01:00
assert: assert.observable,
fixtures: [
new Observable(),
new Subject(),
2019-03-31 20:41:19 +07:00
new ZenObservable(() => {})
2019-04-28 03:15:11 -05:00
],
typename: 'Observable'
}],
2017-11-19 15:16:06 -05:00
['nodeStream', {
2018-10-30 23:19:19 +07:00
is: is.nodeStream,
2020-01-22 12:08:35 +01:00
assert: assert.nodeStream,
2017-11-19 15:16:06 -05:00
fixtures: [
fs.createReadStream('readme.md'),
fs.createWriteStream(tempy.file()),
new net.Socket(),
new Stream.Duplex(),
new Stream.PassThrough(),
new Stream.Readable(),
new Stream.Transform(),
new Stream.Stream(),
new Stream.Writable()
2019-04-28 03:15:11 -05:00
],
typename: 'Object',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.nodeStream
2017-11-19 15:16:06 -05:00
}],
2017-11-06 16:26:59 +01:00
['infinite', {
2018-10-30 23:19:19 +07:00
is: is.infinite,
2020-01-22 12:08:35 +01:00
assert: assert.infinite,
2017-11-06 16:26:59 +01:00
fixtures: [
Infinity,
-Infinity
2019-04-28 03:15:11 -05:00
],
typename: 'number',
2020-01-22 12:08:35 +01:00
typeDescription: AssertionTypeDescription.infinite
2017-11-06 16:26:59 +01:00
}]
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
const testType = (t: ExecutionContext, type: string, exclude?: string[]) => {
2017-11-06 16:26:59 +01:00
const testData = types.get(type);
if (testData === undefined) {
t.fail(`is.${type} not defined`);
return;
}
2020-01-22 12:08:35 +01:00
const {is: testIs, assert: testAssert, typename, typeDescription} = testData;
2017-11-06 16:26:59 +01:00
for (const [key, {fixtures}] of types) {
2017-09-22 00:44:27 +07:00
// TODO: Automatically exclude value types in other tests that we have in the current one.
// Could reduce the use of `exclude`.
2020-01-21 17:56:44 +01:00
if (exclude?.includes(key)) {
2017-09-22 00:44:27 +07:00
continue;
}
2019-04-28 03:15:11 -05:00
const isTypeUnderTest = key === type;
2020-02-22 02:01:58 +07:00
const assertIs = isTypeUnderTest ? t.true : t.false;
2017-09-22 00:44:27 +07:00
for (const fixture of fixtures) {
2020-01-22 12:08:35 +01:00
assertIs(testIs(fixture), `Value: ${inspect(fixture)}`);
2020-01-22 18:47:01 +07:00
const valueType = typeDescription ? typeDescription : typename;
2020-02-22 02:01:58 +07:00
if (isTypeUnderTest) {
t.notThrows(() => {
testAssert(fixture);
});
} else {
t.throws(() => {
testAssert(fixture);
}, {
message: `Expected value which is \`${valueType!}\`, received value of type \`${is(fixture)}\`.`
});
}
2019-04-28 03:15:11 -05:00
if (isTypeUnderTest && typename) {
t.is<TypeName>(is(fixture), typename);
2019-04-28 03:15:11 -05:00
}
2017-09-22 00:44:27 +07:00
}
}
};
test('is.undefined', t => {
testType(t, 'undefined', ['nullOrUndefined']);
});
test('is.null', t => {
testType(t, 'null', ['nullOrUndefined']);
});
test('is.string', t => {
2018-11-01 13:21:49 +02:00
testType(t, 'string', ['emptyString', 'numericString']);
2017-09-22 00:44:27 +07:00
});
test('is.number', t => {
testType(t, 'number', ['integer', 'safeInteger', 'infinite']);
2017-09-22 00:44:27 +07:00
});
2019-11-07 15:56:02 +07:00
test('is.bigint', t => {
testType(t, 'bigint');
});
2019-05-04 12:05:23 +03:00
2017-09-22 00:44:27 +07:00
test('is.boolean', t => {
testType(t, 'boolean');
});
test('is.symbol', t => {
testType(t, 'symbol');
});
2018-11-01 13:21:49 +02:00
test('is.numericString', t => {
testType(t, 'numericString');
t.false(is.numericString(''));
t.false(is.numericString(' '));
t.false(is.numericString(' \t\t\n'));
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
});
2017-09-22 00:44:27 +07:00
test('is.array', t => {
2018-09-28 11:54:35 +05:30
testType(t, 'array', ['emptyArray']);
2017-09-22 00:44:27 +07:00
});
test('is.function', t => {
testType(t, 'function', ['generatorFunction', 'asyncGeneratorFunction', 'asyncFunction', 'boundFunction']);
2017-11-10 19:11:35 +01:00
});
test('is.boundFunction', 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
});
test('is.buffer', t => {
testType(t, 'buffer');
});
test('is.object', t => {
2017-11-06 16:26:59 +01:00
const testData = types.get('object');
if (testData === undefined) {
t.fail('is.object not defined');
return;
}
for (const el of testData.fixtures) {
2018-10-30 23:19:19 +07:00
t.true(is.object(el));
2020-01-22 12:08:35 +01:00
t.notThrows(() => assert.object(el));
2017-09-22 00:44:27 +07:00
}
});
test('is.regExp', t => {
testType(t, 'regExp');
});
test('is.date', t => {
testType(t, 'date');
});
test('is.error', t => {
testType(t, 'error');
});
2019-03-31 21:50:37 +07:00
test('is.nativePromise', t => {
testType(t, 'nativePromise');
});
2017-09-22 00:44:27 +07:00
2019-03-31 21:50:37 +07:00
test('is.promise', t => {
testType(t, 'promise', ['nativePromise']);
});
2017-11-06 16:26:59 +01:00
2019-03-31 21:50:37 +07:00
test('is.asyncFunction', t => {
testType(t, 'asyncFunction', ['function']);
const fixture = async () => {};
if (is.asyncFunction(fixture)) {
// eslint-disable-next-line promise/prefer-await-to-then
t.true(is.function_(fixture().then));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
// eslint-disable-next-line promise/prefer-await-to-then
assert.function_(fixture().then);
});
}
2019-03-31 21:50:37 +07:00
});
2017-09-22 00:44:27 +07:00
test('is.generator', t => {
2017-09-29 10:10:29 +07:00
testType(t, 'generator');
});
test('is.asyncGenerator', t => {
testType(t, 'asyncGenerator');
const fixture = (async function * () {
yield 4;
})();
if (is.asyncGenerator(fixture)) {
t.true(is.function_(fixture.next));
}
});
test('is.generatorFunction', t => {
2017-09-29 10:10:29 +07:00
testType(t, 'generatorFunction', ['function']);
});
test('is.asyncGeneratorFunction', t => {
testType(t, 'asyncGeneratorFunction', ['function']);
const fixture = async function * () {
yield 4;
};
if (is.asyncGeneratorFunction(fixture)) {
t.true(is.function_(fixture().next));
}
});
2017-09-22 00:44:27 +07:00
test('is.map', t => {
2018-09-28 11:54:35 +05:30
testType(t, 'map', ['emptyMap']);
2017-09-22 00:44:27 +07:00
});
test('is.set', t => {
2018-09-28 11:54:35 +05:30
testType(t, 'set', ['emptySet']);
2017-09-22 00:44:27 +07:00
});
test('is.weakMap', t => {
testType(t, 'weakMap');
});
test('is.weakSet', t => {
testType(t, 'weakSet');
});
test('is.int8Array', t => {
testType(t, 'int8Array');
});
test('is.uint8Array', t => {
testType(t, 'uint8Array', ['buffer']);
});
test('is.uint8ClampedArray', t => {
testType(t, 'uint8ClampedArray');
});
test('is.int16Array', t => {
testType(t, 'int16Array');
});
test('is.uint16Array', t => {
testType(t, 'uint16Array');
});
test('is.int32Array', t => {
testType(t, 'int32Array');
});
test('is.uint32Array', t => {
testType(t, 'uint32Array');
});
test('is.float32Array', t => {
testType(t, 'float32Array');
});
test('is.float64Array', t => {
testType(t, 'float64Array');
});
2019-11-07 15:56:02 +07:00
test('is.bigInt64Array', t => {
testType(t, 'bigInt64Array');
});
2019-05-04 12:05:23 +03:00
2019-11-07 15:56:02 +07:00
test('is.bigUint64Array', t => {
testType(t, 'bigUint64Array');
});
2019-05-04 12:05:23 +03:00
2017-09-22 00:44:27 +07:00
test('is.arrayBuffer', t => {
testType(t, 'arrayBuffer');
});
test('is.dataView', t => {
testType(t, 'dataView');
2017-09-22 00:44:27 +07:00
});
2017-12-09 11:55:08 -05:00
test('is.directInstanceOf', t => {
const error = new Error();
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);
});
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
});
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));
2019-11-07 15:56:02 +07:00
// Disabled until TS supports it for an ESnnnn target.
// t.true(is.truthy(1n));
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');
});
t.notThrows(() => {
assert.truthy('🦄');
});
t.notThrows(() => {
assert.truthy(new Set());
});
t.notThrows(() => {
assert.truthy(Symbol('🦄'));
});
t.notThrows(() => {
assert.truthy(true);
});
t.notThrows(() => {
assert.truthy(1);
});
2020-01-22 12:08:35 +01:00
2020-01-22 18:47:01 +07:00
// TODO: Disabled until TS supports it for an ESnnnn target.
2020-01-22 12:08:35 +01:00
// t.notThrows(() => assert.truthy(1n));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.truthy(BigInt(1));
});
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));
t.true(is.falsy(NaN));
2020-01-22 18:47:01 +07:00
// TODO: Disabled until TS supports it for an ESnnnn target.
2019-11-07 15:56:02 +07:00
// t.true(is.falsy(0n));
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);
});
t.notThrows(() => {
assert.falsy(0);
});
t.notThrows(() => {
assert.falsy('');
});
t.notThrows(() => {
assert.falsy(null);
});
t.notThrows(() => {
assert.falsy(undefined);
});
t.notThrows(() => {
assert.falsy(NaN);
});
// TODO: Disabled until TS supports it for an ESnnnn target.
2020-01-22 12:08:35 +01:00
// t.notThrows(() => assert.falsy(0n));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.falsy(BigInt(0));
});
2017-10-20 14:49:52 +00:00
});
2017-09-22 00:44:27 +07:00
test('is.nan', t => {
testType(t, 'nan');
});
test('is.nullOrUndefined', t => {
testType(t, 'nullOrUndefined', ['undefined', 'null']);
});
test('is.primitive', t => {
const primitives: Primitive[] = [
2017-09-22 00:44:27 +07:00
undefined,
null,
'🦄',
6,
Infinity,
-Infinity,
true,
false,
Symbol('🦄')
2019-11-07 15:56:02 +07:00
// Disabled until TS supports it for an ESnnnn target.
// 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
}
});
test('is.integer', t => {
2017-10-17 11:19:17 -04:00
testType(t, 'integer', ['number', 'safeInteger']);
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
});
2017-10-17 11:19:17 -04:00
test('is.safeInteger', t => {
testType(t, 'safeInteger', ['number', 'integer']);
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.plainObject', t => {
testType(t, 'plainObject', ['object', 'promise']);
});
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));
t.false(is.iterable(NaN));
t.false(is.iterable(Infinity));
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(() => {
assert.iterable(NaN);
});
t.throws(() => {
assert.iterable(Infinity);
});
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({
[Symbol.asyncIterator]: () => {}
}));
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));
t.false(is.asyncIterable(NaN));
t.false(is.asyncIterable(Infinity));
t.false(is.asyncIterable({}));
2020-01-22 12:08:35 +01:00
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.asyncIterable({
[Symbol.asyncIterator]: () => { }
});
});
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(() => {
assert.asyncIterable(NaN);
});
t.throws(() => {
assert.asyncIterable(Infinity);
});
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,
2019-03-31 20:41:19 +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) {
t.true(is.class_(classDeclaration));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.class_(classDeclaration);
});
2017-09-24 22:26:15 +03:00
}
});
2017-09-22 00:44:27 +07:00
test('is.typedArray', t => {
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(),
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 () {
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
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
});
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));
t.throws(() => {
2018-10-30 23:19:19 +07:00
is.inRange(0, []);
});
2017-09-26 02:32:58 +07:00
t.throws(() => {
2018-10-30 23:19:19 +07:00
is.inRange(0, [5]);
2017-09-26 02:32:58 +07:00
});
t.throws(() => {
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(() => {
assert.inRange(0, []);
});
t.throws(() => {
assert.inRange(0, [5]);
});
t.throws(() => {
assert.inRange(0, [1, 2, 3]);
});
2017-09-25 20:20:06 +01:00
});
2017-10-05 00:18:25 -07:00
2017-10-07 09:19:11 -07:00
test('is.domElement', t => {
testType(t, 'domElement');
2018-10-30 23:19:19 +07:00
t.false(is.domElement({nodeType: 1, nodeName: 'div'}));
2020-01-22 18:47:01 +07:00
t.throws(() => {
assert.domElement({nodeType: 1, nodeName: 'div'});
});
2019-04-28 03:15:11 -05:00
const tagNames = [
'div',
'input',
'span',
'img',
'canvas',
'script'
];
2019-04-28 03:15:11 -05:00
for (const tagName of tagNames) {
2019-04-28 03:15:11 -05:00
const domElement = createDomElement(tagName);
t.is(is(domElement), 'HTMLElement');
2019-04-28 03:15:11 -05:00
}
2017-10-07 09:19:11 -07:00
});
test('is.observable', t => {
testType(t, 'observable');
});
2017-11-19 15:16:06 -05:00
test('is.nodeStream', t => {
testType(t, 'nodeStream');
});
2017-10-05 00:18:25 -07:00
test('is.infinite', t => {
testType(t, 'infinite', ['number']);
});
2017-10-06 02:39:36 -05:00
test('is.evenInteger', t => {
2017-10-17 11:36:10 -04:00
for (const el of [-6, 2, 4]) {
t.true(is.evenInteger(el));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.evenInteger(el);
});
2017-10-17 11:36:10 -04:00
}
for (const el of [-3, 1, 5]) {
t.false(is.evenInteger(el));
2020-01-22 18:47:01 +07:00
t.throws(() => {
assert.evenInteger(el);
});
2017-10-17 11:36:10 -04:00
}
});
test('is.oddInteger', t => {
2017-10-17 11:36:10 -04:00
for (const el of [-5, 7, 13]) {
t.true(is.oddInteger(el));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.oddInteger(el);
});
2017-10-17 11:36:10 -04:00
}
for (const el of [-8, 8, 10]) {
t.false(is.oddInteger(el));
2020-01-22 18:47:01 +07:00
t.throws(() => {
assert.oddInteger(el);
});
2017-10-17 11:36:10 -04:00
}
});
2018-09-28 11:54:35 +05:30
test('is.emptyArray', t => {
testType(t, 'emptyArray');
});
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
});
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.emptyString', t => {
testType(t, 'emptyString', ['string']);
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
2018-09-28 11:54:35 +05:30
test('is.nonEmptyString', t => {
2018-10-30 23:19:19 +07:00
t.false(is.nonEmptyString(''));
t.false(is.nonEmptyString(String()));
t.true(is.nonEmptyString('🦄'));
2020-01-22 12:08:35 +01:00
2020-01-22 18:47:01 +07:00
t.throws(() => {
assert.nonEmptyString('');
});
t.throws(() => {
assert.nonEmptyString(String());
});
t.notThrows(() => {
assert.nonEmptyString('🦄');
});
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.emptyStringOrWhitespace', t => {
testType(t, 'emptyString', ['string']);
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');
});
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({}));
2019-03-31 20:41:19 +07:00
t.true(is.emptyObject(new Object())); // eslint-disable-line no-new-object
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(() => {
assert.emptyObject(new Object()); // eslint-disable-line no-new-object
});
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({}));
2019-03-31 20:41:19 +07:00
t.false(is.nonEmptyObject(new Object())); // eslint-disable-line no-new-object
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(() => {
assert.nonEmptyObject(new Object()); // eslint-disable-line no-new-object
});
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.emptySet', t => {
testType(t, 'emptySet');
});
2017-10-06 02:39:36 -05:00
2018-09-28 11:54:35 +05:30
test('is.nonEmptySet', t => {
2017-10-06 02:39:36 -05:00
const tempSet = new Set();
2018-10-30 23:19:19 +07:00
t.false(is.nonEmptySet(tempSet));
2020-01-22 18:47:01 +07:00
t.throws(() => {
assert.nonEmptySet(tempSet);
});
2017-10-06 02:39:36 -05:00
tempSet.add(1);
2018-10-30 23:19:19 +07:00
t.true(is.nonEmptySet(tempSet));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.nonEmptySet(tempSet);
});
2018-09-28 11:54:35 +05:30
});
test('is.emptyMap', t => {
testType(t, 'emptyMap');
2017-10-06 02:39:36 -05:00
});
2018-09-28 11:54:35 +05:30
test('is.nonEmptyMap', t => {
const tempMap = new Map();
2018-10-30 23:19:19 +07:00
t.false(is.nonEmptyMap(tempMap));
2020-01-22 18:47:01 +07:00
t.throws(() => {
assert.nonEmptyMap(tempMap);
});
2018-09-28 11:54:35 +05:30
tempMap.set('unicorn', '🦄');
2018-10-30 23:19:19 +07:00
t.true(is.nonEmptyMap(tempMap));
2020-01-22 18:47:01 +07:00
t.notThrows(() => {
assert.nonEmptyMap(tempMap);
});
2017-10-17 16:46:58 -04: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', {}));
t.true(is.any([is.string, is.number], {}, true, '🦄'));
t.false(is.any([is.boolean, is.number], 'unicorns', [], new Map()));
t.throws(() => {
2019-02-04 02:13:23 +07:00
is.any(null as any, true);
});
t.throws(() => {
2018-10-30 23:19:19 +07:00
is.any(is.string);
});
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(() => {
assert.any(null as any, true);
});
t.throws(() => {
assert.any(is.string);
});
});
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(), {}));
t.throws(() => {
2019-02-04 02:13:23 +07:00
is.all(null as any, true);
});
t.throws(() => {
2018-10-30 23:19:19 +07:00
is.all(is.string);
});
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(() => {
assert.all(null as any, true);
});
t.throws(() => {
assert.all(is.string);
});
});
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));
}
});