diff options
Diffstat (limited to 'spec/ruby/core/range/each_spec.rb')
| -rw-r--r-- | spec/ruby/core/range/each_spec.rb | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/spec/ruby/core/range/each_spec.rb b/spec/ruby/core/range/each_spec.rb index 1dce9c1f1e..b4389f864d 100644 --- a/spec/ruby/core/range/each_spec.rb +++ b/spec/ruby/core/range/each_spec.rb @@ -38,57 +38,58 @@ 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 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 + it "works with endless ranges" do + a = [] + (-2..).each { |x| break if x > 2; a << x } + a.should == [-2, -1, 0, 1, 2] + + a = [] + (-2...).each { |x| break if x > 2; a << x } + a.should == [-2, -1, 0, 1, 2] 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 "works with String endless ranges" do + a = [] + ('A'..).each { |x| break if x > "D"; a << x } + a.should == ["A", "B", "C", "D"] + + a = [] + ('A'...).each { |x| break if x > "D"; a << x } + a.should == ["A", "B", "C", "D"] + end + + it "raises a TypeError beginless ranges" do + -> { (..2).each { |x| x } }.should.raise(TypeError) 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) + -> { (0.5..2.4).each { |i| i } }.should.raise(TypeError) b = mock('x') (a = mock('1')).should_receive(:<=>).with(b).and_return(1) - -> { (a..b).each { |i| i } }.should raise_error(TypeError) + -> { (a..b).each { |i| i } }.should.raise(TypeError) end it "returns self" do range = 1..10 - range.each{}.should equal(range) + 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.should.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) + 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 it "passes each Symbol element by using #succ" do |
