-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmatch.feature
225 lines (200 loc) · 5.95 KB
/
match.feature
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
Feature: Match expression
Scenario: Match scalars
Given a file named "main.cloe" with:
"""
(print (match 42 42 "Matched!"))
(print (match 42
2049 "Not matched..."
42 "Matched!"))
(print (match "Cloe" "Cloe" "Matched!"))
(print (match "Cloe"
"cloe" "Not matched..."
"Cloe" "Matched!"))
(print (match true true "Matched!"))
(print (match true
false "Not matched..."
true "Matched!"))
(print (match nil nil "Matched!"))
(print (match "Matched!" x x))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly:
"""
Matched!
Matched!
Matched!
Matched!
Matched!
Matched!
Matched!
Matched!
"""
Scenario: Match collections
Given a file named "main.cloe" with:
"""
(print (match [] [] "Matched!"))
(print (match [42]
[] "Not matched..."
[42 42] "Not matched..."
[42] "Matched!"))
(print (match {} {} "Matched!"))
(print (match {"foo" 42}
{} "Not matched..."
{"foo" 2049} "Not matched..."
{"foo" 42 "bar" 2049} "Not matched..."
{"bar" 42} "Not matched..."
{"foo" 42} "Matched!"))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly:
"""
Matched!
Matched!
Matched!
Matched!
"""
Scenario: Use wildcards
Given a file named "main.cloe" with:
"""
(print (match "Matched!"
42 "Not matched..."
x x))
(print (match [42 2049]
[] "Not matched..."
[2049] "Not matched..."
[42 42] "Not matched..."
[foo 42] "Not matched..."
[42 bar 2049] "Not matched..."
[foo 2049] "Matched!"))
(print (match {"foo" 42 "bar" "Matched!"}
{} "Not matched..."
{"foo" 42} "Not matched..."
{"bar" 42} "Not matched..."
{"foo" foo "bar" 42} "Not matched..."
{"bar" bar "foo" 2049} "Not matched..."
{"foo" foo "bar" bar} bar))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly:
"""
Matched!
Matched!
Matched!
"""
Scenario: Nest collections
Given a file named "main.cloe" with:
"""
(print (match {"foo" 42 "bar" ["The pattern" "is" "Matched!"]}
{"bar" [foo "is" baz] "foo" 42} baz
{"foo" foo "bar" bar} "Not matched..."))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "Matched!"
Scenario: Use rest pattern of list
Given a file named "main.cloe" with:
"""
(print (match [[42 2049] ["This" "is" "Matched!"]]
[[..foo] ["This" "is" "not" "Matched!"]] "Not matched..."
[[..foo] ["This" ..bar]] (@ bar 2)))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "Matched!"
Scenario: Use rest pattern of dictionary
Given a file named "main.cloe" with:
"""
(print (match {"foo" {42 2049} "bar" {"This" "Matched!"}}
[[..foo] ["This" "is" "not" "Matched!"]] "Not matched..."
{"foo" {42 2050} "bar" {"This" "Matched!"}} "Not matched..."
{"foo" {..foo} "bar" {"this" bar}} "Not matched..."
{"foo" {..foo} "bar" {"This" bar}} bar))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "Matched!"
Scenario: Match an invalid list with a valid pattern
Given a file named "main.cloe" with:
"""
(print (match ["Matched!" ..{}]
[x ..xs] x))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "Matched!"
Scenario: Use match expression with let statements
Given a file named "main.cloe" with:
"""
(let y (match [123 456 789]
[x ..xs] xs))
(print y)
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "[456 789]"
Scenario: Match collections with patterns in let statements
Given a file named "main.cloe" with:
"""
(let [x y ..xs] ["foo" "bar" "baz"])
(let {"foo" value ..rest} {"foo" 42 "bar" 2049})
(seq!
(print y)
(print value))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly:
"""
bar
42
"""
Scenario: Use let-match statements in function definitions
Given a file named "main.cloe" with:
"""
(def (f x y)
(let [_ x ..xs] x)
(let {"foo" y ..rest} y)
[x y])
(print (f ["foo" "bar" "baz"] {"foo" 42 "bar" 2049}))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly:
"""
["bar" 42]
"""
Scenario: Nest match expressions
Given a file named "main.cloe" with:
"""
(print (match [1 2 3]
[x ..xs] (match xs
[y ..ys] (+ x y))))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "3"
Scenario: Use many match expressions in a function
Given a file named "main.cloe" with:
"""
(def (f ..xs)
(let xs [1 2 3 ..xs])
(match xs
[] (if true (match xs [x] x [..xs] "OOOOK"))
[x y ..xs] (match xs [] [x y] [x y ..xs] [..xs (+ x y)])))
(print (f 4 5 6))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "[5 6 7]"
Scenario: Use similar match expressions in a expression
Given a file named "main.cloe" with:
"""
(print [(match [1] [x] x) (match [1] [x] x)])
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "[1 1]"
Scenario: Use an anonymous function in a match case
Given a file named "main.cloe" with:
"""
(print (match 42 x ((\ (y) x) 123)))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "42"
Scenario: Use a match expression in an anonymous function
Given a file named "main.cloe" with:
"""
(print ((\ (y) (match 123 x y)) 42))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "42"