json_matcher 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/README.md +9 -0
- data/lib/json_matcher/version.rb +1 -1
- data/lib/json_matcher.rb +19 -11
- data/spec/json_matcher_spec.rb +10 -0
- metadata +7 -7
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm use --create 1.9.2@json_matcher
|
1
|
+
rvm use --create ruby-1.9.2-p180@json_matcher
|
data/README.md
CHANGED
@@ -10,3 +10,12 @@ It gives the difference in diff format with colors
|
|
10
10
|
JsonMatcher.similar a, b
|
11
11
|
|
12
12
|
It returns nil if similar and a diff if different
|
13
|
+
|
14
|
+
Check Spec section for details
|
15
|
+
|
16
|
+
# Options
|
17
|
+
|
18
|
+
The only option currently supported is exact
|
19
|
+
eg JsonMatcher.similar a, b, {:exact => true}
|
20
|
+
This makes the match exact
|
21
|
+
|
data/lib/json_matcher/version.rb
CHANGED
data/lib/json_matcher.rb
CHANGED
@@ -3,22 +3,26 @@ require 'json'
|
|
3
3
|
require 'json_matcher/version'
|
4
4
|
module JsonMatcher
|
5
5
|
|
6
|
-
def self.similar(actual, expected)
|
7
|
-
result = Matcher.new(actual, expected)
|
6
|
+
def self.similar(actual, expected, opts = {})
|
7
|
+
result = Matcher.new(actual, expected, opts)
|
8
8
|
result.similar? ? nil : result.to_s
|
9
9
|
end
|
10
10
|
|
11
11
|
class Matcher
|
12
12
|
include Term::ANSIColor
|
13
13
|
|
14
|
-
def initialize actual, expected,
|
15
|
-
@actual =
|
16
|
-
@expected =
|
14
|
+
def initialize actual, expected, opts = {}
|
15
|
+
@actual = actual
|
16
|
+
@expected = expected
|
17
|
+
@actual_hash = JSON.parse actual
|
18
|
+
@expected_hash = JSON.parse expected
|
19
|
+
@options = opts
|
17
20
|
@failure_msg = { :extra => {}, :less => {}}
|
18
21
|
end
|
19
22
|
|
20
23
|
def similar?
|
21
|
-
|
24
|
+
|
25
|
+
return @options[:exact] ? equal : equal_without_order
|
22
26
|
end
|
23
27
|
|
24
28
|
def to_s
|
@@ -26,18 +30,22 @@ module JsonMatcher
|
|
26
30
|
end
|
27
31
|
|
28
32
|
private
|
33
|
+
def equal
|
34
|
+
@actual == @expected
|
35
|
+
end
|
36
|
+
|
29
37
|
def equal_without_order
|
30
|
-
return true if @
|
31
|
-
@failure_msg[:extra] = @
|
32
|
-
@failure_msg[:less] = @
|
33
|
-
hash_matcher @
|
38
|
+
return true if @actual_hash == @expected_hash
|
39
|
+
@failure_msg[:extra] = @actual_hash.select {|key,val| !@expected_hash.keys.include? key}
|
40
|
+
@failure_msg[:less] = @expected_hash.select {|key,val| !@actual_hash.keys.include? key}
|
41
|
+
hash_matcher @actual_hash, @expected_hash
|
34
42
|
@failure_msg[:extra].length == 0 and @failure_msg[:less].length == 0
|
35
43
|
end
|
36
44
|
|
37
45
|
def hash_matcher first, second
|
38
46
|
first.each do |key, value|
|
39
47
|
return false unless first[key].class == second[key].class
|
40
|
-
if value and
|
48
|
+
if value and second[key]
|
41
49
|
if first[key].is_a? Hash
|
42
50
|
hash_matcher first[key], second[key]
|
43
51
|
elsif first[key].is_a? Array
|
data/spec/json_matcher_spec.rb
CHANGED
@@ -55,4 +55,14 @@ describe JsonMatcher do
|
|
55
55
|
expected = { :array_key => [3,2,1], :key => "value", :hash_key => { :array_key => [6,4,5], :normal_key => 1 } }.to_json
|
56
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
57
|
end
|
58
|
+
|
59
|
+
describe "options" do
|
60
|
+
describe "exact match" do
|
61
|
+
it "should just do exact match if option exact set to true" do
|
62
|
+
actual = { :key => "value", :extra_key => "value2", :another_key => "value3" }.to_json
|
63
|
+
expected = { :extra_key => "value2", :another_key => "value3", :key => "value" }.to_json
|
64
|
+
JsonMatcher.similar(actual, expected, {:exact => true}).should_not be_nil
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
58
68
|
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.4
|
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: &2165169260 !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: *2165169260
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: term-ansicolor
|
27
|
-
requirement: &
|
27
|
+
requirement: &2165168840 !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: *2165168840
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2165168420 !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: *2165168420
|
47
47
|
description: A gem that matches similar json and ignores order
|
48
48
|
email:
|
49
49
|
- garima.slideshare@gmail.com
|