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