Sha256: 2b2966cc99ae8346ecb7fb62c18a5d2ecb6dfed1e544cb8d77946806d3cc4552

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

require 'test_helper'


class EnumeratorTest < Test::Unit::TestCase
  context "Enumerator" do
    context "#with_object" do
      should "conform to doc" do
        animals = %w(cat dog wombat).to_enum 
        hash = animals.with_object({}).each do |item, memo| 
          memo[item] = item.upcase.reverse 
        end 
        assert_equal({"cat"=>"TAC", "dog"=>"GOD", "wombat"=>"TABMOW"}, hash)
      end
    end
    
    context "#new" do
      should "should accept block" do
        enum = Enumerator.new do |yielder|
          yielder.yield "This cool new syntax is sponsored by"
          yielder.yield yielder.class
        end
        assert enum.is_a?(Enumerator)
        2.times do
          assert_equal ["This cool new syntax is sponsored by", Enumerator::Yielder], enum.to_a
        end
      end
    end
    
    context "#next" do
      should "conform to doc" do
        enum = [10, 20].to_enum 
        assert_equal 10, enum.next
        assert_equal 20, enum.next
        assert_raise(StopIteration){ enum.next} 
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
marcandre-backports-1.7.0 test/enumerator_test.rb
marcandre-backports-1.7.1 test/enumerator_test.rb
backports-1.7.1 test/enumerator_test.rb
backports-1.7.0 test/enumerator_test.rb