summaryrefslogtreecommitdiff
path: root/spec/ruby/core/enumerable/shared/collect.rb
blob: 6df1a616ebe1a7da46e3a46bee72f95647ffeef5 (plain)
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
require_relative 'enumerable_enumeratorized'

describe :enumerable_collect, shared: true do
  before :each do
    ScratchPad.record []
  end

  it "returns a new array with the results of passing each element to block" do
    entries = [0, 1, 3, 4, 5, 6]
    numerous = EnumerableSpecs::Numerous.new(*entries)
    numerous.send(@method) { |i| i % 2 }.should == [0, 1, 1, 0, 1, 0]
    numerous.send(@method) { |i| i }.should == entries
  end

  it "passes through the values yielded by #each_with_index" do
    [:a, :b].each_with_index.send(@method) { |x, i| ScratchPad << [x, i]; nil }
    ScratchPad.recorded.should == [[:a, 0], [:b, 1]]
  end

  it "gathers initial args as elements when each yields multiple" do
    multi = EnumerableSpecs::YieldsMulti.new
    multi.send(@method) {|e| e}.should == [1,3,6]
  end

  it "only yields increasing values for a Range" do
    (1..0).send(@method) { |x| x }.should == []
    (1..1).send(@method) { |x| x }.should == [1]
    (1..2).send(@method) { |x| x }.should == [1, 2]
  end

  it "returns an enumerator when no block given" do
    enum = EnumerableSpecs::Numerous.new.send(@method)
    enum.should be_an_instance_of(Enumerator)
    enum.each { |i| -i }.should == [-2, -5, -3, -6, -1, -4]
  end

  it "reports the same arity as the given block" do
    entries = [0, 1, 3, 4, 5, 6]
    numerous = EnumerableSpecs::Numerous.new(*entries)

    def numerous.each(&block)
      ScratchPad << block.arity
      super
    end

    numerous.send(@method) { |a, b| a % 2 }.should == [0, 1, 1, 0, 1, 0]
    ScratchPad.recorded.should == [2]
    ScratchPad.clear
    ScratchPad.record []
    numerous.send(@method) { |i| i }.should == entries
    ScratchPad.recorded.should == [1]
  end

  it "yields an Array of 2 elements for a Hash when block arity is 1" do
    c = Class.new do
      def register(a)
        ScratchPad << a
      end
    end
    m = c.new.method(:register)

    ScratchPad.record []
    { 1 => 'a', 2 => 'b' }.map(&m)
    ScratchPad.recorded.should == [[1, 'a'], [2, 'b']]
  end

  it "yields 2 arguments for a Hash when block arity is 2" do
    c = Class.new do
      def register(a, b)
        ScratchPad << [a, b]
      end
    end
    m = c.new.method(:register)

    ScratchPad.record []
    { 1 => 'a', 2 => 'b' }.map(&m)
    ScratchPad.recorded.should == [[1, 'a'], [2, 'b']]
  end

  it "raises an error for a Hash when an arity enforcing block of arity >2 is passed in" do
    c = Class.new do
      def register(a, b, c)
      end
    end
    m = c.new.method(:register)

    -> do
      { 1 => 'a', 2 => 'b' }.map(&m)
    end.should raise_error(ArgumentError)
  end

  it "calls the each method on sub-classes" do
    c = Class.new(Hash) do
      def each
        ScratchPad << 'in each'
        super
      end
    end
    h = c.new
    h[1] = 'a'
    ScratchPad.record []
    h.send(@method) { |k,v| v }
    ScratchPad.recorded.should == ['in each']
  end

  it_should_behave_like :enumerable_enumeratorized_with_origin_size
end