File tree 2 files changed +80
-2
lines changed
2 files changed +80
-2
lines changed Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ def report_difference(key, reporter)
67
67
if comparable_hash? ( key )
68
68
self . class . new ( left [ key ] , right [ key ] ) . find_differences ( &reporter )
69
69
elsif comparable_array? ( key )
70
- self . class . new ( left [ key ] , right [ key ] ) . find_differences ( &reporter )
70
+ self . class . new ( left [ key ] , right [ key ] ) . find_differences ( &reporter )
71
71
else
72
72
reporter . call (
73
73
value_with_default ( left , key ) ,
@@ -77,7 +77,7 @@ def report_difference(key, reporter)
77
77
end
78
78
79
79
def value_with_default ( obj , key )
80
- obj [ key ] || NO_VALUE
80
+ obj . fetch ( key , NO_VALUE )
81
81
end
82
82
end
83
83
end
Original file line number Diff line number Diff line change
1
+ require "spec_helper"
2
+
3
+ describe HashDiff ::Comparison do
4
+ let ( :left ) {
5
+ [
6
+ {
7
+ foo : 'bar' ,
8
+ bar : 'foo' ,
9
+ } ,
10
+ {
11
+ nested : {
12
+ foo : 'bar' ,
13
+ bar : {
14
+ one : 'foo1'
15
+ }
16
+ } ,
17
+ } ,
18
+ {
19
+ num : 1 ,
20
+ word : nil
21
+ }
22
+ ]
23
+ }
24
+
25
+ def comparison ( to_compare )
26
+ HashDiff ::Comparison . new ( left , to_compare )
27
+ end
28
+
29
+ def right
30
+ [
31
+ {
32
+ foo : 'bar' ,
33
+ bar : 'foo' ,
34
+ } ,
35
+ {
36
+ nested : {
37
+ foo : 'bar' ,
38
+ bar : {
39
+ one : 'foo1'
40
+ }
41
+ } ,
42
+ } ,
43
+ {
44
+ num : 1 ,
45
+ word : nil
46
+ }
47
+ ]
48
+ end
49
+
50
+ describe 'when arrays are the same' do
51
+ it 'properly determines equality' do
52
+ expect ( comparison ( right ) . diff ) . to be_empty
53
+ end
54
+
55
+ it 'handles empty arrays' do
56
+ expect ( HashDiff ::Comparison . new ( [ ] , [ ] ) . diff ) . to be_empty
57
+ end
58
+ end
59
+
60
+ describe 'when arrays are different' do
61
+ it 'reports arrays as not equal with a different order' do
62
+ # move an item from the end to the beginning
63
+ right_shuffled = right
64
+ popped = right_shuffled . pop
65
+ right_shuffled . unshift ( popped )
66
+
67
+ expect ( comparison ( right_shuffled ) . diff ) . to_not be_empty
68
+ end
69
+
70
+ it 'should a deep comparison' do
71
+ right_with_extra_nested_element = right
72
+ right_with_extra_nested_element [ 1 ] [ :nested ] [ :bar ] [ :two ] = 'two'
73
+
74
+ expect ( comparison ( right_with_extra_nested_element ) . diff ) . to_not be_empty
75
+ end
76
+ end
77
+
78
+ end
You can’t perform that action at this time.
0 commit comments