Sha256: f29e9a64d8d64d7de593c190a02a2018b798f8aa724da8272c153e2012868755

Contents?: true

Size: 1.51 KB

Versions: 52

Compression:

Stored size: 1.51 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)

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

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

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

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

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

end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/core/range/each_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/core/range/each_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/core/range/each_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/core/range/each_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/core/range/each_spec.rb