hashery 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/HISTORY.md +26 -0
- data/lib/hashery/dictionary.rb +125 -46
- data/lib/hashery/version.rb +1 -1
- data/test/case_dictionary.rb +270 -15
- data/test/case_ini_hash.rb +4 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8caa9de54771dfc677bf6210b91ca3e9c4df285d0d802d4add8aefd52e82310b
|
|
4
|
+
data.tar.gz: 125886b904e00ca54165212bd3336ec5a8e2507d9c6ede8f4a528eaac83317d8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: acbece4eae105e972288fd55cd5f11c2f7169e62d6c801efae73d345ce65a639bac5ffcccedad27d454d8808faa5a510f08fb795f200deb5ec8138976b296800
|
|
7
|
+
data.tar.gz: cc310523d225dd004b4fb597c1798c4f02d4cd9f7e9c23471c34b3cbca2c33a853dfa1211cfce4f2f089a49bd5585f519193f03163e4d3f93427604bd3d45f07
|
data/HISTORY.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# RELEASE HISTORY
|
|
2
2
|
|
|
3
|
+
## 2.2.1 / 2026-11-17
|
|
4
|
+
|
|
5
|
+
This release fixes a number of bugs in Dictionary. Most notably, a
|
|
6
|
+
Dictionary kept in order with `order_by_key` or `order_by_value` could no
|
|
7
|
+
longer be read at all (not even inspected) once it held a key or value
|
|
8
|
+
that didn't compare with the others, such as `nil` (#7). Now sorting by
|
|
9
|
+
such an order raises and leaves the Dictionary as it was, and adding an
|
|
10
|
+
entry the order can't place raises without storing it. LRUHash is
|
|
11
|
+
unchanged.
|
|
12
|
+
|
|
13
|
+
Changes:
|
|
14
|
+
|
|
15
|
+
* Fix Dictionary sort orders that can't sort the entries (#7).
|
|
16
|
+
* Fix Dictionary#dup and #clone, which shared state with the original
|
|
17
|
+
(clone), or dropped the sort order and default (dup).
|
|
18
|
+
* Fix Dictionary#reject!, which returned a filtered copy instead of
|
|
19
|
+
removing entries.
|
|
20
|
+
* Fix Dictionary#shift on an empty Dictionary, and #shift and #pop with
|
|
21
|
+
a `nil` key.
|
|
22
|
+
* Fix Dictionary[hash] and #replace, which shared the given hash or
|
|
23
|
+
order instead of copying it.
|
|
24
|
+
* Fix Dictionary#insert with an existing key or an index past the end.
|
|
25
|
+
* Fix Dictionary#[]= with an index and a `nil` or `false` value.
|
|
26
|
+
* Fix Dictionary#delete removing 1.0 from the order along with 1.
|
|
27
|
+
* Fix Dictionary#== for a sorted Dictionary changed since it was read.
|
|
28
|
+
|
|
3
29
|
## 2.2.0 / 2026-11-16
|
|
4
30
|
|
|
5
31
|
This release concentrates on LRUHash, by far the most used part of the
|
data/lib/hashery/dictionary.rb
CHANGED
|
@@ -128,15 +128,25 @@ module Hashery
|
|
|
128
128
|
def initialize(*args, &blk)
|
|
129
129
|
@order = []
|
|
130
130
|
@order_by = nil
|
|
131
|
+
@block = blk
|
|
131
132
|
if blk
|
|
132
|
-
|
|
133
|
-
oblk = lambda{ |hsh, key| blk[dict,key] } # dictionary rather then just the interal hash.
|
|
134
|
-
@hash = Hash.new(*args, &oblk)
|
|
133
|
+
@hash = Hash.new(*args, &default_proc_for(blk))
|
|
135
134
|
else
|
|
136
135
|
@hash = Hash.new(*args)
|
|
137
136
|
end
|
|
138
137
|
end
|
|
139
138
|
|
|
139
|
+
#
|
|
140
|
+
# Make a copy independent of the original, as #dup and #clone do. It
|
|
141
|
+
# keeps the original's order, sort rule and default.
|
|
142
|
+
#
|
|
143
|
+
def initialize_copy(source)
|
|
144
|
+
super
|
|
145
|
+
@order = source.order.dup
|
|
146
|
+
@hash = @hash.dup
|
|
147
|
+
@hash.default_proc = default_proc_for(@block) if @block
|
|
148
|
+
end
|
|
149
|
+
|
|
140
150
|
#
|
|
141
151
|
# Order of keys.
|
|
142
152
|
#
|
|
@@ -148,16 +158,20 @@ module Hashery
|
|
|
148
158
|
end
|
|
149
159
|
|
|
150
160
|
#
|
|
151
|
-
# Keep dictionary sorted by a specific sort order.
|
|
161
|
+
# Keep dictionary sorted by a specific sort order. Without a block,
|
|
162
|
+
# stop sorting and keep the current order.
|
|
163
|
+
#
|
|
164
|
+
# If the entries can't be sorted this way (a +nil+ key among others,
|
|
165
|
+
# or keys of types that don't compare), it raises and the dictionary
|
|
166
|
+
# keeps its previous order. While a sort order is set, adding an entry
|
|
167
|
+
# it can't place raises too, and nothing is stored.
|
|
152
168
|
#
|
|
153
169
|
# block - Ordering procedure.
|
|
154
170
|
#
|
|
155
171
|
# Returns +self+.
|
|
156
172
|
#
|
|
157
173
|
def order_by( &block )
|
|
158
|
-
|
|
159
|
-
order
|
|
160
|
-
self
|
|
174
|
+
sort_with(block)
|
|
161
175
|
end
|
|
162
176
|
|
|
163
177
|
#
|
|
@@ -179,12 +193,10 @@ module Hashery
|
|
|
179
193
|
#
|
|
180
194
|
def order_by_key
|
|
181
195
|
if block_given?
|
|
182
|
-
|
|
196
|
+
sort_with(Proc.new{ |k,v| yield(k) })
|
|
183
197
|
else
|
|
184
|
-
|
|
198
|
+
sort_with(Proc.new{ |k,v| k })
|
|
185
199
|
end
|
|
186
|
-
order
|
|
187
|
-
self
|
|
188
200
|
end
|
|
189
201
|
|
|
190
202
|
#
|
|
@@ -202,12 +214,10 @@ module Hashery
|
|
|
202
214
|
#
|
|
203
215
|
def order_by_value
|
|
204
216
|
if block_given?
|
|
205
|
-
|
|
217
|
+
sort_with(Proc.new{ |k,v| yield(v) })
|
|
206
218
|
else
|
|
207
|
-
|
|
219
|
+
sort_with(Proc.new{ |k,v| v })
|
|
208
220
|
end
|
|
209
|
-
order
|
|
210
|
-
self
|
|
211
221
|
end
|
|
212
222
|
|
|
213
223
|
#
|
|
@@ -231,7 +241,7 @@ module Hashery
|
|
|
231
241
|
#
|
|
232
242
|
def ==(hsh2)
|
|
233
243
|
if hsh2.is_a?( Dictionary )
|
|
234
|
-
|
|
244
|
+
order == hsh2.order &&
|
|
235
245
|
@hash == hsh2.instance_variable_get("@hash")
|
|
236
246
|
else
|
|
237
247
|
false
|
|
@@ -261,16 +271,18 @@ module Hashery
|
|
|
261
271
|
#
|
|
262
272
|
# h[key,index] = value
|
|
263
273
|
#
|
|
264
|
-
def []=(k, i=nil, v=
|
|
265
|
-
if v
|
|
266
|
-
insert(i,k,v)
|
|
267
|
-
else
|
|
274
|
+
def []=(k, i=nil, v=NO_VALUE)
|
|
275
|
+
if NO_VALUE.equal?(v)
|
|
268
276
|
store(k,i)
|
|
277
|
+
else
|
|
278
|
+
insert(i,k,v)
|
|
269
279
|
end
|
|
270
280
|
end
|
|
271
281
|
|
|
272
282
|
#
|
|
273
|
-
# Insert entry into dictionary at specific index position.
|
|
283
|
+
# Insert entry into dictionary at specific index position. If +key+ is
|
|
284
|
+
# already present, it moves to that position. An index past the end
|
|
285
|
+
# adds the entry at the end.
|
|
274
286
|
#
|
|
275
287
|
# index - [Integer] Position of order placement.
|
|
276
288
|
# key - [Object] Key to associate with value.
|
|
@@ -279,6 +291,9 @@ module Hashery
|
|
|
279
291
|
# Returns `value` stored.
|
|
280
292
|
#
|
|
281
293
|
def insert(index, key, value)
|
|
294
|
+
check_order(key, value)
|
|
295
|
+
remove_from_order(key) if @hash.key?(key)
|
|
296
|
+
index = @order.size if index > @order.size
|
|
282
297
|
@order.insert(index, key)
|
|
283
298
|
@hash.store(key, value)
|
|
284
299
|
end
|
|
@@ -289,6 +304,7 @@ module Hashery
|
|
|
289
304
|
# Returns `value`.
|
|
290
305
|
#
|
|
291
306
|
def store(key, value)
|
|
307
|
+
check_order(key, value)
|
|
292
308
|
@order.push(key) unless @hash.has_key?(key)
|
|
293
309
|
@hash.store(key, value)
|
|
294
310
|
end
|
|
@@ -305,7 +321,7 @@ module Hashery
|
|
|
305
321
|
# Delete the entry with given +key+.
|
|
306
322
|
#
|
|
307
323
|
def delete(key)
|
|
308
|
-
@
|
|
324
|
+
remove_from_order(key) if @hash.key?(key)
|
|
309
325
|
@hash.delete(key)
|
|
310
326
|
end
|
|
311
327
|
|
|
@@ -385,41 +401,50 @@ module Hashery
|
|
|
385
401
|
end
|
|
386
402
|
|
|
387
403
|
#
|
|
388
|
-
#
|
|
389
|
-
#
|
|
390
|
-
# Returns [Hash] of rejected entries.
|
|
404
|
+
# Remove entries for which the block is true.
|
|
391
405
|
#
|
|
392
|
-
#
|
|
406
|
+
# Returns +self+, or +nil+ if nothing was removed.
|
|
393
407
|
#
|
|
394
408
|
def reject!( &block )
|
|
395
|
-
|
|
396
|
-
|
|
409
|
+
before = length
|
|
410
|
+
delete_if(&block)
|
|
411
|
+
length == before ? nil : self
|
|
397
412
|
end
|
|
398
413
|
|
|
399
414
|
#
|
|
400
|
-
# Replace dictionary entries with new table.
|
|
415
|
+
# Replace dictionary entries with new table. The dictionary copies the
|
|
416
|
+
# entries, so later changes to either one don't affect the other. If a
|
|
417
|
+
# sort order is set and can't sort the new entries, it raises and the
|
|
418
|
+
# dictionary keeps its old entries.
|
|
401
419
|
#
|
|
402
420
|
def replace(hsh2)
|
|
421
|
+
old_hash, old_order = @hash, @order
|
|
403
422
|
case hsh2
|
|
404
423
|
when Dictionary
|
|
405
|
-
@order = hsh2.order
|
|
424
|
+
@order = hsh2.order.dup
|
|
406
425
|
@hash = hsh2.to_h
|
|
407
426
|
when Hash
|
|
408
|
-
@hash = hsh2
|
|
427
|
+
@hash = hsh2.dup
|
|
409
428
|
@order = @hash.keys
|
|
410
429
|
else
|
|
411
430
|
@hash = hsh2.to_h
|
|
412
431
|
@order = @hash.keys
|
|
413
432
|
end
|
|
414
433
|
reorder
|
|
434
|
+
rescue Exception
|
|
435
|
+
@hash, @order = old_hash, old_order
|
|
436
|
+
raise
|
|
415
437
|
end
|
|
416
438
|
|
|
417
439
|
#
|
|
418
440
|
# Remove entry from the to top of dictionary.
|
|
419
441
|
#
|
|
442
|
+
# Returns the [key, value] pair, or +nil+ if the dictionary is empty.
|
|
443
|
+
#
|
|
420
444
|
def shift
|
|
445
|
+
return nil if empty?
|
|
421
446
|
key = order.first
|
|
422
|
-
|
|
447
|
+
[key,delete(key)]
|
|
423
448
|
end
|
|
424
449
|
|
|
425
450
|
#
|
|
@@ -427,6 +452,7 @@ module Hashery
|
|
|
427
452
|
#
|
|
428
453
|
def unshift( k,v )
|
|
429
454
|
unless @hash.include?( k )
|
|
455
|
+
check_order( k,v )
|
|
430
456
|
@order.unshift( k )
|
|
431
457
|
@hash.store( k,v )
|
|
432
458
|
true
|
|
@@ -447,6 +473,7 @@ module Hashery
|
|
|
447
473
|
#
|
|
448
474
|
def push(k,v)
|
|
449
475
|
unless @hash.include?( k )
|
|
476
|
+
check_order( k,v )
|
|
450
477
|
@order.push( k )
|
|
451
478
|
@hash.store( k,v )
|
|
452
479
|
true
|
|
@@ -458,9 +485,12 @@ module Hashery
|
|
|
458
485
|
#
|
|
459
486
|
# Pop entry off the bottom of dictionary.
|
|
460
487
|
#
|
|
488
|
+
# Returns the [key, value] pair, or +nil+ if the dictionary is empty.
|
|
489
|
+
#
|
|
461
490
|
def pop
|
|
491
|
+
return nil if empty?
|
|
462
492
|
key = order.last
|
|
463
|
-
|
|
493
|
+
[key,delete(key)]
|
|
464
494
|
end
|
|
465
495
|
|
|
466
496
|
#
|
|
@@ -474,17 +504,6 @@ module Hashery
|
|
|
474
504
|
'{' + ary.join(", ") + '}'
|
|
475
505
|
end
|
|
476
506
|
|
|
477
|
-
#
|
|
478
|
-
# Duplicate dictionary.
|
|
479
|
-
#
|
|
480
|
-
# Returns [Dictionary].
|
|
481
|
-
#
|
|
482
|
-
def dup
|
|
483
|
-
a = []
|
|
484
|
-
each{ |k,v| a << k; a << v }
|
|
485
|
-
self.class[*a]
|
|
486
|
-
end
|
|
487
|
-
|
|
488
507
|
#
|
|
489
508
|
# Update dictionary with other hash.
|
|
490
509
|
#
|
|
@@ -529,12 +548,15 @@ module Hashery
|
|
|
529
548
|
end
|
|
530
549
|
|
|
531
550
|
#
|
|
532
|
-
# Reverse the order of duplicte dictionary.
|
|
551
|
+
# Reverse the order of duplicte dictionary. The copy doesn't keep the
|
|
552
|
+
# sort order, if one is set, since that would undo the reversal.
|
|
533
553
|
#
|
|
534
554
|
# Returns [Dictionary].
|
|
535
555
|
#
|
|
536
556
|
def reverse
|
|
537
|
-
dup
|
|
557
|
+
copy = dup
|
|
558
|
+
copy.order_by
|
|
559
|
+
copy.reverse!
|
|
538
560
|
end
|
|
539
561
|
|
|
540
562
|
#
|
|
@@ -636,6 +658,63 @@ module Hashery
|
|
|
636
658
|
@hash
|
|
637
659
|
end
|
|
638
660
|
|
|
661
|
+
private
|
|
662
|
+
|
|
663
|
+
# Marks the value argument of #[]= as not given, since +nil+ and
|
|
664
|
+
# +false+ are values that can be stored.
|
|
665
|
+
NO_VALUE = Object.new.freeze
|
|
666
|
+
private_constant :NO_VALUE
|
|
667
|
+
|
|
668
|
+
#
|
|
669
|
+
# Default proc for the internal hash, which passes the block this
|
|
670
|
+
# dictionary rather than the internal hash, so automatic key entry
|
|
671
|
+
# goes through the dictionary.
|
|
672
|
+
#
|
|
673
|
+
def default_proc_for(blk)
|
|
674
|
+
dict = self
|
|
675
|
+
lambda{ |hsh, key| blk[dict,key] }
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
#
|
|
679
|
+
# Set the sort rule and sort by it. If sorting fails, put back the
|
|
680
|
+
# previous rule before raising, so the dictionary stays readable.
|
|
681
|
+
#
|
|
682
|
+
def sort_with(rule)
|
|
683
|
+
previous, @order_by = @order_by, rule
|
|
684
|
+
begin
|
|
685
|
+
order
|
|
686
|
+
rescue Exception
|
|
687
|
+
@order_by = previous
|
|
688
|
+
raise
|
|
689
|
+
end
|
|
690
|
+
self
|
|
691
|
+
end
|
|
692
|
+
|
|
693
|
+
#
|
|
694
|
+
# While a sort rule is set, raise before storing an entry the rule
|
|
695
|
+
# can't place among the others, since every read would fail after.
|
|
696
|
+
# Comparing with one other entry is enough to catch a +nil+, or a key
|
|
697
|
+
# of a type that doesn't compare with the rest, without sorting on
|
|
698
|
+
# every write.
|
|
699
|
+
#
|
|
700
|
+
def check_order(key, value)
|
|
701
|
+
return unless @order_by
|
|
702
|
+
i = @order.index{ |k| !k.eql?(key) }
|
|
703
|
+
return unless i
|
|
704
|
+
other = @order[i]
|
|
705
|
+
[[key, value], [other, @hash[other]]].sort_by(&@order_by)
|
|
706
|
+
nil
|
|
707
|
+
end
|
|
708
|
+
|
|
709
|
+
#
|
|
710
|
+
# Remove +key+ from the order, matching it as the hash does (with
|
|
711
|
+
# #eql?), so a key like 1.0 isn't taken for 1.
|
|
712
|
+
#
|
|
713
|
+
def remove_from_order(key)
|
|
714
|
+
i = @order.index{ |k| k.eql?(key) }
|
|
715
|
+
@order.delete_at(i) if i
|
|
716
|
+
end
|
|
717
|
+
|
|
639
718
|
end
|
|
640
719
|
|
|
641
720
|
end
|
data/lib/hashery/version.rb
CHANGED
data/test/case_dictionary.rb
CHANGED
|
@@ -217,30 +217,38 @@ testcase Dictionary do
|
|
|
217
217
|
end
|
|
218
218
|
|
|
219
219
|
method :each_key do
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
220
|
+
test do
|
|
221
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
222
|
+
d.order_by_key
|
|
223
|
+
a = []
|
|
224
|
+
d.each_key{ |k| a << k }
|
|
225
|
+
a.assert == [:a, :b, :c]
|
|
226
|
+
end
|
|
225
227
|
end
|
|
226
228
|
|
|
227
229
|
method :each_value do
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
230
|
+
test do
|
|
231
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
232
|
+
d.order_by_value
|
|
233
|
+
a = []
|
|
234
|
+
d.each_value{ |v| a << v }
|
|
235
|
+
a.assert == [1, 2, 3]
|
|
236
|
+
end
|
|
233
237
|
end
|
|
234
238
|
|
|
235
239
|
method :clear do
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
240
|
+
test do
|
|
241
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
242
|
+
d.clear
|
|
243
|
+
d.to_a.assert == []
|
|
244
|
+
end
|
|
239
245
|
end
|
|
240
246
|
|
|
241
247
|
method :fetch do
|
|
242
|
-
|
|
243
|
-
|
|
248
|
+
test do
|
|
249
|
+
d = Dictionary[:a=>1, :b=>2, :c=>3]
|
|
250
|
+
d.fetch(:a).assert == 1
|
|
251
|
+
end
|
|
244
252
|
end
|
|
245
253
|
|
|
246
254
|
method :key? do
|
|
@@ -322,6 +330,253 @@ testcase Dictionary do
|
|
|
322
330
|
s = d[:foo]
|
|
323
331
|
s.class.assert == Dictionary
|
|
324
332
|
end
|
|
333
|
+
|
|
334
|
+
test "copy fills itself, not the original" do
|
|
335
|
+
d = Dictionary.auto
|
|
336
|
+
d[:a][:b] = 1
|
|
337
|
+
c = d.dup
|
|
338
|
+
c[:x][:y] = 2
|
|
339
|
+
d.keys.assert == [:a]
|
|
340
|
+
c.keys.assert == [:a, :x]
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# Sorting entries that don't compare (issue #7).
|
|
345
|
+
|
|
346
|
+
method :order_by_key do
|
|
347
|
+
test "with a nil key raises and leaves the dictionary usable" do
|
|
348
|
+
d = Dictionary.new
|
|
349
|
+
d[0] = 1
|
|
350
|
+
d[nil] = 2
|
|
351
|
+
ArgumentError.assert.raised? { d.order_by_key }
|
|
352
|
+
d.keys.assert == [0, nil]
|
|
353
|
+
d.inspect.assert == "{0=>1, nil=>2}"
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
test "with keys of different types raises and leaves the dictionary usable" do
|
|
357
|
+
d = Dictionary[:b, 1, 'a', 2]
|
|
358
|
+
ArgumentError.assert.raised? { d.order_by_key }
|
|
359
|
+
d.keys.assert == [:b, 'a']
|
|
360
|
+
end
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
method :order_by_value do
|
|
364
|
+
test "with a nil value raises and leaves the dictionary usable" do
|
|
365
|
+
d = Dictionary[:a, 2, :b, nil]
|
|
366
|
+
ArgumentError.assert.raised? { d.order_by_value }
|
|
367
|
+
d.values.assert == [2, nil]
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
method :order_by do
|
|
372
|
+
test "keeps the previous order if the new one fails" do
|
|
373
|
+
d = Dictionary[:b, 1, :a, nil]
|
|
374
|
+
d.order_by_key
|
|
375
|
+
ArgumentError.assert.raised? { d.order_by{ |k,v| v } }
|
|
376
|
+
d[:c] = 3
|
|
377
|
+
d.keys.assert == [:a, :b, :c]
|
|
378
|
+
end
|
|
379
|
+
|
|
380
|
+
test "without a block stops sorting" do
|
|
381
|
+
d = Dictionary[:b, 1, :a, 2].order_by_key
|
|
382
|
+
d.order_by
|
|
383
|
+
d[:"0"] = 3
|
|
384
|
+
d.keys.assert == [:a, :b, :"0"]
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
method :store do
|
|
389
|
+
test "refuses a key the sort order can't place" do
|
|
390
|
+
d = Dictionary.new.order_by_key
|
|
391
|
+
d.store(0, 1)
|
|
392
|
+
ArgumentError.assert.raised? { d.store(nil, 2) }
|
|
393
|
+
d.keys.assert == [0]
|
|
394
|
+
d.refute.key?(nil)
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
test "refuses a value the sort order can't place" do
|
|
398
|
+
d = Dictionary.new.order_by_value
|
|
399
|
+
d.store(:a, 1)
|
|
400
|
+
d.store(:b, 2)
|
|
401
|
+
ArgumentError.assert.raised? { d.store(:a, nil) }
|
|
402
|
+
d.to_a.assert == [[:a, 1], [:b, 2]]
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
method :push do
|
|
407
|
+
test "refuses a key the sort order can't place" do
|
|
408
|
+
d = Dictionary[0, 1].order_by_key
|
|
409
|
+
ArgumentError.assert.raised? { d.push(:a, 2) }
|
|
410
|
+
d.keys.assert == [0]
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
method :unshift do
|
|
415
|
+
test "refuses a key the sort order can't place" do
|
|
416
|
+
d = Dictionary[0, 1].order_by_key
|
|
417
|
+
ArgumentError.assert.raised? { d.unshift(:a, 2) }
|
|
418
|
+
d.keys.assert == [0]
|
|
419
|
+
end
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
method :insert do
|
|
423
|
+
test "refuses a key the sort order can't place" do
|
|
424
|
+
d = Dictionary[0, 1].order_by_key
|
|
425
|
+
ArgumentError.assert.raised? { d.insert(0, :a, 2) }
|
|
426
|
+
d.keys.assert == [0]
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Other fixes.
|
|
431
|
+
|
|
432
|
+
method :insert do
|
|
433
|
+
test "moves an existing key" do
|
|
434
|
+
d = Dictionary['a', 1, 'b', 2, 'c', 3]
|
|
435
|
+
d.insert(0, 'c', 9)
|
|
436
|
+
d.keys.assert == ['c', 'a', 'b']
|
|
437
|
+
d['c'].assert == 9
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
test "past the end adds at the end" do
|
|
441
|
+
d = Dictionary['a', 1, 'b', 2]
|
|
442
|
+
d.insert(5, 'c', 3)
|
|
443
|
+
d.keys.assert == ['a', 'b', 'c']
|
|
444
|
+
end
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
method :[]= do
|
|
448
|
+
test "with an index and a nil value" do
|
|
449
|
+
d = Dictionary[:a, 1]
|
|
450
|
+
d[:b, 0] = nil
|
|
451
|
+
d.to_a.assert == [[:b, nil], [:a, 1]]
|
|
452
|
+
end
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
method :delete do
|
|
456
|
+
test "matches keys as the hash does" do
|
|
457
|
+
d = Dictionary[1, :a, 1.0, :b]
|
|
458
|
+
d.delete(1)
|
|
459
|
+
d.keys.assert == [1.0]
|
|
460
|
+
d.length.assert == 1
|
|
461
|
+
end
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
method :shift do
|
|
465
|
+
test "when empty" do
|
|
466
|
+
Dictionary.new.shift.assert == nil
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
test "with a nil key" do
|
|
470
|
+
d = Dictionary[nil, 1, :a, 2]
|
|
471
|
+
d.shift.assert == [nil, 1]
|
|
472
|
+
d.keys.assert == [:a]
|
|
473
|
+
end
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
method :pop do
|
|
477
|
+
test "when empty" do
|
|
478
|
+
Dictionary.new.pop.assert == nil
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
test "with a nil key" do
|
|
482
|
+
d = Dictionary[:a, 1, nil, 2]
|
|
483
|
+
d.pop.assert == [nil, 2]
|
|
484
|
+
d.keys.assert == [:a]
|
|
485
|
+
end
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
method :reject! do
|
|
489
|
+
test "removes entries in place" do
|
|
490
|
+
d = Dictionary[:a, 1, :b, 2]
|
|
491
|
+
d.reject!{ |k,v| k == :a }.assert.equal?(d)
|
|
492
|
+
d.to_a.assert == [[:b, 2]]
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
test "returns nil when nothing is removed" do
|
|
496
|
+
d = Dictionary[:a, 1]
|
|
497
|
+
d.reject!{ false }.assert == nil
|
|
498
|
+
end
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
class_method :[] do
|
|
502
|
+
test "copies a hash argument" do
|
|
503
|
+
h = {:a=>1}
|
|
504
|
+
d = Dictionary[h]
|
|
505
|
+
d[:b] = 2
|
|
506
|
+
h.assert == {:a=>1}
|
|
507
|
+
end
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
method :replace do
|
|
511
|
+
test "copies another dictionary's order" do
|
|
512
|
+
a = Dictionary[:x, 1]
|
|
513
|
+
d = Dictionary.new
|
|
514
|
+
d.replace(a)
|
|
515
|
+
d[:y] = 2
|
|
516
|
+
a.keys.assert == [:x]
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
test "keeps the old entries if the sort order can't place the new ones" do
|
|
520
|
+
d = Dictionary[0, 1].order_by_key
|
|
521
|
+
ArgumentError.assert.raised? { d.replace(:a=>1, 2=>3) }
|
|
522
|
+
d.to_a.assert == [[0, 1]]
|
|
523
|
+
end
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
method :dup do
|
|
527
|
+
test "keeps the sort order" do
|
|
528
|
+
d = Dictionary.new.order_by{ |k,v| -v }
|
|
529
|
+
d[:a] = 1
|
|
530
|
+
d[:b] = 2
|
|
531
|
+
c = d.dup
|
|
532
|
+
c[:c] = 3
|
|
533
|
+
c.keys.assert == [:c, :b, :a]
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
test "keeps the default" do
|
|
537
|
+
Dictionary.new(0).dup[:x].assert == 0
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
test "is independent of the original" do
|
|
541
|
+
d = Dictionary[:a, 1]
|
|
542
|
+
c = d.dup
|
|
543
|
+
c[:b] = 2
|
|
544
|
+
c.delete(:a)
|
|
545
|
+
d.to_a.assert == [[:a, 1]]
|
|
546
|
+
end
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
method :clone do
|
|
550
|
+
test "is independent of the original" do
|
|
551
|
+
d = Dictionary[:a, 1]
|
|
552
|
+
c = d.clone
|
|
553
|
+
c[:b] = 2
|
|
554
|
+
d.keys.assert == [:a]
|
|
555
|
+
end
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
method :merge do
|
|
559
|
+
test "keeps the sort order" do
|
|
560
|
+
d = Dictionary[:b, 1].order_by_key
|
|
561
|
+
d.merge(:a=>2).keys.assert == [:a, :b]
|
|
562
|
+
end
|
|
563
|
+
end
|
|
564
|
+
|
|
565
|
+
method :reverse do
|
|
566
|
+
test "of a sorted dictionary" do
|
|
567
|
+
d = Dictionary[:a, 1, :b, 2].order_by_key
|
|
568
|
+
d.reverse.keys.assert == [:b, :a]
|
|
569
|
+
d.keys.assert == [:a, :b]
|
|
570
|
+
end
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
method :== do
|
|
574
|
+
test "with a sorted dictionary changed since it was last read" do
|
|
575
|
+
d = Dictionary.new.order_by_key
|
|
576
|
+
d[:b] = 1
|
|
577
|
+
d[:a] = 2
|
|
578
|
+
d.assert == Dictionary[:a, 2, :b, 1]
|
|
579
|
+
end
|
|
325
580
|
end
|
|
326
581
|
|
|
327
582
|
end
|
data/test/case_ini_hash.rb
CHANGED