forked from ruby/spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_spec.rb
220 lines (169 loc) · 5.53 KB
/
proc_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
require_relative '../spec_helper'
describe "A Proc" do
it "captures locals from the surrounding scope" do
var = 1
lambda { var }.call.should == 1
end
it "does not capture a local when an argument has the same name" do
var = 1
lambda { |var| var }.call(2).should == 2
var.should == 1
end
describe "taking zero arguments" do
before :each do
@l = lambda { 1 }
end
it "does not raise an exception if no values are passed" do
@l.call.should == 1
end
it "raises an ArgumentErro if a value is passed" do
lambda { @l.call(0) }.should raise_error(ArgumentError)
end
end
describe "taking || arguments" do
before :each do
@l = lambda { || 1 }
end
it "does not raise an exception when passed no values" do
@l.call.should == 1
end
it "raises an ArgumentError if a value is passed" do
lambda { @l.call(0) }.should raise_error(ArgumentError)
end
end
describe "taking |a| arguments" do
before :each do
@l = lambda { |a| a }
end
it "assigns the value passed to the argument" do
@l.call(2).should == 2
end
it "does not destructure a single Array value" do
@l.call([1, 2]).should == [1, 2]
end
it "does not call #to_ary to convert a single passed object to an Array" do
obj = mock("block yield to_ary")
obj.should_not_receive(:to_ary)
@l.call(obj).should equal(obj)
end
it "raises an ArgumentError if no value is passed" do
lambda { @l.call }.should raise_error(ArgumentError)
end
end
describe "taking |a, b| arguments" do
before :each do
@l = lambda { |a, b| [a, b] }
end
it "raises an ArgumentError if passed no values" do
lambda { @l.call }.should raise_error(ArgumentError)
end
it "raises an ArgumentError if passed one value" do
lambda { @l.call(0) }.should raise_error(ArgumentError)
end
it "assigns the values passed to the arguments" do
@l.call(1, 2).should == [1, 2]
end
it "does not call #to_ary to convert a single passed object to an Array" do
obj = mock("proc call to_ary")
obj.should_not_receive(:to_ary)
lambda { @l.call(obj) }.should raise_error(ArgumentError)
end
end
describe "taking |a, *b| arguments" do
before :each do
@l = lambda { |a, *b| [a, b] }
end
it "raises an ArgumentError if passed no values" do
lambda { @l.call }.should raise_error(ArgumentError)
end
it "does not destructure a single Array value yielded" do
@l.call([1, 2, 3]).should == [[1, 2, 3], []]
end
it "assigns all passed values after the first to the rest argument" do
@l.call(1, 2, 3).should == [1, [2, 3]]
end
it "does not call #to_ary to convert a single passed object to an Array" do
obj = mock("block yield to_ary")
obj.should_not_receive(:to_ary)
@l.call(obj).should == [obj, []]
end
end
describe "taking |*| arguments" do
before :each do
@l = lambda { |*| 1 }
end
it "does not raise an exception when passed no values" do
@l.call.should == 1
end
it "does not raise an exception when passed multiple values" do
@l.call(2, 3, 4).should == 1
end
it "does not call #to_ary to convert a single passed object to an Array" do
obj = mock("block yield to_ary")
obj.should_not_receive(:to_ary)
@l.call(obj).should == 1
end
end
describe "taking |*a| arguments" do
before :each do
@l = lambda { |*a| a }
end
it "assigns [] to the argument when passed no values" do
@l.call.should == []
end
it "assigns the argument an Array wrapping one passed value" do
@l.call(1).should == [1]
end
it "assigns the argument an Array wrapping all values passed" do
@l.call(1, 2, 3).should == [1, 2, 3]
end
it "does not call #to_ary to convert a single passed object to an Array" do
obj = mock("block yield to_ary")
obj.should_not_receive(:to_ary)
@l.call(obj).should == [obj]
end
end
describe "taking |a, | arguments" do
before :each do
@l = lambda { |a, | a }
end
it "raises an ArgumentError when passed no values" do
lambda { @l.call }.should raise_error(ArgumentError)
end
it "raises an ArgumentError when passed more than one value" do
lambda { @l.call(1, 2) }.should raise_error(ArgumentError)
end
it "assigns the argument the value passed" do
@l.call(1).should == 1
end
it "does not destructure when passed a single Array" do
@l.call([1,2]).should == [1, 2]
end
it "does not call #to_ary to convert a single passed object to an Array" do
obj = mock("block yield to_ary")
obj.should_not_receive(:to_ary)
@l.call(obj).should == obj
end
end
describe "taking |(a, b)| arguments" do
before :each do
@l = lambda { |(a, b)| [a, b] }
end
it "raises an ArgumentError when passed no values" do
lambda { @l.call }.should raise_error(ArgumentError)
end
it "destructures a single Array value yielded" do
@l.call([1, 2]).should == [1, 2]
end
it "calls #to_ary to convert a single passed object to an Array" do
obj = mock("block yield to_ary")
obj.should_receive(:to_ary).and_return([1, 2])
@l.call(obj).should == [1, 2]
end
it "raises a TypeError if #to_ary does not return an Array" do
obj = mock("block yield to_ary invalid")
obj.should_receive(:to_ary).and_return(1)
lambda { @l.call(obj) }.should raise_error(TypeError)
end
end
end