Sha256: f13cba3ee7948449e7bec85204800518de3aff42c9ba7b3339f2c8b25d77277c

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'rid/core_ext/hash'

describe "Hash" do
  describe "flatten" do
    it "should untouch flat hash" do
      hash = { "a" => 1, "b" => 2 }
      hash.flatten.should == hash
    end

    it "should flaten simple nested hash" do
      hash = { "a" => { "b" => 1 } }
      hash.flatten.should == { "a/b" => 1 }
    end

    it "should flaten deep nested hash" do
      hash = { "a" => { "b" => { "c" => 1 } } }
      hash.flatten.should == { "a/b/c" => 1 }
    end
  end

  describe "flatten!" do
    it "should change receiver" do
      hash = { "a" => { "b" => 1 } }
      hash.flatten!
      hash.should == { "a/b" => 1 }
    end
  end

  describe "at" do
    it "should retrieve value at top level" do
      hash = { "a" => 1 }
      hash.at("a").should == 1
    end

    it "should retrieve value at nested level" do
      hash = { "a" => { "b" => 1 } }
      hash.at("a/b").should == 1
    end

    it "should retrieve value at deep nested level" do
      hash = { "a" => { "b" => { "c" => 1 } } }
      hash.at("a/b/c").should == 1
    end

    it "should return nil if not present" do
      hash = { "a" => { "c" => { "c" => 1 } } }
      hash.at("a/b/c").should == nil
    end
  end

  describe "update_at" do
    it "should insert value at top level" do
      hash = { }
      hash.update_at "a", 1
      hash.should == { "a" => 1 }
    end

    it "should insert value at nested level" do
      hash = { }
      hash.update_at "a/b", 1
      hash.should == { "a" => { "b" => 1 } }
    end

    it "should insert value at deep nested level" do
      hash = { }
      hash.update_at "a/b/c", 1
      hash.should == { "a" => { "b" => { "c" => 1 } } }
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
rid-core-1.5.2 spec/rid/core_ext/hash_spec.rb
rid-core-1.5.1 spec/rid/core_ext/hash_spec.rb
rid-core-1.5.0 spec/rid/core_ext/hash_spec.rb
rid-core-1.4.0 spec/rid/core_ext/hash_spec.rb
rid-core-1.3.0 spec/rid/core_ext/hash_spec.rb
rid-core-1.2.0 spec/rid/core_ext/hash_spec.rb
rid-core-1.1.0 spec/rid/core_ext/hash_spec.rb
rid-core-1.0.3 spec/rid/core_ext/hash_spec.rb
rid-1.0.3 spec/rid/core_ext/hash_spec.rb
rid-1.0.2 spec/rid/core_ext/hash_spec.rb
rid-0.5.2 spec/rid/core_ext/hash_spec.rb
rid-0.5.1 spec/rid/core_ext/hash_spec.rb
rid-0.5.0 spec/rid/core_ext/hash_spec.rb
rid-0.4.1 spec/rid/core_ext/hash_spec.rb
rid-0.4.0 spec/rid/core_ext/hash_spec.rb
rid-0.3.1 spec/rid/core_ext/hash_spec.rb