-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunctions.feature
213 lines (186 loc) · 5.04 KB
/
functions.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
Feature: Functions
Scenario: Define a function
Given a file named "main.cloe" with:
"""
(def (f x) x)
(print (f 42))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "42"
Scenario: Apply a function to a positional argument
Given a file named "main.cloe" with:
"""
(def (f x) x)
(print (f 123))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "123"
Scenario: Apply a function to 2 positional arguments
Given a file named "main.cloe" with:
"""
(def (f x y) (+ x y))
(print (f 123 456))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "579"
Scenario: Override keyword arguments
Given a file named "main.cloe" with:
"""
(def (func . x nil) x)
(seq!
(print (func . x nil ..{"x" 42}))
(print (func . ..{"x" nil} x 42))
(print (func . ..{"x" 42} ..{"x" nil} x 42)))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly:
"""
42
42
42
"""
Scenario: Apply a function to complex arguments
Given a file named "main.cloe" with:
"""
(def (f x ..args . foo 4 ..kwargs) (+ x ..args foo))
(print (f 1 2 . foo 3))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "6"
Scenario: Apply a function to very complex arguments
Given a file named "main.cloe" with:
"""
(def (func x1 x2 ..args . y1 0 y2 1 ..kwargs)
(+ x1 x2 ..args y1 y2))
(print (func 1 1 1 ..[1 1 1] . y1 1 1 100000000 ..{"y2" 1}))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "8"
Scenario: Define a variable
Given a file named "main.cloe" with:
"""
(let foo 123)
(print foo)
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "123"
Scenario: Define a recursive variable
Given a file named "main.cloe" with:
"""
(let l [42 ..l])
(print (l 1))
(print (l 2))
(print (l 3))
"""
When I run `cloe main.cloe`
Then the exit status should not be 0
Scenario: Define a variable in a function
Given a file named "main.cloe" with:
"""
(def (foo x)
(let bar (+ x x))
bar)
(print (foo 21))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "42"
Scenario: Define multiple variables in a function
Given a file named "main.cloe" with:
"""
(def (foo x y)
(let bar (+ x x))
(let baz (- x y))
(* bar baz (+ x y)))
(print (foo 2 3))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "-20"
Scenario: Define nested functions
Given a file named "main.cloe" with:
"""
(def (f x)
(def (g y) (+ x y))
(g 42))
(print (f 2007))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "2049"
Scenario: Define a deeply nested function
Given a file named "main.cloe" with:
"""
(def (f x)
(def (g y)
(def (h z)
(+ x y z))
h)
((g 456) 789))
(print (f 123))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "1368"
Scenario: Shadow an argument
Given a file named "main.cloe" with:
"""
(def (f x)
(let x (+ x 1))
(let x (+ x 1))
x)
(print (f 1))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "3"
Scenario: Define an argument shadowing another
Given a file named "main.cloe" with:
"""
(def (f x)
(def (g x) x)
(g 42))
(print (f 123456))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "42"
Scenario: Call an anonymous function
Given a file named "main.cloe" with:
"""
(print ((\ (x) x) "Hello, world!"))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "Hello, world!"
Scenario: Use let expressions in functions
Given a file named "main.cloe" with:
"""
(def (foo ..xs)
(match xs
[] nil
[x ..xs] (let
y (+ ..xs)
z (- y 0)
(let [v w] xs
(+ v w x y z)))))
(print (foo 2 3 4))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "23"
Scenario: Use let expressions in let statements
Given a file named "main.cloe" with:
"""
(let x 21)
(print
(let
y x
z x
(+ y z)))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "42"
Scenario: Use patterns in let expressions
Given a file named "main.cloe" with:
"""
(print
(let
xs (map (\ (x) (** x 2)) [1 2 3])
[y z _] xs
(+ y z ..xs)))
"""
When I successfully run `cloe main.cloe`
Then the stdout should contain exactly "19"