forked from sinatra/sinatra
/
indifferent_hash_test.rb
251 lines (205 loc) · 7.19 KB
/
indifferent_hash_test.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# frozen_string_literal: true
#
# We don't need the full test helper for this standalone class.
#
require 'minitest/autorun' unless defined?(Minitest)
# Suppress the ActiveSupport warning when this test is executed independently,
# outside of the full suite, on older Rubies.
ENV['SINATRA_ACTIVESUPPORT_WARNING'] = 'false'
require_relative '../lib/sinatra/indifferent_hash'
class TestIndifferentHashBasics < Minitest::Test
def test_flattened_constructor
hash = Sinatra::IndifferentHash[:a, 1, ?b, 2]
assert_equal 1, hash[?a]
assert_equal 2, hash[?b]
end
def test_pairs_constructor
hash = Sinatra::IndifferentHash[[[:a, 1], [?b, 2]]]
assert_equal 1, hash[?a]
assert_equal 2, hash[?b]
end
def test_default_block
hash = Sinatra::IndifferentHash.new { |h, k| h[k] = k.upcase }
assert_nil hash.default
assert_equal ?A, hash.default(:a)
end
def test_default_object
hash = Sinatra::IndifferentHash.new(:a=>1, ?b=>2)
assert_equal({ ?a=>1, ?b=>2 }, hash.default)
assert_equal({ ?a=>1, ?b=>2 }, hash[:a])
end
def test_default_assignment
hash = Sinatra::IndifferentHash.new
hash.default = { :a=>1, ?b=>2 }
assert_equal({ ?a=>1, ?b=>2 }, hash.default)
assert_equal({ ?a=>1, ?b=>2 }, hash[:a])
end
def test_assignment
hash = Sinatra::IndifferentHash.new
hash[:a] = :a
hash[?b] = :b
hash[3] = 3
hash[:simple_nested] = { :a=>:a, ?b=>:b }
assert_equal :a, hash[?a]
assert_equal :b, hash[?b]
assert_equal 3, hash[3]
assert_equal({ ?a=>:a, ?b=>:b }, hash['simple_nested'])
assert_nil hash[?d]
end
def test_merge!
# merge! is already mostly tested by the different constructors, so we
# really just need to test the block form here
hash = Sinatra::IndifferentHash[:a=>'a', ?b=>'b', 3=>3]
hash.merge!(?a=>'A', :b=>'B', :d=>'D') do |key, oldval, newval|
"#{oldval}*#{key}*#{newval}"
end
assert_equal({ ?a=>'a*a*A', ?b=>'b*b*B', 3=>3, ?d=>'D' }, hash)
end
end
class TestIndifferentHash < Minitest::Test
def skip_if_lacking(meth)
skip "Hash##{meth} not supported on this Ruby" unless Hash.method_defined?(meth)
end
def setup
@hash = Sinatra::IndifferentHash[:a=>:a, ?b=>:b, 3=>3,
:simple_nested=>{ :a=>:a, ?b=>:b },
:nested=>{ :a=>[{ :a=>:a, ?b=>:b }, :c, 4], ?f=>:f, 7=>7 }
]
end
def test_hash_constructor
assert_equal :a, @hash[?a]
assert_equal :b, @hash[?b]
assert_equal 3, @hash[3]
assert_equal({ ?a=>:a, ?b=>:b }, @hash['nested'][?a][0])
assert_equal :c, @hash['nested'][?a][1]
assert_equal 4, @hash['nested'][?a][2]
assert_equal :f, @hash['nested'][?f]
assert_equal 7, @hash['nested'][7]
assert_equal :a, @hash['simple_nested'][?a]
assert_equal :b, @hash['simple_nested'][?b]
assert_nil @hash[?d]
end
def test_assoc
assert_nil @hash.assoc(:d)
assert_equal [?a, :a], @hash.assoc(:a)
assert_equal [?b, :b], @hash.assoc(:b)
end
def test_rassoc
assert_nil @hash.rassoc(:d)
assert_equal [?a, :a], @hash.rassoc(:a)
assert_equal [?b, :b], @hash.rassoc(:b)
assert_equal ['simple_nested', { ?a=>:a, ?b=>:b }], @hash.rassoc(:a=>:a, ?b=>:b)
end
def test_fetch
assert_raises(KeyError) { @hash.fetch(:d) }
assert_equal 1, @hash.fetch(:d, 1)
assert_equal 2, @hash.fetch(:d) { 2 }
assert_equal ?d, @hash.fetch(:d) { |k| k }
assert_equal :a, @hash.fetch(:a, 1)
assert_equal :a, @hash.fetch(:a) { 2 }
end
def test_symbolic_retrieval
assert_equal :a, @hash[:a]
assert_equal :b, @hash[:b]
assert_equal({ ?a=>:a, ?b=>:b }, @hash[:nested][:a][0])
assert_equal :c, @hash[:nested][:a][1]
assert_equal 4, @hash[:nested][:a][2]
assert_equal :f, @hash[:nested][:f]
assert_equal 7, @hash[:nested][7]
assert_equal :a, @hash[:simple_nested][:a]
assert_equal :b, @hash[:simple_nested][:b]
assert_nil @hash[:d]
end
def test_key
assert_nil @hash.key(:d)
assert_equal ?a, @hash.key(:a)
assert_equal 'simple_nested', @hash.key(:a=>:a, ?b=>:b)
end
def test_key?
assert_operator @hash, :key?, :a
assert_operator @hash, :key?, ?b
assert_operator @hash, :key?, 3
refute_operator @hash, :key?, :d
end
def test_value?
assert_operator @hash, :value?, :a
assert_operator @hash, :value?, :b
assert_operator @hash, :value?, 3
assert_operator @hash, :value?, { :a=>:a, ?b=>:b }
refute_operator @hash, :value?, :d
end
def test_delete
@hash.delete(:a)
@hash.delete(?b)
assert_nil @hash[:a]
assert_nil @hash[?b]
end
def test_dig
skip_if_lacking :dig
assert_equal :a, @hash.dig(:a)
assert_equal :b, @hash.dig(?b)
assert_nil @hash.dig(:d)
assert_equal :a, @hash.dig(:simple_nested, :a)
assert_equal :b, @hash.dig('simple_nested', ?b)
assert_nil @hash.dig('simple_nested', :d)
assert_equal :a, @hash.dig(:nested, :a, 0, :a)
assert_equal :b, @hash.dig('nested', ?a, 0, ?b)
assert_nil @hash.dig('nested', ?a, 0, :d)
end
def test_slice
skip_if_lacking :slice
assert_equal Sinatra::IndifferentHash[a: :a], @hash.slice(:a)
assert_equal Sinatra::IndifferentHash[b: :b], @hash.slice(?b)
assert_equal Sinatra::IndifferentHash[3 => 3], @hash.slice(3)
assert_equal Sinatra::IndifferentHash.new, @hash.slice(:d)
assert_equal Sinatra::IndifferentHash[a: :a, b: :b, 3 => 3], @hash.slice(:a, :b, 3)
assert_equal Sinatra::IndifferentHash[simple_nested: { a: :a, ?b => :b }], @hash.slice(:simple_nested)
assert_equal Sinatra::IndifferentHash[nested: { a: [{ a: :a, ?b => :b }, :c, 4], ?f => :f, 7 => 7 }], @hash.slice(:nested)
end
def test_fetch_values
skip_if_lacking :fetch_values
assert_raises(KeyError) { @hash.fetch_values(3, :d) }
assert_equal [:a, :b, 3, ?D], @hash.fetch_values(:a, ?b, 3, :d) { |k| k.upcase }
end
def test_values_at
assert_equal [:a, :b, 3, nil], @hash.values_at(:a, ?b, 3, :d)
end
def test_merge
# merge just calls merge!, which is already thoroughly tested
hash2 = @hash.merge(?a=>1, :q=>2) { |key, oldval, newval| "#{oldval}*#{key}*#{newval}" }
refute_equal @hash, hash2
assert_equal 'a*a*1', hash2[:a]
assert_equal 2, hash2[?q]
end
def test_replace
@hash.replace(?a=>1, :q=>2)
assert_equal({ ?a=>1, ?q=>2 }, @hash)
end
def test_transform_values!
skip_if_lacking :transform_values!
@hash.transform_values! { |v| v.is_a?(Hash) ? Hash[v.to_a] : v }
assert_instance_of Sinatra::IndifferentHash, @hash[:simple_nested]
end
def test_transform_values
skip_if_lacking :transform_values
hash2 = @hash.transform_values { |v| v.respond_to?(:upcase) ? v.upcase : v }
refute_equal @hash, hash2
assert_equal :A, hash2[:a]
assert_equal :A, hash2[?a]
end
def test_transform_keys!
skip_if_lacking :transform_keys!
@hash.transform_keys! { |k| k.respond_to?(:to_sym) ? k.to_sym : k }
assert_equal :a, @hash[:a]
assert_equal :a, @hash[?a]
end
def test_transform_keys
skip_if_lacking :transform_keys
hash2 = @hash.transform_keys { |k| k.respond_to?(:upcase) ? k.upcase : k }
refute_equal @hash, hash2
refute_operator hash2, :key?, :a
refute_operator hash2, :key?, ?a
assert_equal :a, hash2[:A]
assert_equal :a, hash2[?A]
end
end