hashery 2.1.2 → 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.
@@ -217,30 +217,38 @@ testcase Dictionary do
217
217
  end
218
218
 
219
219
  method :each_key do
220
- d = Dictionary[:a=>1, :b=>2, :c=>3]
221
- d.order_by_key
222
- a = []
223
- d.each_key{ |k| a << k }
224
- a.assert == [:a, :b, :c]
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
- d = Dictionary[:a=>1, :b=>2, :c=>3]
229
- d.order_by_value
230
- a = []
231
- d.each_value{ |v| a << v }
232
- a.assert == [1, 2, 3]
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
- d = Dictionary[:a=>1, :b=>2, :c=>3]
237
- d.clear
238
- d.to_a.assert == []
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
- d = Dictionary[:a=>1, :b=>2, :c=>3]
243
- d.fetch(:a).assert == 1
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
@@ -6,6 +6,30 @@ test_case IniHash do
6
6
  test do
7
7
  h = IniHash.new('foo.ini', false)
8
8
  end
9
+
10
+ test "from a hash" do
11
+ h = IniHash.new('a' => '1')
12
+ h['a'].assert == '1'
13
+ end
14
+ end
15
+
16
+ class_method :text do
17
+ test do
18
+ IniHash.text('a' => '1', 'b' => {'c' => '3'}).assert == "a=1\n[b]\nc=3\n"
19
+ end
20
+ end
21
+
22
+ class_method :write_to_file do
23
+ test "with a comment" do
24
+ require 'tmpdir'
25
+ Dir.mktmpdir do |dir|
26
+ path = File.join(dir, 'out.ini')
27
+ IniHash.write_to_file(path, {'a' => '1'}, "one\ntwo\n")
28
+ File.read(path).assert == "# one\n# two\na=1\n"
29
+ IniHash.read_comment_from_file(path).assert == "one\ntwo\n"
30
+ IniHash.read_from_file(path).assert == {'a' => '1'}
31
+ end
32
+ end
9
33
  end
10
34
 
11
35
  method :[]= do
@@ -48,8 +72,10 @@ test_case IniHash do
48
72
  end
49
73
 
50
74
  class_method :load do
51
- h = IniHash.load('test/fixture/example.ini')
52
- h['a'].assert == '1'
75
+ test do
76
+ h = IniHash.load('test/fixture/example.ini')
77
+ h['a'].assert == '1'
78
+ end
53
79
  end
54
80
 
55
81
  end
@@ -2,15 +2,51 @@ require 'helper'
2
2
 
3
3
  test_case LRUHash do
4
4
 
5
+ # Build a full hash of :a => 1, :b => 2, :c => 3, used least recently
6
+ # in that order, i.e. :a is next to be evicted.
7
+ def abc(max_size=3)
8
+ h = LRUHash.new(max_size)
9
+ h[:a] = 1
10
+ h[:b] = 2
11
+ h[:c] = 3
12
+ h
13
+ end
14
+
5
15
  class_method :new do
6
- h = LRUHash.new(10)
7
- LRUHash.assert === h
16
+ test do
17
+ h = LRUHash.new(10)
18
+ LRUHash.assert === h
19
+ end
20
+
21
+ test "max_size is converted with #to_i" do
22
+ LRUHash.new("5").max_size.assert == 5
23
+ LRUHash.new(2.9).max_size.assert == 2
24
+ end
25
+
26
+ test "max_size must be positive" do
27
+ ArgumentError.assert.raised? { LRUHash.new(0) }
28
+ ArgumentError.assert.raised? { LRUHash.new(-1) }
29
+ ArgumentError.assert.raised? { LRUHash.new(nil) }
30
+ end
31
+
32
+ test "default value" do
33
+ h = LRUHash.new(10, :none)
34
+ h[:x].assert == :none
35
+ end
8
36
  end
9
37
 
10
38
  method :max_size= do
11
39
  test do
12
40
  h = LRUHash.new(10)
13
41
  h.max_size = 100
42
+ h.max_size.assert == 100
43
+ end
44
+
45
+ test "shrinking evicts least recently used entries" do
46
+ h = abc
47
+ h[:a]
48
+ h.max_size = 1
49
+ h.keys.assert == [:a]
14
50
  end
15
51
  end
16
52
 
@@ -18,7 +54,35 @@ test_case LRUHash do
18
54
  test do
19
55
  h = LRUHash.new(10)
20
56
  h.store(:a, 1)
21
- h[:a].assert == 1
57
+ h[:a].assert == 1
58
+ end
59
+
60
+ test "returns the value" do
61
+ h = LRUHash.new(10)
62
+ h.store(:a, 1).assert == 1
63
+ end
64
+
65
+ test "evicts the least recently used entry when full" do
66
+ h = abc
67
+ h.store(:d, 4)
68
+ h.keys.assert == [:d, :c, :b]
69
+ end
70
+
71
+ test "overwriting a key does not evict" do
72
+ h = abc
73
+ h.store(:a, 10)
74
+ h.size.assert == 3
75
+ h.keys.assert == [:a, :c, :b]
76
+ h[:a].assert == 10
77
+ end
78
+
79
+ test "unfrozen String keys are copied and frozen" do
80
+ h = LRUHash.new(10)
81
+ k = String.new("k")
82
+ h.store(k, 1)
83
+ k << "x"
84
+ h.keys.assert == ["k"]
85
+ h.keys.first.assert.frozen?
22
86
  end
23
87
  end
24
88
 
@@ -33,6 +97,64 @@ test_case LRUHash do
33
97
  h = LRUHash.new(10){ |h,k| h[k] = 0 }
34
98
  h[:a].assert == 0
35
99
  end
100
+
101
+ test "marks the entry most recently used" do
102
+ h = abc
103
+ h[:a]
104
+ h[:d] = 4
105
+ h.keys.assert == [:d, :a, :c]
106
+ end
107
+
108
+ test "nil values are found" do
109
+ h = LRUHash.new(10, :default)
110
+ h[:a] = nil
111
+ h[:a].assert == nil
112
+ end
113
+ end
114
+
115
+ method :fetch do
116
+ test do
117
+ h = LRUHash.new(10)
118
+ h[:a] = 1
119
+ h.fetch(:a).assert == 1
120
+ end
121
+
122
+ test "missing key raises KeyError" do
123
+ h = LRUHash.new(10)
124
+ KeyError.assert.raised? { h.fetch(:x) }
125
+ end
126
+
127
+ test "KeyError names the key and receiver" do
128
+ h = LRUHash.new(10)
129
+ e = (h.fetch(:x) rescue $!)
130
+ KeyError.assert === e
131
+ e.message.assert == "key not found: :x"
132
+ e.key.assert == :x
133
+ e.receiver.assert.equal?(h)
134
+ end
135
+
136
+ test "missing key with block" do
137
+ h = LRUHash.new(10)
138
+ h.fetch(:x){ |k| "no #{k}" }.assert == "no x"
139
+ end
140
+
141
+ test "missing key with default argument" do
142
+ h = LRUHash.new(10)
143
+ h.fetch(:x, 0).assert == 0
144
+ h.fetch(:x, nil).assert == nil
145
+ end
146
+
147
+ test "ignores the hash default" do
148
+ h = LRUHash.new(10, :default)
149
+ KeyError.assert.raised? { h.fetch(:x) }
150
+ end
151
+
152
+ test "marks the entry most recently used" do
153
+ h = abc
154
+ h.fetch(:a)
155
+ h[:d] = 4
156
+ h.keys.assert == [:d, :a, :c]
157
+ end
36
158
  end
37
159
 
38
160
  method :empty? do
@@ -42,6 +164,13 @@ test_case LRUHash do
42
164
  end
43
165
  end
44
166
 
167
+ method :size do
168
+ test do
169
+ abc.size.assert == 3
170
+ abc.length.assert == 3
171
+ end
172
+ end
173
+
45
174
  method :key do
46
175
  test do
47
176
  h = LRUHash.new(10)
@@ -56,6 +185,12 @@ test_case LRUHash do
56
185
  h[:a] = 1
57
186
  h.keys.assert == [:a]
58
187
  end
188
+
189
+ test "most recently used first" do
190
+ h = abc
191
+ h[:a]
192
+ h.keys.assert == [:a, :c, :b]
193
+ end
59
194
  end
60
195
 
61
196
  method :values do
@@ -64,6 +199,12 @@ test_case LRUHash do
64
199
  h[:a] = 1
65
200
  h.values.assert == [1]
66
201
  end
202
+
203
+ test "most recently used first" do
204
+ h = abc
205
+ h[:a]
206
+ h.values.assert == [1, 3, 2]
207
+ end
67
208
  end
68
209
 
69
210
  method :values_at do
@@ -73,6 +214,12 @@ test_case LRUHash do
73
214
  h[:b] = 2
74
215
  h.values_at(:a).assert == [1]
75
216
  end
217
+
218
+ test "missing keys give the default" do
219
+ h = LRUHash.new(10, :none)
220
+ h[:a] = 1
221
+ h.values_at(:a, :x).assert == [1, :none]
222
+ end
76
223
  end
77
224
 
78
225
  method :has_key? do
@@ -80,6 +227,17 @@ test_case LRUHash do
80
227
  h = LRUHash.new(10)
81
228
  h[:a] = 1
82
229
  h.assert.has_key?(:a)
230
+ h.assert.key?(:a)
231
+ h.assert.member?(:a)
232
+ h.assert.include?(:a)
233
+ h.refute.has_key?(:b)
234
+ end
235
+
236
+ test "does not affect LRU order" do
237
+ h = abc
238
+ h.has_key?(:a)
239
+ h[:d] = 4
240
+ h.refute.has_key?(:a)
83
241
  end
84
242
  end
85
243
 
@@ -88,6 +246,7 @@ test_case LRUHash do
88
246
  h = LRUHash.new(10)
89
247
  h[:a] = 1
90
248
  h.assert.has_value?(1)
249
+ h.refute.has_value?(2)
91
250
  end
92
251
  end
93
252
 
@@ -97,6 +256,7 @@ test_case LRUHash do
97
256
  h[:a] = 1
98
257
  h[:b] = 2
99
258
  h.assoc(:a).assert == [:a,1]
259
+ h.assoc(:x).assert == nil
100
260
  end
101
261
  end
102
262
 
@@ -106,6 +266,26 @@ test_case LRUHash do
106
266
  h[:a] = 1
107
267
  h[:b] = 2
108
268
  h.rassoc(1).assert == [:a,1]
269
+ h.rassoc(9).assert == nil
270
+ end
271
+ end
272
+
273
+ method :each do
274
+ test "most recently used first" do
275
+ h = abc
276
+ h[:a]
277
+ h.to_a.assert == [[:a, 1], [:c, 3], [:b, 2]]
278
+ end
279
+
280
+ test "returns an enumerator without a block" do
281
+ Enumerator.assert === abc.each
282
+ abc.each.size.assert == 3
283
+ end
284
+
285
+ test "allows modification while iterating" do
286
+ h = abc
287
+ h.each { |k, v| h[k] = v * 10 }
288
+ h.values.sort.assert == [10, 20, 30]
109
289
  end
110
290
  end
111
291
 
@@ -145,8 +325,18 @@ test_case LRUHash do
145
325
  test do
146
326
  h = LRUHash.new(10)
147
327
  h[:a] = 1
148
- h.delete(:a)
149
- h.assert.empty?
328
+ h.delete(:a).assert == 1
329
+ h.assert.empty?
330
+ end
331
+
332
+ test "missing key returns nil" do
333
+ h = LRUHash.new(10)
334
+ h.delete(:a).assert == nil
335
+ end
336
+
337
+ test "missing key with block" do
338
+ h = LRUHash.new(10)
339
+ h.delete(:a){ |k| "no #{k}" }.assert == "no a"
150
340
  end
151
341
  end
152
342
 
@@ -155,7 +345,87 @@ test_case LRUHash do
155
345
  h = LRUHash.new(10)
156
346
  h[:a] = 1
157
347
  h.delete_if{ |k,v| k == :a }
158
- h.assert.empty?
348
+ h.assert.empty?
349
+ end
350
+
351
+ test "returns self" do
352
+ h = abc
353
+ h.delete_if{ |k,v| v.odd? }.assert.equal?(h)
354
+ h.keys.assert == [:b]
355
+ end
356
+
357
+ test "returns an enumerator without a block" do
358
+ Enumerator.assert === abc.delete_if
359
+ end
360
+ end
361
+
362
+ method :release_proc= do
363
+ test "called for evicted, deleted and cleared entries" do
364
+ log = []
365
+ h = LRUHash.new(2)
366
+ h.release_proc = lambda { |k, v| log << [k, v] }
367
+ h[:a] = 1
368
+ h[:b] = 2
369
+ h[:c] = 3 # evicts :a
370
+ h.delete(:b)
371
+ h[:d] = 4
372
+ h.clear # least recently used first
373
+ log.assert == [[:a, 1], [:b, 2], [:c, 3], [:d, 4]]
374
+ end
375
+
376
+ test "called when shrinking and in delete_if" do
377
+ log = []
378
+ h = abc
379
+ h.release_proc = lambda { |k, v| log << k }
380
+ h.max_size = 2
381
+ h.delete_if { |k, v| k == :c }
382
+ log.assert == [:a, :c]
383
+ end
384
+
385
+ test "not called when a value is overwritten" do
386
+ log = []
387
+ h = LRUHash.new(2)
388
+ h.release_proc = lambda { |k, v| log << k }
389
+ h[:a] = 1
390
+ h[:a] = 2
391
+ log.assert == []
392
+ end
393
+ end
394
+
395
+ method :dup do
396
+ test "copy is independent of the original" do
397
+ a = abc
398
+ b = a.dup
399
+ b[:d] = 4
400
+ b.delete(:b)
401
+ a.to_a.assert == [[:c, 3], [:b, 2], [:a, 1]]
402
+ b.to_a.assert == [[:d, 4], [:c, 3]]
403
+ end
404
+
405
+ test "copy keeps LRU order" do
406
+ a = abc
407
+ a[:a]
408
+ a.dup.keys.assert == [:a, :c, :b]
409
+ end
410
+ end
411
+
412
+ method :inspect do
413
+ test "looks like a Hash, most recently used first" do
414
+ h = LRUHash.new(10)
415
+ h[:a] = "x"
416
+ h[nil] = 1
417
+ h.inspect.assert == {nil => 1, :a => "x"}.inspect
418
+ h.to_s.assert == h.inspect
419
+ end
420
+
421
+ test "empty" do
422
+ LRUHash.new(10).inspect.assert == "{}"
423
+ end
424
+ end
425
+
426
+ method :to_h do
427
+ test do
428
+ abc.to_h.assert == {:a => 1, :b => 2, :c => 3}
159
429
  end
160
430
  end
161
431