hashdiff 0.3.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,248 +1,250 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe HashDiff do
4
- it "should be able to diff two empty hashes" do
5
- diff = HashDiff.diff({}, {})
5
+ describe Hashdiff do
6
+ it 'is able to diff two empty hashes' do
7
+ diff = described_class.diff({}, {})
6
8
  diff.should == []
7
9
  end
8
10
 
9
- it "should be able to diff an hash with an empty hash" do
11
+ it 'is able to diff an hash with an empty hash' do
10
12
  a = { 'a' => 3, 'b' => 2 }
11
13
  b = {}
12
14
 
13
- diff = HashDiff.diff(a, b)
14
- diff.should == [['-', 'a', 3], ['-', 'b', 2]]
15
+ diff = described_class.diff(a, b)
16
+ expect(diff).to eq([['-', 'a', 3], ['-', 'b', 2]])
15
17
 
16
- diff = HashDiff.diff(b, a)
18
+ diff = described_class.diff(b, a)
17
19
  diff.should == [['+', 'a', 3], ['+', 'b', 2]]
18
20
  end
19
21
 
20
- it "should be able to diff two equal hashes" do
21
- diff = HashDiff.diff({ 'a' => 2, 'b' => 2}, { 'a' => 2, 'b' => 2 })
22
+ it 'is able to diff two equal hashes' do
23
+ diff = described_class.diff({ 'a' => 2, 'b' => 2 }, 'a' => 2, 'b' => 2)
22
24
  diff.should == []
23
25
  end
24
26
 
25
- it "should be able to diff two equal hashes with mixed key types" do
27
+ it 'is able to diff two equal hashes with mixed key types' do
26
28
  a = { 'a' => 1, :b => 1 }
27
- diff = HashDiff.diff(a, a)
29
+ diff = described_class.diff(a, a)
28
30
  diff.should == []
29
31
  end
30
32
 
31
- it "should be able to diff if mixed key types are removed" do
33
+ it 'is able to diff if mixed key types are removed' do
32
34
  a = { 'a' => 1, :b => 1 }
33
35
  b = {}
34
- diff = HashDiff.diff(a, b)
35
- diff.should == [["-", "a", 1], ["-", "b", 1]]
36
+ diff = described_class.diff(a, b)
37
+ diff.should == [['-', 'a', 1], ['-', 'b', 1]]
36
38
  end
37
39
 
38
- it "should be able to diff if mixed key types are added" do
40
+ it 'is able to diff if mixed key types are added' do
39
41
  a = { 'a' => 1, :b => 1 }
40
42
  b = {}
41
- diff = HashDiff.diff(b, a)
42
- diff.should == [["+", "a", 1], ["+", "b", 1]]
43
+ diff = described_class.diff(b, a)
44
+ diff.should == [['+', 'a', 1], ['+', 'b', 1]]
43
45
  end
44
46
 
45
- it "should be able to diff two hashes with equivalent numerics, when strict is false" do
46
- diff = HashDiff.diff({ 'a' => 2.0, 'b' => 2 }, { 'a' => 2, 'b' => 2.0 }, :strict => false)
47
+ it 'is able to diff two hashes with equivalent numerics, when strict is false' do
48
+ diff = described_class.diff({ 'a' => 2.0, 'b' => 2 }, { 'a' => 2, 'b' => 2.0 }, strict: false)
47
49
  diff.should == []
48
50
  end
49
51
 
50
- it "should be able to diff changes in hash value" do
51
- diff = HashDiff.diff({ 'a' => 2, 'b' => 3, 'c' => " hello" }, { 'a' => 2, 'b' => 4, 'c' => "hello" })
52
- diff.should == [['~', 'b', 3, 4], ['~', 'c', " hello", "hello"]]
52
+ it 'is able to diff changes in hash value' do
53
+ diff = described_class.diff({ 'a' => 2, 'b' => 3, 'c' => ' hello' }, 'a' => 2, 'b' => 4, 'c' => 'hello')
54
+ diff.should == [['~', 'b', 3, 4], ['~', 'c', ' hello', 'hello']]
53
55
  end
54
56
 
55
- it "should be able to diff changes in hash value which is array" do
56
- diff = HashDiff.diff({ 'a' => 2, 'b' => [1, 2, 3] }, { 'a' => 2, 'b' => [1, 3, 4]})
57
+ it 'is able to diff changes in hash value which is array' do
58
+ diff = described_class.diff({ 'a' => 2, 'b' => [1, 2, 3] }, 'a' => 2, 'b' => [1, 3, 4])
57
59
  diff.should == [['-', 'b[1]', 2], ['+', 'b[2]', 4]]
58
60
  end
59
61
 
60
- it "should be able to diff changes in hash value which is hash" do
61
- diff = HashDiff.diff({ 'a' => { 'x' => 2, 'y' => 3, 'z' => 4 }, 'b' => { 'x' => 3, 'z' => 45 } },
62
- { 'a' => { 'y' => 3 }, 'b' => { 'y' => 3, 'z' => 30 } })
62
+ it 'is able to diff changes in hash value which is hash' do
63
+ diff = described_class.diff({ 'a' => { 'x' => 2, 'y' => 3, 'z' => 4 }, 'b' => { 'x' => 3, 'z' => 45 } },
64
+ 'a' => { 'y' => 3 }, 'b' => { 'y' => 3, 'z' => 30 })
63
65
  diff.should == [['-', 'a.x', 2], ['-', 'a.z', 4], ['-', 'b.x', 3], ['~', 'b.z', 45, 30], ['+', 'b.y', 3]]
64
66
  end
65
67
 
66
- it "should be able to diff similar objects in array" do
67
- diff = HashDiff.best_diff({ 'a' => [{ 'x' => 2, 'y' => 3, 'z' => 4 }, { 'x' => 11, 'y' => 22, 'z' => 33 }], 'b' => { 'x' => 3, 'z' => 45 } },
68
- { 'a' => [{ 'y' => 3 }, { 'x' => 11, 'z' => 33 }], 'b' => { 'y' => 22 } })
68
+ it 'is able to best diff similar objects in array' do
69
+ diff = described_class.best_diff({ 'a' => [{ 'x' => 2, 'y' => 3, 'z' => 4 }, { 'x' => 11, 'y' => 22, 'z' => 33 }], 'b' => { 'x' => 3, 'z' => 45 } },
70
+ 'a' => [{ 'y' => 3 }, { 'x' => 11, 'z' => 33 }], 'b' => { 'y' => 22 })
69
71
  diff.should == [['-', 'a[0].x', 2], ['-', 'a[0].z', 4], ['-', 'a[1].y', 22], ['-', 'b.x', 3], ['-', 'b.z', 45], ['+', 'b.y', 22]]
70
72
  end
71
73
 
72
- it 'should be able to diff addition of key value pair' do
73
- a = {"a"=>3, "c"=>11, "d"=>45, "e"=>100, "f"=>200}
74
- b = {"a"=>3, "c"=>11, "d"=>45, "e"=>100, "f"=>200, "g"=>300}
74
+ it 'is able to diff addition of key value pair' do
75
+ a = { 'a' => 3, 'c' => 11, 'd' => 45, 'e' => 100, 'f' => 200 }
76
+ b = { 'a' => 3, 'c' => 11, 'd' => 45, 'e' => 100, 'f' => 200, 'g' => 300 }
75
77
 
76
- diff = HashDiff.diff(a, b)
77
- diff.should == [['+', 'g', 300]]
78
+ diff = described_class.diff(a, b)
79
+ expect(diff).to eq([['+', 'g', 300]])
78
80
 
79
- diff = HashDiff.diff(b, a)
81
+ diff = described_class.diff(b, a)
80
82
  diff.should == [['-', 'g', 300]]
81
83
  end
82
84
 
83
- it 'should be able to diff value type changes' do
84
- a = {"a" => 3}
85
- b = {"a" => {"a1" => 1, "a2" => 2}}
85
+ it 'is able to diff value type changes' do
86
+ a = { 'a' => 3 }
87
+ b = { 'a' => { 'a1' => 1, 'a2' => 2 } }
86
88
 
87
- diff = HashDiff.diff(a, b)
88
- diff.should == [['~', 'a', 3, {"a1" => 1, "a2" => 2}]]
89
+ diff = described_class.diff(a, b)
90
+ expect(diff).to eq([['~', 'a', 3, { 'a1' => 1, 'a2' => 2 }]])
89
91
 
90
- diff = HashDiff.diff(b, a)
91
- diff.should == [['~', 'a', {"a1" => 1, "a2" => 2}, 3]]
92
+ diff = described_class.diff(b, a)
93
+ diff.should == [['~', 'a', { 'a1' => 1, 'a2' => 2 }, 3]]
92
94
  end
93
95
 
94
- it "should be able to diff value changes: array <=> []" do
95
- a = {"a" => 1, "b" => [1, 2]}
96
- b = {"a" => 1, "b" => []}
96
+ it 'is able to diff value changes: array <=> []' do
97
+ a = { 'a' => 1, 'b' => [1, 2] }
98
+ b = { 'a' => 1, 'b' => [] }
97
99
 
98
- diff = HashDiff.diff(a, b)
100
+ diff = described_class.diff(a, b)
99
101
  diff.should == [['-', 'b[1]', 2], ['-', 'b[0]', 1]]
100
102
  end
101
103
 
102
- it "should be able to diff value changes: array <=> nil" do
103
- a = {"a" => 1, "b" => [1, 2]}
104
- b = {"a" => 1, "b" => nil}
104
+ it 'is able to diff value changes: array <=> nil' do
105
+ a = { 'a' => 1, 'b' => [1, 2] }
106
+ b = { 'a' => 1, 'b' => nil }
105
107
 
106
- diff = HashDiff.diff(a, b)
107
- diff.should == [["~", "b", [1, 2], nil]]
108
+ diff = described_class.diff(a, b)
109
+ diff.should == [['~', 'b', [1, 2], nil]]
108
110
  end
109
111
 
110
- it "should be able to diff value chagnes: remove array completely" do
111
- a = {"a" => 1, "b" => [1, 2]}
112
- b = {"a" => 1}
112
+ it 'is able to diff value chagnes: remove array completely' do
113
+ a = { 'a' => 1, 'b' => [1, 2] }
114
+ b = { 'a' => 1 }
113
115
 
114
- diff = HashDiff.diff(a, b)
115
- diff.should == [["-", "b", [1, 2]]]
116
+ diff = described_class.diff(a, b)
117
+ diff.should == [['-', 'b', [1, 2]]]
116
118
  end
117
119
 
118
- it "should be able to diff value changes: remove whole hash" do
119
- a = {"a" => 1, "b" => {"b1" => 1, "b2" =>2}}
120
- b = {"a" => 1}
120
+ it 'is able to diff value changes: remove whole hash' do
121
+ a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
122
+ b = { 'a' => 1 }
121
123
 
122
- diff = HashDiff.diff(a, b)
123
- diff.should == [["-", "b", {"b1"=>1, "b2"=>2}]]
124
+ diff = described_class.diff(a, b)
125
+ diff.should == [['-', 'b', { 'b1' => 1, 'b2' => 2 }]]
124
126
  end
125
127
 
126
- it "should be able to diff value changes: hash <=> {}" do
127
- a = {"a" => 1, "b" => {"b1" => 1, "b2" =>2}}
128
- b = {"a" => 1, "b" => {}}
128
+ it 'is able to diff value changes: hash <=> {}' do
129
+ a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
130
+ b = { 'a' => 1, 'b' => {} }
129
131
 
130
- diff = HashDiff.diff(a, b)
132
+ diff = described_class.diff(a, b)
131
133
  diff.should == [['-', 'b.b1', 1], ['-', 'b.b2', 2]]
132
134
  end
133
135
 
134
- it "should be able to diff value changes: hash <=> nil" do
135
- a = {"a" => 1, "b" => {"b1" => 1, "b2" =>2}}
136
- b = {"a" => 1, "b" => nil}
136
+ it 'is able to diff value changes: hash <=> nil' do
137
+ a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
138
+ b = { 'a' => 1, 'b' => nil }
137
139
 
138
- diff = HashDiff.diff(a, b)
139
- diff.should == [["~", "b", {"b1"=>1, "b2"=>2}, nil]]
140
+ diff = described_class.diff(a, b)
141
+ diff.should == [['~', 'b', { 'b1' => 1, 'b2' => 2 }, nil]]
140
142
  end
141
143
 
142
- it "should be able to diff similar objects in array" do
143
- a = [{'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}, 3]
144
- b = [1, {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}]
144
+ it 'is able to diff similar objects in array' do
145
+ a = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }, 3]
146
+ b = [1, { 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }]
145
147
 
146
- diff = HashDiff.diff(a, b)
148
+ diff = described_class.diff(a, b)
147
149
  diff.should == [['-', '[0].d', 4], ['+', '[0]', 1], ['-', '[2]', 3]]
148
150
  end
149
151
 
150
- it "should be able to diff similar & equal objects in array" do
151
- a = [{'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}, {'x' => 5, 'y' => 6, 'z' => 3}, 3]
152
- b = [{'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}, 3]
152
+ it 'is able to diff similar & equal objects in array' do
153
+ a = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }, { 'x' => 5, 'y' => 6, 'z' => 3 }, 3]
154
+ b = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }, 3]
153
155
 
154
- diff = HashDiff.diff(a, b)
155
- diff.should == [["-", "[0].d", 4], ["-", "[1]", {"x"=>5, "y"=>6, "z"=>3}]]
156
+ diff = described_class.diff(a, b)
157
+ diff.should == [['-', '[0].d', 4], ['-', '[1]', { 'x' => 5, 'y' => 6, 'z' => 3 }]]
156
158
  end
157
159
 
158
- it "should use custom delimiter when provided" do
159
- a = [{'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}, {'x' => 5, 'y' => 6, 'z' => 3}, 3]
160
- b = [{'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}, 3]
160
+ it 'uses custom delimiter when provided' do
161
+ a = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }, { 'x' => 5, 'y' => 6, 'z' => 3 }, 3]
162
+ b = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }, 3]
161
163
 
162
- diff = HashDiff.diff(a, b, :similarity => 0.8, :delimiter => "\t")
163
- diff.should == [["-", "[0]\td", 4], ["-", "[1]", {"x"=>5, "y"=>6, "z"=>3}]]
164
+ diff = described_class.diff(a, b, similarity: 0.8, delimiter: "\t")
165
+ diff.should == [['-', "[0]\td", 4], ['-', '[1]', { 'x' => 5, 'y' => 6, 'z' => 3 }]]
164
166
  end
165
167
 
166
168
  context 'when :numeric_tolerance requested' do
167
- it "should be able to diff changes in hash value" do
168
- a = {'a' => 0.558, 'b' => 0.0, 'c' => 0.65, 'd' => 'fin'}
169
- b = {'a' => 0.557, 'b' => 'hats', 'c' => 0.67, 'd' => 'fin'}
169
+ it 'is able to diff changes in hash value' do
170
+ a = { 'a' => 0.558, 'b' => 0.0, 'c' => 0.65, 'd' => 'fin' }
171
+ b = { 'a' => 0.557, 'b' => 'hats', 'c' => 0.67, 'd' => 'fin' }
170
172
 
171
- diff = HashDiff.diff(a, b, :numeric_tolerance => 0.01)
172
- diff.should == [["~", "b", 0.0, 'hats'], ["~", "c", 0.65, 0.67]]
173
+ diff = described_class.diff(a, b, numeric_tolerance: 0.01)
174
+ expect(diff).to eq([['~', 'b', 0.0, 'hats'], ['~', 'c', 0.65, 0.67]])
173
175
 
174
- diff = HashDiff.diff(b, a, :numeric_tolerance => 0.01)
175
- diff.should == [["~", "b", 'hats', 0.0], ["~", "c", 0.67, 0.65]]
176
+ diff = described_class.diff(b, a, numeric_tolerance: 0.01)
177
+ diff.should == [['~', 'b', 'hats', 0.0], ['~', 'c', 0.67, 0.65]]
176
178
  end
177
179
 
178
- it "should be able to diff changes in nested values" do
179
- a = {'a' => {'x' => 0.4, 'y' => 0.338}, 'b' => [13, 68.03]}
180
- b = {'a' => {'x' => 0.6, 'y' => 0.341}, 'b' => [14, 68.025]}
180
+ it 'is able to diff changes in nested values' do
181
+ a = { 'a' => { 'x' => 0.4, 'y' => 0.338 }, 'b' => [13, 68.03] }
182
+ b = { 'a' => { 'x' => 0.6, 'y' => 0.341 }, 'b' => [14, 68.025] }
181
183
 
182
- diff = HashDiff.diff(a, b, :numeric_tolerance => 0.01)
183
- diff.should == [["~", "a.x", 0.4, 0.6], ["-", "b[0]", 13], ["+", "b[0]", 14]]
184
+ diff = described_class.diff(a, b, numeric_tolerance: 0.01)
185
+ expect(diff).to eq([['~', 'a.x', 0.4, 0.6], ['-', 'b[0]', 13], ['+', 'b[0]', 14]])
184
186
 
185
- diff = HashDiff.diff(b, a, :numeric_tolerance => 0.01)
186
- diff.should == [["~", "a.x", 0.6, 0.4], ["-", "b[0]", 14], ["+", "b[0]", 13]]
187
+ diff = described_class.diff(b, a, numeric_tolerance: 0.01)
188
+ diff.should == [['~', 'a.x', 0.6, 0.4], ['-', 'b[0]', 14], ['+', 'b[0]', 13]]
187
189
  end
188
190
  end
189
191
 
190
192
  context 'when :strip requested' do
191
- it "should strip strings before comparing" do
192
- a = { 'a' => " foo", 'b' => "fizz buzz"}
193
- b = { 'a' => "foo", 'b' => "fizzbuzz"}
194
- diff = HashDiff.diff(a, b, :strip => true)
195
- diff.should == [['~', 'b', "fizz buzz", "fizzbuzz"]]
193
+ it 'strips strings before comparing' do
194
+ a = { 'a' => ' foo', 'b' => 'fizz buzz' }
195
+ b = { 'a' => 'foo', 'b' => 'fizzbuzz' }
196
+ diff = described_class.diff(a, b, strip: true)
197
+ diff.should == [['~', 'b', 'fizz buzz', 'fizzbuzz']]
196
198
  end
197
199
 
198
- it "should strip nested strings before comparing" do
199
- a = { 'a' => { 'x' => " foo" }, 'b' => ["fizz buzz", "nerf"] }
200
- b = { 'a' => { 'x' => "foo" }, 'b' => ["fizzbuzz", "nerf"] }
201
- diff = HashDiff.diff(a, b, :strip => true)
202
- diff.should == [['-', 'b[0]', "fizz buzz"], ['+', 'b[0]', "fizzbuzz"]]
200
+ it 'strips nested strings before comparing' do
201
+ a = { 'a' => { 'x' => ' foo' }, 'b' => ['fizz buzz', 'nerf'] }
202
+ b = { 'a' => { 'x' => 'foo' }, 'b' => %w[fizzbuzz nerf] }
203
+ diff = described_class.diff(a, b, strip: true)
204
+ diff.should == [['-', 'b[0]', 'fizz buzz'], ['+', 'b[0]', 'fizzbuzz']]
203
205
  end
204
206
  end
205
207
 
206
208
  context 'when :case_insensitive requested' do
207
- it "should strip strings before comparing" do
208
- a = { 'a' => "Foo", 'b' => "fizz buzz"}
209
- b = { 'a' => "foo", 'b' => "fizzBuzz"}
210
- diff = HashDiff.diff(a, b, :case_insensitive => true)
211
- diff.should == [['~', 'b', "fizz buzz", "fizzBuzz"]]
209
+ it 'strips strings before comparing' do
210
+ a = { 'a' => 'Foo', 'b' => 'fizz buzz' }
211
+ b = { 'a' => 'foo', 'b' => 'fizzBuzz' }
212
+ diff = described_class.diff(a, b, case_insensitive: true)
213
+ diff.should == [['~', 'b', 'fizz buzz', 'fizzBuzz']]
212
214
  end
213
215
 
214
- it "should ignore case on nested strings before comparing" do
215
- a = { 'a' => { 'x' => "Foo" }, 'b' => ["fizz buzz", "nerf"] }
216
- b = { 'a' => { 'x' => "foo" }, 'b' => ["fizzbuzz", "nerf"] }
217
- diff = HashDiff.diff(a, b, :case_insensitive => true)
218
- diff.should == [['-', 'b[0]', "fizz buzz"], ['+', 'b[0]', "fizzbuzz"]]
216
+ it 'ignores case on nested strings before comparing' do
217
+ a = { 'a' => { 'x' => 'Foo' }, 'b' => ['fizz buzz', 'nerf'] }
218
+ b = { 'a' => { 'x' => 'foo' }, 'b' => %w[fizzbuzz nerf] }
219
+ diff = described_class.diff(a, b, case_insensitive: true)
220
+ diff.should == [['-', 'b[0]', 'fizz buzz'], ['+', 'b[0]', 'fizzbuzz']]
219
221
  end
220
222
  end
221
223
 
222
224
  context 'when both :strip and :numeric_tolerance requested' do
223
- it 'should apply filters to proper object types' do
224
- a = { 'a' => " foo", 'b' => 35, 'c' => 'bar', 'd' => 'baz' }
225
- b = { 'a' => "foo", 'b' => 35.005, 'c' => 'bar', 'd' => 18.5}
226
- diff = HashDiff.diff(a, b, :strict => false, :numeric_tolerance => 0.01, :strip => true)
227
- diff.should == [['~', 'd', "baz", 18.5]]
225
+ it 'applies filters to proper object types' do
226
+ a = { 'a' => ' foo', 'b' => 35, 'c' => 'bar', 'd' => 'baz' }
227
+ b = { 'a' => 'foo', 'b' => 35.005, 'c' => 'bar', 'd' => 18.5 }
228
+ diff = described_class.diff(a, b, strict: false, numeric_tolerance: 0.01, strip: true)
229
+ diff.should == [['~', 'd', 'baz', 18.5]]
228
230
  end
229
231
  end
230
232
 
231
- context "when both :strip and :case_insensitive requested" do
232
- it "should apply both filters to strings" do
233
- a = { 'a' => " Foo", 'b' => "fizz buzz"}
234
- b = { 'a' => "foo", 'b' => "fizzBuzz"}
235
- diff = HashDiff.diff(a, b, :case_insensitive => true, :strip => true)
236
- diff.should == [['~', 'b', "fizz buzz", "fizzBuzz"]]
233
+ context 'when both :strip and :case_insensitive requested' do
234
+ it 'applies both filters to strings' do
235
+ a = { 'a' => ' Foo', 'b' => 'fizz buzz' }
236
+ b = { 'a' => 'foo', 'b' => 'fizzBuzz' }
237
+ diff = described_class.diff(a, b, case_insensitive: true, strip: true)
238
+ diff.should == [['~', 'b', 'fizz buzz', 'fizzBuzz']]
237
239
  end
238
240
  end
239
241
 
240
242
  context 'with custom comparison' do
241
- let(:a) { { 'a' => 'car', 'b' => 'boat', 'c' => 'plane'} }
242
- let(:b) { { 'a' => 'bus', 'b' => 'truck', 'c' => ' plan'} }
243
+ let(:a) { { 'a' => 'car', 'b' => 'boat', 'c' => 'plane' } }
244
+ let(:b) { { 'a' => 'bus', 'b' => 'truck', 'c' => ' plan' } }
243
245
 
244
- it 'should compare using proc specified in block' do
245
- diff = HashDiff.diff(a, b) do |prefix, obj1, obj2|
246
+ it 'compares using proc specified in block' do
247
+ diff = described_class.diff(a, b) do |prefix, obj1, obj2|
246
248
  case prefix
247
249
  when /a|b|c/
248
250
  obj1.length == obj2.length
@@ -251,11 +253,11 @@ describe HashDiff do
251
253
  diff.should == [['~', 'b', 'boat', 'truck']]
252
254
  end
253
255
 
254
- it 'should yield added keys' do
255
- x = { 'a' => 'car', 'b' => 'boat'}
256
+ it 'yields added keys' do
257
+ x = { 'a' => 'car', 'b' => 'boat' }
256
258
  y = { 'a' => 'car' }
257
259
 
258
- diff = HashDiff.diff(x, y) do |prefix, obj1, obj2|
260
+ diff = described_class.diff(x, y) do |prefix, _obj1, _obj2|
259
261
  case prefix
260
262
  when /b/
261
263
  true
@@ -264,8 +266,8 @@ describe HashDiff do
264
266
  diff.should == []
265
267
  end
266
268
 
267
- it 'should compare with both proc and :strip when both provided' do
268
- diff = HashDiff.diff(a, b, :strip => true) do |prefix, obj1, obj2|
269
+ it 'compares with both proc and :strip when both provided' do
270
+ diff = described_class.diff(a, b, strip: true) do |prefix, obj1, obj2|
269
271
  case prefix
270
272
  when 'a'
271
273
  obj1.length == obj2.length
@@ -273,5 +275,81 @@ describe HashDiff do
273
275
  end
274
276
  diff.should == [['~', 'b', 'boat', 'truck'], ['~', 'c', 'plane', ' plan']]
275
277
  end
278
+
279
+ it 'compares nested arrays using proc specified in block' do
280
+ a = { a: 'car', b: %w[boat plane] }
281
+ b = { a: 'bus', b: ['truck', ' plan'] }
282
+
283
+ diff = described_class.diff(a, b) do |path, obj1, obj2|
284
+ case path
285
+ when 'b[*]'
286
+ obj1.length == obj2.length
287
+ end
288
+ end
289
+
290
+ expect(diff).to eq [['~', 'a', 'car', 'bus'], ['~', 'b[1]', 'plane', ' plan'], ['-', 'b[0]', 'boat'], ['+', 'b[0]', 'truck']]
291
+ end
292
+ end
293
+
294
+ context 'when :array_path is true' do
295
+ it 'returns the diff path in an array rather than a string' do
296
+ x = { 'a' => 'foo' }
297
+ y = { 'a' => 'bar' }
298
+ diff = described_class.diff(x, y, array_path: true)
299
+
300
+ diff.should == [['~', ['a'], 'foo', 'bar']]
301
+ end
302
+
303
+ it 'shows array indexes in paths' do
304
+ x = { 'a' => [0, 1, 2] }
305
+ y = { 'a' => [0, 1, 2, 3] }
306
+
307
+ diff = described_class.diff(x, y, array_path: true)
308
+
309
+ diff.should == [['+', ['a', 3], 3]]
310
+ end
311
+
312
+ it 'shows differences with string and symbol keys' do
313
+ x = { 'a' => 'foo' }
314
+ y = { a: 'bar' }
315
+
316
+ diff = described_class.diff(x, y, array_path: true)
317
+ diff.should == [['-', ['a'], 'foo'], ['+', [:a], 'bar']]
318
+ end
319
+
320
+ it 'supports other key types' do
321
+ time = Time.now
322
+ x = { time => 'foo' }
323
+ y = { 0 => 'bar' }
324
+
325
+ diff = described_class.diff(x, y, array_path: true)
326
+ diff.should == [['-', [time], 'foo'], ['+', [0], 'bar']]
327
+ end
328
+ end
329
+
330
+ context 'when :use_lcs is false' do
331
+ it 'shows items in an array as changed' do
332
+ x = %i[a b]
333
+ y = %i[c d]
334
+ diff = described_class.diff(x, y, use_lcs: false)
335
+
336
+ diff.should == [['~', '[0]', :a, :c], ['~', '[1]', :b, :d]]
337
+ end
338
+
339
+ it 'shows additions to arrays' do
340
+ x = { a: [0] }
341
+ y = { a: [0, 1] }
342
+ diff = described_class.diff(x, y, use_lcs: false)
343
+
344
+ diff.should == [['+', 'a[1]', 1]]
345
+ end
346
+
347
+ it 'shows changes to nested arrays' do
348
+ x = { a: [[0, 1]] }
349
+ y = { a: [[1, 2]] }
350
+ diff = described_class.diff(x, y, use_lcs: false)
351
+
352
+ diff.should == [['~', 'a[0][0]', 0, 1], ['~', 'a[0][1]', 1, 2]]
353
+ end
276
354
  end
277
355
  end
@@ -1,75 +1,76 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
- describe HashDiff do
4
- it "should be able to find LCS between two equal array" do
5
+ describe Hashdiff do
6
+ it 'is able to find LCS between two equal array' do
5
7
  a = [1, 2, 3]
6
8
  b = [1, 2, 3]
7
9
 
8
- lcs = HashDiff.lcs(a, b)
10
+ lcs = described_class.lcs(a, b)
9
11
  lcs.should == [[0, 0], [1, 1], [2, 2]]
10
12
  end
11
13
 
12
- it "should be able to find LCS between two close arrays" do
14
+ it 'is able to find LCS between two close arrays' do
13
15
  a = [1.05, 2, 3.25]
14
16
  b = [1.06, 2, 3.24]
15
17
 
16
- lcs = HashDiff.lcs(a, b, :numeric_tolerance => 0.1)
18
+ lcs = described_class.lcs(a, b, numeric_tolerance: 0.1)
17
19
  lcs.should == [[0, 0], [1, 1], [2, 2]]
18
20
  end
19
21
 
20
- it "should strip strings when finding LCS if requested" do
21
- a = ['foo', 'bar', 'baz']
22
+ it 'strips strings when finding LCS if requested' do
23
+ a = %w[foo bar baz]
22
24
  b = [' foo', 'bar', 'zab']
23
25
 
24
- lcs = HashDiff.lcs(a, b, :strip => true)
26
+ lcs = described_class.lcs(a, b, strip: true)
25
27
  lcs.should == [[0, 0], [1, 1]]
26
28
  end
27
29
 
28
- it "should be able to find LCS with one common elements" do
30
+ it 'is able to find LCS with one common elements' do
29
31
  a = [1, 2, 3]
30
32
  b = [1, 8, 7]
31
33
 
32
- lcs = HashDiff.lcs(a, b)
34
+ lcs = described_class.lcs(a, b)
33
35
  lcs.should == [[0, 0]]
34
36
  end
35
37
 
36
- it "should be able to find LCS with two common elements" do
38
+ it 'is able to find LCS with two common elements' do
37
39
  a = [1, 3, 5, 7]
38
40
  b = [2, 3, 7, 5]
39
41
 
40
- lcs = HashDiff.lcs(a, b)
42
+ lcs = described_class.lcs(a, b)
41
43
  lcs.should == [[1, 1], [2, 3]]
42
44
  end
43
45
 
44
- it "should be able to find LCS with two close elements" do
46
+ it 'is able to find LCS with two close elements' do
45
47
  a = [1, 3.05, 5, 7]
46
48
  b = [2, 3.06, 7, 5]
47
49
 
48
- lcs = HashDiff.lcs(a, b, :numeric_tolerance => 0.1)
50
+ lcs = described_class.lcs(a, b, numeric_tolerance: 0.1)
49
51
  lcs.should == [[1, 1], [2, 3]]
50
52
  end
51
53
 
52
- it "should be able to find LCS with two common elements in different ordering" do
54
+ it 'is able to find LCS with two common elements in different ordering' do
53
55
  a = [1, 3, 4, 7]
54
56
  b = [2, 3, 7, 5]
55
57
 
56
- lcs = HashDiff.lcs(a, b)
58
+ lcs = described_class.lcs(a, b)
57
59
  lcs.should == [[1, 1], [3, 2]]
58
60
  end
59
61
 
60
- it "should be able to find LCS with a similarity value" do
62
+ it 'is able to find LCS with a similarity value' do
61
63
  a = [
62
- {"value" => "New", "onclick" => "CreateNewDoc()"},
63
- {"value" => "Close", "onclick" => "CloseDoc()"}
64
- ]
64
+ { 'value' => 'New', 'onclick' => 'CreateNewDoc()' },
65
+ { 'value' => 'Close', 'onclick' => 'CloseDoc()' }
66
+ ]
65
67
  b = [
66
- {"value" => "New1", "onclick" => "CreateNewDoc()"},
67
- {"value" => "Open", "onclick" => "OpenDoc()"},
68
- {"value" => "Close", "onclick" => "CloseDoc()"}
69
- ]
68
+ { 'value' => 'New1', 'onclick' => 'CreateNewDoc()' },
69
+ { 'value' => 'Open', 'onclick' => 'OpenDoc()' },
70
+ { 'value' => 'Close', 'onclick' => 'CloseDoc()' }
71
+ ]
70
72
 
71
- lcs = HashDiff.lcs(a, b, :similarity => 0.5)
73
+ lcs = described_class.lcs(a, b, similarity: 0.5)
72
74
  lcs.should == [[0, 0], [1, 2]]
73
75
  end
74
76
  end
75
-