is/source/tests/test.ts

892 lines
17 KiB
TypeScript
Raw Normal View History

2018-05-19 15:42:12 +07:00
import fs from 'fs';
import net from 'net';
import Stream from 'stream';
import util from 'util';
import tempy from 'tempy';
2018-07-10 12:04:20 +03:00
import {URL} from 'url';
2017-11-06 16:26:59 +01:00
import test, {TestContext, Context} from 'ava';
import {JSDOM} from 'jsdom';
2018-05-03 05:22:32 +02:00
import {Subject, Observable} from 'rxjs';
import ZenObservable from 'zen-observable';
2018-10-30 23:19:19 +07:00
import is from '..';
2017-09-22 00:44:27 +07:00
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
2018-07-09 20:35:16 +03:00
const isNode10orHigher = Number(process.versions.node.split('.')[0]) >= 10;
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 {
fixtures: unknown[];
2018-11-30 14:28:54 +07:00
is(value: unknown): boolean;
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,
2017-11-06 16:26:59 +01:00
fixtures: [
undefined
]
}],
['null', {
2018-10-30 23:19:19 +07:00
is: is.null_,
2017-11-06 16:26:59 +01:00
fixtures: [
null
]
}],
['string', {
2018-10-30 23:19:19 +07:00
is: is.string,
2017-11-06 16:26:59 +01:00
fixtures: [
'🦄',
'hello world',
''
]
}],
2018-09-28 11:54:35 +05:30
['emptyString', {
2018-10-30 23:19:19 +07:00
is: is.emptyString,
2018-09-28 11:54:35 +05:30
fixtures: [
'',
String()
]
}],
2017-11-06 16:26:59 +01:00
['number', {
2018-10-30 23:19:19 +07:00
is: is.number,
2017-11-06 16:26:59 +01:00
fixtures: [
6,
1.4,
0,
-0,
Infinity,
-Infinity
]
}],
['boolean', {
2018-10-30 23:19:19 +07:00
is: is.boolean,
2017-11-06 16:26:59 +01:00
fixtures: [
true, false
]
}],
['symbol', {
2018-10-30 23:19:19 +07:00
is: is.symbol,
2017-11-06 16:26:59 +01:00
fixtures: [
Symbol('🦄')
]
}],
2018-11-01 13:21:49 +02:00
['numericString', {
is: is.numericString,
2018-11-01 13:21:49 +02:00
fixtures: [
'5',
'-3.2',
'Infinity'
]
}],
2017-11-06 16:26:59 +01:00
['array', {
2018-10-30 23:19:19 +07:00
is: is.array,
2017-11-06 16:26:59 +01:00
fixtures: [
[1, 2],
new Array(2) // tslint:disable-line:prefer-array-literal
]
}],
2018-09-28 11:54:35 +05:30
['emptyArray', {
2018-10-30 23:19:19 +07:00
is: is.emptyArray,
2018-09-28 11:54:35 +05:30
fixtures: [
[],
new Array()
]
}],
2017-11-06 16:26:59 +01:00
['function', {
2018-10-30 23:19:19 +07:00
is: is.function_,
2017-11-06 16:26:59 +01:00
fixtures: [
2017-11-07 10:23:00 +07:00
// tslint:disable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression
function foo() {},
function () {},
2017-11-06 16:26:59 +01:00
() => {},
2017-11-07 10:23:00 +07:00
async function () {},
function * (): unknown {}
2017-11-07 10:23:00 +07:00
// tslint:enable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression
2017-11-06 16:26:59 +01:00
]
}],
['buffer', {
2018-10-30 23:19:19 +07:00
is: is.buffer,
2017-11-06 16:26:59 +01:00
fixtures: [
Buffer.from('🦄')
]
}],
['object', {
2018-10-30 23:19:19 +07:00
is: is.object,
2017-11-06 16:26:59 +01:00
fixtures: [
{x: 1},
Object.create({x: 1})
]
}],
['regExp', {
2018-10-30 23:19:19 +07:00
is: is.regExp,
2017-11-06 16:26:59 +01:00
fixtures: [
/\w/,
new RegExp('\\w')
]
}],
['date', {
2018-10-30 23:19:19 +07:00
is: is.date,
2017-11-06 16:26:59 +01:00
fixtures: [
new Date()
]
}],
['error', {
2018-10-30 23:19:19 +07:00
is: is.error,
2017-11-06 16:26:59 +01:00
fixtures: [
new Error('🦄'),
new ErrorSubclassFixture()
]
}],
['nativePromise', {
2018-10-30 23:19:19 +07:00
is: is.nativePromise,
2017-11-06 16:26:59 +01:00
fixtures: [
Promise.resolve(),
PromiseSubclassFixture.resolve()
2017-11-06 16:26:59 +01:00
]
}],
['promise', {
2018-10-30 23:19:19 +07:00
is: is.promise,
2017-11-06 16:26:59 +01:00
fixtures: [
{then() {}, catch() {}} // tslint:disable-line:no-empty
]
}],
['generator', {
2018-10-30 23:19:19 +07:00
is: is.generator,
2017-11-06 16:26:59 +01:00
fixtures: [
2017-11-07 10:23:00 +07:00
(function * () {
yield 4;
})()
2017-11-06 16:26:59 +01:00
]
}],
['generatorFunction', {
2018-10-30 23:19:19 +07:00
is: is.generatorFunction,
2017-11-06 16:26:59 +01:00
fixtures: [
2017-11-07 10:23:00 +07:00
function * () {
yield 4;
}
2017-11-06 16:26:59 +01:00
]
}],
['asyncFunction', {
2018-10-30 23:19:19 +07:00
is: is.asyncFunction,
2017-11-06 16:26:59 +01:00
fixtures: [
2017-11-07 10:23:00 +07:00
async function () {}, // tslint:disable-line:no-empty only-arrow-functions no-function-expression
2017-11-06 16:26:59 +01:00
async () => {} // tslint:disable-line:no-empty
]
}],
2017-11-10 19:11:35 +01:00
['boundFunction', {
2018-10-30 23:19:19 +07:00
is: is.boundFunction,
2017-11-10 19:11:35 +01:00
fixtures: [
() => {}, // tslint:disable-line:no-empty
function () {}.bind(null), // tslint:disable-line:no-empty only-arrow-functions
]
}],
2017-11-06 16:26:59 +01:00
['map', {
2018-10-30 23:19:19 +07:00
is: is.map,
2017-11-06 16:26:59 +01:00
fixtures: [
2018-09-28 11:54:35 +05:30
new Map([['one', '1']]),
]
}],
['emptyMap', {
2018-10-30 23:19:19 +07:00
is: is.emptyMap,
2018-09-28 11:54:35 +05:30
fixtures: [
new Map(),
2017-11-06 16:26:59 +01:00
]
}],
['set', {
2018-10-30 23:19:19 +07:00
is: is.set,
2017-11-06 16:26:59 +01:00
fixtures: [
2018-09-28 11:54:35 +05:30
new Set(['one'])
]
}],
['emptySet', {
2018-10-30 23:19:19 +07:00
is: is.emptySet,
2018-09-28 11:54:35 +05:30
fixtures: [
new Set(),
2017-11-06 16:26:59 +01:00
]
}],
['weakSet', {
2018-10-30 23:19:19 +07:00
is: is.weakSet,
2017-11-06 16:26:59 +01:00
fixtures: [
new WeakSet()
]
}],
['weakMap', {
2018-10-30 23:19:19 +07:00
is: is.weakMap,
2017-11-06 16:26:59 +01:00
fixtures: [
new WeakMap()
]
}],
['int8Array', {
2018-10-30 23:19:19 +07:00
is: is.int8Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Int8Array(0)
]
}],
['uint8Array', {
2018-10-30 23:19:19 +07:00
is: is.uint8Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint8Array(0)
]
}],
['uint8ClampedArray', {
2018-10-30 23:19:19 +07:00
is: is.uint8ClampedArray,
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint8ClampedArray(0)
]
}],
['int16Array', {
2018-10-30 23:19:19 +07:00
is: is.int16Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Int16Array(0)
]
}],
['uint16Array', {
2018-10-30 23:19:19 +07:00
is: is.uint16Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint16Array(0)
]
}],
['int32Array', {
2018-10-30 23:19:19 +07:00
is: is.int32Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Int32Array(0)
]
}],
['uint32Array', {
2018-10-30 23:19:19 +07:00
is: is.uint32Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint32Array(0)
]
}],
['float32Array', {
2018-10-30 23:19:19 +07:00
is: is.float32Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Float32Array(0)
]
}],
['float64Array', {
2018-10-30 23:19:19 +07:00
is: is.float64Array,
2017-11-06 16:26:59 +01:00
fixtures: [
new Float64Array(0)
]
}],
['arrayBuffer', {
2018-10-30 23:19:19 +07:00
is: is.arrayBuffer,
2017-11-06 16:26:59 +01:00
fixtures: [
new ArrayBuffer(10)
]
}],
['dataView', {
2018-10-30 23:19:19 +07:00
is: is.dataView,
fixtures: [
new DataView(new ArrayBuffer(10))
]
}],
2017-11-06 16:26:59 +01:00
['nan', {
2018-10-30 23:19:19 +07:00
is: is.nan,
2017-11-06 16:26:59 +01:00
fixtures: [
NaN,
Number.NaN
]
}],
['nullOrUndefined', {
2018-10-30 23:19:19 +07:00
is: is.nullOrUndefined,
2017-11-06 16:26:59 +01:00
fixtures: [
null,
undefined
]
}],
['plainObject', {
2018-10-30 23:19:19 +07:00
is: is.plainObject,
2017-11-06 16:26:59 +01:00
fixtures: [
{x: 1},
Object.create(null),
new Object()
]
}],
['integer', {
2018-10-30 23:19:19 +07:00
is: is.integer,
2017-11-06 16:26:59 +01:00
fixtures: [
6
]
}],
['safeInteger', {
2018-10-30 23:19:19 +07:00
is: is.safeInteger,
2017-11-06 16:26:59 +01:00
fixtures: [
Math.pow(2, 53) - 1,
-Math.pow(2, 53) + 1
]
}],
['domElement', {
2018-10-30 23:19:19 +07:00
is: is.domElement,
2017-11-06 16:26:59 +01:00
fixtures: [
'div',
'input',
'span',
'img',
'canvas',
'script'
].map(createDomElement) }
2017-11-19 15:16:06 -05:00
],
['non-domElements', {
2018-10-30 23:19:19 +07:00
is: value => !is.domElement(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,
fixtures: [
new Observable(),
new Subject(),
new ZenObservable(() => {}) // tslint:disable-line:no-empty
]
}],
2017-11-19 15:16:06 -05:00
['nodeStream', {
2018-10-30 23:19:19 +07:00
is: is.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()
]
}],
2017-11-06 16:26:59 +01:00
['infinite', {
2018-10-30 23:19:19 +07:00
is: is.infinite,
2017-11-06 16:26:59 +01:00
fixtures: [
Infinity,
-Infinity
]
}]
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: TestContext & Context<unknown>, 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;
}
2018-10-30 23:19:19 +07:00
const {is: testIs} = 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`.
if (exclude && exclude.indexOf(key) !== -1) {
continue;
}
const assert = key === type ? t.true.bind(t) : t.false.bind(t);
for (const fixture of fixtures) {
2018-10-30 23:19:19 +07:00
assert(testIs(fixture), `Value: ${util.inspect(fixture)}`);
2017-09-22 00:44:27 +07:00
}
}
};
test('is', t => {
2018-10-30 23:19:19 +07:00
t.is(is(null), 'null');
t.is(is(undefined), 'undefined');
// TODO: Expand this to all the supported types. Maybe reuse `testType()` somehow.
});
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 => {
2017-10-17 11:19:17 -04:00
testType(t, 'number', ['nan', 'integer', 'safeInteger', 'infinite']);
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(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 => {
2017-11-10 19:11:35 +01:00
testType(t, 'function', ['generatorFunction', 'asyncFunction', 'boundFunction']);
});
test('is.boundFunction', t => {
2018-10-30 23:19:19 +07:00
t.false(is.boundFunction(function () {})); // tslint:disable-line:no-empty only-arrow-functions
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));
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');
});
if (isNode8orHigher) {
test('is.nativePromise', t => {
testType(t, 'nativePromise');
});
test('is.promise', t => {
testType(t, 'promise', ['nativePromise']);
});
2017-11-06 16:26:59 +01:00
/*test('is.asyncFunction', t => {
testType(t, 'asyncFunction', ['function']);
});*/
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.generatorFunction', t => {
2017-09-29 10:10:29 +07:00
testType(t, 'generatorFunction', ['function']);
});
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');
});
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));
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));
2017-12-09 11:55:08 -05:00
});
2018-07-10 12:04:20 +03:00
test('is.urlInstance', t => {
2018-12-13 17:52:21 +02: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));
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));
t.false(is.urlString(new URL(url)));
t.false(is.urlString({}));
t.false(is.urlString(undefined));
t.false(is.urlString(null));
});
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));
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));
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 = [
undefined,
null,
'🦄',
6,
Infinity,
-Infinity,
true,
false,
Symbol('🦄')
];
for (const el of primitives) {
2018-10-30 23:19:19 +07:00
t.true(is.primitive(el));
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));
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']);
2018-10-30 23:19:19 +07:00
t.false(is.safeInteger(Math.pow(2, 53)));
t.false(is.safeInteger(-Math.pow(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({}));
2017-09-22 00:44:27 +07:00
});
2018-07-09 20:35:16 +03:00
if (isNode10orHigher) {
test('is.asyncIterable', t => {
2018-10-30 23:19:19 +07:00
t.true(is.asyncIterable({
2018-07-09 20:35:16 +03:00
[Symbol.asyncIterator]: () => {} // tslint:disable-line:no-empty
}));
2018-10-30 23:19:19 +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({}));
2018-07-09 20:35:16 +03:00
});
}
2017-09-24 22:26:15 +03:00
test('is.class', t => {
2017-11-06 16:26:59 +01:00
class Foo {} // tslint:disable-line
2017-09-24 22:26:15 +03:00
const classDeclarations = [
Foo,
2017-11-06 16:26:59 +01:00
class Bar extends Foo {} // tslint:disable-line
2017-09-24 22:26:15 +03:00
];
for (const x of classDeclarations) {
2018-10-30 23:19:19 +07:00
t.true(is.class_(x));
2017-09-24 22:26:15 +03:00
}
});
2017-09-22 00:44:27 +07:00
test('is.typedArray', t => {
2018-12-13 00:58:02 +01:00
// TypeScript currently does not support empty constructors for these
2017-11-06 16:26:59 +01:00
// See https://github.com/Microsoft/TypeScript/issues/19680
2018-12-13 00:58:02 +01:00
// TODO: Remove the `0` when targeting `es2017` (Node.js 8), in other places of this file too.
2017-09-22 00:44:27 +07:00
const typedArrays = [
2017-11-06 16:26:59 +01:00
new Int8Array(0),
new Uint8Array(0),
new Uint8ClampedArray(0),
new Uint16Array(0),
new Int32Array(0),
new Uint32Array(0),
new Float32Array(0),
new Float64Array(0)
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));
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({}));
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 => {
2017-11-07 10:23:00 +07:00
(function () { // tslint:disable-line:only-arrow-functions
2018-10-30 23:19:19 +07:00
t.true(is.arrayLike(arguments));
2017-10-26 09:30:17 -04: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({}));
t.false(is.arrayLike(() => {})); // tslint:disable-line:no-empty
t.false(is.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
});
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'}));
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
2017-10-17 11:36:10 -04:00
test('is.even', t => {
for (const el of [-6, 2, 4]) {
2018-10-30 23:19:19 +07:00
t.true(is.even(el));
2017-10-17 11:36:10 -04:00
}
for (const el of [-3, 1, 5]) {
2018-10-30 23:19:19 +07:00
t.false(is.even(el));
2017-10-17 11:36:10 -04:00
}
});
test('is.odd', t => {
for (const el of [-5, 7, 13]) {
2018-10-30 23:19:19 +07:00
t.true(is.odd(el));
2017-10-17 11:36:10 -04:00
}
for (const el of [-8, 8, 10]) {
2018-10-30 23:19:19 +07:00
t.false(is.odd(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([]));
t.false(is.nonEmptyArray(new Array()));
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('🦄'));
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('🦄'));
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'));
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({}));
t.true(is.emptyObject(new Object()));
t.false(is.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({}));
t.false(is.nonEmptyObject(new Object()));
t.true(is.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));
2017-10-06 02:39:36 -05:00
tempSet.add(1);
2018-10-30 23:19:19 +07:00
t.true(is.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));
2018-09-28 11:54:35 +05:30
tempMap.set('unicorn', '🦄');
2018-10-30 23:19:19 +07:00
t.true(is.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.throws(() => {
2018-10-30 23:19:19 +07:00
is.any(null, true);
});
t.throws(() => {
2018-10-30 23:19:19 +07:00
is.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(() => {
2018-10-30 23:19:19 +07:00
is.all(null, true);
});
t.throws(() => {
2018-10-30 23:19:19 +07:00
is.all(is.string);
});
});