json-compare 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,5 @@ language: ruby
2
2
  rvm:
3
3
  - 1.8.7
4
4
  - 1.9.2
5
- - 1.9.3
6
- - rbx-18mode
5
+ - 1.9.3
7
6
  - ree
data/CHANGES CHANGED
@@ -1,3 +1,8 @@
1
+ = 0.1.8
2
+
3
+ * FIX: comparing a String with a Fixnum (Marc Neumann)
4
+ * FIX: return remove when old_hash has more keys then the new_hash (Marc Neumann)
5
+
1
6
  = 0.1.6
2
7
 
3
8
  * Fixed comparison of fixnum array elements (robworley)
@@ -6,7 +6,7 @@ module JsonCompare
6
6
  def is_boolean(obj)
7
7
  !!obj == obj
8
8
  end
9
-
9
+
10
10
  def compare_elements(old, new)
11
11
  diff = {}
12
12
  if old.kind_of? Hash
@@ -29,16 +29,22 @@ module JsonCompare
29
29
  end
30
30
 
31
31
  def compare_hashes(old_hash, new_hash)
32
+ old_hash = old_hash == nil ? {} : old_hash
33
+ new_hash = new_hash == nil ? {} : new_hash
32
34
  keys = (old_hash.keys + new_hash.keys).uniq
33
35
  result = get_diffs_struct
34
36
  keys.each do |k|
35
37
  if !old_hash.has_key? k
36
38
  result[:append][k] = new_hash[k]
37
39
  elsif !new_hash.has_key? k
38
- result[:remove][k] = new_hash[k]
40
+ result[:remove][k] = old_hash[k]
39
41
  else
40
42
  diff = compare_elements(old_hash[k], new_hash[k])
41
- result[:update][k] = diff unless diff.empty?
43
+ if diff.respond_to? "empty?"
44
+ result[:update][k] = diff unless diff.empty?
45
+ else
46
+ result[:update][k] = diff # some classes have no empty? method
47
+ end
42
48
  end
43
49
  end
44
50
  filter_results(result)
@@ -1,3 +1,3 @@
1
1
  module JsonCompare
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -13,6 +13,22 @@ describe 'Json compare' do
13
13
  end
14
14
  end
15
15
 
16
+ describe 'Comparison of different classes' do
17
+ it 'should compare String with Fixnum' do
18
+ json1 = Yajl::Parser.parse('{"a":"1"}')
19
+ json2 = Yajl::Parser.parse('{"a":1}')
20
+ result = JsonCompare.get_diff(json1,json2)
21
+ result.should eq({:update=>{"a"=>1}})
22
+ end
23
+
24
+ it 'should compare String with Float' do
25
+ json1 = Yajl::Parser.parse('{"a":"1"}')
26
+ json2 = Yajl::Parser.parse('{"a":1.0}')
27
+ result = JsonCompare.get_diff(json1,json2)
28
+ result.should eq({:update=>{"a"=>1}})
29
+ end
30
+ end
31
+
16
32
  describe 'Hashes Comparison' do
17
33
  it 'should return empty Hash' do
18
34
  result = JsonCompare.get_diff({}, {})
@@ -97,6 +113,11 @@ describe 'Json compare' do
97
113
  "empty"=>nil
98
114
  }]
99
115
  }]
116
+ },
117
+ :remove => {
118
+ "Something" => [{
119
+ "Something" => [{}]
120
+ }]
100
121
  }
101
122
  }
102
123
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-compare
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-04 00:00:00.000000000 Z
12
+ date: 2013-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake