-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.spec.js
227 lines (194 loc) Β· 6.37 KB
/
array.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const assert = require('assert')
const wavematch = require('../dist/wavematch.cjs.development.js')
const { accept, reject, eq } = require('./shared.js')
describe('wavematch array specification', () => {
const emptyArray = []
it('should match Array constructor', () => {
const matchArray = array =>
wavematch(array)((arg = Array) => accept, _ => reject)
eq(matchArray(emptyArray), accept)
eq(matchArray([1]), accept)
eq(matchArray([1, 2]), accept)
eq(matchArray([1, 2, 3]), accept)
eq(matchArray([{}]), accept)
eq(matchArray([function() {}]), accept)
eq(matchArray(['foo', 'bar']), accept)
eq(matchArray('abcde'.split('')), accept)
// this tests the `isArrayLike` function in array matching cases
;(function() {
eq(matchArray(arguments), accept)
})()
})
it('should match empty array', () => {
const empty = _emptyArray =>
wavematch(_emptyArray)((array = []) => accept, _ => reject)
eq(empty([]), accept)
eq(empty(new Array()), accept)
eq(empty(new Array(0)), accept)
})
it('should prefer to match array destructuring over array constructor', () => {
const emptyFirst = wavematch(emptyArray)(
(a = []) => accept,
(a = Array) => reject,
_ => reject,
)
const constructorFirst = wavematch(emptyArray)(
(a = Array) => reject,
(a = []) => accept,
_ => reject,
)
eq(emptyFirst, accept)
eq(constructorFirst, accept)
})
it('should match empty arrays with only the array constructor', () => {
const emptyInputOnlyConstructor = wavematch(emptyArray)(
(arr = Array) => accept,
_ => reject,
)
eq(emptyInputOnlyConstructor, accept)
})
it('should match arrays whose destructured values are the same', () => {
const notTheSameElements = wavematch([1, 2])(
(nope = ['k', 'i']) => reject,
_ => accept,
)
eq(notTheSameElements, accept)
})
it('should reject patterns which describe a superset of the input', () => {
const rejectMe = wavematch([1, 2])(
(arr = [1, 2, 3, 4]) => reject,
_ => accept,
)
eq(rejectMe, accept)
const constructorSavesTheDay = wavematch([1, 2])(
(arr = [1, 2, 3, 4]) => reject,
(arr = Array) => accept,
_ => reject,
)
eq(constructorSavesTheDay, accept)
})
it('should match arrays with a subset of the elements', () => {
const threeItems = [1, 2, 3]
const one = wavematch(threeItems)((arr = [1]) => reject, _ => accept)
eq(one, accept)
const two = wavematch(threeItems)((arr = [1, 2]) => reject, _ => accept)
eq(two, accept)
const oneAndTwo = wavematch(threeItems)(
(one = [1]) => reject,
(two = [1, 2]) => reject,
_ => accept,
)
eq(oneAndTwo, accept)
const all = wavematch(threeItems)(
(one = [1]) => reject,
(two = [1, 2]) => reject,
(three = [1, 2, 3]) => accept,
_ => reject,
)
eq(all, accept)
})
it('should match arrays with multiple values', () => {
const twoSame = wavematch([{}, {}])((xs = [{}, {}]) => accept, _ => reject)
eq(twoSame, accept)
const twoDifferent = wavematch(['x', 3])(
(nope = [,]) => reject,
(alsoNope = [1, 2]) => reject,
(no = ['x', 0]) => reject,
(yes = ['x', 3]) => accept,
_ => reject,
)
eq(twoDifferent, accept)
const twoDifferentReOrdered = wavematch(['x', 3])(
(nope = [,]) => reject,
(yes = ['x', 3]) => accept,
(no = ['x', 0]) => reject,
(alsoNope = [1, 2]) => reject,
_ => reject,
)
eq(twoDifferentReOrdered, accept)
})
it('should match arrays with respect to internal order', () => {
const foo = wavematch([1, 9, 5])(
(outOfOrder = [5, 9, 1]) => reject,
(duplicate = [1, 1, 5]) => reject,
(outOfOrder = [1, 5, 9]) => reject,
(correct = [1, 9, 5]) => accept,
_ => reject,
)
eq(foo, accept)
})
it('should match arrays of strings', () => {
const arrayOfStrings = wavematch(['foo', 'bar'])(
(wrong = ['x', 'y']) => reject,
(wrong = ['', '']) => reject,
(right = ['foo', 'bar']) => accept,
(wrong = ['bar', 'foo']) => reject,
new Function(`wrong = [ "'i'", "'j'" ]`, `return "${reject.valueOf()}"`),
_ => reject,
)
eq(arrayOfStrings, accept)
const arrayOfStringsWithConstructor = wavematch(['foo'])(
(right = Array) => accept,
_ => reject,
)
eq(arrayOfStringsWithConstructor, accept)
})
it('should not match non-empty inputs to empty array patterns', () => {
const match = wavematch('arg1', [1])(
(foo, array = []) => reject,
_ => accept,
)
eq(match, accept)
})
it('should reject invalid empty arrays', () => {
const empty = array => wavematch(array)((array = []) => accept, _ => reject)
assert.deepEqual(empty(''), reject)
assert.deepEqual(empty(1), reject)
assert.deepEqual(empty([1]), reject)
assert.deepEqual(empty(['']), reject)
assert.deepEqual(empty([{}]), reject)
assert.deepEqual(empty([() => {}]), reject)
assert.deepEqual(empty([[]]), reject)
assert.deepEqual(empty([]), accept)
})
it('should match recursively', () => {
const any = wavematch.create(
(_, xs = []) => false,
(pred, [head, ...rest]) => pred(head) || any(pred, rest),
)
const yes = any(x => x === 3, [1, 2, 3, 4])
const no = any(x => x === 3, [1, 2, 4])
eq(yes, true)
eq(no, false)
})
// TODO: should not work with Boolean pattern
// it('should not work with Boolean pattern', () => {
// let matched = wavematch([1])(
// (x = Boolean) => accept,
// _ => reject
// )
// eq(matched, accept)
// })
// it('should match any element type', () => {
// const array = []
// const fn = _ => {} // cannot contain parenthesis or else SyntaxError
// const number = 1
// const string = "'i am a string'" // string must be quoted twice
// const boolean = false
// // const S = new Set()
// // const M = new Map()
// const elements = [array, fn, number, string]
// elements.forEach(element => {
// const matchElement = wavematch(element)(
// new Function(`arg = [${element}]`, `return "${accept}"`),
// (arg = Array) => reject,
// _ => reject
// )
// eq(matchElement, accept)
// })
// eq(wavematch(elements)(
// (args = Array) => accept,
// _ => reject
// ), accept)
// })
})