Skip to content

Commit 0e43f6e

Browse files
authored
Merge pull request #4 from frank-west-iii/issue-2-nil-no-value
Adds differentiation between nil and NO_VALUE
2 parents 7ba866a + b33395b commit 0e43f6e

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

lib/hash_diff.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
require "hash_diff/comparison"
33

44
module HashDiff
5+
class NO_VALUE; end
6+
57
def self.patch!
68
Hash.class_eval do
79
def diff(right)

lib/hash_diff/comparison.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def combined_keys
3838
end
3939

4040
def equal?(key)
41-
left[key] == right[key]
41+
value_with_default(left, key) == value_with_default(right, key)
4242
end
4343

4444
def hash?(value)
@@ -53,8 +53,15 @@ def report_difference(key, reporter)
5353
if comparable?(key)
5454
self.class.new(left[key], right[key]).find_differences(&reporter)
5555
else
56-
reporter.call(left[key], right[key])
56+
reporter.call(
57+
value_with_default(left, key),
58+
value_with_default(right, key)
59+
)
5760
end
5861
end
62+
63+
def value_with_default(obj, key)
64+
obj.fetch(key, NO_VALUE)
65+
end
5966
end
6067
end

spec/hash_diff/comparison_spec.rb

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,37 @@
22

33
describe HashDiff::Comparison do
44

5-
let(:app_v1_properties) { { foo: 'bar', bar: 'foo', nested: { foo: 'bar', bar: { one: 'foo1' } }, num: 1 } }
6-
let(:app_v2_properties) { { foo: 'bar2', bar: 'foo2', nested: { foo: 'bar2', bar: { two: 'foo2' } }, word: 'monkey' } }
5+
let(:app_v1_properties) {
6+
{
7+
foo: 'bar',
8+
bar: 'foo',
9+
nested: {
10+
foo: 'bar',
11+
bar: {
12+
one: 'foo1'
13+
}
14+
},
15+
num: 1,
16+
word: nil
17+
}
18+
}
19+
let(:app_v2_properties) {
20+
{
21+
foo: 'bar2',
22+
bar: 'foo2',
23+
nested: {
24+
foo: 'bar2',
25+
bar: {
26+
two: 'foo2'
27+
}
28+
},
29+
word: 'monkey'
30+
}
31+
}
732

8-
subject(:comparison) { HashDiff::Comparison.new(app_v1_properties, app_v2_properties) }
33+
subject(:comparison) {
34+
HashDiff::Comparison.new(app_v1_properties, app_v2_properties)
35+
}
936

1037
describe "#diff" do
1138
subject { comparison.diff }
@@ -18,11 +45,11 @@
1845
nested: {
1946
foo: ["bar", "bar2"],
2047
bar: {
21-
one: ["foo1", nil],
22-
two: [nil, "foo2"]
48+
one: ["foo1", HashDiff::NO_VALUE],
49+
two: [HashDiff::NO_VALUE, "foo2"]
2350
}
2451
},
25-
num: [1, nil],
52+
num: [1, HashDiff::NO_VALUE],
2653
word: [nil, "monkey"]
2754
}
2855
}
@@ -57,11 +84,11 @@
5784
nested: {
5885
foo: "bar2",
5986
bar: {
60-
one: nil,
87+
one: HashDiff::NO_VALUE,
6188
two: "foo2"
6289
}
6390
},
64-
num: nil,
91+
num: HashDiff::NO_VALUE,
6592
word: "monkey"
6693
}
6794
}
@@ -80,7 +107,7 @@
80107
foo: "bar",
81108
bar: {
82109
one: "foo1",
83-
two: nil
110+
two: HashDiff::NO_VALUE
84111
}
85112
},
86113
num: 1,

0 commit comments

Comments
 (0)