File: GH-744_GH-264_enum_args_using_call_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 (367 lines) | stat: -rw-r--r-- 10,410 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
require 'rspec'

describe "Enumerable#each_with_index with Enumerable#each implemented with a call rather than a yield" do
  it "passes nil to the block if each passes no args" do
    no_args_each = Class.new do
      include Enumerable
      def each(&block)
        block.call
      end
    end

    no_args_each.new.each_with_index do |args, index|
      args.should == nil
      index.should == 0
    end
  end

  it "passes the arg directly to the block if each passes one arg" do
    one_arg_each = Class.new do
      include Enumerable
      def each(&block)
        block.call("one")
      end
    end

    one_arg_each.new.each_with_index do |args, index|
      args.should == "one"
      index.should == 0
    end
  end

  it "passes an array of arguments to the block if Enumerable#each passes multiple values" do
    many_args_each = Class.new do
      include Enumerable
      def each(&block)
        block.call(0, 1, 2, 3)
      end
    end

    many_args_each.new.each_with_index do |args, index|
      args.should == [0, 1, 2, 3]
      index.should == 0
    end
  end
end

describe "Enumerator#each_with_index for a method implemented with a call rather than a yield" do
  it "passes nil to the block if the method passes no args" do
    no_args_method = Class.new do
      include Enumerable
      def my_method(&block)
        block.call
      end
    end

    no_args_method.new.enum_for(:my_method).each_with_index do |args, index|
      args.should == nil
      index.should == 0
    end

    no_args_method.new.enum_for(:my_method).next.should == nil
  end

  it "passes the arg directly to the block if the method passes one arg" do
    one_arg_each = Class.new do
      include Enumerable
      def my_method(&block)
        block.call("one")
      end
    end

    one_arg_each.new.enum_for(:my_method) do |args, index|
      args.should == "one"
      index.should == 0
    end

    one_arg_each.new.enum_for(:my_method).next.should == "one"
  end

  it "passes an array of arguments to the block if the method passes multiple values" do
    many_args_method = Class.new do
      include Enumerable
      def my_method(&block)
        block.call(0, 1, 2, 3)
      end
    end

    many_args_method.new.enum_for(:my_method).each_with_index do |args, index|
      args.should == [0, 1, 2, 3]
      index.should == 0
    end

    many_args_method.new.enum_for(:my_method).next.should == [0, 1, 2, 3]
  end
end

describe "Enumerables whose #each method passes multiple values to a block.call (rather than a yield)," do
  before do
    @test_enum = Class.new do
      include Enumerable
      def each(&block)
        block.call 1,2,3
      end
    end.new
  end

  shared_examples "an Enumerable method which takes a block" do |arity_one_behavior|
    it "passes all #each args to its block" do
      @test_enum.send(subject) do |a, b, c|
        a.should == 1
        b.should == 2
        c.should == 3
      end
    end

    it "passes the appropriate args to blocks of arity one" do
      case arity_one_behavior
        when :array
          @test_enum.send(subject) do |obj|
            obj.should == [1, 2, 3]
          end
        when :first_arg
          @test_enum.send(subject) do |obj|
            obj.should == 1
          end
        else
          raise 'Unknown arity_one_behavior'
      end

    end
  end

  shared_examples "an Enumerable method which returns an enum element" do
    it "puts an array of all each args in the returned value" do
      @test_enum.send(subject) { true }.should == [1, 2, 3]
    end
  end

  shared_examples "an Enumerable method which returns an array" do |block_ret|
    it "puts an array of all each args in the returned array" do
      @test_enum.send(subject) { block_ret.nil? ? true : block_ret}.should == [[1, 2, 3]]
    end
  end

  describe "Enumerable#to_a" do
    subject { :to_a }
    it_behaves_like "an Enumerable method which returns an array"
  end

  describe "Enumerable#sort" do
    subject { :sort }
    it_behaves_like "an Enumerable method which returns an array"
  end

  describe "Enumerable#sort_by" do
    subject { :sort_by }
    it_behaves_like "an Enumerable method which takes a block", :array
    it_behaves_like "an Enumerable method which returns an array"
  end

  describe "Enumerable#select" do
    subject { :select }
    it_behaves_like "an Enumerable method which takes a block", :array
    it_behaves_like "an Enumerable method which returns an array"
  end

  describe "Enumerable#parition" do
    subject { :partition }
    it_behaves_like "an Enumerable method which takes a block", :array
    it "returns all #each args" do
      @test_enum.partition { true }.should == [[[1, 2, 3]], []]
    end
  end

  describe "Enumerable#reject" do
    subject { :reject }
    it_behaves_like "an Enumerable method which takes a block", :array
    it_behaves_like "an Enumerable method which returns an array", false
  end

  describe "Enumerable#min" do
    subject { :min }
    it_behaves_like "an Enumerable method which takes a block", :array
    it_behaves_like "an Enumerable method which returns an enum element"
  end

  describe "Enumerable#max" do
    subject { :max }
    it_behaves_like "an Enumerable method which takes a block", :array
    it_behaves_like "an Enumerable method which returns an enum element"
  end

  describe "Enumerable#minmax" do
    subject { :minmax }
    it_behaves_like "an Enumerable method which takes a block", :array
    it "returns all #each args" do
      @test_enum.minmax.should == [[1, 2, 3], [1, 2, 3]]
    end
  end

  describe "Enumerable#min_by" do
    subject { :min_by }
    it_behaves_like "an Enumerable method which takes a block", :array
    it_behaves_like "an Enumerable method which returns an enum element"
  end

  describe "Enumerable#max_by" do
    subject { :max_by }
    it_behaves_like "an Enumerable method which takes a block", :array
    it_behaves_like "an Enumerable method which returns an enum element"
  end

  describe "Enumerable#minmax_by" do
    subject { :minmax_by }
    it_behaves_like "an Enumerable method which takes a block", :array
    it "returns all #each args" do
      @test_enum.minmax_by {|o| o}.should == [[1, 2, 3], [1, 2, 3]]
    end
  end

  describe "Enumerable#include?" do
    it "tests against all #each args" do
      @test_enum.include?([1, 2, 3]).should be_true
    end
  end

  describe "Enumerable#any?" do
    subject { :any? }
    it_behaves_like "an Enumerable method which takes a block", RUBY_VERSION >= '1.9' ? :first_arg : :array
  end

  describe "Enumerable#none?" do
    subject { :none? }
    it_behaves_like "an Enumerable method which takes a block", RUBY_VERSION >= '1.9' ? :first_arg : :array
  end

  describe "Enumerable#one?" do
    subject { :one? }
    it_behaves_like "an Enumerable method which takes a block", RUBY_VERSION >= '1.9' ? :first_arg : :array
  end

  describe "Enumerable#all?" do
    subject { :all? }
    it_behaves_like "an Enumerable method which takes a block", RUBY_VERSION >= '1.9' ? :first_arg : :array
  end

  describe "Enumerable#inject" do
    it "passes all each args to its block" do
      @test_enum.inject(0) { |memo, obj| obj.should == [1, 2, 3] }
    end
  end

  describe "Enumerable#group_by" do
    subject { :group_by }
    it_behaves_like "an Enumerable method which takes a block", :array
    it "returns groups containing all #each arguments in an array" do
      @test_enum.group_by { :x }.should == { :x => [[1, 2, 3]]}
    end
  end

  describe "Enumerable#cycle" do
    it "passes all #each args to its block" do
      @test_enum.cycle(1) do |obj|
        obj.should == [1, 2, 3]
      end
    end
  end

  describe "Enumerable#each_slice" do
    it "passes all #each args to its block" do
      @test_enum.each_slice(1) do |obj|
        obj.should == [[1, 2, 3]]
      end
    end
  end

  describe "Enumerable#drop_while" do
    subject { :drop_while }
    it_behaves_like "an Enumerable method which takes a block", :array
    it "returns all #each args even if its block does not use them" do
      @test_enum.drop_while do |a|
        false
      end.should == [[1, 2, 3]]
    end
  end

  describe "Enumerable#take_while" do
    subject { :take_while }
    it_behaves_like "an Enumerable method which takes a block", RUBY_VERSION >= '1.9' ? :first_arg : :array
    it "returns all #each args even if its block does not use them" do
      @test_enum.take_while do |a|
        true
      end.should == [[1, 2, 3]]
    end
  end

  describe "Enumerable#find_index" do
    subject { :find_index }
    it_behaves_like "an Enumerable method which takes a block", RUBY_VERSION >= '1.9' ? :first_arg : :array
  end

  describe "Enumerable#detect" do
    subject { :detect }
    it_behaves_like "an Enumerable method which takes a block", :array
  end

  describe "Enumerable#grep" do
    it "finds array of all #each args" do
      @test_enum.grep([1, 2, 3]).should == [[1, 2, 3]]
    end
  end

  describe "Enumerable#zip" do
    it "includes all #each args in the zipped array" do
      @test_enum.zip.should == [[[1, 2, 3]]]
    end
  end

  describe "Enumerable#each_cons" do
    it "puts all #each args in its block array" do
      @test_enum.each_cons(1) { |obj| obj.should == [[1, 2, 3]] }
    end
  end

  if RUBY_VERSION >= '1.9'

    describe "Enumerable#each_with_object" do
      it "passes all each args to its block" do
        @test_enum.each_with_object([]) { |obj, memo| obj.should == [1, 2, 3] }
      end
    end

    describe "Enumerable#each_entry" do
      subject { :each_entry }
      it_behaves_like "an Enumerable method which takes a block", :array
    end

    describe "Enumerable#slice_before" do
      it "passes all #each args to its block" do
        @test_enum.slice_before do |obj|
          obj.should == [1, 2, 3]
        end.each{}
      end
    end

    describe "Enumerable#flat_map" do
      subject { :flat_map }
      it_behaves_like "an Enumerable method which takes a block", :first_arg
    end

    describe "Enumerable#chunk" do
      it "passes all #each args to its block" do
        @test_enum.chunk do |a, b, c|
          a.should == 1
          b.should == 2
          c.should == 3
        end.each {}
      end
        
      it "passes all #each args to its block" do
        @test_enum.chunk do |obj|
          obj.should == [1, 2, 3]
        end.each{}
      end
    end
  end
end