C0 code coverage information
Generated on Sun Oct 26 11:18:12 -0500 2008 with rcov 0.8.1.2
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 require File.dirname(__FILE__) + '/../spec_helper'
2 require 'aquarium/spec_example_types'
3 require 'aquarium/extensions/hash'
4 require 'aquarium/utils/array_utils'
5 require 'aquarium/utils/hash_utils'
6 require 'set'
7
8 class CC
9 include Comparable
10 def initialize i
11 @value = i
12 end
13 attr_reader :value
14 def == other
15 value == other.value
16 end
17 def <=>
18 value <=> other.value
19 end
20 end
21
22 def before_hash_spec
23 @c1 = CC.new(1)
24 @c2 = CC.new(2)
25 @c3 = CC.new(3)
26 @cc1 = [@c1, @c2]
27 @cc2 = [@c2, @c3]
28 @hash = {:a => ['a1'], :b => [:b1, :b2], :c => @cc1}
29 end
30
31 describe "intersection of hashes", :shared => true do
32 include Aquarium::Utils::ArrayUtils
33 include Aquarium::Utils::HashUtils
34
35 before(:each) do
36 before_hash_spec
37 end
38
39 it "should return the same hash if intersected with itself." do
40 @hash.and(@hash).should == @hash
41 end
42
43 it "should return the same hash if intersected with an equivalent hash." do
44 @hash.and({:a => ['a1'], :b => [:b1, :b2], :c => @cc1}).should == @hash
45 end
46
47 it "should return an empty hash if one of the input hashes is empty." do
48 {}.and(@hash).should == {}
49 end
50
51 it "should return the common subset hash for two, if the values respond to #&." do
52 hash2 = {:b => [:b1], :c => @cc2, :d => ['d']}
53 @hash.and(hash2).should == {:b => [:b1], :c => [@c2]}
54 end
55
56 it "should return the common subset of hash values for partially-overlapping keys as specified by a given block." do
57 hash2 = {:b =>:b1, :c => @cc2, :d => 'd'}
58 @hash.and(hash2){|value1, value2| Set.new(make_array(value1)).intersection(Set.new(make_array(value2)))}.should == {:b => Set.new([:b1]), :c => Set.new([@c2])}
59 end
60 end
61
62 describe Hash, "#intersection" do
63 it_should_behave_like "intersection of hashes"
64 end
65
66 describe Hash, "#and" do
67 it_should_behave_like "intersection of hashes"
68 end
69
70 describe Hash, "#&" do
71 it_should_behave_like "intersection of hashes"
72
73 it "should support operator-style semantics" do
74 ({:a => ['a1', 'a2'], :c => @cc1} & {:a => ['a1'], :b => [:b1, :b2], :c => @cc2}).should == {:a => ['a1'], :c => [@c2]}
75 end
76 end
77
78 describe "union of hashes", :shared => true do
79 include Aquarium::Utils::ArrayUtils
80 include Aquarium::Utils::HashUtils
81
82 before(:each) do
83 before_hash_spec
84 end
85
86 it "should return the same hash if unioned with itself." do
87 @hash.union(@hash).should == @hash
88 end
89
90 it "should return the same hash if unioned with an equivalent hash." do
91 @hash.union({:a => ['a1'], :b => [:b1, :b2], :c => @cc1}).should == @hash
92 end
93
94 it "should return a hash that is equivalent to the non-empty hash if the other hash is empty." do
95 {}.union(@hash).should == @hash
96 @hash.union({}).should == @hash
97 end
98
99 it "should return the same hash if unioned with nil." do
100 @hash.union(nil).should == @hash
101 end
102
103 it "should return the combined hash value, if the values respond to #|." do
104 hash2 = {:b => [:b3], :c => @cc2, :d => ['d']}
105 @hash.union(hash2).should == {:a => ['a1'], :b => [:b1, :b2, :b3], :c => [@c1, @c2, @c3], :d => ['d']}
106 end
107
108 it "should return a hash equivalent to the output of Hash#merge for two, non-equivalent hashes, with no block given and values don't respond to #|." do
109 hash2 = {:b => :b3, :c => @cc2, :d => 'd'}
110 @hash.union(hash2).should == {:a => ['a1'], :b => :b3, :c => [@c1, @c2, @c3], :d => 'd'}
111 end
112
113 it "should return the combined hash values as specified by a given block." do
114 hash2 = {:b => :b3, :c => @cc2, :d => 'd'}
115 @hash.union(hash2){|value1, value2| Set.new(make_array(value1)).union(Set.new(make_array(value2)))}.should == {:a => Set.new(['a1']), :b => Set.new([:b1, :b2, :b3]), :c => Set.new([@c1, @c2, @c3]), :d => Set.new(['d'])}
116 end
117 end
118
119 describe Hash, "#union" do
120 it_should_behave_like "union of hashes"
121 end
122
123 describe Hash, "#or" do
124 it_should_behave_like "union of hashes"
125 end
126
127 describe Hash, "#|" do
128 it_should_behave_like "union of hashes"
129
130 it "should support operator-style semantics" do
131 ({:a => ['a1'], :d => ['d']} | {:a => ['a2'], :b => [:b1, :b2], :c => @cc2}).should == {:a => ['a1', 'a2'], :b => [:b1, :b2], :c => @cc2, :d => ['d']}
132 end
133 end
134
135
136 describe "subtraction of hashes", :shared => true do
137 include Aquarium::Utils::ArrayUtils
138 include Aquarium::Utils::HashUtils
139
140 before(:each) do
141 before_hash_spec
142 # override value:
143 @hash = {:a => ['a1', 'a2'], :b => [:b1, :b2], :c => @cc1}
144 end
145
146 it "should return an empty hash if subtracted from itself." do
147 (@hash - @hash).should be_empty
148 end
149
150 it "should return an empty hash if an equivalent hash is subtracted from it." do
151 (@hash - {:a => ['a1', 'a2'], :b => [:b1, :b2], :c => @cc1}).should be_empty
152 end
153
154 it "should return an equivalent hash if the second hash is empty." do
155 (@hash - {}).should == @hash
156 end
157
158 it "should return an equivalent hash if the second hash is nil." do
159 (@hash - nil).should == @hash
160 end
161
162 it "should return an empty hash if the first hash is empty, independent of the second hash." do
163 ({} - @hash).should be_empty
164 end
165
166 it "should return a hash with all keys in the second hash removed, independent of the corresponding values, if no block is given." do
167 hash2 = {:b =>:b3, :c => 'c', :d => 'd'}
168 (@hash - hash2).should == {:a => ['a1', 'a2']}
169 end
170
171 it "should return a hash with the values subtraced for partially-overlapping keys as specified by a given block." do
172 hash2 = {:b =>:b2, :c => @cc2, :d => 'd'}
173 @hash.minus(hash2) do |value1, value2|
174 Set.new(make_array(value1)) - Set.new(make_array(value2))
175 end.should == {:a => Set.new(['a1', 'a2']), :b => Set.new([:b1]), :c => Set.new([@c1])}
176 end
177
178 it "should be not associative." do
179 hash1 = {:a => :a1, :b =>:b1, :c => :c1, :d => :d1}
180 hash2 = {:b =>:b3, :c => 'c'}
181 hash3 = {:a =>:a4, :c => 'c'}
182 ((hash1 - hash2) - hash3).should == {:d => :d1}
183 (hash1 - (hash2 - hash3)).should == {:a => :a1, :c => :c1, :d => :d1}
184 end
185 end
186
187 describe Hash, "#minus" do
188 it_should_behave_like "subtraction of hashes"
189 end
190
191 describe Hash, "#-" do
192 it_should_behave_like "subtraction of hashes"
193
194 it "should support operator-style semantics" do
195 hash1 = {:a => :a1, :b =>:b1, :c => :c1, :d => :d1}
196 hash2 = {:b =>:b3, :c => 'c'}
197 hash3 = {:a =>:a4, :c => 'c'}
198 ((hash1 - hash2) - hash3).should == {:d => :d1}
199 (hash1 - (hash2 - hash3)).should == {:a => :a1, :c => :c1, :d => :d1}
200 end
201 end
202
203 describe Hash, "#eql_when_keys_compared?" do
204 include Aquarium::Utils::ArrayUtils
205 include Aquarium::Utils::HashUtils
206
207 it "should return true when comparing a hash to itself." do
208 h1={"1" => :a1, "2" => :a2, "3" => :a3}
209 h1.eql_when_keys_compared?(h1).should == true
210 end
211
212 it "should return true for hashes with string keys that satisfy String#==." do
213 h1={"1" => :a1, "2" => :a2, "3" => :a3}
214 h2={"1" => :a1, "2" => :a2, "3" => :a3}
215 h1.eql_when_keys_compared?(h2).should == true
216 end
217
218 it "should return false for hashes with matching keys, but different values." do
219 h1={"1" => :a1, "2" => :a2, "3" => /a/}
220 h2={"1" => :a1, "2" => :a2, "3" => /b/}
221 h1.eql_when_keys_compared?(h2).should == false
222 end
223
224 it "should return false for hashes where one hash is a subset of the other." do
225 h1={"1" => :a1, "2" => :a2}
226 h2={"1" => :a1, "2" => :a2, "3" => :a3}
227 h1.eql_when_keys_compared?(h2).should == false
228 h2.eql_when_keys_compared?(h1).should == false
229 end
230
231 it "should return true for hashes with Object keys that define a #<=> method, while Hash#eql? would return false." do
232 class Key
233 def initialize key
234 @key = key
235 end
236 attr_reader :key
237 def eql? other
238 key.eql? other.key
239 end
240 def <=> other
241 key <=> other.key
242 end
243 end
244
245 h1 = {}; h2 = {}
246 (0...4).each do |index|
247 h1[Key.new(index)] = {index.to_s => [index, index+1]}
248 h2[Key.new(index)] = {index.to_s => [index, index+1]}
249 end
250 h1.eql_when_keys_compared?(h2).should == true
251 h1.eql?(h2).should == false
252 end
253 end
254
255 describe Hash, "#equivalent_key" do
256 it "should return the key in the hash for which input_value#==(key) is true." do
257 class Key
258 def initialize key
259 @key = key
260 end
261 attr_reader :key
262 def eql? other
263 key.eql? other.key
264 end
265 alias :== :eql?
266 end
267
268 h1 = {}; h2 = {}
269 (0...4).each do |index|
270 h1[Key.new(index)] = {index.to_s => [index, index+1]}
271 h2[Key.new(index)] = {index.to_s => [index, index+1]}
272 end
273 h1[Key.new(0)].should be_nil
274 h1.equivalent_key(Key.new(0)).should_not be_nil
275 h1.equivalent_key(Key.new(5)).should be_nil
276 end
277
278 end
Generated using the rcov code coverage analysis tool for Ruby version 0.8.1.2.