diff options
Diffstat (limited to 'spec/ruby/core/range/each_spec.rb')
-rw-r--r-- | spec/ruby/core/range/each_spec.rb | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/spec/ruby/core/range/each_spec.rb b/spec/ruby/core/range/each_spec.rb index 1dce9c1f1e..ecae17c881 100644 --- a/spec/ruby/core/range/each_spec.rb +++ b/spec/ruby/core/range/each_spec.rb @@ -38,32 +38,28 @@ describe "Range#each" do 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 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"] + 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 + a = [] + eval("('A'...)").each { |x| break if x > "D"; a << x } + a.should == ["A", "B", "C", "D"] end - ruby_version_is "2.7" do - it "raises a TypeError beginless ranges" do - -> { eval("(..2)").each { |x| x } }.should raise_error(TypeError) - end + it "raises a TypeError beginless ranges" do + -> { (..2).each { |x| x } }.should raise_error(TypeError) end it "raises a TypeError if the first element does not respond to #succ" do @@ -86,9 +82,27 @@ describe "Range#each" do 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) + ruby_version_is "3.1" do + it "supports Time objects that respond to #succ" do + t = Time.utc(1970) + def t.succ; self + 1 end + t_succ = t.succ + def t_succ.succ; self + 1; end + + (t..t_succ).to_a.should == [Time.utc(1970), Time.utc(1970, nil, nil, nil, nil, 1)] + (t...t_succ).to_a.should == [Time.utc(1970)] + end + end + + ruby_version_is ""..."3.1" do + it "raises a TypeError if the first element is a Time object even if it responds to #succ" do + t = Time.utc(1970) + def t.succ; self + 1 end + t_succ = t.succ + def t_succ.succ; self + 1; end + + -> { (t..t_succ).each { |i| i } }.should raise_error(TypeError) + end end it "passes each Symbol element by using #succ" do |