Skip to content

Commit 82f2423

Browse files
author
Colby Swandale
committed
refactor ordered_hash_spec to keep with convention of the rest of the suite and added a few new specs
1 parent 4b95e2e commit 82f2423

File tree

1 file changed

+140
-57
lines changed

1 file changed

+140
-57
lines changed

spec/core_ext/ordered_hash_spec.rb

Lines changed: 140 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,197 @@
22
require "thor/core_ext/ordered_hash"
33

44
describe Thor::CoreExt::OrderedHash do
5-
before do
6-
@hash = Thor::CoreExt::OrderedHash.new
5+
subject { Thor::CoreExt::OrderedHash.new }
6+
7+
def populate_subject
8+
subject[:foo] = "Foo!"
9+
subject[:bar] = "Bar!"
10+
subject[:baz] = "Baz!"
11+
subject[:bop] = "Bop!"
12+
subject[:bat] = "Bat!"
713
end
814

9-
describe "without any items" do
10-
it "returns nil for an undefined key" do
11-
expect(@hash["foo"]).to be nil
15+
describe "#initialize" do
16+
it "is empty" do
17+
expect(subject).to be_empty
1218
end
19+
end
20+
21+
describe "#replace" do
22+
before { populate_subject }
23+
it "replaces the keys" do
24+
other_hash = Thor::CoreExt::OrderedHash.new
25+
other_hash[1] = "one"
26+
other_hash[2] = "two"
27+
other_hash[3] = "three"
1328

14-
it "doesn't iterate through any items" do
15-
@hash.each { raise }
29+
subject.replace(other_hash)
30+
expect(subject.keys).to eq [1,2,3]
1631
end
32+
end
1733

18-
it "has an empty key and values list" do
19-
expect(@hash.keys).to be_empty
20-
expect(@hash.values).to be_empty
34+
describe "#[]" do
35+
it "returns nil for an undefined key" do
36+
expect(subject[:boom]).to be nil
2137
end
2238

23-
it "must be empty" do
24-
expect(@hash).to be_empty
39+
before { populate_subject }
40+
it "returns the value for each key" do
41+
expect(subject[:foo]).to eq "Foo!"
2542
end
2643
end
2744

28-
describe "with several items" do
29-
before do
30-
@hash[:foo] = "Foo!"
31-
@hash[:bar] = "Bar!"
32-
@hash[:baz] = "Baz!"
33-
@hash[:bop] = "Bop!"
34-
@hash[:bat] = "Bat!"
45+
describe "#[]=" do
46+
it "does not duplicate keys" do
47+
subject[:key] = 1
48+
subject[:key] = 2
49+
50+
expect(subject.keys.size).to eq 1
51+
expect(subject[:key]).to eq 2
3552
end
3653

37-
it "returns nil for an undefined key" do
38-
expect(@hash[:boom]).to be nil
54+
it "does not move an overwritten node to the end of the ordering" do
55+
populate_subject
56+
57+
subject[:baz] = "Bip!"
58+
expect(subject.values).to eq(["Foo!", "Bar!", "Bip!", "Bop!", "Bat!"])
59+
60+
subject[:foo] = "Bip!"
61+
expect(subject.values).to eq(["Bip!", "Bar!", "Bip!", "Bop!", "Bat!"])
62+
63+
subject[:bat] = "Bip!"
64+
expect(subject.values).to eq(["Bip!", "Bar!", "Bip!", "Bop!", "Bip!"])
65+
end
66+
end
67+
68+
describe "#clear" do
69+
before { populate_subject }
70+
it "clears the keys" do
71+
subject.clear
72+
expect(subject.keys).to be_empty
3973
end
74+
end
4075

41-
it "returns the value for each key" do
42-
expect(@hash[:foo]).to eq("Foo!")
43-
expect(@hash[:bar]).to eq("Bar!")
44-
expect(@hash[:baz]).to eq("Baz!")
45-
expect(@hash[:bop]).to eq("Bop!")
46-
expect(@hash[:bat]).to eq("Bat!")
76+
describe "#shift" do
77+
before { populate_subject }
78+
it "pops the first key/value" do
79+
arr = subject.shift
80+
expect(arr).to eq [:foo, "Foo!"]
4781
end
4882

83+
it "removes the key" do
84+
subject.shift
85+
expect(subject.keys).to_not include(:foo)
86+
end
87+
end
88+
89+
describe "#each" do
90+
before { populate_subject }
4991
it "iterates through the keys and values in order of assignment" do
5092
arr = []
51-
@hash.each do |key, value|
93+
subject.each do |key, value|
5294
arr << [key, value]
5395
end
54-
expect(arr).to eq([[:foo, "Foo!"], [:bar, "Bar!"], [:baz, "Baz!"],
55-
[:bop, "Bop!"], [:bat, "Bat!"]])
56-
end
5796

58-
it "returns the keys in order of insertion" do
59-
expect(@hash.keys).to eq([:foo, :bar, :baz, :bop, :bat])
97+
expect(arr).to eq([[:foo, "Foo!"], [:bar, "Bar!"], [:baz, "Baz!"],
98+
[:bop, "Bop!"], [:bat, "Bat!"]])
6099
end
100+
end
61101

62-
it "returns the values in order of insertion" do
63-
expect(@hash.values).to eq(["Foo!", "Bar!", "Baz!", "Bop!", "Bat!"])
64-
end
102+
describe "#merge!" do
103+
it "modifies the existing object" do
104+
populate_subject
65105

66-
it "does not move an overwritten node to the end of the ordering" do
67-
@hash[:baz] = "Bip!"
68-
expect(@hash.values).to eq(["Foo!", "Bar!", "Bip!", "Bop!", "Bat!"])
106+
other_hash = Thor::CoreExt::OrderedHash.new
107+
other_hash[1] = "one"
108+
other_hash[2] = "two"
109+
other_hash[3] = "three"
69110

70-
@hash[:foo] = "Bip!"
71-
expect(@hash.values).to eq(["Bip!", "Bar!", "Bip!", "Bop!", "Bat!"])
111+
subject.merge!(other_hash)
72112

73-
@hash[:bat] = "Bip!"
74-
expect(@hash.values).to eq(["Bip!", "Bar!", "Bip!", "Bop!", "Bip!"])
113+
expect(subject.values).to eq(["Foo!", "Bar!", "Baz!", "Bop!", "Bat!", "one", "two", "three"])
75114
end
115+
end
76116

117+
describe "#merge" do
77118
it "appends another ordered hash while preserving ordering" do
119+
populate_subject
120+
78121
other_hash = Thor::CoreExt::OrderedHash.new
79122
other_hash[1] = "one"
80123
other_hash[2] = "two"
81124
other_hash[3] = "three"
82-
expect(@hash.merge(other_hash).values).to eq(["Foo!", "Bar!", "Baz!", "Bop!", "Bat!", "one", "two", "three"])
125+
126+
127+
merged_list = subject.merge(other_hash)
128+
expect(merged_list.values).to eq(["Foo!", "Bar!", "Baz!", "Bop!", "Bat!", "one", "two", "three"])
83129
end
84130

85131
it "overwrites hash keys with matching appended keys" do
132+
populate_subject
133+
86134
other_hash = Thor::CoreExt::OrderedHash.new
87135
other_hash[:bar] = "bar"
88-
expect(@hash.merge(other_hash)[:bar]).to eq("bar")
89-
expect(@hash[:bar]).to eq("Bar!")
136+
137+
expect(subject.merge(other_hash)[:bar]).to eq("bar")
138+
expect(subject[:bar]).to eq("Bar!")
90139
end
140+
end
91141

142+
describe "#to_a" do
143+
before { populate_subject }
92144
it "converts to an array" do
93-
expect(@hash.to_a).to eq([[:foo, "Foo!"], [:bar, "Bar!"], [:baz, "Baz!"], [:bop, "Bop!"], [:bat, "Bat!"]])
145+
expect(subject.to_a).to eq([[:foo, "Foo!"], [:bar, "Bar!"], [:baz, "Baz!"], [:bop, "Bop!"], [:bat, "Bat!"]])
146+
end
147+
end
148+
149+
describe "#keys" do
150+
context "when list is unpopulated" do
151+
it "has an empty keys list" do
152+
expect(subject.keys).to be_empty
153+
end
94154
end
95155

96-
it "must not be empty" do
97-
expect(@hash).not_to be_empty
156+
it "returns the keys in order of insertion" do
157+
populate_subject
158+
expect(subject.keys).to eq([:foo, :bar, :baz, :bop, :bat])
98159
end
160+
end
99161

100-
it "deletes values from hash" do
101-
expect(@hash.delete(:baz)).to eq("Baz!")
102-
expect(@hash.values).to eq(["Foo!", "Bar!", "Bop!", "Bat!"])
162+
describe "#values" do
163+
it "returns the values in order of insertion" do
164+
populate_subject
165+
expect(subject.values).to eq(["Foo!", "Bar!", "Baz!", "Bop!", "Bat!"])
166+
end
103167

104-
expect(@hash.delete(:foo)).to eq("Foo!")
105-
expect(@hash.values).to eq(["Bar!", "Bop!", "Bat!"])
168+
context "when list is unpopulated" do
169+
it "has an empty list" do
170+
list = described_class.new
171+
expect(list.values).to be_empty
172+
end
173+
end
174+
end
175+
176+
describe "#delete" do
177+
before { populate_subject }
178+
it "deletes the value given the key" do
179+
expect(subject.delete(:baz)).to eq("Baz!")
180+
expect(subject.values).to eq(["Foo!", "Bar!", "Bop!", "Bat!"])
106181

107-
expect(@hash.delete(:bat)).to eq("Bat!")
108-
expect(@hash.values).to eq(["Bar!", "Bop!"])
182+
expect(subject.delete(:foo)).to eq("Foo!")
183+
expect(subject.values).to eq(["Bar!", "Bop!", "Bat!"])
184+
185+
expect(subject.delete(:bat)).to eq("Bat!")
186+
expect(subject.values).to eq(["Bar!", "Bop!"])
109187
end
110188

111189
it "returns nil if the value to be deleted can't be found" do
112-
expect(@hash.delete(:nothing)).to be nil
190+
expect(subject.delete(:nothing)).to be nil
191+
end
192+
193+
it "deletes the given key" do
194+
subject.delete(:baz)
195+
expect(subject.keys).to_not include(:baz)
113196
end
114197
end
115198
end

0 commit comments

Comments
 (0)