Sha256: 62493071bd3f426a774a911b18dde6e38fa79ba842d5b3e8f99ae338e3cd3d88

Contents?: true

Size: 1.77 KB

Versions: 52

Compression:

Stored size: 1.77 KB

Contents

describe :hash_each, :shared => true do
  it "yields a [[key, value]] Array for each pair to a block expecting |*args|" do
    all_args = []
    new_hash(1 => 2, 3 => 4).send(@method) { |*args| all_args << args }
    all_args.sort.should == [[[1, 2]], [[3, 4]]]
  end

  it "yields the key and value of each pair to a block expecting |key, value|" do
    r = new_hash
    h = new_hash(:a => 1, :b => 2, :c => 3, :d => 5)
    h.send(@method) { |k,v| r[k.to_s] = v.to_s }.should equal(h)
    r.should == new_hash("a" => "1", "b" => "2", "c" => "3", "d" => "5")
  end

  it "yields the key only to a block expecting |key,|" do
    ary = []
    h = new_hash("a" => 1, "b" => 2, "c" => 3)
    h.send(@method) { |k,| ary << k }
    ary.sort.should == ["a", "b", "c"]
  end

  it "uses the same order as keys() and values()" do
    h = new_hash(:a => 1, :b => 2, :c => 3, :d => 5)
    keys = []
    values = []

    h.send(@method) do |k, v|
      keys << k
      values << v
    end

    keys.should == h.keys
    values.should == h.values
  end
  
  # Confirming the argument-splatting works from child class for both k, v and [k, v]
  it "properly expands (or not) child class's 'each'-yielded args" do
    cls1 = Class.new(Hash) do
      attr_accessor :k_v
      def each
        super do |k, v|
          @k_v = [k, v]
          yield k, v
        end
      end
    end
    
    cls2 = Class.new(Hash) do
      attr_accessor :k_v
      def each
        super do |k, v|
          @k_v = [k, v]
          yield([k, v])
        end
      end
    end
    
    obj1 = cls1.new
    obj1['a'] = 'b'
    obj1.map {|k, v| [k, v]}.should == [['a', 'b']]
    obj1.k_v.should == ['a', 'b']
    
    obj2 = cls2.new
    obj2['a'] = 'b'
    obj2.map {|k, v| [k, v]}.should == [['a', 'b']]
    obj2.k_v.should == ['a', 'b']
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

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