hashdiff 1.0.1 → 1.2.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.
@@ -1,185 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
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)
10
-
11
- expect(described_class.patch!(a, diff)).to eq(b)
12
-
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
16
- end
17
-
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)
22
-
23
- expect(described_class.patch!(a, diff)).to eq(b)
24
-
25
- a = { 'a' => 3 }
26
- b = { 'a' => { 'a1' => 1, 'a2' => 2 } }
27
- described_class.unpatch!(b, diff).should == a
28
- end
29
-
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)
34
-
35
- expect(described_class.patch!(a, diff)).to eq(b)
36
-
37
- a = { 'a' => 1, 'b' => [1, 2] }
38
- b = { 'a' => 1, 'b' => [] }
39
- described_class.unpatch!(b, diff).should == a
40
- end
41
-
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)
46
-
47
- expect(described_class.patch!(a, diff)).to eq(b)
48
-
49
- a = { 'a' => 1, 'b' => [1, 2] }
50
- b = { 'a' => 1, 'b' => nil }
51
- described_class.unpatch!(b, diff).should == a
52
- end
53
-
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)
58
-
59
- expect(described_class.patch!(a, diff)).to eq(b)
60
-
61
- a = { 'a' => 1, 'b' => [1, 2] }
62
- b = { 'a' => 1 }
63
- described_class.unpatch!(b, diff).should == a
64
- end
65
-
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)
70
-
71
- expect(described_class.patch!(a, diff)).to eq(b)
72
-
73
- a = { 'a' => 1, 'b-b' => [1, 2] }
74
- b = { 'a' => 1, 'b-b' => [2, 1] }
75
- described_class.unpatch!(b, diff).should == a
76
- end
77
-
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)
82
-
83
- expect(described_class.patch!(a, diff)).to eq(b)
84
-
85
- a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
86
- b = { 'a' => 1 }
87
- described_class.unpatch!(b, diff).should == a
88
- end
89
-
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)
94
-
95
- expect(described_class.patch!(a, diff)).to eq(b)
96
-
97
- a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
98
- b = { 'a' => 1, 'b' => {} }
99
- described_class.unpatch!(b, diff).should == a
100
- end
101
-
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)
106
-
107
- expect(described_class.patch!(a, diff)).to eq(b)
108
-
109
- a = { 'a' => 1, 'b' => { 'b1' => 1, 'b2' => 2 } }
110
- b = { 'a' => 1, 'b' => nil }
111
- described_class.unpatch!(b, diff).should == a
112
- end
113
-
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)
118
-
119
- expect(described_class.patch!(a, diff)).to eq(b)
120
-
121
- a = { 'a' => 1, 'b' => nil }
122
- b = { 'a' => 1 }
123
- described_class.unpatch!(b, diff).should == a
124
- end
125
-
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 }]
129
-
130
- diff = described_class.diff(a, b)
131
- expect(described_class.patch!(a, diff)).to eq(b)
132
-
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
136
- end
137
-
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 }]
141
-
142
- diff = described_class.diff(a, b)
143
- expect(described_class.patch!(a, diff)).to eq(b)
144
-
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
148
- end
149
-
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")
154
-
155
- expect(described_class.patch!(a, diff, delimiter: "\n")).to eq(b)
156
-
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
160
- end
161
-
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)
166
-
167
- expect(described_class.patch!(a, diff)).to eq(b)
168
-
169
- a = { 'a' => 1, 'b' => 1 }
170
- b = { 'a' => 1, 'b' => 2 }
171
- described_class.unpatch!(b, diff).should == a
172
- end
173
-
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)
178
-
179
- expect(described_class.patch!(a, diff)).to eq(b)
180
-
181
- a = { 'a' => 1, :a => 2, 0 => 3 }
182
- b = { 'a' => 5, :a => 6, 0 => 7 }
183
- described_class.unpatch!(b, diff).should == a
184
- end
185
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe 'README.md' do
6
- it 'has correct examples' do
7
- File.read('README.md').scan(/```ruby(.*?)```/m).flatten(1).each do |block|
8
- begin
9
- eval block # rubocop:disable Security/Eval
10
- rescue Exception => e # rubocop:disable Lint/RescueException
11
- raise "README.md code block:\n#{block}\n\nhas error:\n#{e}"
12
- end
13
- end
14
- end
15
- end
@@ -1,116 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Hashdiff do
6
- it 'is able to decode property path' do
7
- decoded = described_class.send(:decode_property_path, 'a.b[0].c.city[5]')
8
- decoded.should == ['a', 'b', 0, 'c', 'city', 5]
9
- end
10
-
11
- it 'is able to decode property path with custom delimiter' do
12
- decoded = described_class.send(:decode_property_path, "a\tb[0]\tc\tcity[5]", "\t")
13
- decoded.should == ['a', 'b', 0, 'c', 'city', 5]
14
- end
15
-
16
- it 'is able to tell similiar hash' do
17
- a = { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }
18
- b = { 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5 }
19
- described_class.similar?(a, b).should be true
20
- described_class.similar?(a, b, similarity: 1).should be false
21
- end
22
-
23
- it 'is able to tell similiar empty hash' do
24
- described_class.similar?({}, {}, similarity: 1).should be true
25
- end
26
-
27
- it 'is able to tell similiar empty array' do
28
- described_class.similar?([], [], similarity: 1).should be true
29
- end
30
-
31
- it 'is able to tell similiar hash with values within tolerance' do
32
- a = { 'a' => 1.5, 'b' => 2.25, 'c' => 3, 'd' => 4, 'e' => 5 }
33
- b = { 'a' => 1.503, 'b' => 2.22, 'c' => 3, 'e' => 5 }
34
- described_class.similar?(a, b, numeric_tolerance: 0.05).should be true
35
- described_class.similar?(a, b).should be false
36
- end
37
-
38
- it 'is able to tell numbers and strings' do
39
- described_class.similar?(1, 2).should be false
40
- described_class.similar?('a', 'b').should be false
41
- described_class.similar?('a', [1, 2, 3]).should be false
42
- described_class.similar?(1, 'a' => 1, 'b' => 2, 'c' => 3, 'e' => 5).should be false
43
- end
44
-
45
- it 'is able to tell true when similarity == 0.5' do
46
- a = { 'value' => 'New1', 'onclick' => 'CreateNewDoc()' }
47
- b = { 'value' => 'New', 'onclick' => 'CreateNewDoc()' }
48
-
49
- described_class.similar?(a, b, similarity: 0.5).should be true
50
- end
51
-
52
- it 'is able to tell false when similarity == 0.5' do
53
- a = { 'value' => 'New1', 'onclick' => 'open()' }
54
- b = { 'value' => 'New', 'onclick' => 'CreateNewDoc()' }
55
-
56
- described_class.similar?(a, b, similarity: 0.5).should be false
57
- end
58
-
59
- describe '.compare_values' do
60
- it 'compares numeric values exactly when no tolerance' do
61
- expect(described_class.compare_values(10.004, 10.003)).to be false
62
- end
63
-
64
- it 'allows tolerance with numeric values' do
65
- expect(described_class.compare_values(10.004, 10.003, numeric_tolerance: 0.01)).to be true
66
- end
67
-
68
- it 'compares different objects without tolerance' do
69
- expect(described_class.compare_values('hats', 'ninjas')).to be false
70
- end
71
-
72
- it 'compares other objects with tolerance' do
73
- expect(described_class.compare_values('hats', 'ninjas', numeric_tolerance: 0.01)).to be false
74
- end
75
-
76
- it 'compares same objects without tolerance' do
77
- expect(described_class.compare_values('horse', 'horse')).to be true
78
- end
79
-
80
- it 'compares strings for spaces exactly by default' do
81
- expect(described_class.compare_values(' horse', 'horse')).to be false
82
- end
83
-
84
- it 'compares strings for capitalization exactly by default' do
85
- expect(described_class.compare_values('horse', 'Horse')).to be false
86
- end
87
-
88
- it 'strips strings before comparing when requested' do
89
- expect(described_class.compare_values(' horse', 'horse', strip: true)).to be true
90
- end
91
-
92
- it 'ignores string case when requested' do
93
- expect(described_class.compare_values('horse', 'Horse', case_insensitive: true)).to be true
94
- end
95
- end
96
-
97
- describe '.comparable?' do
98
- it 'identifies hashes as comparable' do
99
- expect(described_class.comparable?({}, {})).to be true
100
- end
101
-
102
- it 'identifies a subclass of Hash to be comparable with a Hash' do
103
- other = Class.new(Hash)
104
- expect(described_class.comparable?(other.new, {})).to be true
105
- end
106
-
107
- it 'identifies a Hash to be comparable with a subclass of Hash' do
108
- other = Class.new(Hash)
109
- expect(described_class.comparable?({}, other.new)).to be true
110
- end
111
-
112
- it 'does not identify a Numeric as comparable with a Hash' do
113
- expect(described_class.comparable?(1, {})).to be false
114
- end
115
- end
116
- end
data/spec/spec_helper.rb DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
4
-
5
- require 'rubygems'
6
- require 'rspec'
7
- require 'rspec/autorun'
8
-
9
- require 'hashdiff'
10
-
11
- RSpec.configure do |config|
12
- config.mock_framework = :rspec
13
-
14
- config.include RSpec::Matchers
15
- end