summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range/each_spec.rb
blob: 1dce9c1f1ef9766f406ea328ee9f2038de30f759 (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
require_relative '../../spec_helper'
require_relative '../enumerable/shared/enumeratorized'

describe "Range#each" do
  it "passes each element to the given block by using #succ" do
    a = []
    (-5..5).each { |i| a << i }
    a.should == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]

    a = []
    ('A'..'D').each { |i| a << i }
    a.should == ['A','B','C','D']

    a = []
    ('A'...'D').each { |i| a << i }
    a.should == ['A','B','C']

    a = []
    (0xfffd...0xffff).each { |i| a << i }
    a.should == [0xfffd, 0xfffe]

    y = mock('y')
    x = mock('x')
    x.should_receive(:<=>).with(y).any_number_of_times.and_return(-1)
    x.should_receive(:<=>).with(x).any_number_of_times.and_return(0)
    x.should_receive(:succ).any_number_of_times.and_return(y)
    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 "works for non-ASCII ranges" do
    a = []
    ('Σ'..'Ω').each { |i| a << i }
    a.should == ["Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω"]
  end

  ruby_version_is "2.6" do
    it "works with endless ranges" do
      a = []
      eval("(-2..)").each { |x| break if x > 2; a << x }
      a.should == [-2, -1, 0, 1, 2]

      a = []
      eval("(-2...)").each { |x| break if x > 2; a << x }
      a.should == [-2, -1, 0, 1, 2]
    end

    it "works with String endless ranges" do
      a = []
      eval("('A'..)").each { |x| break if x > "D"; a << x }
      a.should == ["A", "B", "C", "D"]

      a = []
      eval("('A'...)").each { |x| break if x > "D"; a << x }
      a.should == ["A", "B", "C", "D"]
    end
  end

  ruby_version_is "2.7" do
    it "raises a TypeError beginless ranges" do
      -> { eval("(..2)").each { |x| x } }.should raise_error(TypeError)
    end
  end

  it "raises a TypeError if the first element does not respond to #succ" do
    -> { (0.5..2.4).each { |i| i } }.should raise_error(TypeError)

    b = mock('x')
    (a = mock('1')).should_receive(:<=>).with(b).and_return(1)

    -> { (a..b).each { |i| i } }.should raise_error(TypeError)
  end

  it "returns self" do
    range = 1..10
    range.each{}.should equal(range)
  end

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

  it "raises a TypeError if the first element is a Time object" do
    t = Time.now
    -> { (t..t+1).each { |i| i } }.should raise_error(TypeError)
  end

  it "passes each Symbol element by using #succ" do
    (:aa..:ac).each.to_a.should == [:aa, :ab, :ac]
    (:aa...:ac).each.to_a.should == [:aa, :ab]
  end

  it_behaves_like :enumeratorized_with_origin_size, :each, (1..3)
end