summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range/reverse_each_spec.rb
blob: 56390cc0da48229b79739121481772819c679c26 (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
require_relative '../../spec_helper'

ruby_version_is "3.3" do
  describe "Range#reverse_each" do
    it "traverses the Range in reverse order and passes each element to block" do
      a = []
      (1..3).reverse_each { |i| a << i }
      a.should == [3, 2, 1]

      a = []
      (1...3).reverse_each { |i| a << i }
      a.should == [2, 1]
    end

    it "returns self" do
      r = (1..3)
      r.reverse_each { |x| }.should equal(r)
    end

    it "returns an Enumerator if no block given" do
      enum = (1..3).reverse_each
      enum.should be_an_instance_of(Enumerator)
      enum.to_a.should == [3, 2, 1]
    end

    it "raises a TypeError for endless Ranges of Integers" do
      -> {
        (1..).reverse_each.take(3)
      }.should raise_error(TypeError, "can't iterate from NilClass")
    end

    it "raises a TypeError for endless Ranges of non-Integers" do
      -> {
        ("a"..).reverse_each.take(3)
      }.should raise_error(TypeError, "can't iterate from NilClass")
    end

    context "Integer boundaries" do
      it "supports beginningless Ranges" do
        (..5).reverse_each.take(3).should == [5, 4, 3]
      end
    end

    context "non-Integer boundaries" do
      it "uses #succ to iterate a Range of non-Integer elements" do
        y = mock('y')
        x = mock('x')

        x.should_receive(:succ).any_number_of_times.and_return(y)
        x.should_receive(:<=>).with(y).any_number_of_times.and_return(-1)
        x.should_receive(:<=>).with(x).any_number_of_times.and_return(0)
        y.should_receive(:<=>).with(x).any_number_of_times.and_return(1)
        y.should_receive(:<=>).with(y).any_number_of_times.and_return(0)

        a = []
        (x..y).each { |i| a << i }
        a.should == [x, y]
      end

      it "uses #succ to iterate a Range of Strings" do
        a = []
        ('A'..'D').reverse_each { |i| a << i }
        a.should == ['D','C','B','A']
      end

      it "uses #succ to iterate a Range of Symbols" do
        a = []
        (:A..:D).reverse_each { |i| a << i }
        a.should == [:D, :C, :B, :A]
      end

      it "raises a TypeError when `begin` value does not respond to #succ" do
        -> { (Time.now..Time.now).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Time/)
        -> { (//..//).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Regexp/)
        -> { ([]..[]).reverse_each { |x| x } }.should raise_error(TypeError, /can't iterate from Array/)
      end

      it "does not support beginningless Ranges" do
        -> {
          (..'a').reverse_each { |x| x }
        }.should raise_error(TypeError, /can't iterate from NilClass/)
      end
    end

    context "when no block is given" do
      describe "returned Enumerator size" do
        it "returns the Range size when Range size is finite" do
          (1..3).reverse_each.size.should == 3
        end

        ruby_bug "#20936", "3.4"..."4.0" do
          it "returns Infinity when Range size is infinite" do
            (..3).reverse_each.size.should == Float::INFINITY
          end
        end

        it "returns nil when Range size is unknown" do
          ('a'..'z').reverse_each.size.should == nil
        end
      end
    end
  end
end