This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
type.test.js
67 lines (59 loc) · 2.02 KB
/
type.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
const { type } = require('./index')
const { expect } = require('chai')
type.addTerminal('funkyTerm', v => /^[ab]+$/.test(v))
type.addModifier('funkyMod', (v, s, e) => e(v.sub, s))
describe('type checking Suite', () => {
;[
['hi', 'string', 'number'],
[42, 'number', 'string']
].forEach(v => {
it(v[0] + ' should be a ' + v[1], () => {
expect(type[v[1]](v[0])).equals(true)
expect(type.is.a[v[1]](v[0])).equals(true)
expect(type.not[v[1]](v[0])).equals(false)
expect(type.is.not.a[v[1]](v[0])).equals(false)
type.assert[v[1]](v[0])
type.assert.is.a[v[1]](v[0])
expect(() => type.assert.not[v[1]](v[0])).throws()
expect(() => type.assert.is.not.a[v[1]](v[0])).throws()
})
it(v[0] + ' should not be a ' + v[2], () => {
expect(type[v[2]](v[0])).equals(false)
expect(type.is.a[v[2]](v[0])).equals(false)
expect(type.not[v[2]](v[0])).equals(true)
expect(type.is.not.a[v[2]](v[0])).equals(true)
type.assert.not[v[2]](v[0])
type.assert.is.not.a[v[2]](v[0])
expect(() => type.assert[v[2]](v[0])).throws()
expect(() => type.assert.is.a[v[2]](v[0])).throws()
})
})
;[
[42, 'number', false],
[[], 'string', true],
[[42], 'string', false],
[['hi'], 'string', true]
].forEach(v => {
it('should ' + (v[2] ? '' : 'not ') + 'arrayOf.' + v[1] + '(' + JSON.stringify(v[0]) + ')', () => {
expect(type.arrayOf[v[1]](v[0])).equals(v[2])
})
})
it('should support custom terminal', () => {
expect(type.funkyTerm('aaabba')).equals(true)
expect(type.funkyTerm('abc')).equals(false)
})
it('should support custom modifier', () => {
expect(type.funkyMod.string({ sub: 'yo' })).equals(true)
expect(type.funkyMod.string('yo')).equals(false)
})
it('should throw on dup modifier', () => {
expect(() => {
type.addModifier('string', () => {})
}).throws()
})
it('should throw on dup terminal', () => {
expect(() => {
type.addTerminal('string', () => {})
}).throws()
})
})