File: proc_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (265 lines) | stat: -rw-r--r-- 6,905 bytes parent folder | download
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
require File.expand_path('../../spec_helper', __FILE__)

describe "A Proc" do
  it "captures locals from the surrounding scope" do
    var = 1
    lambda { var }.call.should == 1
  end

  ruby_version_is ""..."1.9" do
    it "overwrites a captured local when used as an argument" do
      var = 1
      lambda { |var| var }.call(2).should == 2
      var.should == 2
    end
  end

  ruby_version_is "1.9" do
    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
  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

    ruby_version_is ""..."1.9" do
      it "does not raise an exception if a value is passed" do
        @l.call(0).should == 1
      end
    end

    ruby_version_is "1.9" do
      it "raises an ArgumentErro if a value is passed" do
        lambda { @l.call(0) }.should raise_error(ArgumentError)
      end
    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

    ruby_version_is ""..."1.9" do
      it "assigns nil to the argument if no value is passed" do
        @l.call.should be_nil
      end

      it "assigns all the values passed to the argument as an Array" do
        @l.call(1, 2).should == [1, 2]
      end
    end

    ruby_version_is "1.9" do
      it "raises an ArgumentError if no value is passed" do
        lambda { @l.call }.should raise_error(ArgumentError)
      end
    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

    ruby_version_is ""..."1.9" do
      it "raises an ArgumentError when passed a single Array" do
        lambda { @l.call([1, 2]) }.should raise_error(ArgumentError)
      end

      it "raises an ArgumentError when passed a single object" do
        obj = mock("block yield to_ary")
        obj.should_not_receive(:to_ary)

        lambda { @l.call(obj) }.should raise_error(ArgumentError)
      end
    end

    ruby_version_is "1.9" do
      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 an 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
end