1+ # frozen_string_literal: true
2+
3+ RSpec . shared_examples_for "a snaked hash" do
4+ include_context "base hash"
5+
6+ it { is_expected . to be_a ( Hashie ::Mash ) }
7+
8+ it "creates a new rash where all the keys are underscored instead of camelcased" do
9+ expect ( subject . var_one ) . to eq ( 1 )
10+ expect ( subject . two ) . to eq ( 2 )
11+ expect ( subject . three ) . to eq ( 3 )
12+ expect ( subject . var_four ) . to eq ( 4 )
13+ expect ( subject . five_hump_humps ) . to eq ( 5 )
14+ expect ( subject . nested ) . to be_a ( subject . class )
15+ expect ( subject . nested . nested_one ) . to eq ( "One" )
16+ expect ( subject . nested . two ) . to eq ( "two" )
17+ expect ( subject . nested . nested_three ) . to eq ( "three" )
18+ expect ( subject . nested_two ) . to be_a ( subject . class )
19+ expect ( subject . nested_two . nested_two ) . to eq ( 22 )
20+ expect ( subject . nested_two . nested_three ) . to eq ( 23 )
21+ expect ( subject . spaced_key ) . to eq ( "When would this happen?" )
22+ expect ( subject . trailing_spaces ) . to eq ( "better safe than sorry" )
23+ expect ( subject . extra_spaces ) . to eq ( "hopefully this never happens" )
24+ end
25+
26+ it "allows camelCased accessors" do
27+ # avoiding hashie v5- warnings
28+ subject . class . disable_warnings ( :varOne ) if Gem ::Version . new ( Hashie ::VERSION ) >= Gem ::Version . new ( "5.0.0" )
29+
30+ expect ( subject . varOne ) . to eq ( 1 )
31+ subject . varOne = "once"
32+ expect ( subject . varOne ) . to eq ( "once" )
33+ expect ( subject . var_one ) . to eq ( "once" )
34+ end
35+
36+ it "allows camelCased accessors on nested hashes" do
37+ # avoiding hashie v5- warnings
38+ subject . class . disable_warnings ( :nestedOne ) if Gem ::Version . new ( Hashie ::VERSION ) >= Gem ::Version . new ( "5.0.0" )
39+
40+ expect ( subject . nested . nestedOne ) . to eq ( "One" )
41+ subject . nested . nestedOne = "once"
42+ expect ( subject . nested . nested_one ) . to eq ( "once" )
43+ end
44+
45+ it "merges well with a Mash" do
46+ merged = subject . merge Hashie ::Mash . new (
47+ nested : { fourTimes : "a charm" } ,
48+ nested3 : { helloWorld : "hi" }
49+ )
50+
51+ expect ( merged . nested . four_times ) . to eq ( "a charm" )
52+ expect ( merged . nested . fourTimes ) . to eq ( "a charm" )
53+ expect ( merged . nested3 ) . to be_a ( subject . class )
54+ expect ( merged . nested3 . hello_world ) . to eq ( "hi" )
55+ expect ( merged . nested3 . helloWorld ) . to eq ( "hi" )
56+ expect ( merged [ :nested3 ] [ :helloWorld ] ) . to eq ( "hi" )
57+ end
58+
59+ it "updates well with a Mash" do
60+ subject . update Hashie ::Mash . new (
61+ nested : { fourTimes : "a charm" } ,
62+ nested3 : { helloWorld : "hi" }
63+ )
64+
65+ expect ( subject . nested . four_times ) . to eq ( "a charm" )
66+ expect ( subject . nested . fourTimes ) . to eq ( "a charm" )
67+ expect ( subject . nested3 ) . to be_a ( subject . class )
68+ expect ( subject . nested3 . hello_world ) . to eq ( "hi" )
69+ expect ( subject . nested3 . helloWorld ) . to eq ( "hi" )
70+ expect ( subject [ :nested3 ] [ :helloWorld ] ) . to eq ( "hi" )
71+ end
72+
73+ it "merges well with a Hash" do
74+ merged = subject . merge (
75+ nested : { fourTimes : "work like a charm" } ,
76+ nested3 : { helloWorld : "hi" }
77+ )
78+
79+ expect ( merged . nested . four_times ) . to eq ( "work like a charm" )
80+ expect ( merged . nested . fourTimes ) . to eq ( "work like a charm" )
81+ expect ( merged . nested3 ) . to be_a ( subject . class )
82+ expect ( merged . nested3 . hello_world ) . to eq ( "hi" )
83+ expect ( merged . nested3 . helloWorld ) . to eq ( "hi" )
84+ expect ( merged [ :nested3 ] [ :helloWorld ] ) . to eq ( "hi" )
85+ end
86+
87+ it "handles assigning a new Hash and convert it to a rash" do
88+ subject . nested3 = { helloWorld : "hi" }
89+
90+ expect ( subject . nested3 ) . to be_a ( subject . class )
91+ expect ( subject . nested3 . hello_world ) . to eq ( "hi" )
92+ expect ( subject . nested3 . helloWorld ) . to eq ( "hi" )
93+ expect ( subject [ :nested3 ] [ :helloWorld ] ) . to eq ( "hi" )
94+ end
95+
96+ it "converts an array of Hashes" do
97+ expect ( subject . nested_three ) . to be_a ( Array )
98+ expect ( subject . nested_three [ 0 ] ) . to be_a ( subject . class )
99+ expect ( subject . nested_three [ 0 ] . nested_four ) . to eq ( 4 )
100+ expect ( subject . nested_three [ 1 ] ) . to be_a ( subject . class )
101+ expect ( subject . nested_three [ 1 ] . nested_four ) . to eq ( 4 )
102+ end
103+
104+ it "allows initializing reader" do
105+ subject . nested3! . helloWorld = "hi"
106+ expect ( subject . nested3 . hello_world ) . to eq ( "hi" )
107+ end
108+
109+ it "does not transform non-Symbolizable keys" do
110+ expect ( subject [ 4 ] ) . to eq ( "not symbolizable" )
111+ expect ( subject [ :"4" ] ) . to be_nil
112+ expect ( subject [ "4" ] ) . to be_nil
113+ expect ( subject . key? ( "4" ) ) . to be false
114+ expect ( subject . key? ( :"4" ) ) . to be false
115+ expect ( subject . key? ( 4 ) ) . to be true
116+ end
117+ end
0 commit comments