1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
require 'spec_helper'
require 'hashie'
describe Hashie::Mash::Rash do
subject {
Hashie::Mash::Rash.new({
"varOne" => 1,
"two" => 2,
:three => 3,
:varFour => 4,
"fiveHumpHumps" => 5,
:nested => {
"NestedOne" => "One",
:two => "two",
"nested_three" => "three"
},
"nestedTwo" => {
"nested_two" => 22,
:nestedThree => 23
},
:nestedThree => [
{ :nestedFour => 4 },
{ "nestedFour" => 4 }
],
"spaced Key" => "When would this happen?",
"trailing spaces " => "better safe than sorry",
"extra spaces" => "hopefully this never happens"
})
}
it { should be_a(Hashie::Mash) }
it "should create a new rash where all the keys are underscored instead of camelcased" do
subject.var_one.should == 1
subject.two.should == 2
subject.three.should == 3
subject.var_four.should == 4
subject.five_hump_humps.should == 5
subject.nested.should be_a(Hashie::Mash::Rash)
subject.nested.nested_one.should == "One"
subject.nested.two.should == "two"
subject.nested.nested_three.should == "three"
subject.nested_two.should be_a(Hashie::Mash::Rash)
subject.nested_two.nested_two.should == 22
subject.nested_two.nested_three.should == 23
subject.spaced_key.should == "When would this happen?"
subject.trailing_spaces.should == "better safe than sorry"
subject.extra_spaces.should == "hopefully this never happens"
end
it "should allow camelCased accessors" do
#avoiding hashie v5- warnings
subject.class.disable_warnings(:varOne) if Gem::Version.new(Hashie::VERSION) >= Gem::Version.new("5.0.0")
subject.varOne.should == 1
subject.varOne = "once"
subject.varOne.should == "once"
subject.var_one.should == "once"
end
it "should allow camelCased accessors on nested hashes" do
# avoiding hashie v5- warnings
subject.class.disable_warnings(:nestedOne) if Gem::Version.new(Hashie::VERSION) >= Gem::Version.new("5.0.0")
subject.nested.nestedOne.should == "One"
subject.nested.nestedOne = "once"
subject.nested.nested_one.should == "once"
end
it "should merge well with a Mash" do
merged = subject.merge Hashie::Mash.new(
:nested => {:fourTimes => "a charm"},
:nested3 => {:helloWorld => "hi"}
)
merged.nested.four_times.should == "a charm"
merged.nested.fourTimes.should == "a charm"
merged.nested3.should be_a(Hashie::Mash::Rash)
merged.nested3.hello_world.should == "hi"
merged.nested3.helloWorld.should == "hi"
merged[:nested3][:helloWorld].should == "hi"
end
it "should update well with a Mash" do
subject.update Hashie::Mash.new(
:nested => {:fourTimes => "a charm"},
:nested3 => {:helloWorld => "hi"}
)
subject.nested.four_times.should == "a charm"
subject.nested.fourTimes.should == "a charm"
subject.nested3.should be_a(Hashie::Mash::Rash)
subject.nested3.hello_world.should == "hi"
subject.nested3.helloWorld.should == "hi"
subject[:nested3][:helloWorld].should == "hi"
end
it "should merge well with a Hash" do
merged = subject.merge({
:nested => {:fourTimes => "work like a charm"},
:nested3 => {:helloWorld => "hi"}
})
merged.nested.four_times.should == "work like a charm"
merged.nested.fourTimes.should == "work like a charm"
merged.nested3.should be_a(Hashie::Mash::Rash)
merged.nested3.hello_world.should == "hi"
merged.nested3.helloWorld.should == "hi"
merged[:nested3][:helloWorld].should == "hi"
end
it "should handle assigning a new Hash and convert it to a rash" do
subject.nested3 = {:helloWorld => "hi"}
subject.nested3.should be_a(Hashie::Mash::Rash)
subject.nested3.hello_world.should == "hi"
subject.nested3.helloWorld.should == "hi"
subject[:nested3][:helloWorld].should == "hi"
end
it "should convert an array of Hashes" do
subject.nested_three.should be_a(Array)
subject.nested_three[0].should be_a(Hashie::Mash::Rash)
subject.nested_three[0].nested_four.should == 4
subject.nested_three[1].should be_a(Hashie::Mash::Rash)
subject.nested_three[1].nested_four.should == 4
end
it "should allow initializing reader" do
subject.nested3!.helloWorld = "hi"
subject.nested3.hello_world.should == "hi"
end
end
|