-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
executable file
·278 lines (227 loc) · 5.83 KB
/
index.test.js
File metadata and controls
executable file
·278 lines (227 loc) · 5.83 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const { test } = require('tape')
const { isNil } = require('./helpers')
const { check, gen } = require('tape-check')
const { version } = require('../package')
const {
reference,
instanceOf,
matches,
typed,
t,
ref,
every,
some,
multi,
patroon,
NoMatchError,
UnevenArgumentCountError,
_
} = require('./index')
test('Matches on a simple primitive', t => {
t.equal(patroon(
1, 2,
2, 3
)(2), 3)
t.end()
})
test('Matches using a RegExp', t => {
patroon(
{ a: /^bunion/ }, () => t.fail(),
{ a: /^banana/ }, () => t.pass()
)({ a: 'banana tree' })
patroon(
/^bunion/, () => t.fail(),
/^banana/, () => t.end()
)('banana tree')
})
test('Match using reference', t => {
patroon(
1, () => t.fail(),
Number, () => t.fail(),
reference(Number), t.end()
)(Number)
})
test('Matches on type and value', t => {
patroon(
every(instanceOf(Error), { x: 20 }), () => t.fail(),
every(instanceOf(Error), { x: 10 }), () => t.end(),
instanceOf(Error), () => t.fail()
)(Object.assign(new Error(), { x: 10 }))
})
test('Matches always when pattern equals value', check(gen.any, (t, val) => {
patroon(val, () => t.end())(val)
}))
test('Matches when empty array matches with empty array', t => {
patroon([], () => t.end())([])
})
test('May or may not match with any value', check(gen.any, gen.any, (t, a, b) => {
patroon(
a, () => t.end(),
_, () => t.end()
)(b)
}))
test('Matches none of the patterns and throws', t => {
t.plan(1)
t.throws(() =>
patroon(
1, () => t.fail(),
2, () => t.fail()
)(3), NoMatchError)
})
test('Does not match when a value does not exist', t => {
patroon(
{ a: { b: 2 } }, () => t.fail(),
_, () => t.end()
)({})
})
test('Throws in a predicate function', t => {
class SomeError extends Error { }
t.plan(1)
t.throws(
() => patroon(() => { throw new SomeError() }, () => {})(),
SomeError
)
})
function wrappedInFunctions (value, times) {
if (times >= 0) { return wrappedInFunctions(() => value, times - 1) }
return () => value
}
test('Functions', check(gen.any, gen.int, (t, a, times) => {
const value = wrappedInFunctions(a, times)
patroon(value, a, _, _)(a)
patroon(value, value, _, _)(a)
patroon(a, value, _, _)(a)
patroon(value, a, _, _)(value)
patroon(value, value, _, _)(value)
patroon(a, value, _, _)(value)
t.end()
}))
test('Throws when an uneven amount of arguments are passed', t => {
t.plan(1)
t.throws(() => patroon(1), UnevenArgumentCountError)
})
test('Matches when every pattern matches', t => {
t.plan(4)
const F = () => {
t.pass('False called')
return false
}
const T = () => {
t.pass('True called')
return true
}
patroon(
every(F, F, F), () => t.fail(),
every(T, T, T), () => t.end()
)(null)
})
test('Matches when some pattern matches', t => {
t.plan(4)
const F = () => {
t.pass('False called')
return false
}
const T = () => {
t.pass('True called')
return true
}
patroon(
some(F, F, F), () => t.fail(),
some(T, T, T), () => t.end()
)(null)
})
test('Matches when comparing array with object', t => {
patroon(
[1], () => t.end()
)({ 0: 1 })
})
test('Matches when comparing array with object', t => {
patroon(
{ 0: 1 }, () => t.end()
)([1])
})
test('Matches always when pattern equals value', check(gen.any, (t, val) => {
patroon(multi(val), true)(val)
patroon(multi(_, val), true)(null, val)
patroon(multi(_, _, val), true)(null, null, val)
t.end()
}))
test('Matches always when arguments match multi pattern', check(gen.array(gen.any), (t, args) => {
patroon(
multi(...args), () => t.end()
)(...args)
}))
test('Deprecated functions', ts => {
ts.plan(3)
const consoleError = console.error
console.error = message =>
ts.ok(message.startsWith('[patroon] deprecated: '))
typed(Error, null)(new Error())
t(Error, null)(new Error())
ref(Error, null)(Error)
console.error = consoleError
ts.end()
})
test('Returns primitive values', check(gen.primitive, (t, primitive) => {
t.equals(patroon(true, primitive)(true), primitive)
t.end()
}))
test(
'Does not match primitive with empty object or array',
check(gen.primitive, (t, primitive) => {
t.plan(2)
t.throws(() => patroon([], true)(primitive), NoMatchError)
t.throws(() => patroon({}, true)(primitive), NoMatchError)
}))
test('Matching on nil values', check(gen.any, v => !isNil(v), (t, any) => {
t.plan(2)
t.throws(() => patroon(any, true)(undefined), NoMatchError)
t.throws(() => patroon(any, true)(null), NoMatchError)
}))
// Circular reference
const circular = {}
circular.a = circular
test('Allows recursive patterns', t => {
t.plan(1)
t.ok(patroon(circular, true)(circular))
})
test('Throws and prints because path is not defined', t => {
t.plan(1)
t.throws(() => patroon({ b: _ }, true)(circular), NoMatchError)
})
test('Does not throw when value is recursive', t => {
t.plan(1)
t.ok(patroon({ a: _ }, true)(circular))
})
test('Does not match on reference', t => {
t.plan(4)
t.ok(patroon({ a: {} }, true)({ a: {} }))
t.ok(patroon({ a: [] }, true)({ a: [] }))
t.ok(patroon({ a: [] }, true)({ a: {} }))
t.ok(patroon({ a: {} }, true)({ a: [] }))
t.end()
})
test('Matches function', check(gen.any, gen.any, (t, a, b) => {
t.ok(matches(a)(a))
t.equals(typeof matches(a)(b), 'boolean')
t.end()
}))
test('Check that matches throws non patroon errors', t => {
class SomeError extends Error { }
const predicate = () => {
throw new SomeError()
}
const pattern = {
predicate
}
t.throws(() => matches(pattern)({ predicate: 2 }), SomeError)
t.end()
})
if (version.startsWith('2')) {
test('Deprecated functions in version 2', t => {
t.plan(3)
t.ok(isNil(require('./index').typed))
t.ok(isNil(require('./index').t))
t.ok(isNil(require('./index').ref))
})
}