json-deep-compare 0.0.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +68 -0
- data/Rakefile +1 -0
- data/json-deep-compare.gemspec +23 -0
- data/lib/json-deep-compare.rb +115 -0
- data/test/json_deep_compare_test.rb +126 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Francis Hwang
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# JsonDeepCompare
|
2
|
+
|
3
|
+
For quickly finding differences between two large JSON documents.
|
4
|
+
|
5
|
+
Currently JsonDeepCompare is test-oriented. Its utility might be
|
6
|
+
expanded in the future.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
To use in a Test::Unit test case, include `JsonDeepCompare::Assertions`:
|
11
|
+
|
12
|
+
class MyTest < Test::Unit::TestCase
|
13
|
+
include JsonDeepCompare::Assertions
|
14
|
+
|
15
|
+
If you're using Rails, put this in `test/test_helper.rb`:
|
16
|
+
|
17
|
+
class ActiveSupport::TestCase
|
18
|
+
include JsonDeepCompare::Assertions
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
`JsonDeepCompare::Assertions` provides the `assert_json_equal` method:
|
23
|
+
|
24
|
+
class MyTest
|
25
|
+
include JsonDeepCompare::Assertions
|
26
|
+
|
27
|
+
def test_comparison
|
28
|
+
left_value = {
|
29
|
+
'total_rows' => 2,
|
30
|
+
'rows' => [
|
31
|
+
{
|
32
|
+
'id' => 'foo',
|
33
|
+
'doc' => {
|
34
|
+
'_id' => 'foo', 'title' => 'Foo', 'sub_document' => { 'one' => 'two' }
|
35
|
+
}
|
36
|
+
}
|
37
|
+
]
|
38
|
+
}
|
39
|
+
right_value = {
|
40
|
+
'total_rows' => 2,
|
41
|
+
'rows' => [
|
42
|
+
{
|
43
|
+
'id' => 'foo',
|
44
|
+
'doc' => {
|
45
|
+
'_id' => 'foo', 'title' => 'Foo', 'sub_document' => { 'one' => '1' }
|
46
|
+
}
|
47
|
+
}
|
48
|
+
]
|
49
|
+
}
|
50
|
+
assert_json_equal(left_value, right_value)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Running this test will yield this error:
|
55
|
+
|
56
|
+
RuntimeError: ":root > .rows :nth-child(1) > .doc > .sub_document > .one" expected to be "two" but was "1"
|
57
|
+
|
58
|
+
The selector syntax uses a limited subset of
|
59
|
+
[JSONSelect](http://jsonselect.org/) to describe where to find the
|
60
|
+
differences.
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
1. Fork it
|
65
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
66
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
67
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
68
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'json-deep-compare'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "json-deep-compare"
|
8
|
+
spec.version = JsonDeepCompare::VERSION
|
9
|
+
spec.authors = ["Francis Hwang"]
|
10
|
+
spec.email = ["sera@fhwang.net"]
|
11
|
+
spec.description = %q{For quickly finding differences between two large JSON documents.}
|
12
|
+
spec.summary = %q{For quickly finding differences between two large JSON documents.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module JsonDeepCompare
|
2
|
+
VERSION = '0.0.1'
|
3
|
+
|
4
|
+
class NodeComparison
|
5
|
+
ExcerptPadding = 15
|
6
|
+
attr_reader :left_value, :right_value, :selector
|
7
|
+
|
8
|
+
def initialize(left_value, right_value, options = {})
|
9
|
+
@left_value, @right_value = left_value, right_value
|
10
|
+
@selector = options[:selector] || ':root'
|
11
|
+
@exclusions = options[:exclusions]
|
12
|
+
@exclusions = [@exclusions] unless @exclusions.is_a?(Array)
|
13
|
+
@children = []
|
14
|
+
if left_value.is_a?(Hash)
|
15
|
+
if right_value.is_a?(Hash)
|
16
|
+
left_value.each do |key, left_sub_value|
|
17
|
+
@children << NodeComparison.new(
|
18
|
+
left_sub_value, right_value[key],
|
19
|
+
selector: "#{selector} > .#{key}", exclusions: @exclusions
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
elsif left_value.is_a?(Array)
|
24
|
+
if right_value.is_a?(Array)
|
25
|
+
left_value.each_with_index do |left_sub_value, i|
|
26
|
+
@children << NodeComparison.new(
|
27
|
+
left_sub_value, right_value[i],
|
28
|
+
selector: "#{selector} :nth-child(#{i+1})",
|
29
|
+
exclusions: @exclusions
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def value_inspect(value)
|
37
|
+
str = value.inspect
|
38
|
+
if str.length >= 40
|
39
|
+
"#{value.class.name} #{str[0..37]}..."
|
40
|
+
else
|
41
|
+
str
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def difference_message
|
46
|
+
unless equal?
|
47
|
+
if leaf?
|
48
|
+
if excerptable_difference?
|
49
|
+
excerpted_difference
|
50
|
+
else
|
51
|
+
"#{@selector.inspect} expected to be #{value_inspect(@left_value)} but was #{value_inspect(@right_value)}"
|
52
|
+
end
|
53
|
+
else
|
54
|
+
@children.reject(&:equal?).map(&:difference_message).join("\n")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def equal?
|
60
|
+
if leaf?
|
61
|
+
@exclusions.include?(@selector) || @left_value == @right_value
|
62
|
+
else
|
63
|
+
@children.all?(&:equal?)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def excerptable_difference?
|
68
|
+
@left_value.is_a?(String) and @right_value.is_a?(String) && (
|
69
|
+
@left_value.size > ExcerptPadding * 2 ||
|
70
|
+
@right_value.size > ExcerptPadding * 2
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def excerpted_difference
|
75
|
+
difference_start = (0..@left_value.length).detect { |i|
|
76
|
+
@left_value[i] != @right_value[i]
|
77
|
+
}
|
78
|
+
range_start = if difference_start > ExcerptPadding
|
79
|
+
difference_start - ExcerptPadding
|
80
|
+
else
|
81
|
+
0
|
82
|
+
end
|
83
|
+
left_excerpt = @left_value[
|
84
|
+
range_start..difference_start+ExcerptPadding
|
85
|
+
]
|
86
|
+
right_excerpt = @right_value[
|
87
|
+
range_start..difference_start+ExcerptPadding
|
88
|
+
]
|
89
|
+
if difference_start - ExcerptPadding > 0
|
90
|
+
left_excerpt = "..." + left_excerpt
|
91
|
+
right_excerpt = "..." + right_excerpt
|
92
|
+
end
|
93
|
+
if difference_start + ExcerptPadding < @left_value.length
|
94
|
+
left_excerpt = left_excerpt + '...'
|
95
|
+
end
|
96
|
+
if difference_start + ExcerptPadding < @right_value.length
|
97
|
+
right_excerpt = right_excerpt + '...'
|
98
|
+
end
|
99
|
+
"#{@selector.inspect} differs starting at char #{difference_start}: #{left_excerpt.inspect} differs from #{right_excerpt.inspect}"
|
100
|
+
end
|
101
|
+
|
102
|
+
def leaf?
|
103
|
+
@children.empty?
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
module Assertions
|
108
|
+
def assert_json_equal(expected, actual, exclusions = nil)
|
109
|
+
comparison = NodeComparison.new(expected, actual, exclusions: exclusions)
|
110
|
+
unless comparison.equal?
|
111
|
+
fail comparison.difference_message
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$: << '.'
|
3
|
+
require 'lib/json-deep-compare'
|
4
|
+
|
5
|
+
class NodeComparisonTestCase < Test::Unit::TestCase
|
6
|
+
def test_detects_difference_in_atoms
|
7
|
+
left_value = {
|
8
|
+
'total_rows' => 2,
|
9
|
+
'rows' => [
|
10
|
+
{
|
11
|
+
'id' => 'foo',
|
12
|
+
'doc' => {
|
13
|
+
'_id' => 'foo',
|
14
|
+
'title' => 'Foo',
|
15
|
+
'sub_document' => { 'one' => 'two' }
|
16
|
+
}
|
17
|
+
}
|
18
|
+
]
|
19
|
+
}
|
20
|
+
right_value = {
|
21
|
+
'total_rows' => 2,
|
22
|
+
'rows' => [
|
23
|
+
{
|
24
|
+
'id' => 'foo',
|
25
|
+
'doc' => {
|
26
|
+
'_id' => 'foo',
|
27
|
+
'title' => 'Foo',
|
28
|
+
'sub_document' => { 'one' => '1' }
|
29
|
+
}
|
30
|
+
}
|
31
|
+
]
|
32
|
+
}
|
33
|
+
comparison = JsonDeepCompare::NodeComparison.new(left_value, right_value)
|
34
|
+
assert !comparison.equal?
|
35
|
+
assert_equal(
|
36
|
+
"\":root > .rows :nth-child(1) > .doc > .sub_document > .one\" expected to be \"two\" but was \"1\"",
|
37
|
+
comparison.difference_message
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_detects_differences_in_keys
|
42
|
+
left_value = {"doc" => {"foo" => "bar"}}
|
43
|
+
right_value = {"doc" => {"biz" => "bang", "bing" => nil}}
|
44
|
+
comparison = JsonDeepCompare::NodeComparison.new(left_value, right_value)
|
45
|
+
assert !comparison.equal?
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_detects_difference_in_type
|
49
|
+
lval = {"foo" => {'bar' => 'bang'}}
|
50
|
+
rval = {"foo" => [3,4,5]}
|
51
|
+
comparison = JsonDeepCompare::NodeComparison.new(lval, rval)
|
52
|
+
assert !comparison.equal?
|
53
|
+
assert_equal(
|
54
|
+
"\":root > .foo\" expected to be {\"bar\"=>\"bang\"} but was [3, 4, 5]",
|
55
|
+
comparison.difference_message
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_detects_difference_in_type_and_truncates
|
60
|
+
lval_hash = {}
|
61
|
+
'A'.upto('Z').each do |letter|
|
62
|
+
lval_hash[letter] = letter
|
63
|
+
end
|
64
|
+
lval = {"foo" => lval_hash}
|
65
|
+
rval = {"foo" => [3,4,5]}
|
66
|
+
comparison = JsonDeepCompare::NodeComparison.new(lval, rval)
|
67
|
+
assert !comparison.equal?
|
68
|
+
assert_equal(
|
69
|
+
"\":root > .foo\" expected to be Hash {\"A\"=>\"A\", \"B\"=>\"B\", \"C\"=>\"C\", \"D\"=>\"D... but was [3, 4, 5]",
|
70
|
+
comparison.difference_message
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_lists_multliple_differences
|
75
|
+
lval = {'one' => {'two' => 'three', 'four' => 'five'}, 'six' => 'seven'}
|
76
|
+
rval = {'one' => {'two' => 'TWO', 'four' => 'five'}, 'six' => 'SIX'}
|
77
|
+
comparison = JsonDeepCompare::NodeComparison.new(lval, rval)
|
78
|
+
assert !comparison.equal?
|
79
|
+
assert_equal(
|
80
|
+
"\":root > .one > .two\" expected to be \"three\" but was \"TWO\"\n\":root > .six\" expected to be \"seven\" but was \"SIX\"",
|
81
|
+
comparison.difference_message
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_detects_difference_between_array_and_nil
|
86
|
+
lval = {'one' => [1,2,3]}
|
87
|
+
rval = {'one' => nil}
|
88
|
+
comparison = JsonDeepCompare::NodeComparison.new(lval, rval)
|
89
|
+
assert !comparison.equal?
|
90
|
+
assert_equal(
|
91
|
+
"\":root > .one\" expected to be [1, 2, 3] but was nil",
|
92
|
+
comparison.difference_message
|
93
|
+
)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_shows_first_string_difference
|
97
|
+
lval = {'body' => "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."}
|
98
|
+
rval = {'body' => "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal."}
|
99
|
+
comparison = JsonDeepCompare::NodeComparison.new(lval, rval)
|
100
|
+
assert !comparison.equal?
|
101
|
+
assert_equal(
|
102
|
+
"\":root > .body\" differs starting at char 103: \"..., conceived in Liberty, and ded...\" differs from \"..., conceived in liberty, and ded...\"",
|
103
|
+
comparison.difference_message
|
104
|
+
)
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_shows_string_difference_start
|
108
|
+
lval = {'rev' => "22-23c92a95665bb692313229c8224b7088"}
|
109
|
+
rval = {'rev' => "23-54a5106f8c522a57d6d4c6963bc36611"}
|
110
|
+
comparison = JsonDeepCompare::NodeComparison.new(lval, rval)
|
111
|
+
assert !comparison.equal?
|
112
|
+
assert_equal(
|
113
|
+
"\":root > .rev\" differs starting at char 1: \"22-23c92a95665bb6...\" differs from \"23-54a5106f8c522a...\"",
|
114
|
+
comparison.difference_message
|
115
|
+
)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_simple_exclusion
|
119
|
+
lval = {'one' => 'two', 'three' => 'four'}
|
120
|
+
rval = {'one' => 'two', 'three' => 'THREE'}
|
121
|
+
comparison = JsonDeepCompare::NodeComparison.new(
|
122
|
+
lval, rval, exclusions: [":root > .three"]
|
123
|
+
)
|
124
|
+
assert comparison.equal?
|
125
|
+
end
|
126
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json-deep-compare
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Francis Hwang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: For quickly finding differences between two large JSON documents.
|
47
|
+
email:
|
48
|
+
- sera@fhwang.net
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- json-deep-compare.gemspec
|
59
|
+
- lib/json-deep-compare.rb
|
60
|
+
- test/json_deep_compare_test.rb
|
61
|
+
homepage: ''
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
hash: 383603838726367257
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
hash: 383603838726367257
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.8.23
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: For quickly finding differences between two large JSON documents.
|
92
|
+
test_files:
|
93
|
+
- test/json_deep_compare_test.rb
|