Browse Source

Enforce stricter ow type definition - fixes #31 (#33)

iss58
Sam Verschueren 7 years ago
committed by Sindre Sorhus
parent
commit
73b51d6b93
  1. 10
      source/index.ts
  2. 4
      source/test/array-buffer.ts
  3. 24
      source/test/array.ts
  4. 10
      source/test/boolean.ts
  5. 4
      source/test/buffer.ts
  6. 6
      source/test/date.ts
  7. 2
      source/test/error.ts
  8. 4
      source/test/function.ts
  9. 2
      source/test/iterable.ts
  10. 4
      source/test/null.ts
  11. 38
      source/test/number.ts
  12. 4
      source/test/promise.ts
  13. 4
      source/test/regexp.ts
  14. 34
      source/test/string.ts
  15. 2
      source/test/symbol.ts
  16. 2
      source/test/test.ts
  17. 40
      source/test/typed-array.ts
  18. 6
      source/test/undefined.ts

10
source/index.ts

@ -10,7 +10,7 @@ import {ErrorPredicate} from './lib/predicates/error';
export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
export interface Ow { export interface Ow {
(value: any, predicate: Predicate): void; <T>(value: T, predicate: Predicate<T>): void;
/** /**
* Test the value to be a string. * Test the value to be a string.
*/ */
@ -96,11 +96,11 @@ export interface Ow {
*/ */
uint32Array: Predicate<Uint32Array>; uint32Array: Predicate<Uint32Array>;
/** /**
* Test the value to be a Int32Array. * Test the value to be a Float32Array.
*/ */
float32Array: Predicate<Float64Array>; float32Array: Predicate<Float32Array>;
/** /**
* Test the value to be a Uint64Array. * Test the value to be a Float64Array.
*/ */
float64Array: Predicate<Float64Array>; float64Array: Predicate<Float64Array>;
/** /**
@ -113,7 +113,7 @@ export interface Ow {
iterable: Predicate<Iterable<any>>; iterable: Predicate<Iterable<any>>;
} }
const main = (value: any, predicate: Predicate) => { const main = <T>(value: T, predicate: Predicate<T>) => {
for (const {validator, message} of predicate[validatorSymbol]) { for (const {validator, message} of predicate[validatorSymbol]) {
if (!validator(value)) { if (!validator(value)) {
// TODO: Modify the stack output to show the original `ow()` call instead of this `throw` statement // TODO: Modify the stack output to show the original `ow()` call instead of this `throw` statement

4
source/test/array-buffer.ts

@ -3,6 +3,6 @@ import m from '..';
test('arrayBuffer', t => { test('arrayBuffer', t => {
t.notThrows(() => m(new ArrayBuffer(1), m.arrayBuffer)); t.notThrows(() => m(new ArrayBuffer(1), m.arrayBuffer));
t.throws(() => m('foo', m.arrayBuffer), 'Expected argument to be of type `arrayBuffer` but received type `string`'); t.throws(() => m('foo' as any, m.arrayBuffer), 'Expected argument to be of type `arrayBuffer` but received type `string`');
t.throws(() => m(12, m.arrayBuffer), 'Expected argument to be of type `arrayBuffer` but received type `number`'); t.throws(() => m(12 as any, m.arrayBuffer), 'Expected argument to be of type `arrayBuffer` but received type `number`');
}); });

24
source/test/array.ts

@ -3,67 +3,67 @@ import m from '..';
test('array', t => { test('array', t => {
t.notThrows(() => m([], m.array)); t.notThrows(() => m([], m.array));
t.throws(() => m('12', m.array), 'Expected argument to be of type `array` but received type `string`'); t.throws(() => m('12' as any, m.array), 'Expected argument to be of type `array` but received type `string`');
}); });
test('array.length', t => { test('array.length', t => {
t.notThrows(() => m(['foo'], m.array.length(1))); t.notThrows(() => m(['foo'], m.array.length(1)));
t.notThrows(() => m(['foo', 'bar'], m.array.length(2))); t.notThrows(() => m(['foo', 'bar'], m.array.length(2)));
t.throws(() => m(['foo'], m.array.length(2)), 'Expected array to have length `2`, got `1`'); t.throws(() => m(['foo'] as any, m.array.length(2)), 'Expected array to have length `2`, got `1`');
}); });
test('array.minLength', t => { test('array.minLength', t => {
t.notThrows(() => m(['foo'], m.array.minLength(1))); t.notThrows(() => m(['foo'], m.array.minLength(1)));
t.notThrows(() => m(['foo', 'bar'], m.array.minLength(1))); t.notThrows(() => m(['foo', 'bar'], m.array.minLength(1)));
t.throws(() => m(['foo'], m.array.minLength(2)), 'Expected array to have a minimum length of `2`, got `1`'); t.throws(() => m(['foo'] as any, m.array.minLength(2)), 'Expected array to have a minimum length of `2`, got `1`');
}); });
test('array.maxLength', t => { test('array.maxLength', t => {
t.notThrows(() => m(['foo'], m.array.maxLength(1))); t.notThrows(() => m(['foo'], m.array.maxLength(1)));
t.notThrows(() => m(['foo', 'bar'], m.array.maxLength(4))); t.notThrows(() => m(['foo', 'bar'], m.array.maxLength(4)));
t.throws(() => m(['foo', 'bar'], m.array.maxLength(1)), 'Expected array to have a maximum length of `1`, got `2`'); t.throws(() => m(['foo' as any, 'bar'], m.array.maxLength(1)), 'Expected array to have a maximum length of `1`, got `2`');
}); });
test('array.startsWith', t => { test('array.startsWith', t => {
t.notThrows(() => m(['foo', 'bar'], m.array.startsWith('foo'))); t.notThrows(() => m(['foo', 'bar'], m.array.startsWith('foo')));
t.throws(() => m(['foo', 'bar'], m.array.startsWith('bar')), 'Expected array to start with `bar`, got `foo`'); t.throws(() => m(['foo' as any, 'bar'], m.array.startsWith('bar')), 'Expected array to start with `bar`, got `foo`');
}); });
test('array.endsWith', t => { test('array.endsWith', t => {
t.notThrows(() => m(['foo', 'bar'], m.array.endsWith('bar'))); t.notThrows(() => m(['foo', 'bar'], m.array.endsWith('bar')));
t.throws(() => m(['foo', 'bar'], m.array.endsWith('foo')), 'Expected array to end with `foo`, got `bar`'); t.throws(() => m(['foo' as any, 'bar'], m.array.endsWith('foo')), 'Expected array to end with `foo`, got `bar`');
}); });
test('array.includes', t => { test('array.includes', t => {
t.notThrows(() => m(['foo', 'bar'], m.array.includes('foo'))); t.notThrows(() => m(['foo', 'bar'], m.array.includes('foo')));
t.notThrows(() => m(['foo', 'bar', 'unicorn'], m.array.includes('foo', 'bar'))); t.notThrows(() => m(['foo', 'bar', 'unicorn'], m.array.includes('foo', 'bar')));
t.throws(() => m(['foo', 'bar'], m.array.includes('foo', 'unicorn')), 'Expected array to include all elements of `["foo","unicorn"]`, got `["foo","bar"]`'); t.throws(() => m(['foo' as any, 'bar'], m.array.includes('foo', 'unicorn')), 'Expected array to include all elements of `["foo","unicorn"]`, got `["foo","bar"]`');
}); });
test('array.includesAny', t => { test('array.includesAny', t => {
t.notThrows(() => m(['foo', 'bar'], m.array.includesAny('foo'))); t.notThrows(() => m(['foo', 'bar'], m.array.includesAny('foo')));
t.notThrows(() => m(['foo', 'bar', 'unicorn'], m.array.includesAny('unicorn', 'rainbow'))); t.notThrows(() => m(['foo', 'bar', 'unicorn'], m.array.includesAny('unicorn', 'rainbow')));
t.throws(() => m(['foo', 'bar'], m.array.includesAny('unicorn')), 'Expected array to include any element of `["unicorn"]`, got `["foo","bar"]`'); t.throws(() => m(['foo' as any, 'bar'], m.array.includesAny('unicorn')), 'Expected array to include any element of `["unicorn"]`, got `["foo","bar"]`');
}); });
test('array.empty', t => { test('array.empty', t => {
t.notThrows(() => m([], m.array.empty)); t.notThrows(() => m([], m.array.empty));
t.throws(() => m(['foo'], m.array.empty), 'Expected array to be empty, got `["foo"]`'); t.throws(() => m(['foo'] as any, m.array.empty), 'Expected array to be empty, got `["foo"]`');
}); });
test('array.nonEmpty', t => { test('array.nonEmpty', t => {
t.notThrows(() => m(['foo'], m.array.nonEmpty)); t.notThrows(() => m(['foo'], m.array.nonEmpty));
t.throws(() => m([], m.array.nonEmpty), 'Expected array to not be empty'); t.throws(() => m([] as any, m.array.nonEmpty), 'Expected array to not be empty');
}); });
test('array.deepEqual', t => { test('array.deepEqual', t => {
t.notThrows(() => m(['foo'], m.array.deepEqual(['foo']))); t.notThrows(() => m(['foo'], m.array.deepEqual(['foo'])));
t.notThrows(() => m(['foo', {id: 1}], m.array.deepEqual(['foo', {id: 1}]))); t.notThrows(() => m(['foo', {id: 1}], m.array.deepEqual(['foo', {id: 1}])));
t.throws(() => m(['foo', {id: 1}], m.array.deepEqual(['foo', {id: 2}])), 'Expected array to be deeply equal to `["foo",{"id":2}]`, got `["foo",{"id":1}]`'); t.throws(() => m(['foo' as any, {id: 1}], m.array.deepEqual(['foo', {id: 2}])), 'Expected array to be deeply equal to `["foo",{"id":2}]`, got `["foo",{"id":1}]`');
}); });
test('array.ofType', t => { test('array.ofType', t => {
t.notThrows(() => m(['foo', 'bar'], m.array.ofType(m.string))); t.notThrows(() => m(['foo', 'bar'], m.array.ofType(m.string)));
t.notThrows(() => m(['foo', 'bar'], m.array.ofType(m.string.minLength(3)))); t.notThrows(() => m(['foo', 'bar'], m.array.ofType(m.string.minLength(3))));
t.throws(() => m(['foo', 'b'], m.array.ofType(m.string.minLength(3))), 'Expected string to have a minimum length of `3`, got `b`'); t.throws(() => m(['foo' as any, 'b'], m.array.ofType(m.string.minLength(3))), 'Expected string to have a minimum length of `3`, got `b`');
}); });

10
source/test/boolean.ts

@ -3,21 +3,21 @@ import m from '..';
test('boolean', t => { test('boolean', t => {
t.notThrows(() => m(true, m.boolean)); t.notThrows(() => m(true, m.boolean));
t.throws(() => m('12', m.boolean), 'Expected argument to be of type `boolean` but received type `string`'); t.throws(() => m('12' as any, m.boolean), 'Expected argument to be of type `boolean` but received type `string`');
}); });
test('boolean.true', t => { test('boolean.true', t => {
t.notThrows(() => m(true, m.boolean.true)); t.notThrows(() => m(true, m.boolean.true));
t.notThrows(() => m(Boolean(true), m.boolean.true)); t.notThrows(() => m(Boolean(true), m.boolean.true));
t.notThrows(() => m(Boolean(1), m.boolean.true)); t.notThrows(() => m(Boolean(1), m.boolean.true));
t.throws(() => m(false, m.boolean.true), 'Expected false to be true'); t.throws(() => m(false as any, m.boolean.true), 'Expected false to be true');
t.throws(() => m(Boolean(0), m.boolean.true), 'Expected false to be true'); t.throws(() => m(Boolean(0) as any, m.boolean.true), 'Expected false to be true');
}); });
test('boolean.false', t => { test('boolean.false', t => {
t.notThrows(() => m(false, m.boolean.false)); t.notThrows(() => m(false, m.boolean.false));
t.notThrows(() => m(Boolean(false), m.boolean.false)); t.notThrows(() => m(Boolean(false), m.boolean.false));
t.notThrows(() => m(Boolean(0), m.boolean.false)); t.notThrows(() => m(Boolean(0), m.boolean.false));
t.throws(() => m(true, m.boolean.false), 'Expected true to be false'); t.throws(() => m(true as any, m.boolean.false), 'Expected true to be false');
t.throws(() => m(Boolean(1), m.boolean.false), 'Expected true to be false'); t.throws(() => m(Boolean(1) as any, m.boolean.false), 'Expected true to be false');
}); });

4
source/test/buffer.ts

@ -4,6 +4,6 @@ import m from '..';
test('buffer', t => { test('buffer', t => {
t.notThrows(() => m(Buffer.alloc(2), m.buffer)); t.notThrows(() => m(Buffer.alloc(2), m.buffer));
t.notThrows(() => m(Buffer.from('f'), m.buffer)); t.notThrows(() => m(Buffer.from('f'), m.buffer));
t.throws(() => m('foo', m.buffer), 'Expected argument to be of type `buffer` but received type `string`'); t.throws(() => m('foo' as any, m.buffer), 'Expected argument to be of type `buffer` but received type `string`');
t.throws(() => m(12, m.buffer), 'Expected argument to be of type `buffer` but received type `number`'); t.throws(() => m(12 as any, m.buffer), 'Expected argument to be of type `buffer` but received type `number`');
}); });

6
source/test/date.ts

@ -3,17 +3,17 @@ import m from '..';
test('date', t => { test('date', t => {
t.notThrows(() => m(new Date(), m.date)); t.notThrows(() => m(new Date(), m.date));
t.throws(() => m('12', m.date), 'Expected argument to be of type `date` but received type `string`'); t.throws(() => m('12' as any, m.date), 'Expected argument to be of type `date` but received type `string`');
}); });
test('date.before', t => { test('date.before', t => {
t.notThrows(() => m(new Date('2017-11-25'), m.date.before(new Date('2017-11-26')))); t.notThrows(() => m(new Date('2017-11-25'), m.date.before(new Date('2017-11-26'))));
t.notThrows(() => m(new Date('2017-11-25T12:00:00Z'), m.date.before(new Date('2017-11-25T12:00:01Z')))); t.notThrows(() => m(new Date('2017-11-25T12:00:00Z'), m.date.before(new Date('2017-11-25T12:00:01Z'))));
t.throws(() => m(new Date('2017-11-25T12:00:00Z'), m.date.before(new Date('2017-11-25T12:00:00Z'))), 'Expected 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z'); t.throws(() => m(new Date('2017-11-25T12:00:00Z') as any, m.date.before(new Date('2017-11-25T12:00:00Z'))), 'Expected 2017-11-25T12:00:00.000Z to be before 2017-11-25T12:00:00.000Z');
}); });
test('date.after', t => { test('date.after', t => {
t.notThrows(() => m(new Date('2017-11-26'), m.date.after(new Date('2017-11-25')))); t.notThrows(() => m(new Date('2017-11-26'), m.date.after(new Date('2017-11-25'))));
t.notThrows(() => m(new Date('2017-11-26T12:00:00Z'), m.date.after(new Date('2017-11-26T11:59:59Z')))); t.notThrows(() => m(new Date('2017-11-26T12:00:00Z'), m.date.after(new Date('2017-11-26T11:59:59Z'))));
t.throws(() => m(new Date('2017-11-26T12:00:00Z'), m.date.after(new Date('2017-11-26T12:00:00Z'))), 'Expected 2017-11-26T12:00:00.000Z to be after 2017-11-26T12:00:00.000Z'); t.throws(() => m(new Date('2017-11-26T12:00:00Z') as any, m.date.after(new Date('2017-11-26T12:00:00Z'))), 'Expected 2017-11-26T12:00:00.000Z to be after 2017-11-26T12:00:00.000Z');
}); });

2
source/test/error.ts

@ -10,7 +10,7 @@ class CustomError extends Error {
test('error', t => { test('error', t => {
t.notThrows(() => m(new Error('foo'), m.error)); t.notThrows(() => m(new Error('foo'), m.error));
t.throws(() => m('12', m.error), 'Expected argument to be of type `error` but received type `string`'); t.throws(() => m('12' as any, m.error), 'Expected argument to be of type `error` but received type `string`');
}); });
test('error.name', t => { test('error.name', t => {

4
source/test/function.ts

@ -3,6 +3,6 @@ import m from '..';
test('function', t => { test('function', t => {
t.notThrows(() => m(() => {}, m.function)); // tslint:disable-line:no-empty t.notThrows(() => m(() => {}, m.function)); // tslint:disable-line:no-empty
t.throws(() => m('foo', m.function), 'Expected argument to be of type `function` but received type `string`'); t.throws(() => m('foo' as any, m.function), 'Expected argument to be of type `function` but received type `string`');
t.throws(() => m(12, m.function), 'Expected argument to be of type `function` but received type `number`'); t.throws(() => m(12 as any, m.function), 'Expected argument to be of type `function` but received type `number`');
}); });

2
source/test/iterable.ts

@ -5,5 +5,5 @@ test('iterable', t => {
t.notThrows(() => m([], m.iterable)); t.notThrows(() => m([], m.iterable));
t.notThrows(() => m('foo', m.iterable)); t.notThrows(() => m('foo', m.iterable));
t.notThrows(() => m(new Map(), m.iterable)); t.notThrows(() => m(new Map(), m.iterable));
t.throws(() => m(12, m.iterable), 'Expected argument to be of type `iterable` but received type `number`'); t.throws(() => m(12 as any, m.iterable), 'Expected argument to be of type `iterable` but received type `number`');
}); });

4
source/test/null.ts

@ -6,6 +6,6 @@ test('null', t => {
t.notThrows(() => m(null, m.null)); t.notThrows(() => m(null, m.null));
t.notThrows(() => m(x, m.null)); t.notThrows(() => m(x, m.null));
t.throws(() => m(undefined, m.null), 'Expected argument to be of type `null` but received type `undefined`'); t.throws(() => m(undefined as any, m.null), 'Expected argument to be of type `null` but received type `undefined`');
t.throws(() => m('foo', m.null), 'Expected argument to be of type `null` but received type `string`'); t.throws(() => m('foo' as any, m.null), 'Expected argument to be of type `null` but received type `string`');
}); });

38
source/test/number.ts

@ -3,73 +3,73 @@ import m from '..';
test('number', t => { test('number', t => {
t.notThrows(() => m(1, m.number)); t.notThrows(() => m(1, m.number));
t.throws(() => m('12', m.number), 'Expected argument to be of type `number` but received type `string`'); t.throws(() => m('12' as any, m.number), 'Expected argument to be of type `number` but received type `string`');
}); });
test('number.inRange', t => { test('number.inRange', t => {
t.notThrows(() => m(10, m.number.inRange(0, 20))); t.notThrows(() => m(10, m.number.inRange(0, 20)));
t.notThrows(() => m(10, m.number.inRange(10, 20))); t.notThrows(() => m(10, m.number.inRange(10, 20)));
t.notThrows(() => m(10, m.number.inRange(0, 10))); t.notThrows(() => m(10, m.number.inRange(0, 10)));
t.throws(() => m(10, m.number.inRange(0, 9)), 'Expected 10 to be in range [0..9]'); t.throws(() => m(10 as any, m.number.inRange(0, 9)), 'Expected 10 to be in range [0..9]');
t.throws(() => m(10, m.number.inRange(11, 20)), 'Expected 10 to be in range [11..20]'); t.throws(() => m(10 as any, m.number.inRange(11, 20)), 'Expected 10 to be in range [11..20]');
}); });
test('number.greaterThan', t => { test('number.greaterThan', t => {
t.notThrows(() => m(10, m.number.greaterThan(5))); t.notThrows(() => m(10, m.number.greaterThan(5)));
t.notThrows(() => m(10, m.number.greaterThan(9))); t.notThrows(() => m(10, m.number.greaterThan(9)));
t.throws(() => m(10, m.number.greaterThan(10)), 'Expected 10 to be greater than 10'); t.throws(() => m(10 as any, m.number.greaterThan(10)), 'Expected 10 to be greater than 10');
t.throws(() => m(10, m.number.greaterThan(11)), 'Expected 10 to be greater than 11'); t.throws(() => m(10 as any, m.number.greaterThan(11)), 'Expected 10 to be greater than 11');
t.throws(() => m(10, m.number.greaterThan(20)), 'Expected 10 to be greater than 20'); t.throws(() => m(10 as any, m.number.greaterThan(20)), 'Expected 10 to be greater than 20');
}); });
test('number.greaterThanOrEqual', t => { test('number.greaterThanOrEqual', t => {
t.notThrows(() => m(10, m.number.greaterThanOrEqual(5))); t.notThrows(() => m(10, m.number.greaterThanOrEqual(5)));
t.notThrows(() => m(10, m.number.greaterThanOrEqual(10))); t.notThrows(() => m(10, m.number.greaterThanOrEqual(10)));
t.throws(() => m(10, m.number.greaterThanOrEqual(11)), 'Expected 10 to be greater than or equal to 11'); t.throws(() => m(10 as any, m.number.greaterThanOrEqual(11)), 'Expected 10 to be greater than or equal to 11');
t.throws(() => m(10, m.number.greaterThanOrEqual(20)), 'Expected 10 to be greater than or equal to 20'); t.throws(() => m(10 as any, m.number.greaterThanOrEqual(20)), 'Expected 10 to be greater than or equal to 20');
}); });
test('number.lessThan', t => { test('number.lessThan', t => {
t.notThrows(() => m(10, m.number.lessThan(20))); t.notThrows(() => m(10, m.number.lessThan(20)));
t.notThrows(() => m(10, m.number.lessThan(11))); t.notThrows(() => m(10, m.number.lessThan(11)));
t.throws(() => m(10, m.number.lessThan(10)), 'Expected 10 to be less than 10'); t.throws(() => m(10 as any, m.number.lessThan(10)), 'Expected 10 to be less than 10');
t.throws(() => m(10, m.number.lessThan(9)), 'Expected 10 to be less than 9'); t.throws(() => m(10 as any, m.number.lessThan(9)), 'Expected 10 to be less than 9');
t.throws(() => m(10, m.number.lessThan(0)), 'Expected 10 to be less than 0'); t.throws(() => m(10 as any, m.number.lessThan(0)), 'Expected 10 to be less than 0');
}); });
test('number.lessThanOrEqual', t => { test('number.lessThanOrEqual', t => {
t.notThrows(() => m(10, m.number.lessThanOrEqual(20))); t.notThrows(() => m(10, m.number.lessThanOrEqual(20)));
t.notThrows(() => m(10, m.number.lessThanOrEqual(10))); t.notThrows(() => m(10, m.number.lessThanOrEqual(10)));
t.throws(() => m(10, m.number.lessThanOrEqual(9)), 'Expected 10 to be less than or equal to 9'); t.throws(() => m(10 as any, m.number.lessThanOrEqual(9)), 'Expected 10 to be less than or equal to 9');
t.throws(() => m(10, m.number.lessThanOrEqual(0)), 'Expected 10 to be less than or equal to 0'); t.throws(() => m(10 as any, m.number.lessThanOrEqual(0)), 'Expected 10 to be less than or equal to 0');
}); });
test('number.equal', t => { test('number.equal', t => {
t.notThrows(() => m(10, m.number.equal(10))); t.notThrows(() => m(10, m.number.equal(10)));
t.throws(() => m(10, m.number.equal(5)), 'Expected 10 to be equal to 5'); t.throws(() => m(10 as any, m.number.equal(5)), 'Expected 10 to be equal to 5');
}); });
test('number.integer', t => { test('number.integer', t => {
t.notThrows(() => m(10, m.number.integer)); t.notThrows(() => m(10, m.number.integer));
t.throws(() => m(10.1, m.number.integer), 'Expected 10.1 to be an integer'); t.throws(() => m(10.1 as any, m.number.integer), 'Expected 10.1 to be an integer');
}); });
test('number.finite', t => { test('number.finite', t => {
t.notThrows(() => m(10, m.number.finite)); t.notThrows(() => m(10, m.number.finite));
t.throws(() => m(Infinity, m.number.finite), 'Expected Infinity to be finite'); t.throws(() => m(Infinity as any, m.number.finite), 'Expected Infinity to be finite');
}); });
test('number.infinite', t => { test('number.infinite', t => {
t.notThrows(() => m(Infinity, m.number.infinite)); t.notThrows(() => m(Infinity, m.number.infinite));
t.throws(() => m(10, m.number.infinite), 'Expected 10 to be infinite'); t.throws(() => m(10 as any, m.number.infinite), 'Expected 10 to be infinite');
}); });
test('number.positive', t => { test('number.positive', t => {
t.notThrows(() => m(1, m.number.positive)); t.notThrows(() => m(1, m.number.positive));
t.throws(() => m(-1, m.number.positive), 'Expected -1 to be positive'); t.throws(() => m(-1 as any, m.number.positive), 'Expected -1 to be positive');
}); });
test('number.negative', t => { test('number.negative', t => {
t.notThrows(() => m(-1, m.number.negative)); t.notThrows(() => m(-1, m.number.negative));
t.throws(() => m(1, m.number.negative), 'Expected 1 to be negative'); t.throws(() => m(1 as any, m.number.negative), 'Expected 1 to be negative');
}); });

4
source/test/promise.ts

@ -4,6 +4,6 @@ import m from '..';
test('promise', t => { test('promise', t => {
t.notThrows(() => m(Promise.resolve(), m.promise)); t.notThrows(() => m(Promise.resolve(), m.promise));
t.notThrows(() => m(new Promise(resolve => resolve()), m.promise)); t.notThrows(() => m(new Promise(resolve => resolve()), m.promise));
t.throws(() => m('foo', m.promise), 'Expected argument to be of type `promise` but received type `string`'); t.throws(() => m('foo' as any, m.promise), 'Expected argument to be of type `promise` but received type `string`');
t.throws(() => m(12, m.promise), 'Expected argument to be of type `promise` but received type `number`'); t.throws(() => m(12 as any, m.promise), 'Expected argument to be of type `promise` but received type `number`');
}); });

4
source/test/regexp.ts

@ -4,6 +4,6 @@ import m from '..';
test('regExp', t => { test('regExp', t => {
t.notThrows(() => m(/\d/, m.regExp)); t.notThrows(() => m(/\d/, m.regExp));
t.notThrows(() => m(new RegExp('\d'), m.regExp)); t.notThrows(() => m(new RegExp('\d'), m.regExp));
t.throws(() => m('foo', m.regExp), 'Expected argument to be of type `regExp` but received type `string`'); t.throws(() => m('foo' as any, m.regExp), 'Expected argument to be of type `regExp` but received type `string`');
t.throws(() => m(12, m.regExp), 'Expected argument to be of type `regExp` but received type `number`'); t.throws(() => m(12 as any, m.regExp), 'Expected argument to be of type `regExp` but received type `number`');
}); });

34
source/test/string.ts

@ -3,80 +3,80 @@ import m from '..';
test('string', t => { test('string', t => {
t.notThrows(() => m('foo', m.string)); t.notThrows(() => m('foo', m.string));
t.throws(() => m(12, m.string), 'Expected argument to be of type `string` but received type `number`'); t.throws(() => m(12 as any, m.string), 'Expected argument to be of type `string` but received type `number`');
}); });
test('string.length', t => { test('string.length', t => {
t.notThrows(() => m('foo', m.string.length(3))); t.notThrows(() => m('foo', m.string.length(3)));
t.notThrows(() => m('foobar', m.string.length(6))); t.notThrows(() => m('foobar', m.string.length(6)));
t.throws(() => m('foo', m.string.length(4)), 'Expected string to have length `4`, got `foo`'); t.throws(() => m('foo' as any, m.string.length(4)), 'Expected string to have length `4`, got `foo`');
}); });
test('string.minLength', t => { test('string.minLength', t => {
t.notThrows(() => m('foo', m.string.minLength(2))); t.notThrows(() => m('foo', m.string.minLength(2)));
t.notThrows(() => m('foo', m.string.minLength(3))); t.notThrows(() => m('foo', m.string.minLength(3)));
t.throws(() => m('foo', m.string.minLength(4)), 'Expected string to have a minimum length of `4`, got `foo`'); t.throws(() => m('foo' as any, m.string.minLength(4)), 'Expected string to have a minimum length of `4`, got `foo`');
}); });
test('string.maxLength', t => { test('string.maxLength', t => {
t.notThrows(() => m('foo', m.string.maxLength(3))); t.notThrows(() => m('foo', m.string.maxLength(3)));
t.notThrows(() => m('foo', m.string.maxLength(5))); t.notThrows(() => m('foo', m.string.maxLength(5)));
t.throws(() => m('foo', m.string.maxLength(2)), 'Expected string to have a maximum length of `2`, got `foo`'); t.throws(() => m('foo' as any, m.string.maxLength(2)), 'Expected string to have a maximum length of `2`, got `foo`');
}); });
test('string.matches', t => { test('string.matches', t => {
t.notThrows(() => m('foo', m.string.matches(/^f.o$/))); t.notThrows(() => m('foo', m.string.matches(/^f.o$/)));
t.notThrows(() => m('Foo', m.string.matches(/^f.o$/i))); t.notThrows(() => m('Foo', m.string.matches(/^f.o$/i)));
t.throws(() => m('Foo', m.string.matches(/^f.o$/)), 'Expected string to match `/^f.o$/`, got `Foo`'); t.throws(() => m('Foo' as any, m.string.matches(/^f.o$/)), 'Expected string to match `/^f.o$/`, got `Foo`');
t.throws(() => m('bar', m.string.matches(/^f.o$/i)), 'Expected string to match `/^f.o$/i`, got `bar`'); t.throws(() => m('bar' as any, m.string.matches(/^f.o$/i)), 'Expected string to match `/^f.o$/i`, got `bar`');
}); });
test('string.startsWith', t => { test('string.startsWith', t => {
t.notThrows(() => m('foo', m.string.startsWith('fo'))); t.notThrows(() => m('foo', m.string.startsWith('fo')));
t.notThrows(() => m('foo', m.string.startsWith('f'))); t.notThrows(() => m('foo', m.string.startsWith('f')));
t.throws(() => m('foo', m.string.startsWith('oo')), 'Expected string to start with `oo`, got `foo`'); t.throws(() => m('foo' as any, m.string.startsWith('oo')), 'Expected string to start with `oo`, got `foo`');
t.throws(() => m('foo', m.string.startsWith('b')), 'Expected string to start with `b`, got `foo`'); t.throws(() => m('foo' as any, m.string.startsWith('b')), 'Expected string to start with `b`, got `foo`');
}); });
test('string.endsWith', t => { test('string.endsWith', t => {
t.notThrows(() => m('foo', m.string.endsWith('oo'))); t.notThrows(() => m('foo', m.string.endsWith('oo')));
t.notThrows(() => m('foo', m.string.endsWith('o'))); t.notThrows(() => m('foo', m.string.endsWith('o')));
t.throws(() => m('foo', m.string.endsWith('fo')), 'Expected string to end with `fo`, got `foo`'); t.throws(() => m('foo' as any, m.string.endsWith('fo')), 'Expected string to end with `fo`, got `foo`');
t.throws(() => m('foo', m.string.endsWith('ar')), 'Expected string to end with `ar`, got `foo`'); t.throws(() => m('foo' as any, m.string.endsWith('ar')), 'Expected string to end with `ar`, got `foo`');
}); });
test('string.includes', t => { test('string.includes', t => {
t.notThrows(() => m('foo', m.string.includes('fo'))); t.notThrows(() => m('foo', m.string.includes('fo')));
t.throws(() => m('foo', m.string.includes('bar')), 'Expected string to include `bar`, got `foo`'); t.throws(() => m('foo' as any, m.string.includes('bar')), 'Expected string to include `bar`, got `foo`');
}); });
test('string.empty', t => { test('string.empty', t => {
t.notThrows(() => m('', m.string.empty)); t.notThrows(() => m('', m.string.empty));
t.throws(() => m('foo', m.string.empty), 'Expected string to be empty, got `foo`'); t.throws(() => m('foo' as any, m.string.empty), 'Expected string to be empty, got `foo`');
}); });
test('string.nonEmpty', t => { test('string.nonEmpty', t => {
t.notThrows(() => m('foo', m.string.nonEmpty)); t.notThrows(() => m('foo', m.string.nonEmpty));
t.throws(() => m('', m.string.nonEmpty), 'Expected string to not be empty'); t.throws(() => m('' as any, m.string.nonEmpty), 'Expected string to not be empty');
}); });
test('string.equals', t => { test('string.equals', t => {
t.notThrows(() => m('foo', m.string.equals('foo'))); t.notThrows(() => m('foo', m.string.equals('foo')));
t.throws(() => m('bar', m.string.equals('foo')), 'Expected string to be equal to `foo`, got `bar`'); t.throws(() => m('bar' as any, m.string.equals('foo')), 'Expected string to be equal to `foo`, got `bar`');
}); });
test('string.alphanumeric', t => { test('string.alphanumeric', t => {
t.notThrows(() => m('Foo123', m.string.alphanumeric)); t.notThrows(() => m('Foo123', m.string.alphanumeric));
t.throws(() => m('Foo123!', m.string.alphanumeric), 'Expected string to be alphanumeric, got `Foo123!`'); t.throws(() => m('Foo123!' as any, m.string.alphanumeric), 'Expected string to be alphanumeric, got `Foo123!`');
}); });
test('string.numeric', t => { test('string.numeric', t => {
t.notThrows(() => m('123', m.string.numeric)); t.notThrows(() => m('123', m.string.numeric));
t.throws(() => m('Foo123', m.string.numeric), 'Expected string to be numeric, got `Foo123`'); t.throws(() => m('Foo123' as any, m.string.numeric), 'Expected string to be numeric, got `Foo123`');
}); });
test('string.date', t => { test('string.date', t => {
t.notThrows(() => m('2017-03-02', m.string.date)); t.notThrows(() => m('2017-03-02', m.string.date));
t.notThrows(() => m('2017-03-02T10:00:00Z', m.string.date)); t.notThrows(() => m('2017-03-02T10:00:00Z', m.string.date));
t.throws(() => m('foo', m.string.date), 'Expected string to be a date, got `foo`'); t.throws(() => m('foo' as any, m.string.date), 'Expected string to be a date, got `foo`');
}); });

2
source/test/symbol.ts

@ -4,5 +4,5 @@ import m from '..';
test('symbol', t => { test('symbol', t => {
t.notThrows(() => m(Symbol.iterator, m.symbol)); t.notThrows(() => m(Symbol.iterator, m.symbol));
t.notThrows(() => m(Symbol('foo'), m.symbol)); t.notThrows(() => m(Symbol('foo'), m.symbol));
t.throws(() => m(12, m.symbol), 'Expected argument to be of type `symbol` but received type `number`'); t.throws(() => m(12 as any, m.symbol), 'Expected argument to be of type `symbol` but received type `number`');
}); });

2
source/test/test.ts

@ -5,5 +5,5 @@ test('not', t => {
t.notThrows(() => m(1, m.number.not.infinite)); t.notThrows(() => m(1, m.number.not.infinite));
t.notThrows(() => m(1, m.number.not.infinite.greaterThan(5))); t.notThrows(() => m(1, m.number.not.infinite.greaterThan(5)));
t.notThrows(() => m('foo!', m.string.not.alphanumeric)); t.notThrows(() => m('foo!', m.string.not.alphanumeric));
t.throws(() => m('', m.string.not.empty), '[NOT] Expected string to be empty, got ``'); t.throws(() => m('' as any, m.string.not.empty), '[NOT] Expected string to be empty, got ``');
}); });

40
source/test/typed-array.ts

@ -6,60 +6,60 @@ test('typedArray', t => {
t.notThrows(() => m(new Uint8Array(2), m.typedArray)); t.notThrows(() => m(new Uint8Array(2), m.typedArray));
t.notThrows(() => m(new Int32Array(2), m.typedArray)); t.notThrows(() => m(new Int32Array(2), m.typedArray));
t.notThrows(() => m(new Float64Array(2), m.typedArray)); t.notThrows(() => m(new Float64Array(2), m.typedArray));
t.throws(() => m('foo', m.typedArray), 'Expected argument to be of type `typedArray` but received type `string`'); t.throws(() => m('foo' as any, m.typedArray), 'Expected argument to be of type `typedArray` but received type `string`');
t.throws(() => m(12, m.typedArray), 'Expected argument to be of type `typedArray` but received type `number`'); t.throws(() => m(12 as any, m.typedArray), 'Expected argument to be of type `typedArray` but received type `number`');
}); });
test('int8Array', t => { test('int8Array', t => {
t.notThrows(() => m(new Int8Array(2), m.int8Array)); t.notThrows(() => m(new Int8Array(2), m.int8Array));
t.throws(() => m('foo', m.int8Array), 'Expected argument to be of type `int8Array` but received type `string`'); t.throws(() => m('foo' as any, m.int8Array), 'Expected argument to be of type `int8Array` but received type `string`');
t.throws(() => m(12, m.int8Array), 'Expected argument to be of type `int8Array` but received type `number`'); t.throws(() => m(12 as any, m.int8Array), 'Expected argument to be of type `int8Array` but received type `number`');
}); });
test('uint8Array', t => { test('uint8Array', t => {
t.notThrows(() => m(new Uint8Array(2), m.uint8Array)); t.notThrows(() => m(new Uint8Array(2), m.uint8Array));
t.throws(() => m('foo', m.uint8Array), 'Expected argument to be of type `uint8Array` but received type `string`'); t.throws(() => m('foo' as any, m.uint8Array), 'Expected argument to be of type `uint8Array` but received type `string`');
t.throws(() => m(12, m.uint8Array), 'Expected argument to be of type `uint8Array` but received type `number`'); t.throws(() => m(12 as any, m.uint8Array), 'Expected argument to be of type `uint8Array` but received type `number`');
}); });
test('uint8ClampedArray', t => { test('uint8ClampedArray', t => {
t.notThrows(() => m(new Uint8ClampedArray(2), m.uint8ClampedArray)); t.notThrows(() => m(new Uint8ClampedArray(2), m.uint8ClampedArray));
t.throws(() => m('foo', m.uint8ClampedArray), 'Expected argument to be of type `uint8ClampedArray` but received type `string`'); t.throws(() => m('foo' as any, m.uint8ClampedArray), 'Expected argument to be of type `uint8ClampedArray` but received type `string`');
t.throws(() => m(12, m.uint8ClampedArray), 'Expected argument to be of type `uint8ClampedArray` but received type `number`'); t.throws(() => m(12 as any, m.uint8ClampedArray), 'Expected argument to be of type `uint8ClampedArray` but received type `number`');
}); });
test('int16Array', t => { test('int16Array', t => {
t.notThrows(() => m(new Int16Array(2), m.int16Array)); t.notThrows(() => m(new Int16Array(2), m.int16Array));
t.throws(() => m('foo', m.int16Array), 'Expected argument to be of type `int16Array` but received type `string`'); t.throws(() => m('foo' as any, m.int16Array), 'Expected argument to be of type `int16Array` but received type `string`');
t.throws(() => m(12, m.int16Array), 'Expected argument to be of type `int16Array` but received type `number`'); t.throws(() => m(12 as any, m.int16Array), 'Expected argument to be of type `int16Array` but received type `number`');
}); });
test('uint16Array', t => { test('uint16Array', t => {
t.notThrows(() => m(new Uint16Array(2), m.uint16Array)); t.notThrows(() => m(new Uint16Array(2), m.uint16Array));
t.throws(() => m('foo', m.uint16Array), 'Expected argument to be of type `uint16Array` but received type `string`'); t.throws(() => m('foo' as any, m.uint16Array), 'Expected argument to be of type `uint16Array` but received type `string`');
t.throws(() => m(12, m.uint16Array), 'Expected argument to be of type `uint16Array` but received type `number`'); t.throws(() => m(12 as any, m.uint16Array), 'Expected argument to be of type `uint16Array` but received type `number`');
}); });
test('int32Array', t => { test('int32Array', t => {
t.notThrows(() => m(new Int32Array(2), m.int32Array)); t.notThrows(() => m(new Int32Array(2), m.int32Array));
t.throws(() => m('foo', m.int32Array), 'Expected argument to be of type `int32Array` but received type `string`'); t.throws(() => m('foo' as any, m.int32Array), 'Expected argument to be of type `int32Array` but received type `string`');
t.throws(() => m(12, m.int32Array), 'Expected argument to be of type `int32Array` but received type `number`'); t.throws(() => m(12 as any, m.int32Array), 'Expected argument to be of type `int32Array` but received type `number`');
}); });
test('uint32Array', t => { test('uint32Array', t => {
t.notThrows(() => m(new Uint32Array(2), m.uint32Array)); t.notThrows(() => m(new Uint32Array(2), m.uint32Array));
t.throws(() => m('foo', m.uint32Array), 'Expected argument to be of type `uint32Array` but received type `string`'); t.throws(() => m('foo' as any, m.uint32Array), 'Expected argument to be of type `uint32Array` but received type `string`');
t.throws(() => m(12, m.uint32Array), 'Expected argument to be of type `uint32Array` but received type `number`'); t.throws(() => m(12 as any, m.uint32Array), 'Expected argument to be of type `uint32Array` but received type `number`');
}); });
test('float32Array', t => { test('float32Array', t => {
t.notThrows(() => m(new Float32Array(2), m.float32Array)); t.notThrows(() => m(new Float32Array(2), m.float32Array));
t.throws(() => m('foo', m.float32Array), 'Expected argument to be of type `float32Array` but received type `string`'); t.throws(() => m('foo' as any, m.float32Array), 'Expected argument to be of type `float32Array` but received type `string`');
t.throws(() => m(12, m.float32Array), 'Expected argument to be of type `float32Array` but received type `number`'); t.throws(() => m(12 as any, m.float32Array), 'Expected argument to be of type `float32Array` but received type `number`');
}); });
test('float64Array', t => { test('float64Array', t => {
t.notThrows(() => m(new Float64Array(2), m.float64Array)); t.notThrows(() => m(new Float64Array(2), m.float64Array));
t.throws(() => m('foo', m.float64Array), 'Expected argument to be of type `float64Array` but received type `string`'); t.throws(() => m('foo' as any, m.float64Array), 'Expected argument to be of type `float64Array` but received type `string`');
t.throws(() => m(12, m.float64Array), 'Expected argument to be of type `float64Array` but received type `number`'); t.throws(() => m(12 as any, m.float64Array), 'Expected argument to be of type `float64Array` but received type `number`');
}); });

6
source/test/undefined.ts

@ -7,7 +7,7 @@ test('undefined', t => {
t.notThrows(() => m(undefined, m.undefined)); t.notThrows(() => m(undefined, m.undefined));
t.notThrows(() => m(x, m.undefined)); t.notThrows(() => m(x, m.undefined));
t.throws(() => m(y, m.undefined), 'Expected argument to be of type `undefined` but received type `number`'); t.throws(() => m(y as any, m.undefined), 'Expected argument to be of type `undefined` but received type `number`');
t.throws(() => m(null, m.undefined), 'Expected argument to be of type `undefined` but received type `null`'); t.throws(() => m(null as any, m.undefined), 'Expected argument to be of type `undefined` but received type `null`');
t.throws(() => m('foo', m.undefined), 'Expected argument to be of type `undefined` but received type `string`'); t.throws(() => m('foo' as any, m.undefined), 'Expected argument to be of type `undefined` but received type `string`');
}); });

Loading…
Cancel
Save