json_matcher 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/json_matcher.gemspec +26 -0
- data/lib/json_matcher.rb +53 -0
- data/lib/json_matcher/version.rb +3 -0
- data/spec/json_matcher_spec.rb +46 -0
- data/spec/spec_helper.rb +9 -0
- metadata +89 -0
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create 1.9.2@json_matcher
|
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "json_matcher/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "json_matcher"
|
7
|
+
s.version = JsonMatcher::VERSION
|
8
|
+
s.authors = ["Garima Singh"]
|
9
|
+
s.email = ["garima.slideshare@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A gem that matches similar json and ignores order}
|
12
|
+
s.description = %{A gem that matches similar json and ignores order}
|
13
|
+
|
14
|
+
s.rubyforge_project = "json_matcher"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_runtime_dependency "json"
|
23
|
+
s.add_runtime_dependency "term-ansicolor"
|
24
|
+
|
25
|
+
s.add_development_dependency "rspec"
|
26
|
+
end
|
data/lib/json_matcher.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'term/ansicolor'
|
2
|
+
require 'json'
|
3
|
+
require 'json_matcher/version'
|
4
|
+
module JsonMatcher
|
5
|
+
|
6
|
+
def self.similar(actual, expected)
|
7
|
+
result = Matcher.new(actual, expected)
|
8
|
+
result.similar? ? nil : result.to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
class Matcher
|
12
|
+
include Term::ANSIColor
|
13
|
+
|
14
|
+
def initialize actual, expected
|
15
|
+
@actual = JSON.parse actual
|
16
|
+
@expected = JSON.parse expected
|
17
|
+
@failure_msg = { :extra => {}, :less => {}}
|
18
|
+
end
|
19
|
+
|
20
|
+
def similar?
|
21
|
+
return equal_without_order
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
red { bold {"expected #{@expected.to_json},\ngot #{@actual.to_json}\nDiff:\n+#{@failure_msg[:extra].to_json}\n-#{@failure_msg[:less].to_json}" } }
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def equal_without_order
|
30
|
+
return true if @actual == @expected
|
31
|
+
@failure_msg[:extra] = @actual.select {|key,val| !@expected.keys.include? key}
|
32
|
+
@failure_msg[:less] = @expected.select {|key,val| !@actual.keys.include? key}
|
33
|
+
@actual.each do |key, value|
|
34
|
+
return false unless @actual[key].class == @expected[key].class
|
35
|
+
if value and @expected[key]
|
36
|
+
if @actual[key].is_a? Array
|
37
|
+
if value.sort != @expected[key].sort
|
38
|
+
@failure_msg[:extra].merge!(key => value)
|
39
|
+
@failure_msg[:less].merge!(key => @expected[key])
|
40
|
+
end
|
41
|
+
else
|
42
|
+
if value != @expected[key]
|
43
|
+
@failure_msg[:extra].merge!(key => value)
|
44
|
+
@failure_msg[:less].merge!(key => @expected[key])
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
@failure_msg[:extra].length == 0 and @failure_msg[:less].length == 0
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'term/ansicolor'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe JsonMatcher do
|
5
|
+
it "should check if both json arguments match" do
|
6
|
+
actual = { :key => "value" }.to_json
|
7
|
+
expected = { :key => "value" }.to_json
|
8
|
+
JsonMatcher.similar(actual, expected).should be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should check if both json arguments dont match" do
|
12
|
+
actual = { :key => "value1" }.to_json
|
13
|
+
expected = { :key => "value" }.to_json
|
14
|
+
JsonMatcher.similar(actual, expected).should == "\e[31m\e[1mexpected #{expected},\ngot #{actual}\nDiff:\n+{\"key\":\"value1\"}\n-{\"key\":\"value\"}\e[0m\e[0m"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should check if number of keys are unequal" do
|
18
|
+
actual = { :key => "value", :extra_key => "extra_value" }.to_json
|
19
|
+
expected = { :key => "value" }.to_json
|
20
|
+
JsonMatcher.similar(actual, expected).should == "\e[31m\e[1mexpected #{expected},\ngot #{actual}\nDiff:\n+{\"extra_key\":\"extra_value\"}\n-{}\e[0m\e[0m"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should check if number of keys are equal but values aren't" do
|
24
|
+
actual = { :key => "value", :extra_key => "value" }.to_json
|
25
|
+
expected = { :key => "value", :extra_key => "value_new" }.to_json
|
26
|
+
JsonMatcher.similar(actual, expected).should == "\e[31m\e[1mexpected #{expected},\ngot #{actual}\nDiff:\n+{\"extra_key\":\"value\"}\n-{\"extra_key\":\"value_new\"}\e[0m\e[0m"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should check if number of keys are equal, values are equal but order is different" do
|
30
|
+
actual = { :key => "value", :extra_key => "value2", :another_key => "value3" }.to_json
|
31
|
+
expected = { :extra_key => "value2", :another_key => "value3", :key => "value" }.to_json
|
32
|
+
JsonMatcher.similar(actual, expected).should be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should check if number of keys and values are equal but array value is not sorted" do
|
36
|
+
actual = { :key => "value", :extra_key => "value2", :array_key => [1,2,3] }.to_json
|
37
|
+
expected = { :extra_key => "value2", :array_key => [3,2,1], :key => "value" }.to_json
|
38
|
+
JsonMatcher.similar(actual, expected).should be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not match if array for the a key doesnt match and give required error" do
|
42
|
+
actual = { :key => "value", :array_key => [1,2,3] }.to_json
|
43
|
+
expected = { :array_key => [3,2,1,4], :key => "value" }.to_json
|
44
|
+
JsonMatcher.similar(actual, expected).should == "\e[31m\e[1mexpected #{expected},\ngot #{actual}\nDiff:\n+{\"array_key\":[1,2,3]}\n-{\"array_key\":[3,2,1,4]}\e[0m\e[0m"
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_matcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Garima Singh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-19 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: &2155964220 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2155964220
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: term-ansicolor
|
27
|
+
requirement: &2155963800 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2155963800
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &2155963380 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2155963380
|
47
|
+
description: A gem that matches similar json and ignores order
|
48
|
+
email:
|
49
|
+
- garima.slideshare@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rvmrc
|
56
|
+
- Gemfile
|
57
|
+
- Rakefile
|
58
|
+
- json_matcher.gemspec
|
59
|
+
- lib/json_matcher.rb
|
60
|
+
- lib/json_matcher/version.rb
|
61
|
+
- spec/json_matcher_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: ''
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project: json_matcher
|
83
|
+
rubygems_version: 1.8.5
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: A gem that matches similar json and ignores order
|
87
|
+
test_files:
|
88
|
+
- spec/json_matcher_spec.rb
|
89
|
+
- spec/spec_helper.rb
|