/
pure_fun_spec.rb
243 lines (206 loc) · 5 KB
/
pure_fun_spec.rb
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
require File.dirname(__FILE__) + '/pure_spec_base'
require 'levitate'
describe "pure" do
describe "`fun' definitions" do
it "should work with symbols" do
pure do
fun :area => [:width, :height] do |w, h|
w*h
end
fun :width => [:border] do |b|
20 + b
end
fun :height => :border do |b|
30 + b
end
fun :border do
5
end
end.compute(4).area.should == (20 + 5)*(30 + 5)
end
it "should work with symbols and parens" do
pure do
fun(:area => [:width, :height]) do |w, h|
w*h
end
fun(:width => [:border]) do |b|
20 + b
end
fun(:height => :border) do |b|
30 + b
end
fun(:border) do
5
end
end.compute(4).area.should == (20 + 5)*(30 + 5)
end
it "should work with mixed symbols and strings" do
pure do
fun :area => [:width, "height"] do |w, h|
w*h
end
fun "width" => [:border] do |b|
20 + b
end
fun :height => "border" do |b|
30 + b
end
fun :border do
5
end
end.compute(4).area.should == (20 + 5)*(30 + 5)
end
it "should work with `def' definitions" do
pure do
fun :width do
33
end
def height
44
end
fun :area => [:width, :height] do |w, h|
w*h
end
end.compute(3).area.should == 33*44
end
it "should be overwritten by later `def' definitions" do
Levitate.no_warnings {
pure do
fun :f do
44
end
def f
33
end
end.compute(10).f.should == 33
}
end
it "should overwrite earlier `def' definitions" do
Levitate.no_warnings {
pure do
def f
33
end
fun :f do
44
end
end.compute(10).f.should == 44
}
end
it "should support splat in block args" do
pure do
fun :area => [:width, :height] do |*a|
a[0]*a[1]
end
def width
3
end
def height
4
end
end.compute(3).area.should == 12
end
it "should support splat with single-element array" do
pure do
name = [:f]
fun(*name) do
33
end
end.compute(3).f.should == 33
end
it "should not preclude `def' definitions called `fun'" do
pure do
def misery(fun)
fun**2
end
def fun(a, b)
a + b
end
def a
3
end
def b
5
end
end.compute(3).misery.should == 64
end
it "should raise error when given hash of size != 1" do
lambda {
pure do
fun :x => 1, :y => 2 do
end
end
}.should raise_error(ArgumentError)
end
it "should raise error given more than 1 argument" do
lambda {
pure do
fun :x, :y do
end
end
}.should raise_error(ArgumentError)
end
it "should raise error with &block unless Pure::Parser::Internal is used" do
code = lambda {
pure do
fun :f, &lambda { 33 }
end.compute(4).f.should == 33
}
if Pure.parser.name == "Pure::Parser::Internal"
code.should_not raise_error
else
code.should raise_error(Pure::ParseError)
end
code = lambda {
pure do
t = lambda { 33 }
fun :f, &t
end.compute(4).f.should == 33
}
if Pure.parser.name == "Pure::Parser::Internal"
code.should_not raise_error
else
code.should raise_error(Pure::ParseError)
end
end
it "should allow function names containing any characters" do
%w[- / ? : ; . ! [ ] ( )].each { |char|
pure do
fun "f#{char}f" do
33
end
end.compute[:"f#{char}f"].should == 33
}
end
it "should have fun_name and arg_names, given multiple args" do
pure do
fun :f => [:x, :y] do |x, y|
[fun_name, arg_names, x + y]
end
end.compute(:x => 11, :y => 22).f.should == [:f, [:x, :y], 33]
end
it "should have fun_name and fun_args, given 1 arg" do
pure do
fun :f => :x do |x|
[fun_name, arg_names, x]
end
end.compute(:x => 11).f.should == [:f, [:x], 11]
end
it "should have fun_name and fun_args, given no args" do
pure do
fun :f do
[fun_name, arg_names]
end
end.compute.f.should == [:f, []]
end
it "should not see internals of the compiler" do
lambda {
pure do
fun :f do
spec
end
end.compute.f
}.should raise_error(NameError)
end
end
end