pattern-match 0.1.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +1 -0
- data/README.rdoc +203 -37
- data/lib/pattern-match.rb +470 -119
- data/lib/pattern-match/version.rb +1 -1
- data/test/test_pattern-match.rb +435 -202
- metadata +9 -19
data/test/test_pattern-match.rb
CHANGED
@@ -4,66 +4,60 @@ require_relative '../lib/pattern-match'
|
|
4
4
|
class TestPatternMatch < Test::Unit::TestCase
|
5
5
|
def test_basic
|
6
6
|
this = self
|
7
|
-
ret = match([0, 1, 2, 3])
|
7
|
+
ret = match([0, 1, 2, 3]) do
|
8
8
|
with(nil) { flunk }
|
9
9
|
with(_[a, 0, 0, b]) { flunk }
|
10
|
-
with(_[a, Fixnum , 2, b])
|
10
|
+
with(_[a, Fixnum , 2, b]) do
|
11
11
|
assert_equal(this, self)
|
12
12
|
assert_equal(0, a)
|
13
13
|
assert_equal(3, b)
|
14
14
|
4
|
15
|
-
|
15
|
+
end
|
16
16
|
with(_) { flunk }
|
17
|
-
|
17
|
+
end
|
18
18
|
assert_equal(4, ret)
|
19
19
|
assert_raise(NameError) { a }
|
20
20
|
assert_raise(NameError) { b }
|
21
21
|
|
22
|
-
assert_raise(PatternMatch::NoMatchingPatternError)
|
23
|
-
match(0)
|
22
|
+
assert_raise(PatternMatch::NoMatchingPatternError) do
|
23
|
+
match(0) do
|
24
24
|
with(1) { flunk }
|
25
25
|
with(2) { flunk }
|
26
|
-
|
27
|
-
|
26
|
+
end
|
27
|
+
end
|
28
28
|
|
29
|
-
match(0)
|
29
|
+
match(0) do
|
30
30
|
with(i, guard { i.odd? }) { flunk }
|
31
31
|
with(i, guard { i.even? }) { pass }
|
32
32
|
with(_) { flunk }
|
33
|
-
|
34
|
-
|
35
|
-
assert_raise(PatternMatch::MalformedPatternError) {
|
36
|
-
match(0) {
|
37
|
-
with(_[a, a]) {}
|
38
|
-
}
|
39
|
-
}
|
33
|
+
end
|
40
34
|
end
|
41
35
|
|
42
36
|
def test_variable_shadowing
|
43
|
-
match(0)
|
44
|
-
with(a)
|
37
|
+
match(0) do
|
38
|
+
with(a) do
|
45
39
|
assert_equal(0, a)
|
46
|
-
match([1, 2])
|
47
|
-
with(_[a, b])
|
40
|
+
match([1, 2]) do
|
41
|
+
with(_[a, b]) do
|
48
42
|
assert_equal(1, a)
|
49
43
|
assert_equal(2, b)
|
50
|
-
match([3, 4, 5])
|
51
|
-
with(_[a, b, c])
|
44
|
+
match([3, 4, 5]) do
|
45
|
+
with(_[a, b, c]) do
|
52
46
|
assert_equal(3, a)
|
53
47
|
assert_equal(4, b)
|
54
48
|
assert_equal(5, c)
|
55
|
-
|
56
|
-
|
49
|
+
end
|
50
|
+
end
|
57
51
|
assert_equal(1, a)
|
58
52
|
assert_equal(2, b)
|
59
53
|
assert_raise(NameError) { c }
|
60
|
-
|
61
|
-
|
54
|
+
end
|
55
|
+
end
|
62
56
|
assert_equal(0, a)
|
63
57
|
assert_raise(NameError) { b }
|
64
58
|
assert_raise(NameError) { c }
|
65
|
-
|
66
|
-
|
59
|
+
end
|
60
|
+
end
|
67
61
|
assert_raise(NameError) { a }
|
68
62
|
assert_raise(NameError) { b }
|
69
63
|
assert_raise(NameError) { c }
|
@@ -72,240 +66,436 @@ class TestPatternMatch < Test::Unit::TestCase
|
|
72
66
|
def test_lexical_scoping(rec_call = false, f = nil)
|
73
67
|
skip 'not supported'
|
74
68
|
unless rec_call
|
75
|
-
match(0)
|
76
|
-
with(a)
|
69
|
+
match(0) do
|
70
|
+
with(a) do
|
77
71
|
assert_equal(0, a)
|
78
|
-
test_lexical_scoping(true, ->{ a })
|
72
|
+
test_lexical_scoping(true, ->{ a }) do |g|
|
79
73
|
assert_equal(0, a)
|
80
74
|
assert_equal(1, g.())
|
81
|
-
|
75
|
+
end
|
82
76
|
assert_equal(0, a)
|
83
|
-
|
84
|
-
|
77
|
+
end
|
78
|
+
end
|
85
79
|
else
|
86
80
|
assert_raise(NameError) { a }
|
87
81
|
assert_equal(0, f.())
|
88
|
-
match(1)
|
89
|
-
with(a)
|
82
|
+
match(1) do
|
83
|
+
with(a) do
|
90
84
|
assert_equal(1, a)
|
91
85
|
assert_equal(0, f.())
|
92
86
|
yield ->{ a }
|
93
87
|
assert_equal(1, a)
|
94
88
|
assert_equal(0, f.())
|
95
|
-
|
96
|
-
|
89
|
+
end
|
90
|
+
end
|
97
91
|
end
|
98
92
|
end
|
99
93
|
|
100
94
|
def test_override_singleton_method
|
101
95
|
skip 'Module#prepend not supported' unless Module.private_method_defined?(:prepend)
|
102
|
-
match(0)
|
103
|
-
with(_test_override_singleton_method)
|
96
|
+
match(0) do
|
97
|
+
with(_test_override_singleton_method) do
|
104
98
|
def self._test_override_singleton_method
|
105
99
|
1
|
106
100
|
end
|
107
101
|
assert_equal(0, _test_override_singleton_method)
|
108
|
-
|
109
|
-
|
102
|
+
end
|
103
|
+
end
|
110
104
|
end
|
111
105
|
|
112
106
|
def test_uscore
|
113
|
-
match([0, 1, Fixnum])
|
114
|
-
with(_[_, ! _(Float), _(Fixnum, :==)])
|
107
|
+
match([0, 1, Fixnum]) do
|
108
|
+
with(_[_, ! _(Float), _(Fixnum, :==)]) do
|
115
109
|
assert_raise(NameError) { _ }
|
116
|
-
|
110
|
+
end
|
117
111
|
with(_) { flunk }
|
118
|
-
|
112
|
+
end
|
119
113
|
end
|
120
114
|
|
121
115
|
def test_splat
|
122
|
-
match([0, 1, 2])
|
123
|
-
with(_[a, *b])
|
116
|
+
match([0, 1, 2]) do
|
117
|
+
with(_[a, *b]) do
|
124
118
|
assert_equal(0, a)
|
125
119
|
assert_equal([1, 2], b)
|
126
|
-
|
120
|
+
end
|
127
121
|
with(_) { flunk }
|
128
|
-
|
122
|
+
end
|
129
123
|
|
130
|
-
match([0, 1])
|
131
|
-
with(_[a, *b, c])
|
124
|
+
match([0, 1]) do
|
125
|
+
with(_[a, *b, c]) do
|
132
126
|
assert_equal(0, a)
|
133
127
|
assert_equal([], b)
|
134
128
|
assert_equal(1, c)
|
135
|
-
|
129
|
+
end
|
136
130
|
with(_) { flunk }
|
137
|
-
|
131
|
+
end
|
138
132
|
|
139
|
-
match([0, 1, 2])
|
140
|
-
with(_[a, *b, c])
|
133
|
+
match([0, 1, 2]) do
|
134
|
+
with(_[a, *b, c]) do
|
141
135
|
assert_equal(0, a)
|
142
136
|
assert_equal([1], b)
|
143
137
|
assert_equal(2, c)
|
144
|
-
|
138
|
+
end
|
145
139
|
with(_) { flunk }
|
146
|
-
|
140
|
+
end
|
147
141
|
|
148
|
-
match([[0], [1], [2]])
|
149
|
-
with(_[*_[a]])
|
142
|
+
match([[0], [1], [2]]) do
|
143
|
+
with(_[*_[a]]) do
|
150
144
|
assert_equal([0, 1, 2], a)
|
151
|
-
|
145
|
+
end
|
152
146
|
with(_) { flunk }
|
153
|
-
|
147
|
+
end
|
154
148
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
149
|
+
match([0]) do
|
150
|
+
with(_[*a, *b]) do
|
151
|
+
assert_equal([0], a)
|
152
|
+
assert_equal([], b)
|
153
|
+
end
|
154
|
+
end
|
160
155
|
end
|
161
156
|
|
162
157
|
def test_quantifier
|
163
|
-
match([0])
|
164
|
-
with(_[a, _[b, c], ___])
|
158
|
+
match([0]) do
|
159
|
+
with(_[a, _[b, c], ___]) do
|
165
160
|
assert_equal(0, a)
|
166
161
|
assert_equal([], b)
|
167
162
|
assert_equal([], c)
|
168
|
-
|
163
|
+
end
|
169
164
|
with(_) { flunk }
|
170
|
-
|
165
|
+
end
|
171
166
|
|
172
|
-
match([0, [1, 2], [3, 4]])
|
173
|
-
with(_[a, _[b, c], ___])
|
167
|
+
match([0, [1, 2], [3, 4]]) do
|
168
|
+
with(_[a, _[b, c], ___]) do
|
174
169
|
assert_equal(0, a)
|
175
170
|
assert_equal([1, 3], b)
|
176
171
|
assert_equal([2, 4], c)
|
177
|
-
|
172
|
+
end
|
178
173
|
with(_) { flunk }
|
179
|
-
|
174
|
+
end
|
180
175
|
|
181
|
-
match([0, [1, 2], [3, 4]])
|
182
|
-
with(_[a, _[b, c], ___, d])
|
176
|
+
match([0, [1, 2], [3, 4]]) do
|
177
|
+
with(_[a, _[b, c], ___, d]) do
|
183
178
|
assert_equal(0, a)
|
184
179
|
assert_equal([1], b)
|
185
180
|
assert_equal([2], c)
|
186
181
|
assert_equal([3, 4], d)
|
187
|
-
|
182
|
+
end
|
188
183
|
with(_) { flunk }
|
189
|
-
|
184
|
+
end
|
190
185
|
|
191
|
-
match([0, [1, 2], [3, 4]])
|
186
|
+
match([0, [1, 2], [3, 4]]) do
|
192
187
|
with(_[a, _[b, c], __3]) { flunk }
|
193
|
-
with(_[a, _[b, c], __2])
|
188
|
+
with(_[a, _[b, c], __2]) do
|
194
189
|
assert_equal(0, a)
|
195
190
|
assert_equal([1, 3], b)
|
196
191
|
assert_equal([2, 4], c)
|
197
|
-
|
192
|
+
end
|
198
193
|
with(_) { flunk }
|
199
|
-
|
194
|
+
end
|
200
195
|
|
201
|
-
match([0, [1, 2], [3, 4]])
|
202
|
-
with(_[a, _[b, ___], ___])
|
196
|
+
match([0, [1, 2], [3, 4]]) do
|
197
|
+
with(_[a, _[b, ___], ___]) do
|
203
198
|
assert_equal(0, a)
|
204
199
|
assert_equal([[1, 2], [3, 4]], b)
|
205
|
-
|
200
|
+
end
|
206
201
|
with(_) { flunk }
|
207
|
-
|
202
|
+
end
|
208
203
|
|
209
|
-
match([[0, [1, 2], [3, 4]], [5, [6, 7], [8, 9]], [10, [11, 12], [13, 14]]])
|
210
|
-
with(_[_[a, _[b, ___], ___], ___])
|
204
|
+
match([[0, [1, 2], [3, 4]], [5, [6, 7], [8, 9]], [10, [11, 12], [13, 14]]]) do
|
205
|
+
with(_[_[a, _[b, ___], ___], ___]) do
|
211
206
|
assert_equal([0, 5, 10], a)
|
212
207
|
assert_equal([[[1, 2], [3, 4]], [[6, 7], [8, 9]], [[11, 12], [13, 14]]], b)
|
213
|
-
|
208
|
+
end
|
214
209
|
with(_) { flunk }
|
215
|
-
|
210
|
+
end
|
211
|
+
|
212
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
213
|
+
match(0) do
|
214
|
+
with(___) {}
|
215
|
+
end
|
216
|
+
end
|
216
217
|
|
217
|
-
assert_raise(PatternMatch::MalformedPatternError)
|
218
|
-
match(0)
|
218
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
219
|
+
match(0) do
|
219
220
|
with(_[___]) {}
|
220
|
-
|
221
|
-
|
221
|
+
end
|
222
|
+
end
|
222
223
|
|
223
|
-
assert_raise(PatternMatch::MalformedPatternError)
|
224
|
-
match(0)
|
224
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
225
|
+
match(0) do
|
225
226
|
with(_[_[___]]) {}
|
226
|
-
|
227
|
-
|
227
|
+
end
|
228
|
+
end
|
228
229
|
|
229
|
-
assert_raise(PatternMatch::MalformedPatternError)
|
230
|
-
match(0)
|
230
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
231
|
+
match(0) do
|
231
232
|
with(_[a, ___, ___]) {}
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
match([0]) do
|
237
|
+
with(_[a, ___, *b]) do
|
238
|
+
assert_equal([0], a)
|
239
|
+
assert_equal([], b)
|
240
|
+
end
|
241
|
+
with(_) { flunk }
|
242
|
+
end
|
243
|
+
|
244
|
+
match([0]) do
|
245
|
+
with(_[a, ___?, *b]) do
|
246
|
+
assert_equal([], a)
|
247
|
+
assert_equal([0], b)
|
248
|
+
end
|
249
|
+
with(_) { flunk }
|
250
|
+
end
|
251
|
+
|
252
|
+
match([[0, 1, :a, 'A'], [2, :b, :c, 'B'], ['C'], 3]) do
|
253
|
+
with(_[_[a & Fixnum, ___, b & Symbol, ___, c], ___, d]) do
|
254
|
+
assert_equal([[0, 1], [2], []], a)
|
255
|
+
assert_equal([[:a], [:b, :c], []], b)
|
256
|
+
assert_equal(['A', 'B', 'C'], c)
|
257
|
+
assert_equal(3, d)
|
258
|
+
end
|
259
|
+
with(_) { flunk }
|
260
|
+
end
|
261
|
+
|
262
|
+
match([0, 1, 2, 4, 5]) do
|
263
|
+
with(_[*a, b & Fixnum, __2, *c], guard { b.all?(&:even?) }) do
|
264
|
+
assert_equal([0, 1], a)
|
265
|
+
assert_equal([2, 4], b)
|
266
|
+
assert_equal([5], c)
|
267
|
+
end
|
268
|
+
with(_) { flunk }
|
269
|
+
end
|
270
|
+
|
271
|
+
match([0, 1, 1, 2, 3, 3, 4]) do
|
272
|
+
with(_[*a, b, b, *c]) do
|
273
|
+
assert_equal([0, 1, 1, 2], a)
|
274
|
+
assert_equal(3, b)
|
275
|
+
assert_equal([4], c)
|
276
|
+
end
|
277
|
+
with(_) { flunk }
|
278
|
+
end
|
279
|
+
|
280
|
+
match([0, 1, 1, 2, 3, 3, 4]) do
|
281
|
+
with(_[*a, b, b, *c], guard { b < 3 }) do
|
282
|
+
assert_equal([0], a)
|
283
|
+
assert_equal(1, b)
|
284
|
+
assert_equal([2, 3, 3, 4], c)
|
285
|
+
end
|
286
|
+
with(_) { flunk }
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_sequence
|
291
|
+
match([0, 1]) do
|
292
|
+
with(_[Seq(a)]) { flunk }
|
293
|
+
with(_[Seq(a, b)]) do
|
294
|
+
assert_equal(0, a)
|
295
|
+
assert_equal(1, b)
|
296
|
+
end
|
297
|
+
with(_) { flunk }
|
298
|
+
end
|
299
|
+
|
300
|
+
match([0, 1]) do
|
301
|
+
with(_[Seq(a), Seq(b)]) do
|
302
|
+
assert_equal(0, a)
|
303
|
+
assert_equal(1, b)
|
304
|
+
end
|
305
|
+
with(_) { flunk }
|
306
|
+
end
|
307
|
+
|
308
|
+
match([0, :a, 1, 2, :b, 3]) do
|
309
|
+
with(_[Seq(a & Fixnum, b & Symbol, c & Fixnum), ___]) do
|
310
|
+
assert_equal([0, 2], a)
|
311
|
+
assert_equal([:a, :b], b)
|
312
|
+
assert_equal([1, 3], c)
|
313
|
+
end
|
314
|
+
with(_) { flunk }
|
315
|
+
end
|
316
|
+
|
317
|
+
match([0, :a, 1, 2, :b, :c]) do
|
318
|
+
with(_[Seq(a & Fixnum, b & Symbol, c & Fixnum), ___]) { flunk }
|
319
|
+
with(_) { pass }
|
320
|
+
end
|
321
|
+
|
322
|
+
match([0, 1, :a, 2, 3, :b, 4, 5]) do
|
323
|
+
with(_[a, Seq(b & Fixnum, c & Symbol, d & Fixnum), ___, e]) do
|
324
|
+
assert_equal(0, a)
|
325
|
+
assert_equal([1, 3], b)
|
326
|
+
assert_equal([:a, :b], c)
|
327
|
+
assert_equal([2, 4], d)
|
328
|
+
assert_equal(5, e)
|
329
|
+
end
|
330
|
+
with(_) { flunk }
|
331
|
+
end
|
332
|
+
|
333
|
+
match([:a, [[0, 1], [2, 3], [4, 5]], :b]) do
|
334
|
+
with(_[a, _[_[Seq(b), ___], ___], ___, c]) do
|
335
|
+
assert_equal(:a, a)
|
336
|
+
assert_equal([[[0, 1], [2, 3], [4, 5]]], b)
|
337
|
+
assert_equal(:b, c)
|
338
|
+
end
|
339
|
+
with(_) { flunk }
|
340
|
+
end
|
341
|
+
|
342
|
+
match([0]) do
|
343
|
+
with(_[Seq(a), ___, *b]) do
|
344
|
+
assert_equal([0], a)
|
345
|
+
assert_equal([], b)
|
346
|
+
end
|
347
|
+
with(_) { flunk }
|
348
|
+
end
|
349
|
+
|
350
|
+
match([0]) do
|
351
|
+
with(_[Seq(a), ___?, *b]) do
|
352
|
+
assert_equal([], a)
|
353
|
+
assert_equal([0], b)
|
354
|
+
end
|
355
|
+
with(_) { flunk }
|
356
|
+
end
|
357
|
+
|
358
|
+
match([0]) do
|
359
|
+
with(_[Seq(a), ___, Seq(b), __1]) do
|
360
|
+
assert_equal([], a)
|
361
|
+
assert_equal([0], b)
|
362
|
+
end
|
363
|
+
with(_) { flunk }
|
364
|
+
end
|
365
|
+
|
366
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
367
|
+
match(0) do
|
368
|
+
with(Seq()) {}
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
373
|
+
match(0) do
|
374
|
+
with(_[Seq()]) {}
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
379
|
+
match([0]) do
|
380
|
+
with(_[a & Seq(0)]) {}
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
assert_raise(NotImplementedError) do
|
385
|
+
match([0]) do
|
386
|
+
with(_[Seq(a & Fixnum, ___), ___]) {}
|
387
|
+
end
|
388
|
+
end
|
240
389
|
end
|
241
390
|
|
242
391
|
def test_and_or_not
|
243
|
-
match(1)
|
392
|
+
match(1) do
|
244
393
|
with(_(0) & _(1)) { flunk }
|
245
394
|
with(_) { pass }
|
246
|
-
|
395
|
+
end
|
247
396
|
|
248
|
-
match(1)
|
397
|
+
match(1) do
|
249
398
|
with(_(0) | _(1)) { pass }
|
250
399
|
with(_) { flunk }
|
251
|
-
|
400
|
+
end
|
252
401
|
|
253
|
-
match(1)
|
402
|
+
match(1) do
|
254
403
|
with(_[] | _(1)) { pass }
|
255
404
|
with(_) { flunk }
|
256
|
-
|
405
|
+
end
|
257
406
|
|
258
|
-
match(1)
|
407
|
+
match(1) do
|
259
408
|
with(! _(0)) { pass }
|
260
409
|
with(_) { flunk }
|
261
|
-
|
410
|
+
end
|
262
411
|
|
263
|
-
match(1)
|
412
|
+
match(1) do
|
264
413
|
with(! _[]) { pass }
|
265
414
|
with(_) { flunk }
|
266
|
-
|
415
|
+
end
|
267
416
|
|
268
|
-
match(1)
|
269
|
-
with(a & b)
|
417
|
+
match(1) do
|
418
|
+
with(a & b) do
|
270
419
|
assert_equal(1, a)
|
271
420
|
assert_equal(1, b)
|
272
|
-
|
421
|
+
end
|
273
422
|
with(_) { flunk }
|
274
|
-
|
423
|
+
end
|
275
424
|
|
276
|
-
match(1)
|
277
|
-
with(
|
425
|
+
match(1) do
|
426
|
+
# You can not just write `with(0 | 1)',
|
427
|
+
# because alternation method `|' is an instance method of Pattern.
|
428
|
+
with(_ & 0 | 1) { pass }
|
278
429
|
with(_) { flunk }
|
279
|
-
|
430
|
+
end
|
280
431
|
|
281
|
-
assert_raise(PatternMatch::MalformedPatternError)
|
282
|
-
match(1)
|
432
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
433
|
+
match(1) do
|
283
434
|
with(a | b) {}
|
284
|
-
|
285
|
-
|
435
|
+
end
|
436
|
+
end
|
286
437
|
|
287
|
-
match(1)
|
438
|
+
match(1) do
|
288
439
|
with(! _(0)) { pass }
|
289
440
|
with(_) { flunk }
|
290
|
-
|
441
|
+
end
|
291
442
|
|
292
|
-
assert_raise(PatternMatch::MalformedPatternError)
|
293
|
-
match(1)
|
443
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
444
|
+
match(1) do
|
294
445
|
with(! a) {}
|
295
|
-
|
296
|
-
|
446
|
+
end
|
447
|
+
end
|
297
448
|
|
298
|
-
assert_raise(PatternMatch::MalformedPatternError)
|
299
|
-
match(1)
|
449
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
450
|
+
match(1) do
|
300
451
|
with(a | ___) {}
|
301
|
-
|
302
|
-
|
452
|
+
end
|
453
|
+
end
|
303
454
|
|
304
|
-
assert_raise(PatternMatch::MalformedPatternError)
|
305
|
-
match(1)
|
455
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
456
|
+
match(1) do
|
306
457
|
with(a & ___) {}
|
307
|
-
|
308
|
-
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
match(1) do
|
462
|
+
with(And(0, 1)) { flunk }
|
463
|
+
with(_) { pass }
|
464
|
+
end
|
465
|
+
|
466
|
+
match(1) do
|
467
|
+
with(Or(0, 1)) { pass }
|
468
|
+
with(_) { flunk }
|
469
|
+
end
|
470
|
+
|
471
|
+
match(1) do
|
472
|
+
with(Not(0)) { pass }
|
473
|
+
with(_) { flunk }
|
474
|
+
end
|
475
|
+
|
476
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
477
|
+
match(1) do
|
478
|
+
with(And()) {}
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
483
|
+
match(1) do
|
484
|
+
with(Or()) {}
|
485
|
+
end
|
486
|
+
end
|
487
|
+
|
488
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
489
|
+
match(1) do
|
490
|
+
with(Not()) {}
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
495
|
+
match(1) do
|
496
|
+
with(Not(0, 1)) {}
|
497
|
+
end
|
498
|
+
end
|
309
499
|
end
|
310
500
|
|
311
501
|
def test_match_without_argument
|
@@ -313,120 +503,163 @@ class TestPatternMatch < Test::Unit::TestCase
|
|
313
503
|
end
|
314
504
|
|
315
505
|
def test_deconstructor_class
|
316
|
-
assert_raise(NotImplementedError)
|
506
|
+
assert_raise(NotImplementedError) do
|
317
507
|
c = Class.new
|
318
|
-
match(0)
|
319
|
-
with(c.(a))
|
320
|
-
|
321
|
-
|
322
|
-
|
508
|
+
match(0) do
|
509
|
+
with(c.(a)) do
|
510
|
+
end
|
511
|
+
end
|
512
|
+
end
|
323
513
|
end
|
324
514
|
|
325
515
|
def test_deconstructor_class_struct
|
326
516
|
s = Struct.new(:a, :b, :c)
|
327
|
-
match(s[0, 1, 2])
|
328
|
-
with(s.(a, b, c))
|
517
|
+
match(s[0, 1, 2]) do
|
518
|
+
with(s.(a, b, c)) do
|
329
519
|
assert_equal(0, a)
|
330
520
|
assert_equal(1, b)
|
331
521
|
assert_equal(2, c)
|
332
|
-
|
522
|
+
end
|
333
523
|
with(_) { flunk }
|
334
|
-
|
524
|
+
end
|
525
|
+
end
|
526
|
+
|
527
|
+
def test_deconstructor_class_attributes_with_hash
|
528
|
+
person_class = Struct.new(:name, :age) do
|
529
|
+
include PatternMatch::AttributeMatcher
|
530
|
+
end
|
531
|
+
|
532
|
+
company_class = Struct.new(:name) do
|
533
|
+
include PatternMatch::AttributeMatcher
|
534
|
+
end
|
535
|
+
|
536
|
+
match([person_class.new("Mary", 50), company_class.new("C")]) do
|
537
|
+
with(_[company_class.(:name => "Mary"), company_class.(:name => "C")]) { flunk }
|
538
|
+
with(_[person_class.(:age, :name => person_name), company_class.(:name => company_name)]) do
|
539
|
+
assert_equal("Mary", person_name)
|
540
|
+
assert_equal(50, age)
|
541
|
+
assert_equal("C", company_name)
|
542
|
+
end
|
543
|
+
with(_) { flunk }
|
544
|
+
end
|
335
545
|
end
|
336
546
|
|
337
547
|
def test_deconstructor_class_complex
|
338
|
-
match(Complex(0, 1))
|
339
|
-
with(Complex.(a, b))
|
548
|
+
match(Complex(0, 1)) do
|
549
|
+
with(Complex.(a, b)) do
|
340
550
|
assert_equal(0, a)
|
341
551
|
assert_equal(1, b)
|
342
|
-
|
552
|
+
end
|
343
553
|
with(_) { flunk }
|
344
|
-
|
554
|
+
end
|
345
555
|
end
|
346
556
|
|
347
557
|
def test_deconstructor_class_rational
|
348
|
-
match(Rational(0, 1))
|
349
|
-
with(Rational.(a, b))
|
558
|
+
match(Rational(0, 1)) do
|
559
|
+
with(Rational.(a, b)) do
|
350
560
|
assert_equal(0, a)
|
351
561
|
assert_equal(1, b)
|
352
|
-
|
562
|
+
end
|
353
563
|
with(_) { flunk }
|
354
|
-
|
564
|
+
end
|
355
565
|
end
|
356
566
|
|
357
567
|
def test_deconstructor_class_matchdata
|
358
568
|
m = /.../.match('abc')
|
359
|
-
match(m)
|
360
|
-
with(MatchData.(a))
|
569
|
+
match(m) do
|
570
|
+
with(MatchData.(a)) do
|
361
571
|
assert_equal('abc', a)
|
362
|
-
|
572
|
+
end
|
363
573
|
with(_) { flunk }
|
364
|
-
|
574
|
+
end
|
365
575
|
|
366
576
|
m = /(.)(.)(.)/.match('abc')
|
367
|
-
match(m)
|
368
|
-
with(MatchData.(a, b, c))
|
577
|
+
match(m) do
|
578
|
+
with(MatchData.(a, b, c)) do
|
369
579
|
assert_equal('a', a)
|
370
580
|
assert_equal('b', b)
|
371
581
|
assert_equal('c', c)
|
372
|
-
|
582
|
+
end
|
373
583
|
with(_) { flunk }
|
374
|
-
|
584
|
+
end
|
375
585
|
end
|
376
586
|
|
377
587
|
def test_deconstructor_obj_regexp
|
378
|
-
match('abc')
|
588
|
+
match('abc') do
|
379
589
|
with(/./.(a)) { flunk }
|
380
|
-
with(a & /.../.(b))
|
590
|
+
with(a & /.../.(b)) do
|
381
591
|
assert_equal('abc', a)
|
382
592
|
assert_equal('abc', b)
|
383
|
-
|
593
|
+
end
|
384
594
|
with(_) { flunk }
|
385
|
-
|
595
|
+
end
|
386
596
|
|
387
|
-
match('abc')
|
388
|
-
with(a & /(.)(.)(.)/.(b, c ,d))
|
597
|
+
match('abc') do
|
598
|
+
with(a & /(.)(.)(.)/.(b, c ,d)) do
|
389
599
|
assert_equal('abc', a)
|
390
600
|
assert_equal('a', b)
|
391
601
|
assert_equal('b', c)
|
392
602
|
assert_equal('c', d)
|
393
|
-
|
603
|
+
end
|
394
604
|
with(_) { flunk }
|
395
|
-
|
605
|
+
end
|
396
606
|
end
|
397
607
|
|
398
|
-
def
|
399
|
-
match(
|
400
|
-
with(
|
401
|
-
with(
|
402
|
-
assert_equal(
|
403
|
-
assert_equal(
|
404
|
-
|
608
|
+
def test_deconstructor_class_hash
|
609
|
+
match({a: 0, b: 1}) do
|
610
|
+
with(Hash.(a: a, b: b, c: c)) { flunk }
|
611
|
+
with(Hash.(a: a, b: b)) do
|
612
|
+
assert_equal(0, a)
|
613
|
+
assert_equal(1, b)
|
614
|
+
end
|
405
615
|
with(_) { flunk }
|
406
|
-
|
616
|
+
end
|
407
617
|
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
618
|
+
match({a: 0, b: 1}) do
|
619
|
+
with(Hash.(a: a)) do
|
620
|
+
assert_equal(0, a)
|
621
|
+
end
|
622
|
+
with(_) { flunk }
|
623
|
+
end
|
414
624
|
|
415
|
-
|
416
|
-
|
417
|
-
with(
|
418
|
-
|
625
|
+
match({a: 0}) do
|
626
|
+
with(Hash.(a: 0)) { pass }
|
627
|
+
with(_) { flunk }
|
628
|
+
end
|
629
|
+
|
630
|
+
match({a: 0, b: 1}) do
|
631
|
+
with(Hash.(:a, :b, :c)) { flunk }
|
632
|
+
with(Hash.(:a, :b)) do
|
419
633
|
assert_equal(0, a)
|
420
634
|
assert_equal(1, b)
|
421
|
-
|
635
|
+
end
|
422
636
|
with(_) { flunk }
|
423
|
-
|
637
|
+
end
|
424
638
|
|
425
|
-
match({a: 0, b: 1})
|
426
|
-
with(Hash.(a:
|
639
|
+
match({a: 0, b: 1}) do
|
640
|
+
with(Hash.(:a, :b, b: b2)) do
|
427
641
|
assert_equal(0, a)
|
428
|
-
|
642
|
+
assert_raise(NameError) { b }
|
643
|
+
assert_equal(1, b2)
|
644
|
+
end
|
645
|
+
with(_) { flunk }
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
def test_object
|
650
|
+
match(0) do
|
651
|
+
with(Object.(:to_s, :to_i => i & 1)) { flunk }
|
652
|
+
with(Object.(:to_s, :to_i => i & 0)) do
|
653
|
+
assert_equal('0', to_s)
|
654
|
+
assert_equal(0, i)
|
655
|
+
end
|
429
656
|
with(_) { flunk }
|
430
|
-
|
657
|
+
end
|
658
|
+
|
659
|
+
assert_raise(PatternMatch::MalformedPatternError) do
|
660
|
+
match(0) do
|
661
|
+
with(Object.(a, b)) {}
|
662
|
+
end
|
663
|
+
end
|
431
664
|
end
|
432
665
|
end
|