|
2 | 2 | require "thor/core_ext/ordered_hash"
|
3 | 3 |
|
4 | 4 | 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!" |
7 | 13 | end
|
8 | 14 |
|
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 |
12 | 18 | 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" |
13 | 28 |
|
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] |
16 | 31 | end
|
| 32 | + end |
17 | 33 |
|
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 |
21 | 37 | end
|
22 | 38 |
|
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!" |
25 | 42 | end
|
26 | 43 | end
|
27 | 44 |
|
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 |
35 | 52 | end
|
36 | 53 |
|
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 |
39 | 73 | end
|
| 74 | + end |
40 | 75 |
|
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!"] |
47 | 81 | end
|
48 | 82 |
|
| 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 } |
49 | 91 | it "iterates through the keys and values in order of assignment" do
|
50 | 92 | arr = []
|
51 |
| - @hash.each do |key, value| |
| 93 | + subject.each do |key, value| |
52 | 94 | arr << [key, value]
|
53 | 95 | end
|
54 |
| - expect(arr).to eq([[:foo, "Foo!"], [:bar, "Bar!"], [:baz, "Baz!"], |
55 |
| - [:bop, "Bop!"], [:bat, "Bat!"]]) |
56 |
| - end |
57 | 96 |
|
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!"]]) |
60 | 99 | end
|
| 100 | + end |
61 | 101 |
|
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 |
65 | 105 |
|
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" |
69 | 110 |
|
70 |
| - @hash[:foo] = "Bip!" |
71 |
| - expect(@hash.values).to eq(["Bip!", "Bar!", "Bip!", "Bop!", "Bat!"]) |
| 111 | + subject.merge!(other_hash) |
72 | 112 |
|
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"]) |
75 | 114 | end
|
| 115 | + end |
76 | 116 |
|
| 117 | + describe "#merge" do |
77 | 118 | it "appends another ordered hash while preserving ordering" do
|
| 119 | + populate_subject |
| 120 | + |
78 | 121 | other_hash = Thor::CoreExt::OrderedHash.new
|
79 | 122 | other_hash[1] = "one"
|
80 | 123 | other_hash[2] = "two"
|
81 | 124 | 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"]) |
83 | 129 | end
|
84 | 130 |
|
85 | 131 | it "overwrites hash keys with matching appended keys" do
|
| 132 | + populate_subject |
| 133 | + |
86 | 134 | other_hash = Thor::CoreExt::OrderedHash.new
|
87 | 135 | 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!") |
90 | 139 | end
|
| 140 | + end |
91 | 141 |
|
| 142 | + describe "#to_a" do |
| 143 | + before { populate_subject } |
92 | 144 | 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 |
94 | 154 | end
|
95 | 155 |
|
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]) |
98 | 159 | end
|
| 160 | + end |
99 | 161 |
|
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 |
103 | 167 |
|
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!"]) |
106 | 181 |
|
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!"]) |
109 | 187 | end
|
110 | 188 |
|
111 | 189 | 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) |
113 | 196 | end
|
114 | 197 | end
|
115 | 198 | end
|
0 commit comments