hashdiff 0.3.7 → 1.0.1
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.
- checksums.yaml +5 -5
- data/.rubocop.yml +32 -0
- data/.travis.yml +3 -5
- data/Gemfile +3 -1
- data/README.md +42 -31
- data/Rakefile +9 -4
- data/changelog.md +31 -3
- data/hashdiff.gemspec +26 -12
- data/lib/hashdiff.rb +4 -0
- data/lib/hashdiff/compare_hashes.rb +69 -0
- data/lib/hashdiff/diff.rb +61 -116
- data/lib/hashdiff/lcs.rb +27 -30
- data/lib/hashdiff/lcs_compare_arrays.rb +32 -0
- data/lib/hashdiff/linear_compare_array.rb +10 -6
- data/lib/hashdiff/patch.rb +5 -5
- data/lib/hashdiff/util.rb +44 -35
- data/lib/hashdiff/version.rb +4 -2
- data/spec/hashdiff/best_diff_spec.rb +44 -43
- data/spec/hashdiff/diff_array_spec.rb +19 -19
- data/spec/hashdiff/diff_spec.rb +180 -159
- data/spec/hashdiff/lcs_spec.rb +27 -26
- data/spec/hashdiff/linear_compare_array_spec.rb +20 -18
- data/spec/hashdiff/patch_spec.rb +123 -121
- data/spec/hashdiff/readme_spec.rb +15 -0
- data/spec/hashdiff/util_spec.rb +81 -43
- data/spec/spec_helper.rb +2 -0
- metadata +59 -23
data/spec/hashdiff/lcs_spec.rb
CHANGED
@@ -1,75 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
|
-
describe
|
4
|
-
it
|
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 =
|
10
|
+
lcs = described_class.lcs(a, b)
|
9
11
|
lcs.should == [[0, 0], [1, 1], [2, 2]]
|
10
12
|
end
|
11
13
|
|
12
|
-
it
|
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 =
|
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
|
21
|
-
a = [
|
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 =
|
26
|
+
lcs = described_class.lcs(a, b, strip: true)
|
25
27
|
lcs.should == [[0, 0], [1, 1]]
|
26
28
|
end
|
27
29
|
|
28
|
-
it
|
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 =
|
34
|
+
lcs = described_class.lcs(a, b)
|
33
35
|
lcs.should == [[0, 0]]
|
34
36
|
end
|
35
37
|
|
36
|
-
it
|
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 =
|
42
|
+
lcs = described_class.lcs(a, b)
|
41
43
|
lcs.should == [[1, 1], [2, 3]]
|
42
44
|
end
|
43
45
|
|
44
|
-
it
|
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 =
|
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
|
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 =
|
58
|
+
lcs = described_class.lcs(a, b)
|
57
59
|
lcs.should == [[1, 1], [3, 2]]
|
58
60
|
end
|
59
61
|
|
60
|
-
it
|
62
|
+
it 'is able to find LCS with a similarity value' do
|
61
63
|
a = [
|
62
|
-
|
63
|
-
|
64
|
-
|
64
|
+
{ 'value' => 'New', 'onclick' => 'CreateNewDoc()' },
|
65
|
+
{ 'value' => 'Close', 'onclick' => 'CloseDoc()' }
|
66
|
+
]
|
65
67
|
b = [
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
{ 'value' => 'New1', 'onclick' => 'CreateNewDoc()' },
|
69
|
+
{ 'value' => 'Open', 'onclick' => 'OpenDoc()' },
|
70
|
+
{ 'value' => 'Close', 'onclick' => 'CloseDoc()' }
|
71
|
+
]
|
70
72
|
|
71
|
-
lcs =
|
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
|
-
|
@@ -1,48 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
|
-
describe
|
4
|
-
it
|
5
|
+
describe Hashdiff::LinearCompareArray do
|
6
|
+
it 'finds no differences between two empty arrays' do
|
5
7
|
difference = described_class.call([], [])
|
6
8
|
difference.should == []
|
7
9
|
end
|
8
10
|
|
9
|
-
it
|
10
|
-
difference = described_class.call([], [
|
11
|
+
it 'finds added items when the old array is empty' do
|
12
|
+
difference = described_class.call([], %i[a b])
|
11
13
|
difference.should == [['+', '[0]', :a], ['+', '[1]', :b]]
|
12
14
|
end
|
13
15
|
|
14
|
-
it
|
15
|
-
difference = described_class.call([
|
16
|
+
it 'finds removed items when the new array is empty' do
|
17
|
+
difference = described_class.call(%i[a b], [])
|
16
18
|
difference.should == [['-', '[1]', :b], ['-', '[0]', :a]]
|
17
19
|
end
|
18
20
|
|
19
|
-
it
|
20
|
-
difference = described_class.call([
|
21
|
+
it 'finds no differences between identical arrays' do
|
22
|
+
difference = described_class.call(%i[a b], %i[a b])
|
21
23
|
difference.should == []
|
22
24
|
end
|
23
25
|
|
24
|
-
it
|
25
|
-
difference = described_class.call([
|
26
|
+
it 'finds added items in an array' do
|
27
|
+
difference = described_class.call(%i[a d], %i[a b c d])
|
26
28
|
difference.should == [['+', '[1]', :b], ['+', '[2]', :c]]
|
27
29
|
end
|
28
30
|
|
29
|
-
it
|
30
|
-
difference = described_class.call([
|
31
|
+
it 'finds removed items in an array' do
|
32
|
+
difference = described_class.call(%i[a b c d e f], %i[a d f])
|
31
33
|
difference.should == [['-', '[4]', :e], ['-', '[2]', :c], ['-', '[1]', :b]]
|
32
34
|
end
|
33
35
|
|
34
|
-
it
|
35
|
-
difference = described_class.call([
|
36
|
+
it 'shows additions and deletions as changed items' do
|
37
|
+
difference = described_class.call(%i[a b c], %i[c b a])
|
36
38
|
difference.should == [['~', '[0]', :a, :c], ['~', '[2]', :c, :a]]
|
37
39
|
end
|
38
40
|
|
39
|
-
it
|
40
|
-
difference = described_class.call([{ :
|
41
|
+
it 'shows changed items in a hash' do
|
42
|
+
difference = described_class.call([{ a: :b }], [{ a: :c }])
|
41
43
|
difference.should == [['~', '[0].a', :b, :c]]
|
42
44
|
end
|
43
45
|
|
44
|
-
it
|
45
|
-
difference = described_class.call([{ :
|
46
|
+
it 'shows changed items and added items' do
|
47
|
+
difference = described_class.call([{ a: 1, b: 2 }], [{ a: 2, b: 2 }, :item])
|
46
48
|
difference.should == [['~', '[0].a', 1, 2], ['+', '[1]', :item]]
|
47
49
|
end
|
48
50
|
end
|
data/spec/hashdiff/patch_spec.rb
CHANGED
@@ -1,183 +1,185 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
|
-
describe
|
4
|
-
it
|
5
|
-
a = {
|
6
|
-
b = {
|
7
|
-
diff =
|
5
|
+
describe Hashdiff do
|
6
|
+
it 'is able to patch key addition' do
|
7
|
+
a = { 'a' => 3, 'c' => 11, 'd' => 45, 'e' => 100, 'f' => 200 }
|
8
|
+
b = { 'a' => 3, 'c' => 11, 'd' => 45, 'e' => 100, 'f' => 200, 'g' => 300 }
|
9
|
+
diff = described_class.diff(a, b)
|
8
10
|
|
9
|
-
|
11
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
10
12
|
|
11
|
-
a = {
|
12
|
-
b = {
|
13
|
-
|
13
|
+
a = { 'a' => 3, 'c' => 11, 'd' => 45, 'e' => 100, 'f' => 200 }
|
14
|
+
b = { 'a' => 3, 'c' => 11, 'd' => 45, 'e' => 100, 'f' => 200, 'g' => 300 }
|
15
|
+
described_class.unpatch!(b, diff).should == a
|
14
16
|
end
|
15
17
|
|
16
|
-
it
|
17
|
-
a = {
|
18
|
-
b = {
|
19
|
-
diff =
|
18
|
+
it 'is able to patch value type changes' do
|
19
|
+
a = { 'a' => 3 }
|
20
|
+
b = { 'a' => { 'a1' => 1, 'a2' => 2 } }
|
21
|
+
diff = described_class.diff(a, b)
|
20
22
|
|
21
|
-
|
23
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
22
24
|
|
23
|
-
a = {
|
24
|
-
b = {
|
25
|
-
|
25
|
+
a = { 'a' => 3 }
|
26
|
+
b = { 'a' => { 'a1' => 1, 'a2' => 2 } }
|
27
|
+
described_class.unpatch!(b, diff).should == a
|
26
28
|
end
|
27
29
|
|
28
|
-
it
|
29
|
-
a = {
|
30
|
-
b = {
|
31
|
-
diff =
|
30
|
+
it 'is able to patch value array <=> []' do
|
31
|
+
a = { 'a' => 1, 'b' => [1, 2] }
|
32
|
+
b = { 'a' => 1, 'b' => [] }
|
33
|
+
diff = described_class.diff(a, b)
|
32
34
|
|
33
|
-
|
35
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
34
36
|
|
35
|
-
a = {
|
36
|
-
b = {
|
37
|
-
|
37
|
+
a = { 'a' => 1, 'b' => [1, 2] }
|
38
|
+
b = { 'a' => 1, 'b' => [] }
|
39
|
+
described_class.unpatch!(b, diff).should == a
|
38
40
|
end
|
39
41
|
|
40
|
-
it
|
41
|
-
a = {
|
42
|
-
b = {
|
43
|
-
diff =
|
42
|
+
it 'is able to patch value array <=> nil' do
|
43
|
+
a = { 'a' => 1, 'b' => [1, 2] }
|
44
|
+
b = { 'a' => 1, 'b' => nil }
|
45
|
+
diff = described_class.diff(a, b)
|
44
46
|
|
45
|
-
|
47
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
46
48
|
|
47
|
-
a = {
|
48
|
-
b = {
|
49
|
-
|
49
|
+
a = { 'a' => 1, 'b' => [1, 2] }
|
50
|
+
b = { 'a' => 1, 'b' => nil }
|
51
|
+
described_class.unpatch!(b, diff).should == a
|
50
52
|
end
|
51
53
|
|
52
|
-
it
|
53
|
-
a = {
|
54
|
-
b = {
|
55
|
-
diff =
|
54
|
+
it 'is able to patch array value removal' do
|
55
|
+
a = { 'a' => 1, 'b' => [1, 2] }
|
56
|
+
b = { 'a' => 1 }
|
57
|
+
diff = described_class.diff(a, b)
|
56
58
|
|
57
|
-
|
59
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
58
60
|
|
59
|
-
a = {
|
60
|
-
b = {
|
61
|
-
|
61
|
+
a = { 'a' => 1, 'b' => [1, 2] }
|
62
|
+
b = { 'a' => 1 }
|
63
|
+
described_class.unpatch!(b, diff).should == a
|
62
64
|
end
|
63
65
|
|
64
|
-
it
|
65
|
-
a = {
|
66
|
-
b = {
|
67
|
-
diff =
|
66
|
+
it 'is able to patch array under hash key with non-word characters' do
|
67
|
+
a = { 'a' => 1, 'b-b' => [1, 2] }
|
68
|
+
b = { 'a' => 1, 'b-b' => [2, 1] }
|
69
|
+
diff = described_class.diff(a, b)
|
68
70
|
|
69
|
-
|
71
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
70
72
|
|
71
|
-
a = {
|
72
|
-
b = {
|
73
|
-
|
73
|
+
a = { 'a' => 1, 'b-b' => [1, 2] }
|
74
|
+
b = { 'a' => 1, 'b-b' => [2, 1] }
|
75
|
+
described_class.unpatch!(b, diff).should == a
|
74
76
|
end
|
75
77
|
|
76
|
-
it
|
77
|
-
a = {
|
78
|
-
b = {
|
79
|
-
diff =
|
78
|
+
it 'is able to patch hash value removal' do
|
79
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
80
|
+
b = { 'a' => 1 }
|
81
|
+
diff = described_class.diff(a, b)
|
80
82
|
|
81
|
-
|
83
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
82
84
|
|
83
|
-
a = {
|
84
|
-
b = {
|
85
|
-
|
85
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
86
|
+
b = { 'a' => 1 }
|
87
|
+
described_class.unpatch!(b, diff).should == a
|
86
88
|
end
|
87
89
|
|
88
|
-
it
|
89
|
-
a = {
|
90
|
-
b = {
|
91
|
-
diff =
|
90
|
+
it 'is able to patch value hash <=> {}' do
|
91
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
92
|
+
b = { 'a' => 1, 'b' => {} }
|
93
|
+
diff = described_class.diff(a, b)
|
92
94
|
|
93
|
-
|
95
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
94
96
|
|
95
|
-
a = {
|
96
|
-
b = {
|
97
|
-
|
97
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
98
|
+
b = { 'a' => 1, 'b' => {} }
|
99
|
+
described_class.unpatch!(b, diff).should == a
|
98
100
|
end
|
99
101
|
|
100
|
-
it
|
101
|
-
a = {
|
102
|
-
b = {
|
103
|
-
diff =
|
102
|
+
it 'is able to patch value hash <=> nil' do
|
103
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
104
|
+
b = { 'a' => 1, 'b' => nil }
|
105
|
+
diff = described_class.diff(a, b)
|
104
106
|
|
105
|
-
|
107
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
106
108
|
|
107
|
-
a = {
|
108
|
-
b = {
|
109
|
-
|
109
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
110
|
+
b = { 'a' => 1, 'b' => nil }
|
111
|
+
described_class.unpatch!(b, diff).should == a
|
110
112
|
end
|
111
113
|
|
112
|
-
it
|
113
|
-
a = {
|
114
|
-
b = {
|
115
|
-
diff =
|
114
|
+
it 'is able to patch value nil removal' do
|
115
|
+
a = { 'a' => 1, 'b' => nil }
|
116
|
+
b = { 'a' => 1 }
|
117
|
+
diff = described_class.diff(a, b)
|
116
118
|
|
117
|
-
|
119
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
118
120
|
|
119
|
-
a = {
|
120
|
-
b = {
|
121
|
-
|
121
|
+
a = { 'a' => 1, 'b' => nil }
|
122
|
+
b = { 'a' => 1 }
|
123
|
+
described_class.unpatch!(b, diff).should == a
|
122
124
|
end
|
123
125
|
|
124
|
-
it
|
125
|
-
a = [{'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}, 3]
|
126
|
-
b = [1, {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}]
|
126
|
+
it 'is able to patch similar objects between arrays' do
|
127
|
+
a = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }, 3]
|
128
|
+
b = [1, { 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }]
|
127
129
|
|
128
|
-
diff =
|
129
|
-
|
130
|
+
diff = described_class.diff(a, b)
|
131
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
130
132
|
|
131
|
-
a = [{'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}, 3]
|
132
|
-
b = [1, {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}]
|
133
|
-
|
133
|
+
a = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }, 3]
|
134
|
+
b = [1, { 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }]
|
135
|
+
described_class.unpatch!(b, diff).should == a
|
134
136
|
end
|
135
137
|
|
136
|
-
it
|
137
|
-
a = [{'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}, {'x' => 5, 'y' => 6, 'z' => 3}, 1]
|
138
|
-
b = [1, {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}]
|
138
|
+
it 'is able to patch similar & equal objects between arrays' do
|
139
|
+
a = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }, { 'x' => 5, 'y' => 6, 'z' => 3 }, 1]
|
140
|
+
b = [1, { 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }]
|
139
141
|
|
140
|
-
diff =
|
141
|
-
|
142
|
+
diff = described_class.diff(a, b)
|
143
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
142
144
|
|
143
|
-
a = [{'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5}, {'x' => 5, 'y' => 6, 'z' => 3}, 1]
|
144
|
-
b = [1, {'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5}]
|
145
|
-
|
145
|
+
a = [{ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }, { 'x' => 5, 'y' => 6, 'z' => 3 }, 1]
|
146
|
+
b = [1, { 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }]
|
147
|
+
described_class.unpatch!(b, diff).should == a
|
146
148
|
end
|
147
149
|
|
148
|
-
it
|
149
|
-
a = {
|
150
|
-
b = {
|
151
|
-
diff =
|
150
|
+
it 'is able to patch hash value removal with custom delimiter' do
|
151
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
152
|
+
b = { 'a' => 1, 'b' => { 'b1' => 3 } }
|
153
|
+
diff = described_class.diff(a, b, delimiter: "\n")
|
152
154
|
|
153
|
-
|
155
|
+
expect(described_class.patch!(a, diff, delimiter: "\n")).to eq(b)
|
154
156
|
|
155
|
-
a = {
|
156
|
-
b = {
|
157
|
-
|
157
|
+
a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
|
158
|
+
b = { 'a' => 1, 'b' => { 'b1' => 3 } }
|
159
|
+
described_class.unpatch!(b, diff, delimiter: "\n").should == a
|
158
160
|
end
|
159
161
|
|
160
|
-
it
|
161
|
-
a = {
|
162
|
-
b = {
|
163
|
-
diff =
|
162
|
+
it 'is able to patch when the diff is generated with an array_path' do
|
163
|
+
a = { 'a' => 1, 'b' => 1 }
|
164
|
+
b = { 'a' => 1, 'b' => 2 }
|
165
|
+
diff = described_class.diff(a, b, array_path: true)
|
164
166
|
|
165
|
-
|
167
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
166
168
|
|
167
|
-
a = {
|
168
|
-
b = {
|
169
|
-
|
169
|
+
a = { 'a' => 1, 'b' => 1 }
|
170
|
+
b = { 'a' => 1, 'b' => 2 }
|
171
|
+
described_class.unpatch!(b, diff).should == a
|
170
172
|
end
|
171
173
|
|
172
|
-
it
|
173
|
-
a = {
|
174
|
-
b = {
|
175
|
-
diff =
|
174
|
+
it 'is able to use non string keys when diff is generated with an array_path' do
|
175
|
+
a = { 'a' => 1, :a => 2, 0 => 3 }
|
176
|
+
b = { 'a' => 5, :a => 6, 0 => 7 }
|
177
|
+
diff = described_class.diff(a, b, array_path: true)
|
176
178
|
|
177
|
-
|
179
|
+
expect(described_class.patch!(a, diff)).to eq(b)
|
178
180
|
|
179
|
-
a = {
|
180
|
-
b = {
|
181
|
-
|
181
|
+
a = { 'a' => 1, :a => 2, 0 => 3 }
|
182
|
+
b = { 'a' => 5, :a => 6, 0 => 7 }
|
183
|
+
described_class.unpatch!(b, diff).should == a
|
182
184
|
end
|
183
185
|
end
|