gorillib 0.5.0 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.gitmodules +3 -0
- data/.travis.yml +11 -0
- data/Gemfile +2 -16
- data/Rakefile +2 -74
- data/away/aliasing_spec.rb +180 -0
- data/away/confidence.rb +17 -0
- data/away/stub_module.rb +33 -0
- data/gorillib.gemspec +31 -246
- data/lib/gorillib/collection/model_collection.rb +1 -0
- data/lib/gorillib/data_munging.rb +0 -1
- data/lib/gorillib/hashlike/slice.rb +2 -0
- data/lib/gorillib/model/field.rb +2 -2
- data/lib/gorillib/model/serialization.rb +9 -4
- data/lib/gorillib/model/serialization/csv.rb +1 -0
- data/lib/gorillib/model/serialization/lines.rb +2 -0
- data/lib/gorillib/model/serialization/tsv.rb +1 -0
- data/lib/gorillib/pathname.rb +1 -1
- data/lib/gorillib/pathname/utils.rb +6 -0
- data/lib/gorillib/string/inflector.rb +1 -1
- data/lib/gorillib/system.rb +1 -0
- data/lib/gorillib/system/runner.rb +36 -0
- data/lib/gorillib/type/ip_address.rb +2 -2
- data/lib/gorillib/version.rb +3 -0
- data/old/lib/gorillib/hash/indifferent_access.rb +207 -0
- data/old/lib/gorillib/hash/tree_merge.rb +4 -0
- data/old/lib/gorillib/hashlike/tree_merge.rb +49 -0
- data/old/lib/gorillib/metaprogramming/cattr_accessor.rb +79 -0
- data/old/lib/gorillib/metaprogramming/mattr_accessor.rb +61 -0
- data/old/lib/gorillib/receiver.rb +402 -0
- data/old/lib/gorillib/receiver/active_model_shim.rb +32 -0
- data/old/lib/gorillib/receiver/acts_as_hash.rb +195 -0
- data/old/lib/gorillib/receiver/acts_as_loadable.rb +42 -0
- data/old/lib/gorillib/receiver/locale/en.yml +27 -0
- data/old/lib/gorillib/receiver/tree_diff.rb +74 -0
- data/old/lib/gorillib/receiver/validations.rb +30 -0
- data/old/lib/gorillib/receiver_model.rb +21 -0
- data/old/lib/gorillib/struct/acts_as_hash.rb +108 -0
- data/old/lib/gorillib/struct/hashlike_iteration.rb +0 -0
- data/old/spec/gorillib/hash/indifferent_access_spec.rb +391 -0
- data/old/spec/gorillib/metaprogramming/cattr_accessor_spec.rb +43 -0
- data/old/spec/gorillib/metaprogramming/mattr_accessor_spec.rb +45 -0
- data/old/spec/gorillib/receiver/receiver/acts_as_hash_spec.rb +295 -0
- data/old/spec/gorillib/receiver_spec.rb +551 -0
- data/old/spec/gorillib/struct/acts_as_hash_fuzz_spec.rb +71 -0
- data/old/spec/gorillib/struct/acts_as_hash_spec.rb +422 -0
- data/spec/gorillib/array/compact_blank_spec.rb +2 -2
- data/spec/gorillib/collection_spec.rb +6 -6
- data/spec/gorillib/factories_spec.rb +2 -2
- data/spec/gorillib/hashlike_spec.rb +2 -1
- data/spec/gorillib/model/defaults_spec.rb +3 -3
- data/spec/gorillib/model/serialization/csv_spec.rb +35 -0
- data/spec/gorillib/model/serialization/tsv_spec.rb +20 -4
- data/spec/gorillib/model/serialization_spec.rb +3 -3
- data/spec/spec_helper.rb +6 -1
- data/spec/support/factory_test_helpers.rb +2 -2
- data/spec/support/gorillib_test_helpers.rb +4 -4
- data/spec/support/hashlike_fuzzing_helper.rb +1 -15
- data/spec/support/hashlike_helper.rb +5 -1
- data/spec/support/model_test_helpers.rb +12 -1
- metadata +192 -168
- data/notes/HOWTO.md +0 -22
- data/notes/bucket.md +0 -155
- data/notes/builder.md +0 -170
- data/notes/collection.md +0 -81
- data/notes/factories.md +0 -86
- data/notes/model-overlay.md +0 -209
- data/notes/model.md +0 -135
- data/notes/structured-data-classes.md +0 -127
File without changes
|
@@ -0,0 +1,391 @@
|
|
1
|
+
require 'gorillib/hash/indifferent_access'
|
2
|
+
require 'gorillib/hash/slice'
|
3
|
+
require 'gorillib/hash/reverse_merge'
|
4
|
+
require 'gorillib/hash/deep_merge'
|
5
|
+
require 'gorillib/hash/deep_dup'
|
6
|
+
|
7
|
+
describe Gorillib::HashWithIndifferentAccess, :hashlike_spec => true do
|
8
|
+
class IndifferentHash < Gorillib::HashWithIndifferentAccess
|
9
|
+
end
|
10
|
+
|
11
|
+
class SubclassingArray < Array
|
12
|
+
end
|
13
|
+
|
14
|
+
class SubclassingHash < Hash
|
15
|
+
end
|
16
|
+
|
17
|
+
class NonIndifferentHash < Hash
|
18
|
+
def nested_under_indifferent_access
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
before do
|
24
|
+
@strings = { 'a' => 1, 'b' => 2 }
|
25
|
+
@symbols = { :a => 1, :b => 2 }
|
26
|
+
@mixed = { :a => 1, 'b' => 2 }
|
27
|
+
@fixnums = { 0 => 1, 1 => 2 }
|
28
|
+
if RUBY_VERSION < '1.9.0'
|
29
|
+
@illegal_symbols = { "\0" => 1, "" => 2, [] => 3 }
|
30
|
+
else
|
31
|
+
@illegal_symbols = { [] => 3 }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'symbolize_keys_for_hash_with_indifferent_access' do
|
36
|
+
@symbols.with_indifferent_access.symbolize_keys.should be_an_instance_of(Hash)
|
37
|
+
@symbols.with_indifferent_access.symbolize_keys.should == @symbols
|
38
|
+
@strings.with_indifferent_access.symbolize_keys.should == @symbols
|
39
|
+
@mixed.with_indifferent_access.symbolize_keys.should == @symbols
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'symbolize_keys_bang_for_hash_with_indifferent_access' do
|
43
|
+
lambda{ @symbols.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
|
44
|
+
lambda{ @strings.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
|
45
|
+
lambda{ @mixed.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'symbolize_keys_preserves_keys_that_cant_be_symbolized_for_hash_with_indifferent_access' do
|
49
|
+
@illegal_symbols.with_indifferent_access.symbolize_keys.should == @illegal_symbols
|
50
|
+
lambda{ @illegal_symbols.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'symbolize_keys_preserves_fixnum_keys_for_hash_with_indifferent_access' do
|
54
|
+
@fixnums.with_indifferent_access.symbolize_keys.should == @fixnums
|
55
|
+
lambda{ @fixnums.with_indifferent_access.dup.symbolize_keys! }.should raise_error(NoMethodError)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'stringify_keys_for_hash_with_indifferent_access' do
|
59
|
+
@symbols.with_indifferent_access.stringify_keys.should be_an_instance_of(Gorillib::HashWithIndifferentAccess)
|
60
|
+
@symbols.with_indifferent_access.stringify_keys.should == @strings
|
61
|
+
@strings.with_indifferent_access.stringify_keys.should == @strings
|
62
|
+
@mixed.with_indifferent_access.stringify_keys.should == @strings
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'stringify_keys_bang_for_hash_with_indifferent_access' do
|
66
|
+
@symbols.with_indifferent_access.dup.stringify_keys!.should be_an_instance_of(Gorillib::HashWithIndifferentAccess)
|
67
|
+
@symbols.with_indifferent_access.dup.stringify_keys!.should == @strings
|
68
|
+
@strings.with_indifferent_access.dup.stringify_keys!.should == @strings
|
69
|
+
@mixed.with_indifferent_access.dup.stringify_keys!.should == @strings
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'nested_under_indifferent_access' do
|
73
|
+
foo = { "foo" => SubclassingHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access
|
74
|
+
foo["foo"].should be_a(Gorillib::HashWithIndifferentAccess)
|
75
|
+
|
76
|
+
foo = { "foo" => NonIndifferentHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access
|
77
|
+
foo["foo"].should be_a(NonIndifferentHash)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'indifferent_assorted' do
|
81
|
+
@strings = @strings.with_indifferent_access
|
82
|
+
@symbols = @symbols.with_indifferent_access
|
83
|
+
@mixed = @mixed.with_indifferent_access
|
84
|
+
|
85
|
+
@strings.__send__(:convert_key, :a).should == 'a'
|
86
|
+
|
87
|
+
@strings.fetch('a').should == 1
|
88
|
+
@strings.fetch(:a.to_s).should == 1
|
89
|
+
@strings.fetch(:a).should == 1
|
90
|
+
|
91
|
+
hashes = { :@strings => @strings, :@symbols => @symbols, :@mixed => @mixed }
|
92
|
+
method_map = { :'[]' => 1, :fetch => 1, :values_at => [1],
|
93
|
+
:has_key? => true, :include? => true, :key? => true,
|
94
|
+
:member? => true }
|
95
|
+
|
96
|
+
hashes.each do |name, hash|
|
97
|
+
method_map.sort_by { |m| m.to_s }.each do |meth, expected|
|
98
|
+
hash.__send__(meth, 'a').should == expected
|
99
|
+
hash.__send__(meth, :a).should == expected
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
@strings.values_at('a', 'b').should == [1, 2]
|
104
|
+
@strings.values_at(:a, :b).should == [1, 2]
|
105
|
+
@symbols.values_at('a', 'b').should == [1, 2]
|
106
|
+
@symbols.values_at(:a, :b).should == [1, 2]
|
107
|
+
@mixed.values_at('a', 'b').should == [1, 2]
|
108
|
+
@mixed.values_at(:a, :b).should == [1, 2]
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'indifferent_reading' do
|
112
|
+
hash = Gorillib::HashWithIndifferentAccess.new
|
113
|
+
hash["a"] = 1
|
114
|
+
hash["b"] = true
|
115
|
+
hash["c"] = false
|
116
|
+
hash["d"] = nil
|
117
|
+
|
118
|
+
hash[:a].should == 1
|
119
|
+
hash[:b].should == true
|
120
|
+
hash[:c].should == false
|
121
|
+
hash[:d].should == nil
|
122
|
+
hash[:e].should == nil
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'indifferent_reading_with_nonnil_default' do
|
126
|
+
hash = Gorillib::HashWithIndifferentAccess.new(1)
|
127
|
+
hash["a"] = 1
|
128
|
+
hash["b"] = true
|
129
|
+
hash["c"] = false
|
130
|
+
hash["d"] = nil
|
131
|
+
|
132
|
+
hash[:a].should == 1
|
133
|
+
hash[:b].should == true
|
134
|
+
hash[:c].should == false
|
135
|
+
hash[:d].should == nil
|
136
|
+
hash[:e].should == 1
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'indifferent_writing' do
|
140
|
+
hash = Gorillib::HashWithIndifferentAccess.new
|
141
|
+
hash[:a] = 1
|
142
|
+
hash['b'] = 2
|
143
|
+
hash[3] = 3
|
144
|
+
|
145
|
+
1.should == hash['a']
|
146
|
+
2.should == hash['b']
|
147
|
+
1.should == hash[:a]
|
148
|
+
2.should == hash[:b]
|
149
|
+
3.should == hash[3]
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'indifferent_update' do
|
153
|
+
hash = Gorillib::HashWithIndifferentAccess.new
|
154
|
+
hash[:a] = 'a'
|
155
|
+
hash['b'] = 'b'
|
156
|
+
|
157
|
+
updated_with_strings = hash.update(@strings)
|
158
|
+
updated_with_symbols = hash.update(@symbols)
|
159
|
+
updated_with_mixed = hash.update(@mixed)
|
160
|
+
|
161
|
+
1.should == updated_with_strings[:a]
|
162
|
+
1.should == updated_with_strings['a']
|
163
|
+
2.should == updated_with_strings['b']
|
164
|
+
|
165
|
+
1.should == updated_with_symbols[:a]
|
166
|
+
2.should == updated_with_symbols['b']
|
167
|
+
2.should == updated_with_symbols[:b]
|
168
|
+
|
169
|
+
1.should == updated_with_mixed[:a]
|
170
|
+
2.should == updated_with_mixed['b']
|
171
|
+
|
172
|
+
[updated_with_strings, updated_with_symbols, updated_with_mixed].all?{ |h| h.keys.size == 2 }.should be_true
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'indifferent_merging' do
|
176
|
+
hash = Gorillib::HashWithIndifferentAccess.new
|
177
|
+
hash[:a] = 'failure'
|
178
|
+
hash['b'] = 'failure'
|
179
|
+
|
180
|
+
other = { 'a' => 1, :b => 2 }
|
181
|
+
|
182
|
+
merged = hash.merge(other)
|
183
|
+
|
184
|
+
merged.class.should == Gorillib::HashWithIndifferentAccess
|
185
|
+
merged[:a].should == 1
|
186
|
+
merged['b'].should == 2
|
187
|
+
|
188
|
+
hash.update(other)
|
189
|
+
|
190
|
+
hash[:a].should == 1
|
191
|
+
hash['b'].should == 2
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'indifferent_reverse_merging' do
|
195
|
+
hash = Gorillib::HashWithIndifferentAccess.new('some' => 'value', 'other' => 'value')
|
196
|
+
hash.reverse_merge!(:some => 'noclobber', :another => 'clobber')
|
197
|
+
hash[:some].should == 'value'
|
198
|
+
hash[:another].should == 'clobber'
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'indifferent_deleting' do
|
202
|
+
get_hash = proc{ { :a => 'foo' }.with_indifferent_access }
|
203
|
+
hash = get_hash.call
|
204
|
+
'foo'.should == hash.delete(:a)
|
205
|
+
nil.should == hash.delete(:a)
|
206
|
+
hash = get_hash.call
|
207
|
+
'foo'.should == hash.delete('a')
|
208
|
+
nil.should == hash.delete('a')
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'indifferent_to_hash' do
|
212
|
+
# Should convert to a Hash with String keys.
|
213
|
+
@mixed.with_indifferent_access.to_hash.should == @strings
|
214
|
+
|
215
|
+
# Should preserve the default value.
|
216
|
+
mixed_with_default = @mixed.dup
|
217
|
+
mixed_with_default.default = '1234'
|
218
|
+
roundtrip = mixed_with_default.with_indifferent_access.to_hash
|
219
|
+
roundtrip.should == @strings
|
220
|
+
roundtrip.default.should == '1234'
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'indifferent_hash_with_array_of_hashes' do
|
224
|
+
hash = { "urls" => { "url" => [ { "address" => "1" }, { "address" => "2" } ] }}.with_indifferent_access
|
225
|
+
hash[:urls][:url].first[:address].should == "1"
|
226
|
+
end
|
227
|
+
|
228
|
+
it 'should_preserve_array_subclass_when_value_is_array' do
|
229
|
+
array = SubclassingArray.new
|
230
|
+
array << { "address" => "1" }
|
231
|
+
hash = { "urls" => { "url" => array }}.with_indifferent_access
|
232
|
+
hash[:urls][:url].class.should == SubclassingArray
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'should_preserve_array_class_when_hash_value_is_frozen_array' do
|
236
|
+
array = SubclassingArray.new
|
237
|
+
array << { "address" => "1" }
|
238
|
+
hash = { "urls" => { "url" => array.freeze }}.with_indifferent_access
|
239
|
+
hash[:urls][:url].class.should == SubclassingArray
|
240
|
+
end
|
241
|
+
|
242
|
+
it 'stringify_and_symbolize_keys_on_indifferent_preserves_hash' do
|
243
|
+
h = Gorillib::HashWithIndifferentAccess.new
|
244
|
+
h[:first] = 1
|
245
|
+
h = h.stringify_keys
|
246
|
+
h['first'].should == 1
|
247
|
+
h = Gorillib::HashWithIndifferentAccess.new
|
248
|
+
h['first'] = 1
|
249
|
+
h = h.symbolize_keys
|
250
|
+
h[:first].should == 1
|
251
|
+
end
|
252
|
+
|
253
|
+
it 'to_options_on_indifferent_preserves_hash' do
|
254
|
+
h = Gorillib::HashWithIndifferentAccess.new
|
255
|
+
h['first'] = 1
|
256
|
+
h.to_options!
|
257
|
+
h['first'].should == 1
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'indifferent_subhashes' do
|
261
|
+
h = {'user' => {'id' => 5}}.with_indifferent_access
|
262
|
+
['user', :user].each{|user| [:id, 'id'].each{|id| h[user][id].should == 5 }}
|
263
|
+
|
264
|
+
h = {:user => {:id => 5}}.with_indifferent_access
|
265
|
+
['user', :user].each{|user| [:id, 'id'].each{|id| h[user][id].should == 5 }}
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'indifferent_duplication' do
|
269
|
+
# Should preserve default value
|
270
|
+
h = Gorillib::HashWithIndifferentAccess.new
|
271
|
+
h.default = '1234'
|
272
|
+
h.dup.default.should == h.default
|
273
|
+
|
274
|
+
# Should preserve class for subclasses
|
275
|
+
h = IndifferentHash.new
|
276
|
+
h.dup.class.should == h.class
|
277
|
+
end
|
278
|
+
|
279
|
+
it 'assorted_keys_not_stringified' do
|
280
|
+
original = {Object.new => 2, 1 => 2, [] => true}
|
281
|
+
indiff = original.with_indifferent_access
|
282
|
+
indiff.keys.any?{|k| k.kind_of? String }.should_not be_true
|
283
|
+
end
|
284
|
+
|
285
|
+
it 'deep_merge' do
|
286
|
+
hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }
|
287
|
+
hash_2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
|
288
|
+
expected = { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
|
289
|
+
hash_1.deep_merge(hash_2).should == expected
|
290
|
+
|
291
|
+
hash_1.deep_merge!(hash_2)
|
292
|
+
hash_1.should == expected
|
293
|
+
end
|
294
|
+
|
295
|
+
it 'deep_merge_on_indifferent_access' do
|
296
|
+
hash_1 = Gorillib::HashWithIndifferentAccess.new({ :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } })
|
297
|
+
hash_2 = Gorillib::HashWithIndifferentAccess.new({ :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } })
|
298
|
+
hash_3 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
|
299
|
+
expected = { "a" => 1, "b" => "b", "c" => { "c1" => 2, "c2" => "c2", "c3" => { "d1" => "d1", "d2" => "d2" } } }
|
300
|
+
hash_1.deep_merge(hash_2).should == expected
|
301
|
+
hash_1.deep_merge(hash_3).should == expected
|
302
|
+
|
303
|
+
hash_1.deep_merge!(hash_2)
|
304
|
+
hash_1.should == expected
|
305
|
+
end
|
306
|
+
|
307
|
+
it 'deep_dup' do
|
308
|
+
hash = { :a => { :b => 'b' } }
|
309
|
+
dup = hash.deep_dup
|
310
|
+
dup[:a][:c] = 'c'
|
311
|
+
hash[:a][:c].should == nil
|
312
|
+
dup[:a][:c].should == 'c'
|
313
|
+
end
|
314
|
+
|
315
|
+
it 'deep_dup_initialize' do
|
316
|
+
zero_hash = Hash.new 0
|
317
|
+
hash = { :a => zero_hash }
|
318
|
+
dup = hash.deep_dup
|
319
|
+
dup[:a][44].should == 0
|
320
|
+
end
|
321
|
+
|
322
|
+
it 'store_on_indifferent_access' do
|
323
|
+
hash = Gorillib::HashWithIndifferentAccess.new
|
324
|
+
hash.store(:test1, 1)
|
325
|
+
hash.store('test1', 11)
|
326
|
+
hash[:test2] = 2
|
327
|
+
hash['test2'] = 22
|
328
|
+
expected = { "test1" => 11, "test2" => 22 }
|
329
|
+
hash.should == expected
|
330
|
+
end
|
331
|
+
|
332
|
+
it 'indifferent_slice' do
|
333
|
+
original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
|
334
|
+
expected = { :a => 'x', :b => 'y' }.with_indifferent_access
|
335
|
+
|
336
|
+
[['a', 'b'], [:a, :b]].each do |keys|
|
337
|
+
# Should return a new hash with only the given keys.
|
338
|
+
original.slice(*keys).should == expected
|
339
|
+
original.should_not == expected
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
it 'indifferent_slice_inplace' do
|
344
|
+
original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
|
345
|
+
expected = { :c => 10 }.with_indifferent_access
|
346
|
+
|
347
|
+
[['a', 'b'], [:a, :b]].each do |keys|
|
348
|
+
# Should replace the hash with only the given keys.
|
349
|
+
copy = original.dup
|
350
|
+
copy.slice!(*keys).should == expected
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
it 'indifferent_slice_access_with_symbols' do
|
355
|
+
original = {'login' => 'bender', 'password' => 'shiny', 'stuff' => 'foo'}
|
356
|
+
original = original.with_indifferent_access
|
357
|
+
|
358
|
+
slice = original.slice(:login, :password)
|
359
|
+
|
360
|
+
slice[:login].should == 'bender'
|
361
|
+
slice['login'].should == 'bender'
|
362
|
+
end
|
363
|
+
|
364
|
+
it 'should_use_default_value_for_unknown_key' do
|
365
|
+
hash_wia = Gorillib::HashWithIndifferentAccess.new(3)
|
366
|
+
hash_wia[:new_key].should == 3
|
367
|
+
end
|
368
|
+
|
369
|
+
it 'should_use_default_value_if_no_key_is_supplied' do
|
370
|
+
hash_wia = Gorillib::HashWithIndifferentAccess.new(3)
|
371
|
+
hash_wia.default.should == 3
|
372
|
+
end
|
373
|
+
|
374
|
+
it 'should_nil_if_no_default_value_is_supplied' do
|
375
|
+
hash_wia = Gorillib::HashWithIndifferentAccess.new
|
376
|
+
hash_wia.default.should be_nil
|
377
|
+
end
|
378
|
+
|
379
|
+
it 'should_return_dup_for_with_indifferent_access' do
|
380
|
+
hash_wia = Gorillib::HashWithIndifferentAccess.new
|
381
|
+
hash_wia.with_indifferent_access.should == hash_wia
|
382
|
+
hash_wia.with_indifferent_access.should_not equal(hash_wia)
|
383
|
+
end
|
384
|
+
|
385
|
+
it 'should_copy_the_default_value_when_converting_to_hash_with_indifferent_access' do
|
386
|
+
hash = Hash.new(3)
|
387
|
+
hash_wia = hash.with_indifferent_access
|
388
|
+
hash_wia.default.should == 3
|
389
|
+
end
|
390
|
+
|
391
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
require 'gorillib/metaprogramming/cattr_accessor'
|
3
|
+
|
4
|
+
describe 'metaprogramming' do
|
5
|
+
describe 'cattr_accessor' do
|
6
|
+
before do
|
7
|
+
@class = Class.new do
|
8
|
+
cattr_accessor :foo
|
9
|
+
cattr_accessor :bar, :instance_writer => false
|
10
|
+
cattr_reader :shaq, :instance_reader => false
|
11
|
+
end
|
12
|
+
@object = @class.new
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'does not have an effect if already provided by another library.' unless ENV['QUIET_RSPEC']
|
16
|
+
|
17
|
+
it 'uses mattr default' do
|
18
|
+
@class.foo.should be_nil
|
19
|
+
@object.foo.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'sets mattr value' do
|
23
|
+
@class.foo = :test
|
24
|
+
@object.foo.should == :test
|
25
|
+
|
26
|
+
@object.foo = :test2
|
27
|
+
@class.foo.should == :test2
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'with instance_writer => false, does not create an instance writer' do
|
31
|
+
@class.should respond_to(:foo)
|
32
|
+
@class.should respond_to(:foo=)
|
33
|
+
@object.should respond_to(:bar)
|
34
|
+
@object.should_not respond_to(:bar=)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'with instance_reader => false, does not create an instance reader' do
|
38
|
+
@class.should respond_to(:shaq)
|
39
|
+
@object.should_not respond_to(:shaq)
|
40
|
+
@object.should_not respond_to(:shaq=)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2
|
+
require 'gorillib/metaprogramming/mattr_accessor'
|
3
|
+
|
4
|
+
describe Module do
|
5
|
+
describe 'mattr_accessor' do
|
6
|
+
before do
|
7
|
+
m = @module = Module.new do
|
8
|
+
mattr_accessor :foo
|
9
|
+
mattr_accessor :bar, :instance_writer => false
|
10
|
+
mattr_reader :shaq, :instance_reader => false
|
11
|
+
end
|
12
|
+
@class = Class.new
|
13
|
+
@class.instance_eval { include m }
|
14
|
+
@object = @class.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'does not have an effect if already provided by another library.' unless ENV['QUIET_RSPEC']
|
18
|
+
|
19
|
+
it 'uses mattr default' do
|
20
|
+
@module.foo.should be_nil
|
21
|
+
@object.foo.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'sets mattr value' do
|
25
|
+
@module.foo = :test
|
26
|
+
@object.foo.should == :test
|
27
|
+
|
28
|
+
@object.foo = :test2
|
29
|
+
@module.foo.should == :test2
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'with :instance_writer => false, does not create instance writer' do
|
33
|
+
@module.should respond_to(:foo)
|
34
|
+
@module.should respond_to(:foo=)
|
35
|
+
@object.should respond_to(:bar)
|
36
|
+
@object.should_not respond_to(:bar=)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'with :instance_writer => false, does not create instance reader' do
|
40
|
+
@module.should respond_to(:shaq)
|
41
|
+
@object.should_not respond_to(:shaq)
|
42
|
+
@object.should_not respond_to(:shaq=)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|