-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.spec.js
155 lines (136 loc) Β· 3.84 KB
/
misc.spec.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const assert = require('assert')
const wavematch = require('../dist/wavematch.cjs.development.js')
const { accept, reject, eq } = require('./shared.js')
describe('wavematch miscellaneous specification', () => {
it('should throw if default is out of scope', () => {
assert.throws(() => {
const foo = 'foo'
const match = wavematch(foo)((arg = foo) => accept, _ => accept)
match(3)
}, Error)
})
it('should throw if invalid JSON5', () => {
assert.throws(() => {
const m = wavematch('doesnt matter')(
(o = { k: Error }) => 1,
(x = Function) => 0,
_ => 2,
)
m(null)
}, Error)
})
it('null behavior', () => {
let nullTest = (value, acceptOrReject) =>
eq(wavematch(value)((arg = null) => accept, _ => reject), acceptOrReject)
nullTest(null, accept)
nullTest(undefined, reject)
})
it('undefined behavior', () => {
let undefinedTest = (value, acceptOrReject) =>
eq(
wavematch(value)((arg = undefined) => accept, _ => reject),
acceptOrReject,
)
undefinedTest(undefined, accept)
undefinedTest(null, reject)
})
describe('should allow destructuring', () => {
// todo
// describe('object destructuring', () => {
// })
describe('array destructuring', () => {
it('head-rest pattern', () => {
const match = wavematch([1, 2, 3])(
([head, ...rest]) => accept,
_ => reject,
)
eq(match, accept)
})
it('array#zip destructured', () => {
const zip = (xs, ys) =>
wavematch(xs, ys)(
(xs, ys = []) => [],
(xs = [], ys) => [],
([x, ...xs], [y, ...ys]) => [x, y].concat(zip(xs, ys)),
_ => reject,
)
assert.deepEqual(zip([1, 2, 3], ['a', 'b', 'c']), [
1,
'a',
2,
'b',
3,
'c',
])
})
it('array#zipWith destructured', () => {
const zipWith = (fn, xs, ys) =>
wavematch(fn, xs, ys)(
(fn, xs = [], ys) => [],
(fn, xs, ys = []) => [],
(fn, [x, ...xs], [y, ...ys]) =>
[fn(x, y)].concat(zipWith(fn, xs, ys)),
_ => reject,
)
assert.deepEqual(zipWith((x, y) => x + y, [1, 1, 1], [1, 1, 1]), [
2,
2,
2,
])
assert.throws(() => {
zipWith()
})
const zip = (xs, ys) => zipWith((x, y) => [x, y], xs, ys)
const flattenOnce = arr => arr.reduce((xs, x) => xs.concat(x), [])
assert.deepEqual(flattenOnce(zip([1, 2, 3], ['a', 'b', 'c'])), [
1,
'a',
2,
'b',
3,
'c',
])
})
})
})
it('should bind undefined to underscore named parameters', () => {
let match = wavematch(1, 2, 3)(
(n1, _, n3) => {
if (_ === void 0) {
return accept
}
},
_ => reject,
)
eq(match, accept)
let match2 = wavematch(1, 2, 3)(
(n1, _, __) => {
if (_ === void 0 && __ === void 0) {
return accept
}
},
_ => reject,
)
eq(match2, accept)
})
it('should throw for out of scope variables used as pattern', () => {
let fn = () => {
const m = wavematch('foo')(
(irrelevant = outOfScopeVarNameWillCauseError) => 42,
_ => 147,
)
m('n/a')
}
assert.throws(fn)
let fn2 = () => wavematch('bar')((_ = willCauseErr) => {})
assert.throws(fn2)
let capitalLetter = () =>
wavematch('qux')(
// wavematch thinks variable names used as a default parameter
// (as a pattern) is a custom class name when the variable name
// starts with acapital letter.
(capitalized = NotAClassThisShouldThrow) => {},
)
assert.throws(capitalLetter)
})
})