json_matcher 0.0.2 → 0.0.3
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.
- data/lib/json_matcher.rb +15 -9
- data/lib/json_matcher/version.rb +1 -1
- data/spec/json_matcher_spec.rb +12 -0
- metadata +7 -7
data/lib/json_matcher.rb
CHANGED
@@ -11,7 +11,7 @@ module JsonMatcher
|
|
11
11
|
class Matcher
|
12
12
|
include Term::ANSIColor
|
13
13
|
|
14
|
-
def initialize actual, expected
|
14
|
+
def initialize actual, expected, opt = {}
|
15
15
|
@actual = JSON.parse actual
|
16
16
|
@expected = JSON.parse expected
|
17
17
|
@failure_msg = { :extra => {}, :less => {}}
|
@@ -30,23 +30,29 @@ module JsonMatcher
|
|
30
30
|
return true if @actual == @expected
|
31
31
|
@failure_msg[:extra] = @actual.select {|key,val| !@expected.keys.include? key}
|
32
32
|
@failure_msg[:less] = @expected.select {|key,val| !@actual.keys.include? key}
|
33
|
-
@actual
|
34
|
-
|
33
|
+
hash_matcher @actual, @expected
|
34
|
+
@failure_msg[:extra].length == 0 and @failure_msg[:less].length == 0
|
35
|
+
end
|
36
|
+
|
37
|
+
def hash_matcher first, second
|
38
|
+
first.each do |key, value|
|
39
|
+
return false unless first[key].class == second[key].class
|
35
40
|
if value and @expected[key]
|
36
|
-
if
|
37
|
-
|
41
|
+
if first[key].is_a? Hash
|
42
|
+
hash_matcher first[key], second[key]
|
43
|
+
elsif first[key].is_a? Array
|
44
|
+
if value.sort != second[key].sort
|
38
45
|
@failure_msg[:extra].merge!(key => value)
|
39
|
-
@failure_msg[:less].merge!(key =>
|
46
|
+
@failure_msg[:less].merge!(key => second[key])
|
40
47
|
end
|
41
48
|
else
|
42
|
-
if value !=
|
49
|
+
if value != second[key]
|
43
50
|
@failure_msg[:extra].merge!(key => value)
|
44
|
-
@failure_msg[:less].merge!(key =>
|
51
|
+
@failure_msg[:less].merge!(key => second[key])
|
45
52
|
end
|
46
53
|
end
|
47
54
|
end
|
48
55
|
end
|
49
|
-
@failure_msg[:extra].length == 0 and @failure_msg[:less].length == 0
|
50
56
|
end
|
51
57
|
|
52
58
|
end
|
data/lib/json_matcher/version.rb
CHANGED
data/spec/json_matcher_spec.rb
CHANGED
@@ -43,4 +43,16 @@ describe JsonMatcher do
|
|
43
43
|
expected = { :array_key => [3,2,1,4], :key => "value" }.to_json
|
44
44
|
JsonMatcher.similar(actual, expected).should == "\e[31m\e[1mDiff:\n+{\"array_key\":[1,2,3]}\n-{\"array_key\":[3,2,1,4]}\e[0m\e[0m"
|
45
45
|
end
|
46
|
+
|
47
|
+
it "should match if json with a hash with a array has different sort but exact value" do
|
48
|
+
actual = { :key => "value", :array_key => [1,2,3], :hash_key => { :normal_key => 1, :array_key => [4,5,6] }, :zkey => 9 }.to_json
|
49
|
+
expected = { :array_key => [3,2,1], :key => "value", :hash_key => { :array_key => [6,4,5], :normal_key => 1 }, :zkey =>9 }.to_json
|
50
|
+
JsonMatcher.similar(actual, expected).should be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should not match if json with a hash with a array has different sort but exact value" do
|
54
|
+
actual = { :key => "value", :array_key => [1,2,3], :hash_key => { :normal_key => 1, :array_key => [4,5,6,7] } }.to_json
|
55
|
+
expected = { :array_key => [3,2,1], :key => "value", :hash_key => { :array_key => [6,4,5], :normal_key => 1 } }.to_json
|
56
|
+
JsonMatcher.similar(actual, expected).should == "\e[31m\e[1mDiff:\n+{\"array_key\":[4,5,6,7]}\n-{\"array_key\":[6,4,5]}\e[0m\e[0m"
|
57
|
+
end
|
46
58
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-22 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &2164428680 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2164428680
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: term-ansicolor
|
27
|
-
requirement: &
|
27
|
+
requirement: &2164428260 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2164428260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2164427840 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2164427840
|
47
47
|
description: A gem that matches similar json and ignores order
|
48
48
|
email:
|
49
49
|
- garima.slideshare@gmail.com
|