Skip to content

Commit 850d71c

Browse files
committed
Add spec for Hash class.
1 parent d68dd7d commit 850d71c

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

spec/hash.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
module Test
3+
eval(File.read(File.expand_path(File.dirname(__FILE__)+'/../lib/core/hash.rb')))
4+
eval(File.read(File.expand_path(File.dirname(__FILE__)+'/../lib/core/hash_ext.rb')))
5+
end
6+
7+
describe Hash do
8+
it "should return inserted items in insertion order" do
9+
h = Test::Hash.new
10+
h["xyz"] = 1
11+
h["abc"] = 2
12+
h["ghi"] = 3
13+
h["def"] = 4
14+
h.to_a.should eq [["xyz",1],["abc",2],["ghi",3],["def",4]]
15+
end
16+
17+
it "should return inserted items in insertion order after a #delete" do
18+
h = Test::Hash.new
19+
h["xyz"] = 1
20+
h["abc"] = 2
21+
h["ghi"] = 3
22+
h["def"] = 4
23+
h.delete("abc")
24+
h.to_a.should eq [["xyz",1],["ghi",3],["def",4]]
25+
end
26+
27+
it "should return inserted items in insertion order after a #delete and re-insert" do
28+
h = Test::Hash.new
29+
h["xyz"] = 1
30+
h["abc"] = 2
31+
h["ghi"] = 3
32+
h["def"] = 4
33+
h.delete("abc")
34+
h["abc"] = 5
35+
h.to_a.should eq [["xyz",1],["ghi",3],["def",4],["abc", 5]]
36+
end
37+
38+
it "after a reinsert with the same key, there should be no Deleted entries" do
39+
h = Test::Hash.new
40+
h["xyz"] = 1
41+
p h._data
42+
h.delete("xyz").should eq 1
43+
p h._state
44+
p h._data
45+
h["xyz"] = 1
46+
p h._state
47+
p h._data
48+
h._data.compact.should eq ["xyz", 1]
49+
h.to_a.should eq [["xyz", 1]]
50+
51+
h["abc"] = 2
52+
# FIXME: The result of this is unstable because
53+
# `#hash` changes from run to run under MRI
54+
#h._data.compact.should eq ["xyz", 1,24, "abc", 2, 0]
55+
h.to_a.should eq [["xyz", 1], ["abc", 2]]
56+
p :xyz_abc
57+
p h._state
58+
p h._data
59+
h.delete("xyz").should eq 1
60+
p :abc
61+
p h._state
62+
p h._data
63+
h.to_a.should eq [["abc", 2]]
64+
h["xyz"] = 3
65+
p :abc_xyz
66+
p h._state
67+
p h._data
68+
h.to_a.should eq [["abc", 2], ["xyz", 3]]
69+
end
70+
end

0 commit comments

Comments
 (0)