forked from mbklein/confstruct
/
hash_with_struct_access_spec.rb
194 lines (165 loc) · 5.9 KB
/
hash_with_struct_access_spec.rb
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'confstruct/hash_with_struct_access'
describe Confstruct::HashWithStructAccess do
it "should initialize empty" do
hwsa = Confstruct::HashWithStructAccess.new
hwsa.is_a?(Hash).should be_true
hwsa.is_a?(Confstruct::HashWithStructAccess).should be_true
hwsa.should == {}
end
it "should respond to #ordered?" do
hwsa = Confstruct::HashWithStructAccess.new
[true,false].should include(hwsa.ordered?)
end
context "data manipulation" do
before :all do
@hash = {
:project => 'confstruct',
:github => {
:url => 'http://www.github.com/mbklein/confstruct',
:default_branch => 'master'
}
}
end
before :each do
@hwsa = Confstruct::HashWithStructAccess.from_hash(@hash)
end
it "should initialize from a hash" do
hwsa = Confstruct::HashWithStructAccess.from_hash({
'project' => 'confstruct',
:github => {
:url => 'http://www.github.com/mbklein/confstruct',
'default branch' => 'master'
}
})
hwsa.should == @hwsa
hwsa.should == @hash
end
it "should provide hash access" do
@hwsa[:project].should == @hash[:project]
@hwsa['project'].should == @hash[:project]
@hwsa[:github].should == @hash[:github]
@hwsa[:github][:url].should == @hash[:github][:url]
end
it "should provide struct access" do
@hwsa.project.should == @hash[:project]
@hwsa.github.should == @hash[:github]
@hwsa.github.url.should == @hash[:github][:url]
end
it "should provide block access" do
u = @hash[:github][:url]
@hwsa.github do
url.should == u
end
@hwsa.github do |g|
g.url.should == @hash[:github][:url]
end
end
it "should properly respond to #has?" do
@hwsa.has?('github.url').should be_true
@hwsa.has?('github.foo.bar.baz').should be_false
end
it "should properly respond to #lookup!" do
@hwsa.lookup!('github.url').should == @hash[:github][:url]
@hwsa.lookup!('github.foo.bar.baz').should be_nil
end
it "should provide introspection" do
@hwsa.should_respond_to(:project)
@hash.keys.each do |m|
@hwsa.methods.should include("#{m}")
@hwsa.methods.should include("#{m}=")
end
end
it "should #deep_merge" do
hwsa = @hwsa.deep_merge({ :new_foo => 'bar', :github => { :default_branch => 'develop' } })
@hwsa.should == @hash
hwsa.should_not == @hwsa
hwsa.should_not == @hash
hwsa.should == {
:new_foo => 'bar',
:project => 'confstruct',
:github => {
:url => 'http://www.github.com/mbklein/confstruct',
:default_branch => 'develop'
}
}
end
it "should #deep_merge!" do
@hwsa.deep_merge!({ :github => { :default_branch => 'develop' } })
@hwsa.should_not == @hash
@hwsa.should == {
:project => 'confstruct',
:github => {
:url => 'http://www.github.com/mbklein/confstruct',
:default_branch => 'develop'
}
}
end
it "should create values on demand" do
@hwsa.github.foo = 'bar'
@hwsa.github.should == {
:foo => 'bar',
:url => 'http://www.github.com/mbklein/confstruct',
:default_branch => 'master'
}
@hwsa.baz do
quux 'default_for_quux'
end
@hwsa[:baz].should == { :quux => 'default_for_quux' }
end
it "should replace an existing hash" do
@hwsa.github = { :url => 'http://www.github.com/somefork/other-project', :branch => 'pre-1.0' }
@hwsa.github.has_key?(:default_branch).should == false
@hwsa.github.should == { :url => 'http://www.github.com/somefork/other-project', :branch => 'pre-1.0' }
end
it "should fail on other method signatures" do
lambda { @hwsa.error(1, 2, 3) }.should raise_error(NoMethodError)
end
end
context "Proc values as virtual methods" do
before :all do
@hash = {
:project => 'confstruct',
:github => {
:url => 'http://www.github.com/mbklein/confstruct',
:default_branch => 'master',
:regular_proc => lambda { |a,b,c| puts "#{a}: #{b} #{c}" },
:reverse_url => Confstruct.deferred { self.url.reverse },
:upcase_url => Confstruct.deferred { |c| c.url.upcase }
}
}
end
before :each do
@hwsa = Confstruct::HashWithStructAccess.from_hash(@hash)
end
it "should only evaluate Confstruct::Deferred procs" do
@hwsa.github.regular_proc.is_a?(Proc).should be_true
@hwsa.github.upcase_url.is_a?(Proc).should be_false
@hwsa.github.reverse_url.is_a?(Proc).should be_false
end
it "should instance_eval the proc with no params" do
@hwsa.github.reverse_url.should == @hash[:github][:url].reverse
end
it "should call the proc with params" do
@hwsa.github.upcase_url.should == @hash[:github][:url].upcase
end
it "should evaluate deferreds when enumerating values" do
@hash[:github].values.should_not include(@hash[:github][:url].reverse)
@hwsa.github.values.should include(@hash[:github][:url].reverse)
end
it "should evaluate deferreds when inspecting" do
s = @hwsa.inspect
s.should =~ %r[:reverse_url=>"tcurtsfnoc/nielkbm/moc.buhtig.www//:ptth"]
s.should =~ %r[:regular_proc=>#<Proc:]
end
it "should allow definition of deferreds in block mode" do
@hwsa.github do
defproc deferred! { reverse_url + upcase_url }
regproc lambda { reverse_url + upcase_url }
end
@hwsa.github.defproc.is_a?(Proc).should be_false
@hwsa.github.defproc.should == @hwsa.github.reverse_url + @hwsa.github.upcase_url
@hwsa.github.regproc.is_a?(Proc).should be_true
end
end
end