-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
110 lines (94 loc) · 3.78 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// @flow
'use strict';
const test = require('ava');
const is = require('./');
test('is without name', t => {
t.is(is(true, is.boolean), true);
t.is(is(false, is.boolean), false);
t.throws(() => is(42, is.boolean));
});
function fmt(...keyPath) /*: string */ {
return ['value', ...keyPath].join(' > ');
}
test('is with name as func', t => {
t.throws(() => {
is({ foo: [{ bar: false }] }, is.shape({ foo: is.arrayOf(is.objectOf(is.string)) }), fmt);
}, 'value > foo > 0 > bar must be a string');
});
test('is.boolean', t => {
t.is(is(true, is.boolean, 'true'), true);
t.is(is(false, is.boolean, 'false'), false);
t.throws(() => is(42, is.boolean, '42'));
});
test('is.number', t => {
t.is(is(42, is.number, '42'), 42);
t.is(is(NaN, is.number, 'NaN'), NaN);
t.throws(() => is(true, is.number, 'true'));
});
test('is.string', t => {
t.is(is('', is.string, ''), '');
t.is(is('hi', is.string, 'hi'), 'hi');
t.throws(() => is(true, is.string, 'true'));
});
test('is.array', t => {
t.deepEqual(is([], is.array, '[]'), []);
t.deepEqual(is([1, 2, 3], is.array, '[1, 2, 3]'), [1, 2, 3]);
t.throws(() => is({}, is.array, '{}'));
});
test('is.func', t => {
let fn = () => {};
t.deepEqual(is(fn, is.func, 'func'), fn);
t.throws(() => is({}, is.func, '{}'));
t.throws(() => is(/regex/, is.func, '/regex/'));
});
test('is.object', t => {
t.deepEqual(is({}, is.object, '{}'), {});
t.deepEqual(is({ foo: true }, is.object, '{ foo: true }'), { foo: true });
t.throws(() => is([], is.object, '[]'));
t.throws(() => is(null, is.object, 'null'));
});
test('is.arrayOf', t => {
t.deepEqual(is([], is.arrayOf(is.number), '[]'), []);
t.deepEqual(is([1, 2, 3], is.arrayOf(is.number), '[1, 2, 3]'), [1, 2, 3]);
t.throws(() => is({}, is.arrayOf(is.number), '{}'));
t.throws(() => is(["hi"], is.arrayOf(is.number), '["hi"]'));
});
test('is.arrayish', t => {
t.deepEqual(is(1, is.arrayish(is.number), '[1]'), [1]);
t.deepEqual(is([], is.arrayish(is.number), '[]'), []);
t.deepEqual(is([1, 2, 3], is.arrayish(is.number), '[1, 2, 3]'), [1, 2, 3]);
t.throws(() => is("hi", is.arrayish(is.number), '"hi"'));
t.throws(() => is({}, is.arrayish(is.number), '{}'));
t.throws(() => is(["hi"], is.arrayish(is.number), '["hi"]'));
});
test('is.objectOf', t => {
t.deepEqual(is({}, is.objectOf(is.boolean), '{}'), {});
t.deepEqual(is({ foo: true }, is.objectOf(is.boolean), '{ foo: true }'), { foo: true });
t.throws(() => is([], is.objectOf(is.boolean), '[]'));
t.throws(() => is(null, is.objectOf(is.boolean), 'null'));
t.throws(() => is({ foo: 42 }, is.objectOf(is.boolean), '{ foo: 42 }'));
});
test('is.shape', t => {
t.deepEqual(is({ foo: true }, is.shape({ foo: is.boolean }), '{ foo: true }'), { foo: true })
t.deepEqual(is({ foo: true, bar: false }, is.shape({ foo: is.boolean }), '{ foo: true, bar: false }'), { foo: true })
t.throws(() => is([], is.shape({ foo: is.boolean }), '[]'));
t.throws(() => is(null, is.shape({ foo: is.boolean }), 'null'));
t.throws(() => is({ foo: 42 }, is.shape({ foo: is.boolean }), '{ foo: 42 }'));
});
test('is.maybe', t => {
t.is(is(undefined, is.maybe(is.boolean), 'undefined'), null);
t.is(is(null, is.maybe(is.boolean), 'null'), null);
t.is(is(true, is.maybe(is.boolean), 'true'), true);
t.throws(() => is(42, is.maybe(is.boolean), '42'));
});
test('is.default', t => {
t.is(is(undefined, is.default(is.number, 42), 'undefined'), 42);
t.is(is(null, is.default(is.number, 42), 'null'), 42);
t.is(is(3.14, is.default(is.number, 42), '3.14'), 3.14);
t.throws(() => is("hi", is.default(is.number, 42), '"hi"'));
});
test('is.either', t => {
t.is(is(true, is.either(is.boolean, is.string), 'true'), true);
t.is(is('hi', is.either(is.boolean, is.string), 'hi'), 'hi');
t.throws(() => is(42, is.either(is.boolean, is.string), '42'));
});