is/source/tests/test.ts

807 lines
15 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';
2017-11-06 16:26:59 +01:00
import m 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;
2017-11-06 16:26:59 +01:00
const createDomElement = (el: string) => document.createElement(el);
2017-10-07 09:19:11 -07:00
2017-11-06 16:26:59 +01:00
interface Test {
is(value: any): boolean;
fixtures: any[];
}
const types = new Map<string, Test>([
['undefined', {
is: m.undefined,
fixtures: [
undefined
]
}],
['null', {
is: m.null_,
fixtures: [
null
]
}],
['string', {
is: m.string,
fixtures: [
'🦄',
'hello world',
''
]
}],
['number', {
is: m.number,
fixtures: [
6,
1.4,
0,
-0,
Infinity,
-Infinity
]
}],
['boolean', {
is: m.boolean,
fixtures: [
true, false
]
}],
['symbol', {
is: m.symbol,
fixtures: [
Symbol('🦄')
]
}],
['array', {
is: m.array,
fixtures: [
[1, 2],
new Array(2) // tslint:disable-line:prefer-array-literal
]
}],
['function', {
is: m.function_,
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 * (): any {}
// tslint:enable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression
2017-11-06 16:26:59 +01:00
]
}],
['buffer', {
is: m.buffer,
fixtures: [
Buffer.from('🦄')
]
}],
['object', {
is: m.object,
fixtures: [
{x: 1},
Object.create({x: 1})
]
}],
['regExp', {
is: m.regExp,
fixtures: [
/\w/,
new RegExp('\\w')
]
}],
['date', {
is: m.date,
fixtures: [
new Date()
]
}],
['error', {
is: m.error,
fixtures: [
new Error('🦄'),
new ErrorSubclassFixture()
]
}],
['nativePromise', {
is: m.nativePromise,
fixtures: [
Promise.resolve(),
PromiseSubclassFixture.resolve()
2017-11-06 16:26:59 +01:00
]
}],
['promise', {
is: m.promise,
fixtures: [
{then() {}, catch() {}} // tslint:disable-line:no-empty
]
}],
['generator', {
is: m.generator,
fixtures: [
2017-11-07 10:23:00 +07:00
(function * () {
yield 4;
})()
2017-11-06 16:26:59 +01:00
]
}],
['generatorFunction', {
is: m.generatorFunction,
fixtures: [
2017-11-07 10:23:00 +07:00
function * () {
yield 4;
}
2017-11-06 16:26:59 +01:00
]
}],
['asyncFunction', {
is: m.asyncFunction,
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', {
is: m.boundFunction,
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', {
is: m.map,
fixtures: [
new Map()
]
}],
['set', {
is: m.set,
fixtures: [
new Set()
]
}],
['weakSet', {
is: m.weakSet,
fixtures: [
new WeakSet()
]
}],
['weakMap', {
is: m.weakMap,
fixtures: [
new WeakMap()
]
}],
['int8Array', {
is: m.int8Array,
fixtures: [
new Int8Array(0)
]
}],
['uint8Array', {
is: m.uint8Array,
fixtures: [
new Uint8Array(0)
]
}],
['uint8ClampedArray', {
is: m.uint8ClampedArray,
fixtures: [
new Uint8ClampedArray(0)
]
}],
['int16Array', {
is: m.int16Array,
fixtures: [
new Int16Array(0)
]
}],
['uint16Array', {
is: m.uint16Array,
fixtures: [
new Uint16Array(0)
]
}],
['int32Array', {
is: m.int32Array,
fixtures: [
new Int32Array(0)
]
}],
['uint32Array', {
is: m.uint32Array,
fixtures: [
new Uint32Array(0)
]
}],
['float32Array', {
is: m.float32Array,
fixtures: [
new Float32Array(0)
]
}],
['float64Array', {
is: m.float64Array,
fixtures: [
new Float64Array(0)
]
}],
['arrayBuffer', {
is: m.arrayBuffer,
fixtures: [
new ArrayBuffer(10)
]
}],
['dataView', {
is: m.dataView,
fixtures: [
new DataView(new ArrayBuffer(10))
]
}],
2017-11-06 16:26:59 +01:00
['nan', {
is: m.nan,
fixtures: [
NaN,
Number.NaN
]
}],
['nullOrUndefined', {
is: m.nullOrUndefined,
fixtures: [
null,
undefined
]
}],
['plainObject', {
is: m.plainObject,
fixtures: [
{x: 1},
Object.create(null),
new Object()
]
}],
['integer', {
is: m.integer,
fixtures: [
6
]
}],
['safeInteger', {
is: m.safeInteger,
fixtures: [
Math.pow(2, 53) - 1,
-Math.pow(2, 53) + 1
]
}],
['domElement', {
is: m.domElement,
fixtures: [
'div',
'input',
'span',
'img',
'canvas',
'script'
].map(createDomElement) }
2017-11-19 15:16:06 -05:00
],
['non-domElements', {
2017-11-06 16:26:59 +01:00
is: value => !m.domElement(value),
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()
]
}],
2017-11-19 15:16:06 -05:00
['nodeStream', {
is: m.nodeStream,
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()
]
}],
2018-05-03 05:22:32 +02:00
['observable', {
is: m.observable,
fixtures: [
new Observable(),
new Subject(),
new ZenObservable(() => {}) // tslint:disable-line:no-empty
]
}],
2017-11-06 16:26:59 +01:00
['infinite', {
is: m.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<any>, type: string, exclude?: string[]) => {
const testData = types.get(type);
if (testData === undefined) {
t.fail(`is.${type} not defined`);
return;
}
const {is} = testData;
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) {
assert(is(fixture), `Value: ${util.inspect(fixture)}`);
}
}
};
test('is', t => {
t.is(m(null), 'null');
t.is(m(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 => {
testType(t, 'string');
});
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');
});
test('is.array', t => {
testType(t, 'array');
});
test('is.function', t => {
2017-11-10 19:11:35 +01:00
testType(t, 'function', ['generatorFunction', 'asyncFunction', 'boundFunction']);
});
test('is.boundFunction', t => {
t.false(m.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) {
2017-09-22 00:44:27 +07:00
t.true(m.object(el));
}
});
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 => {
testType(t, 'map');
});
test('is.set', t => {
testType(t, 'set');
});
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();
t.true(m.directInstanceOf(error, Error));
t.true(m.directInstanceOf(errorSubclass, ErrorSubclassFixture));
t.false(m.directInstanceOf(error, ErrorSubclassFixture));
t.false(m.directInstanceOf(errorSubclass, Error));
});
2018-07-10 12:04:20 +03:00
test('is.urlInstance', t => {
const url = new URL('https://www.example.com');
t.true(m.urlInstance(url));
t.false(m.urlInstance({}));
t.false(m.urlInstance(undefined));
t.false(m.urlInstance(null));
});
2017-10-20 14:49:52 +00:00
test('is.truthy', t => {
t.true(m.truthy('unicorn'));
t.true(m.truthy('🦄'));
t.true(m.truthy(new Set()));
t.true(m.truthy(Symbol('🦄')));
t.true(m.truthy(true));
});
test('is.falsy', t => {
t.true(m.falsy(false));
t.true(m.falsy(0));
t.true(m.falsy(''));
t.true(m.falsy(null));
t.true(m.falsy(undefined));
t.true(m.falsy(NaN));
});
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) {
t.true(m.primitive(el));
}
});
test('is.integer', t => {
2017-10-17 11:19:17 -04:00
testType(t, 'integer', ['number', 'safeInteger']);
2017-09-22 00:44:27 +07:00
t.false(m.integer(1.4));
});
2017-10-17 11:19:17 -04:00
test('is.safeInteger', t => {
testType(t, 'safeInteger', ['number', 'integer']);
t.false(m.safeInteger(Math.pow(2, 53)));
t.false(m.safeInteger(-Math.pow(2, 53)));
});
2017-09-22 00:44:27 +07:00
test('is.plainObject', t => {
testType(t, 'plainObject', ['object', 'promise']);
});
test('is.iterable', t => {
t.true(m.iterable(''));
t.true(m.iterable([]));
t.true(m.iterable(new Map()));
t.false(m.iterable(null));
t.false(m.iterable(undefined));
t.false(m.iterable(0));
t.false(m.iterable(NaN));
t.false(m.iterable(Infinity));
t.false(m.iterable({}));
});
2018-07-09 20:35:16 +03:00
if (isNode10orHigher) {
test('is.asyncIterable', t => {
t.true(m.asyncIterable({
[Symbol.asyncIterator]: () => {} // tslint:disable-line:no-empty
}));
t.false(m.asyncIterable(null));
t.false(m.asyncIterable(undefined));
t.false(m.asyncIterable(0));
t.false(m.asyncIterable(NaN));
t.false(m.asyncIterable(Infinity));
t.false(m.asyncIterable({}));
});
}
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) {
2017-11-06 16:26:59 +01:00
t.true(m.class_(x));
2017-09-24 22:26:15 +03:00
}
});
2017-09-22 00:44:27 +07:00
test('is.typedArray', t => {
2017-11-06 16:26:59 +01:00
// Typescript currently does not support empty constructors for these
// See https://github.com/Microsoft/TypeScript/issues/19680
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
];
for (const el of typedArrays) {
t.true(m.typedArray(el));
}
t.false(m.typedArray(new ArrayBuffer(1)));
t.false(m.typedArray([]));
t.false(m.typedArray({}));
});
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
2017-10-26 09:30:17 -04:00
t.true(m.arrayLike(arguments));
})();
t.true(m.arrayLike([]));
t.true(m.arrayLike('unicorn'));
t.false(m.arrayLike({}));
2017-11-06 16:26:59 +01:00
t.false(m.arrayLike(() => {})); // tslint:disable-line:no-empty
2017-10-26 09:30:17 -04:00
t.false(m.arrayLike(new Map()));
});
2017-09-25 20:20:06 +01:00
test('is.inRange', t => {
const x = 3;
t.true(m.inRange(x, [0, 5]));
t.true(m.inRange(x, [5, 0]));
t.true(m.inRange(x, [-5, 5]));
t.true(m.inRange(x, [5, -5]));
t.false(m.inRange(x, [4, 8]));
2017-09-26 02:32:58 +07:00
t.true(m.inRange(-7, [-5, -10]));
t.true(m.inRange(-5, [-5, -10]));
t.true(m.inRange(-10, [-5, -10]));
2017-09-25 20:20:06 +01:00
t.true(m.inRange(x, 10));
2017-09-26 02:32:58 +07:00
t.true(m.inRange(0, 0));
t.true(m.inRange(-2, -3));
2017-09-25 20:20:06 +01:00
t.false(m.inRange(x, 2));
t.false(m.inRange(-3, -2));
t.throws(() => {
m.inRange(0, []);
});
2017-09-26 02:32:58 +07:00
t.throws(() => {
m.inRange(0, [5]);
2017-09-26 02:32:58 +07:00
});
t.throws(() => {
m.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');
t.false(m.domElement({nodeType: 1, nodeName: 'div'}));
});
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]) {
t.true(m.even(el));
}
for (const el of [-3, 1, 5]) {
t.false(m.even(el));
}
});
test('is.odd', t => {
for (const el of [-5, 7, 13]) {
t.true(m.odd(el));
}
for (const el of [-8, 8, 10]) {
t.false(m.odd(el));
}
});
2017-10-06 02:39:36 -05:00
test('is.empty', t => {
t.true(m.empty(null));
t.true(m.empty(undefined));
t.true(m.empty(false));
t.false(m.empty(true));
t.true(m.empty(''));
t.false(m.empty('🦄'));
t.true(m.empty([]));
t.false(m.empty(['🦄']));
t.true(m.empty({}));
t.false(m.empty({unicorn: '🦄'}));
const tempMap = new Map();
t.true(m.empty(tempMap));
tempMap.set('unicorn', '🦄');
t.false(m.empty(tempMap));
const tempSet = new Set();
t.true(m.empty(tempSet));
tempSet.add(1);
t.false(m.empty(tempSet));
});
2017-10-17 16:46:58 -04:00
test('is.emptyOrWhitespace', t => {
t.true(m.emptyOrWhitespace(''));
t.true(m.emptyOrWhitespace(' '));
t.false(m.emptyOrWhitespace('🦄'));
t.false(m.emptyOrWhitespace('unicorn'));
});
test('is.any', t => {
t.true(m.any(m.string, {}, true, '🦄'));
t.true(m.any(m.object, false, {}, 'unicorns'));
t.false(m.any(m.boolean, '🦄', [], 3));
t.false(m.any(m.integer, true, 'lol', {}));
t.throws(() => {
m.any(null, true);
});
t.throws(() => {
m.any(m.string);
});
});
test('is.all', t => {
t.true(m.all(m.object, {}, new Set(), new Map()));
t.true(m.all(m.boolean, true, false));
t.false(m.all(m.string, '🦄', []));
t.false(m.all(m.set, new Map(), {}));
t.throws(() => {
m.all(null, true);
});
t.throws(() => {
m.all(m.string);
});
});