Sha256: ae412fb7ab2b2860d38dee4b88bcc432cd71705b5b39ebfcf96358a4f2ab3f86

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'hash_key_orderable'

describe HashKeyOrderable do

  describe :each do
    it "should each with key order" do
      hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4}
      hash.extend(HashKeyOrderable)
      hash.key_order = %w(b d c a)
      actuals = []
      hash.each do |key, value|
        actuals << key
      end
      actuals.should == hash.key_order
    end

    it "should use original each without key_order" do
      hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4}
      hash.extend(HashKeyOrderable)
      hash.should_receive(:each_without_key_order) # original each method
      hash.each{ }
    end

    it "should appear remain key after key_order in each" do
      hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}
      hash.extend(HashKeyOrderable)
      hash.key_order = %w(b e d)
      actuals = []
      hash.each do |key, value|
        actuals << key
      end
      actuals[0..2].should == hash.key_order
      actuals[3..4].sort.should == %w(a c)
    end

    it "should ignore unexist key in key_order" do
      hash = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}
      hash.extend(HashKeyOrderable)
      hash.key_order = %w(b z x d)
      actuals = []
      hash.each do |key, value|
        actuals << key
      end
      actuals[0..1].should == %w(b d)
      actuals[2..4].sort.should == %w(a c e)
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
schema_comments-0.2.0 spec/hash_key_orderable_spec.rb
schema_comments-0.2.0.alpha5 spec/hash_key_orderable_spec.rb
schema_comments-0.2.0.alpha4 spec/hash_key_orderable_spec.rb
schema_comments-0.2.0.alpha3 spec/hash_key_orderable_spec.rb
schema_comments-0.2.0.alpha2 spec/hash_key_orderable_spec.rb
schema_comments-0.2.0.alpha1 spec/hash_key_orderable_spec.rb