@@ -165,14 +165,18 @@ class ConfigTest < Test::Unit::TestCase
165
165
end
166
166
167
167
test ".[] for all x.y versions" do
168
- original = Config [ 0 ]
168
+ original = Config [ 0 r ]
169
169
assert_kind_of Config , original
170
+ assert_same original , Config [ 0 ]
170
171
assert_same original , Config [ 0.0 ]
171
172
assert_same original , Config [ 0.1 ]
172
173
assert_same original , Config [ 0.2 ]
173
174
assert_same original , Config [ 0.3 ]
174
175
( ( 0.4 r..FUTURE_VERSION . to_r ) % 0.1 r) . each do |version |
175
- assert_kind_of Config , Config [ version . to_f ]
176
+ config = Config [ version ]
177
+ assert_kind_of Config , config
178
+ assert_same config , Config [ version . to_f ]
179
+ assert_same config , Config [ version . to_f . to_r ]
176
180
end
177
181
end
178
182
@@ -186,6 +190,8 @@ class ConfigTest < Test::Unit::TestCase
186
190
187
191
test ".[] key errors" do
188
192
assert_raise ( KeyError ) do Config [ :nonexistent ] end
193
+ assert_raise ( KeyError ) do Config [ "nonexistent" ] end
194
+ assert_raise ( KeyError ) do Config [ "0.01" ] end
189
195
end
190
196
191
197
test ".[] with symbol names" do
@@ -195,6 +201,52 @@ class ConfigTest < Test::Unit::TestCase
195
201
assert_same Config [ FUTURE_VERSION ] , Config [ :future ]
196
202
end
197
203
204
+ test ".[] with string names" do
205
+ assert_same Config [ :original ] , Config [ "original" ]
206
+ assert_same Config [ :current ] , Config [ "current" ]
207
+ assert_same Config [ 0.4 r] , Config [ "0.4.11" ]
208
+ assert_same Config [ 0.5 r] , Config [ "0.5.6" ]
209
+ assert_same Config [ :current ] , Config [ Net ::IMAP ::VERSION ]
210
+ end
211
+
212
+ test ".[] with object responding to to_sym, to_r, or to_f" do
213
+ # responds to none of the methods
214
+ duck = Object . new
215
+ assert_raise TypeError do Config [ duck ] end
216
+
217
+ # to_sym
218
+ duck = Object . new
219
+ def duck . to_sym = :current
220
+ assert_same Config [ :current ] , Config [ duck ]
221
+
222
+ # to_r
223
+ duck = Object . new
224
+ def duck . to_r = 0.6 r
225
+ assert_same Config [ 0.6 r] , Config [ duck ]
226
+
227
+ # to_f
228
+ duck = Object . new
229
+ def duck . to_f = 0.4
230
+ assert_same Config [ 0.4 ] , Config [ duck ]
231
+
232
+ # prefer to_r over to_f
233
+ def duck . to_r = 0.5 r
234
+ assert_same Config [ 0.5 r] , Config [ duck ]
235
+
236
+ # prefer to_sym over to_r
237
+ def duck . to_sym = :original
238
+ assert_same Config [ :original ] , Config [ duck ]
239
+
240
+ # keeps trying if to_sym finds nothing
241
+ duck = Object . new
242
+ def duck . to_sym = :nope
243
+ def duck . to_f = 0.5
244
+ assert_same Config [ 0.5 ] , Config [ duck ]
245
+ # keeps trying if to_sym and to_r both find nothing
246
+ def duck . to_r = 1 /11111
247
+ assert_same Config [ 0.5 ] , Config [ duck ]
248
+ end
249
+
198
250
test ".[] with a hash" do
199
251
config = Config [ { responses_without_block : :raise , sasl_ir : false } ]
200
252
assert config . frozen?
0 commit comments