hashery 2.1.2 → 2.2.2
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 +5 -5
- data/HISTORY.md +77 -0
- data/README.md +59 -42
- data/lib/hashery/core_ext.rb +1 -1
- data/lib/hashery/crud_hash.rb +2 -2
- data/lib/hashery/dictionary.rb +125 -46
- data/lib/hashery/fuzzy_hash.rb +1 -1
- data/lib/hashery/ini_hash.rb +4 -4
- data/lib/hashery/linked_list.rb +1 -1
- data/lib/hashery/lru_hash.rb +167 -160
- data/lib/hashery/open_hash.rb +2 -2
- data/lib/hashery/path_hash.rb +120 -46
- data/lib/hashery/static_hash.rb +2 -0
- data/lib/hashery/version.rb +3 -0
- data/lib/hashery.rb +1 -3
- data/test/case_association.rb +2 -2
- data/test/case_dictionary.rb +270 -15
- data/test/case_ini_hash.rb +28 -2
- data/test/case_lru_hash.rb +276 -6
- data/test/case_path_hash.rb +225 -0
- metadata +16 -72
- data/.index +0 -78
- data/Index.yml +0 -52
- data/lib/hashery.yml +0 -78
data/test/case_lru_hash.rb
CHANGED
|
@@ -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
|
-
|
|
7
|
-
|
|
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
|
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
require 'helper'
|
|
2
|
+
require 'hashery/path_hash'
|
|
3
|
+
|
|
4
|
+
test_case PathHash do
|
|
5
|
+
|
|
6
|
+
class_method :[] do
|
|
7
|
+
test "makes a PathHash from a Hash" do
|
|
8
|
+
s = PathHash['a' => 'b', 'c' => {'d' => :e}]
|
|
9
|
+
s.flat.assert == {'a' => 'b', 'c/d' => :e}
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class_method :new do
|
|
14
|
+
test "flattens nested hashes" do
|
|
15
|
+
s = PathHash.new('a' => 'b', 'c' => {'d' => :e})
|
|
16
|
+
s.flat.assert == {'a' => 'b', 'c/d' => :e}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
test "collects slashed values from an array" do
|
|
20
|
+
s = PathHash.new(['a/b', 'a/c'])
|
|
21
|
+
s.flat.assert == {'a' => ['b', 'c']}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
test "copies another PathHash" do
|
|
25
|
+
s = PathHash.new('c' => {'d' => :e})
|
|
26
|
+
PathHash.new(s).flat.assert == {'c/d' => :e}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
method :[] do
|
|
31
|
+
test "reads top-level and slashed keys" do
|
|
32
|
+
s = PathHash['a' => 'b', 'c' => {'d' => :e}]
|
|
33
|
+
s['a'].assert == 'b'
|
|
34
|
+
s['c/d'].assert == :e
|
|
35
|
+
s['c']['d'].assert == :e
|
|
36
|
+
s['c'].assert.is_a?(PathHash)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test "returns nil for a missing key" do
|
|
40
|
+
PathHash['a' => 1]['x'].assert == nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
test "doesn't match keys that only start the same" do
|
|
44
|
+
s = PathHash['ab' => 1, 'a' => {'b' => 2}]
|
|
45
|
+
s['a'].flat.assert == {'b' => 2}
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
test "takes keys literally, not as patterns" do
|
|
49
|
+
s = PathHash['a.b' => 1, 'axb' => {'c' => 2}]
|
|
50
|
+
s['a.b'].assert == 1
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
test "converts keys to strings" do
|
|
54
|
+
PathHash['a' => 1][:a].assert == 1
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
method :[]= do
|
|
59
|
+
test "stores with a slashed key" do
|
|
60
|
+
s = PathHash.new
|
|
61
|
+
s['a/b'] = 1
|
|
62
|
+
s['a']['b'].assert == 1
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
test "flattens a Hash value" do
|
|
66
|
+
s = PathHash.new
|
|
67
|
+
s['a'] = {'b' => 1}
|
|
68
|
+
s.flat.assert == {'a/b' => 1}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
test "replaces what was below the key" do
|
|
72
|
+
s = PathHash['a' => {'b' => 1}]
|
|
73
|
+
s['a'] = 2
|
|
74
|
+
s.flat.assert == {'a' => 2}
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
method :fetch do
|
|
79
|
+
test "reads slashed keys" do
|
|
80
|
+
s = PathHash['c' => {'d' => {'e' => 1}}]
|
|
81
|
+
s.fetch('c/d/e').assert == 1
|
|
82
|
+
s.fetch('c/d').flat.assert == {'e' => 1}
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
test "returns the default or the block's value for a missing key" do
|
|
86
|
+
s = PathHash['a' => 1]
|
|
87
|
+
s.fetch('x', 0).assert == 0
|
|
88
|
+
s.fetch('x'){ |k| k * 2 }.assert == 'xx'
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
test "raises KeyError for a missing key" do
|
|
92
|
+
KeyError.assert.raised? do
|
|
93
|
+
PathHash['a' => 1].fetch('x')
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
method :delete do
|
|
99
|
+
test "removes a key and what is below it" do
|
|
100
|
+
s = PathHash['a' => 1, 'c' => {'d' => 2}]
|
|
101
|
+
s.delete('c').flat.assert == {'d' => 2}
|
|
102
|
+
s.flat.assert == {'a' => 1}
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
test "returns nil, or the block's value, for a missing key" do
|
|
106
|
+
s = PathHash['a' => 1]
|
|
107
|
+
s.delete('x').assert == nil
|
|
108
|
+
s.delete('x'){ |k| "no #{k}" }.assert == 'no x'
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
method :key? do
|
|
113
|
+
test "finds top-level and slashed keys" do
|
|
114
|
+
s = PathHash['a' => 1, 'c' => {'d' => 2}]
|
|
115
|
+
s.assert.key?('a')
|
|
116
|
+
s.assert.key?('c')
|
|
117
|
+
s.assert.key?('c/d')
|
|
118
|
+
s.refute.key?('x')
|
|
119
|
+
s.refute.key?('c/x')
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
method :key do
|
|
124
|
+
test "gives the slashed key of a value" do
|
|
125
|
+
PathHash['c' => {'d' => :e}].key(:e).assert == 'c/d'
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
method :index do
|
|
130
|
+
test "is the same as key" do
|
|
131
|
+
PathHash['c' => {'d' => :e}].index(:e).assert == 'c/d'
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
method :keys do
|
|
136
|
+
test "lists the top-level keys" do
|
|
137
|
+
PathHash['a' => 1, 'c' => {'d' => 2, 'e' => 3}].keys.assert == ['a', 'c']
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
method :size do
|
|
142
|
+
test "counts the top-level keys" do
|
|
143
|
+
PathHash['a' => 1, 'c' => {'d' => 2, 'e' => 3}].size.assert == 2
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
method :each do
|
|
148
|
+
test "yields the top-level keys and their values" do
|
|
149
|
+
s = PathHash['a' => 1, 'c' => {'d' => 2}]
|
|
150
|
+
pairs = []
|
|
151
|
+
s.each{ |k, v| pairs << [k, v] }
|
|
152
|
+
pairs.map(&:first).assert == ['a', 'c']
|
|
153
|
+
pairs[1][1].flat.assert == {'d' => 2}
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
test "returns an Enumerator without a block" do
|
|
157
|
+
PathHash['a' => 1].each.to_a.assert == [['a', 1]]
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
method :expand do
|
|
162
|
+
test "gives back nested hashes" do
|
|
163
|
+
h = {'a' => 'b', 'c' => {'d' => :e}}
|
|
164
|
+
PathHash.new(h).expand.assert == h
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
method :to_string_array do
|
|
169
|
+
test "lists each value with its slashed key" do
|
|
170
|
+
PathHash['a' => 'b', 'c' => {'d' => :e}].to_string_array.assert == ['a/b', 'c/d/e']
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
method :ordered! do
|
|
175
|
+
test "puts the entries in the given order" do
|
|
176
|
+
s = PathHash['a' => {'b' => 'c', 'c' => 'd'}, 'b' => 'c']
|
|
177
|
+
s.ordered!('b', 'a/c', 'a/b')
|
|
178
|
+
s.expand.to_a.assert == [['b', 'c'], ['a', {'c' => 'd', 'b' => 'c'}]]
|
|
179
|
+
s.expand['a'].keys.assert == ['c', 'b']
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
method :ordered do
|
|
184
|
+
test "orders a copy" do
|
|
185
|
+
s = PathHash['a' => 1, 'b' => 2]
|
|
186
|
+
s.ordered('b').keys.assert == ['b', 'a']
|
|
187
|
+
s.keys.assert == ['a', 'b']
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
method :== do
|
|
192
|
+
test "compares entries" do
|
|
193
|
+
s = PathHash['a' => 1, 'c' => {'d' => 2}]
|
|
194
|
+
s.assert == PathHash['a' => 1, 'c' => {'d' => 2}]
|
|
195
|
+
s.refute == PathHash['a' => 1]
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
test "compares with a Hash" do
|
|
199
|
+
s = PathHash['a' => 1, 'c' => {'d' => 2}]
|
|
200
|
+
s.assert == {'a' => 1, 'c' => {'d' => 2}}
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
test "is false for anything else" do
|
|
204
|
+
PathHash['a' => 1].refute == 1
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
method :dup do
|
|
209
|
+
test "makes an independent copy" do
|
|
210
|
+
s = PathHash['a' => 1]
|
|
211
|
+
d = s.dup
|
|
212
|
+
d['a'] = 2
|
|
213
|
+
s['a'].assert == 1
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
method :clear do
|
|
218
|
+
test "removes everything" do
|
|
219
|
+
s = PathHash['a' => 1]
|
|
220
|
+
s.clear.assert.equal?(s)
|
|
221
|
+
s.assert.empty?
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
end
|