Minor code style tweaks in the tests

This commit is contained in:
Sindre Sorhus 2018-10-30 23:19:19 +07:00
parent c983ffa4cd
commit 9ac56f1be7

View file

@ -8,7 +8,7 @@ import test, {TestContext, Context} from 'ava';
import {JSDOM} from 'jsdom';
import {Subject, Observable} from 'rxjs';
import ZenObservable from 'zen-observable';
import m from '..';
import is from '..';
const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8;
const isNode10orHigher = Number(process.versions.node.split('.')[0]) >= 10;
@ -18,7 +18,7 @@ class ErrorSubclassFixture extends Error {}
const {window} = new JSDOM();
const {document} = window;
const createDomElement = (el: string) => document.createElement(el);
const createDomElement = (element: string) => document.createElement(element);
interface Test {
is(value: any): boolean;
@ -27,19 +27,19 @@ interface Test {
const types = new Map<string, Test>([
['undefined', {
is: m.undefined,
is: is.undefined,
fixtures: [
undefined
]
}],
['null', {
is: m.null_,
is: is.null_,
fixtures: [
null
]
}],
['string', {
is: m.string,
is: is.string,
fixtures: [
'🦄',
'hello world',
@ -47,14 +47,14 @@ const types = new Map<string, Test>([
]
}],
['emptyString', {
is: m.emptyString,
is: is.emptyString,
fixtures: [
'',
String()
]
}],
['number', {
is: m.number,
is: is.number,
fixtures: [
6,
1.4,
@ -65,33 +65,33 @@ const types = new Map<string, Test>([
]
}],
['boolean', {
is: m.boolean,
is: is.boolean,
fixtures: [
true, false
]
}],
['symbol', {
is: m.symbol,
is: is.symbol,
fixtures: [
Symbol('🦄')
]
}],
['array', {
is: m.array,
is: is.array,
fixtures: [
[1, 2],
new Array(2) // tslint:disable-line:prefer-array-literal
]
}],
['emptyArray', {
is: m.emptyArray,
is: is.emptyArray,
fixtures: [
[],
new Array()
]
}],
['function', {
is: m.function_,
is: is.function_,
fixtures: [
// tslint:disable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression
function foo() {},
@ -103,53 +103,53 @@ const types = new Map<string, Test>([
]
}],
['buffer', {
is: m.buffer,
is: is.buffer,
fixtures: [
Buffer.from('🦄')
]
}],
['object', {
is: m.object,
is: is.object,
fixtures: [
{x: 1},
Object.create({x: 1})
]
}],
['regExp', {
is: m.regExp,
is: is.regExp,
fixtures: [
/\w/,
new RegExp('\\w')
]
}],
['date', {
is: m.date,
is: is.date,
fixtures: [
new Date()
]
}],
['error', {
is: m.error,
is: is.error,
fixtures: [
new Error('🦄'),
new ErrorSubclassFixture()
]
}],
['nativePromise', {
is: m.nativePromise,
is: is.nativePromise,
fixtures: [
Promise.resolve(),
PromiseSubclassFixture.resolve()
]
}],
['promise', {
is: m.promise,
is: is.promise,
fixtures: [
{then() {}, catch() {}} // tslint:disable-line:no-empty
]
}],
['generator', {
is: m.generator,
is: is.generator,
fixtures: [
(function * () {
yield 4;
@ -157,7 +157,7 @@ const types = new Map<string, Test>([
]
}],
['generatorFunction', {
is: m.generatorFunction,
is: is.generatorFunction,
fixtures: [
function * () {
yield 4;
@ -165,137 +165,137 @@ const types = new Map<string, Test>([
]
}],
['asyncFunction', {
is: m.asyncFunction,
is: is.asyncFunction,
fixtures: [
async function () {}, // tslint:disable-line:no-empty only-arrow-functions no-function-expression
async () => {} // tslint:disable-line:no-empty
]
}],
['boundFunction', {
is: m.boundFunction,
is: is.boundFunction,
fixtures: [
() => {}, // tslint:disable-line:no-empty
function () {}.bind(null), // tslint:disable-line:no-empty only-arrow-functions
]
}],
['map', {
is: m.map,
is: is.map,
fixtures: [
new Map([['one', '1']]),
]
}],
['emptyMap', {
is: m.emptyMap,
is: is.emptyMap,
fixtures: [
new Map(),
]
}],
['set', {
is: m.set,
is: is.set,
fixtures: [
new Set(['one'])
]
}],
['emptySet', {
is: m.emptySet,
is: is.emptySet,
fixtures: [
new Set(),
]
}],
['weakSet', {
is: m.weakSet,
is: is.weakSet,
fixtures: [
new WeakSet()
]
}],
['weakMap', {
is: m.weakMap,
is: is.weakMap,
fixtures: [
new WeakMap()
]
}],
['int8Array', {
is: m.int8Array,
is: is.int8Array,
fixtures: [
new Int8Array(0)
]
}],
['uint8Array', {
is: m.uint8Array,
is: is.uint8Array,
fixtures: [
new Uint8Array(0)
]
}],
['uint8ClampedArray', {
is: m.uint8ClampedArray,
is: is.uint8ClampedArray,
fixtures: [
new Uint8ClampedArray(0)
]
}],
['int16Array', {
is: m.int16Array,
is: is.int16Array,
fixtures: [
new Int16Array(0)
]
}],
['uint16Array', {
is: m.uint16Array,
is: is.uint16Array,
fixtures: [
new Uint16Array(0)
]
}],
['int32Array', {
is: m.int32Array,
is: is.int32Array,
fixtures: [
new Int32Array(0)
]
}],
['uint32Array', {
is: m.uint32Array,
is: is.uint32Array,
fixtures: [
new Uint32Array(0)
]
}],
['float32Array', {
is: m.float32Array,
is: is.float32Array,
fixtures: [
new Float32Array(0)
]
}],
['float64Array', {
is: m.float64Array,
is: is.float64Array,
fixtures: [
new Float64Array(0)
]
}],
['arrayBuffer', {
is: m.arrayBuffer,
is: is.arrayBuffer,
fixtures: [
new ArrayBuffer(10)
]
}],
['dataView', {
is: m.dataView,
is: is.dataView,
fixtures: [
new DataView(new ArrayBuffer(10))
]
}],
['nan', {
is: m.nan,
is: is.nan,
fixtures: [
NaN,
Number.NaN
]
}],
['nullOrUndefined', {
is: m.nullOrUndefined,
is: is.nullOrUndefined,
fixtures: [
null,
undefined
]
}],
['plainObject', {
is: m.plainObject,
is: is.plainObject,
fixtures: [
{x: 1},
Object.create(null),
@ -303,20 +303,20 @@ const types = new Map<string, Test>([
]
}],
['integer', {
is: m.integer,
is: is.integer,
fixtures: [
6
]
}],
['safeInteger', {
is: m.safeInteger,
is: is.safeInteger,
fixtures: [
Math.pow(2, 53) - 1,
-Math.pow(2, 53) + 1
]
}],
['domElement', {
is: m.domElement,
is: is.domElement,
fixtures: [
'div',
'input',
@ -327,7 +327,7 @@ const types = new Map<string, Test>([
].map(createDomElement) }
],
['non-domElements', {
is: value => !m.domElement(value),
is: value => !is.domElement(value),
fixtures: [
document.createTextNode('data'),
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
@ -338,7 +338,7 @@ const types = new Map<string, Test>([
]
}],
['nodeStream', {
is: m.nodeStream,
is: is.nodeStream,
fixtures: [
fs.createReadStream('readme.md'),
fs.createWriteStream(tempy.file()),
@ -352,7 +352,7 @@ const types = new Map<string, Test>([
]
}],
['observable', {
is: m.observable,
is: is.observable,
fixtures: [
new Observable(),
new Subject(),
@ -360,7 +360,7 @@ const types = new Map<string, Test>([
]
}],
['infinite', {
is: m.infinite,
is: is.infinite,
fixtures: [
Infinity,
-Infinity
@ -378,7 +378,7 @@ const testType = (t: TestContext & Context<any>, type: string, exclude?: string[
return;
}
const {is} = testData;
const {is: testIs} = testData;
for (const [key, {fixtures}] of types) {
// TODO: Automatically exclude value types in other tests that we have in the current one.
@ -390,14 +390,14 @@ const testType = (t: TestContext & Context<any>, type: string, exclude?: string[
const assert = key === type ? t.true.bind(t) : t.false.bind(t);
for (const fixture of fixtures) {
assert(is(fixture), `Value: ${util.inspect(fixture)}`);
assert(testIs(fixture), `Value: ${util.inspect(fixture)}`);
}
}
};
test('is', t => {
t.is(m(null), 'null');
t.is(m(undefined), 'undefined');
t.is(is(null), 'null');
t.is(is(undefined), 'undefined');
// TODO: Expand this to all the supported types. Maybe reuse `testType()` somehow.
});
@ -435,7 +435,7 @@ test('is.function', t => {
});
test('is.boundFunction', t => {
t.false(m.boundFunction(function () {})); // tslint:disable-line:no-empty only-arrow-functions
t.false(is.boundFunction(function () {})); // tslint:disable-line:no-empty only-arrow-functions
});
test('is.buffer', t => {
@ -452,7 +452,7 @@ test('is.object', t => {
}
for (const el of testData.fixtures) {
t.true(m.object(el));
t.true(is.object(el));
}
});
@ -554,36 +554,36 @@ 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.true(is.directInstanceOf(error, Error));
t.true(is.directInstanceOf(errorSubclass, ErrorSubclassFixture));
t.false(m.directInstanceOf(error, ErrorSubclassFixture));
t.false(m.directInstanceOf(errorSubclass, Error));
t.false(is.directInstanceOf(error, ErrorSubclassFixture));
t.false(is.directInstanceOf(errorSubclass, Error));
});
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));
t.true(is.urlInstance(url));
t.false(is.urlInstance({}));
t.false(is.urlInstance(undefined));
t.false(is.urlInstance(null));
});
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));
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));
});
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));
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));
});
test('is.nan', t => {
@ -608,19 +608,19 @@ test('is.primitive', t => {
];
for (const el of primitives) {
t.true(m.primitive(el));
t.true(is.primitive(el));
}
});
test('is.integer', t => {
testType(t, 'integer', ['number', 'safeInteger']);
t.false(m.integer(1.4));
t.false(is.integer(1.4));
});
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)));
t.false(is.safeInteger(Math.pow(2, 53)));
t.false(is.safeInteger(-Math.pow(2, 53)));
});
test('is.plainObject', t => {
@ -628,29 +628,29 @@ test('is.plainObject', t => {
});
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({}));
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({}));
});
if (isNode10orHigher) {
test('is.asyncIterable', t => {
t.true(m.asyncIterable({
t.true(is.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({}));
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({}));
});
}
@ -662,7 +662,7 @@ test('is.class', t => {
];
for (const x of classDeclarations) {
t.true(m.class_(x));
t.true(is.class_(x));
}
});
@ -680,61 +680,61 @@ test('is.typedArray', t => {
new Float64Array(0)
];
for (const el of typedArrays) {
t.true(m.typedArray(el));
for (const item of typedArrays) {
t.true(is.typedArray(item));
}
t.false(m.typedArray(new ArrayBuffer(1)));
t.false(m.typedArray([]));
t.false(m.typedArray({}));
t.false(is.typedArray(new ArrayBuffer(1)));
t.false(is.typedArray([]));
t.false(is.typedArray({}));
});
test('is.arrayLike', t => {
(function () { // tslint:disable-line:only-arrow-functions
t.true(m.arrayLike(arguments));
t.true(is.arrayLike(arguments));
})();
t.true(m.arrayLike([]));
t.true(m.arrayLike('unicorn'));
t.true(is.arrayLike([]));
t.true(is.arrayLike('unicorn'));
t.false(m.arrayLike({}));
t.false(m.arrayLike(() => {})); // tslint:disable-line:no-empty
t.false(m.arrayLike(new Map()));
t.false(is.arrayLike({}));
t.false(is.arrayLike(() => {})); // tslint:disable-line:no-empty
t.false(is.arrayLike(new Map()));
});
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]));
t.true(m.inRange(-7, [-5, -10]));
t.true(m.inRange(-5, [-5, -10]));
t.true(m.inRange(-10, [-5, -10]));
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(m.inRange(x, 10));
t.true(m.inRange(0, 0));
t.true(m.inRange(-2, -3));
t.false(m.inRange(x, 2));
t.false(m.inRange(-3, -2));
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(() => {
m.inRange(0, []);
is.inRange(0, []);
});
t.throws(() => {
m.inRange(0, [5]);
is.inRange(0, [5]);
});
t.throws(() => {
m.inRange(0, [1, 2, 3]);
is.inRange(0, [1, 2, 3]);
});
});
test('is.domElement', t => {
testType(t, 'domElement');
t.false(m.domElement({nodeType: 1, nodeName: 'div'}));
t.false(is.domElement({nodeType: 1, nodeName: 'div'}));
});
test('is.nodeStream', t => {
@ -747,21 +747,21 @@ test('is.infinite', t => {
test('is.even', t => {
for (const el of [-6, 2, 4]) {
t.true(m.even(el));
t.true(is.even(el));
}
for (const el of [-3, 1, 5]) {
t.false(m.even(el));
t.false(is.even(el));
}
});
test('is.odd', t => {
for (const el of [-5, 7, 13]) {
t.true(m.odd(el));
t.true(is.odd(el));
}
for (const el of [-8, 8, 10]) {
t.false(m.odd(el));
t.false(is.odd(el));
}
});
@ -770,39 +770,42 @@ test('is.emptyArray', t => {
});
test('is.nonEmptyArray', t => {
t.true(m.nonEmptyArray([1, 2, 3]));
t.false(m.nonEmptyArray([]));
t.false(m.nonEmptyArray(new Array()));
t.true(is.nonEmptyArray([1, 2, 3]));
t.false(is.nonEmptyArray([]));
t.false(is.nonEmptyArray(new Array()));
});
test('is.emptyString', t => {
testType(t, 'emptyString', ['string']);
t.false(m.emptyString('🦄'));
t.false(is.emptyString('🦄'));
});
test('is.nonEmptyString', t => {
t.false(m.nonEmptyString(''));
t.false(m.nonEmptyString(String()));
t.true(m.nonEmptyString('🦄'));
t.false(is.nonEmptyString(''));
t.false(is.nonEmptyString(String()));
t.true(is.nonEmptyString('🦄'));
});
test('is.emptyStringOrWhitespace', t => {
testType(t, 'emptyString', ['string']);
t.true(m.emptyStringOrWhitespace(' '));
t.false(m.emptyStringOrWhitespace('🦄'));
t.false(m.emptyStringOrWhitespace('unicorn'));
t.true(is.emptyStringOrWhitespace(' '));
t.false(is.emptyStringOrWhitespace('🦄'));
t.false(is.emptyStringOrWhitespace('unicorn'));
});
test('is.emptyObject', t => {
t.true(m.emptyObject({}));
t.true(m.emptyObject(new Object()));
t.false(m.emptyObject({unicorn: '🦄'}));
t.true(is.emptyObject({}));
t.true(is.emptyObject(new Object()));
t.false(is.emptyObject({unicorn: '🦄'}));
});
test('is.nonEmptyObject', t => {
t.false(m.nonEmptyObject({}));
t.false(m.nonEmptyObject(new Object()));
t.true(m.nonEmptyObject({unicorn: '🦄'}));
const foo = {};
is.nonEmptyObject(foo);
t.false(is.nonEmptyObject({}));
t.false(is.nonEmptyObject(new Object()));
t.true(is.nonEmptyObject({unicorn: '🦄'}));
});
test('is.emptySet', t => {
@ -811,10 +814,10 @@ test('is.emptySet', t => {
test('is.nonEmptySet', t => {
const tempSet = new Set();
t.false(m.nonEmptySet(tempSet));
t.false(is.nonEmptySet(tempSet));
tempSet.add(1);
t.true(m.nonEmptySet(tempSet));
t.true(is.nonEmptySet(tempSet));
});
test('is.emptyMap', t => {
@ -823,38 +826,38 @@ test('is.emptyMap', t => {
test('is.nonEmptyMap', t => {
const tempMap = new Map();
t.false(m.nonEmptyMap(tempMap));
t.false(is.nonEmptyMap(tempMap));
tempMap.set('unicorn', '🦄');
t.true(m.nonEmptyMap(tempMap));
t.true(is.nonEmptyMap(tempMap));
});
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.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(() => {
m.any(null, true);
is.any(null, true);
});
t.throws(() => {
m.any(m.string);
is.any(is.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.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(() => {
m.all(null, true);
is.all(null, true);
});
t.throws(() => {
m.all(m.string);
is.all(is.string);
});
});