minitest-match_json 0.0.1 → 0.1.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/README.md +24 -3
- data/Rakefile +1 -1
- data/lib/minitest/match_json/configuration.rb +14 -0
- data/lib/minitest/match_json/version.rb +2 -1
- data/lib/minitest/match_json.rb +53 -30
- data/minitest-match_json.gemspec +7 -5
- data/test/minitest/match_json_test.rb +111 -5
- metadata +20 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c4027d485fb16d15e957b930a8da529ab72cbe1
|
4
|
+
data.tar.gz: 23bbd0c28f7c4ca0428f880641b51fb3c34d8abc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b353ade92cb392f4f73611b6220b461c1ead810545a55ca44eef92b532d5fbd56f13709b41f0f7fb8c7ef694f30ce05226447d184b028b81727f84f7406a237b
|
7
|
+
data.tar.gz: 129aa8951a8953e668ef81c2d9943aa7fc9043d46bebd4ed8f1cb25c7c04dd676be336c2a02a49b2becd9e8b6806f564035ee7114cd9ecea01c1761c76da0cf4
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Minitest::MatchJson
|
2
2
|
|
3
|
-
|
3
|
+
Compare two JSON objects. If a String is passed in, it is expected to be valid JSON, any other type will be converted to json if possible.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,11 +20,32 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
```
|
24
|
+
require 'minitest-match_json'
|
25
|
+
```
|
26
|
+
|
27
|
+
[Diffy](https://github.com/samg/diffy) is used to compared the two JSON objects. Options to control the output format and number of context lines to be displayed can be passed through to Diffy.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Minitest::MatchJson.configure do |config|
|
31
|
+
config.format = :color
|
32
|
+
config.context = 1
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
### Assert style
|
37
|
+
```ruby
|
38
|
+
assert_match_json expected_json, actual_json
|
39
|
+
```
|
40
|
+
|
41
|
+
### Spec style
|
42
|
+
```ruby
|
43
|
+
actual_json.must_match_json expected_json
|
44
|
+
```
|
24
45
|
|
25
46
|
## Contributing
|
26
47
|
|
27
|
-
1. Fork it ( https://github.com/
|
48
|
+
1. Fork it ( https://github.com/ggordon/minitest-match_json/fork )
|
28
49
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
50
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
51
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
data/lib/minitest/match_json.rb
CHANGED
@@ -1,55 +1,78 @@
|
|
1
1
|
# require 'minitest/match_json/version'
|
2
|
+
require 'minitest/match_json/configuration'
|
2
3
|
require 'minitest/spec'
|
3
4
|
require 'json'
|
4
5
|
require 'diffy'
|
5
|
-
Diffy::Diff.default_format = :color
|
6
6
|
|
7
7
|
module Minitest
|
8
|
+
# JSON matchers
|
9
|
+
module MatchJson
|
10
|
+
class << self
|
11
|
+
attr_writer :configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure
|
19
|
+
yield configuration
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.compare_json(actual_json, expected_json)
|
23
|
+
Diffy::Diff.new(
|
24
|
+
expected_json,
|
25
|
+
actual_json,
|
26
|
+
context: configuration.context
|
27
|
+
).to_s(configuration.format)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.pretty_json(param)
|
31
|
+
json = (param.is_a?(String)) ? param : param.to_json
|
32
|
+
JSON.pretty_generate(JSON.parse(json))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Assert style
|
8
37
|
module Assertions
|
9
38
|
#
|
10
39
|
# Fails unless +param and +actual have the same items.
|
11
40
|
#
|
12
41
|
def assert_match_json(expected, actual)
|
13
|
-
|
42
|
+
match_json(expected, actual)
|
14
43
|
end
|
15
44
|
|
16
45
|
private
|
17
46
|
|
18
47
|
# Helper to highlight diffs between two json strings
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
diff = Diffy::Diff.new(expected_pretty, actual_pretty)
|
25
|
-
|
26
|
-
if diff.to_s.strip.empty?
|
27
|
-
pass "JSON matched"
|
28
|
-
else
|
29
|
-
puts "\n#{diff}\n"
|
30
|
-
flunk 'JSON did not match'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def _convert_to_json(param)
|
35
|
-
case param
|
36
|
-
when String
|
37
|
-
param
|
38
|
-
when Hash
|
39
|
-
param.to_json
|
40
|
-
else
|
41
|
-
flunk 'Expected parameter must be a String or a Hash'
|
42
|
-
end
|
48
|
+
def match_json(expected, actual)
|
49
|
+
diff_str = Minitest::MatchJson.compare_json(
|
50
|
+
pretty_json(actual), pretty_json(expected)
|
51
|
+
)
|
52
|
+
diff_str.strip.empty? ? match_json_passes : match_json_fails(diff_str)
|
43
53
|
end
|
44
|
-
end
|
45
54
|
|
55
|
+
def match_json_fails(diff_str)
|
56
|
+
puts "\n#{diff_str}\n"
|
57
|
+
flunk 'JSON did not match'
|
58
|
+
end
|
59
|
+
|
60
|
+
def match_json_passes
|
61
|
+
pass 'JSON matched'
|
62
|
+
end
|
46
63
|
|
64
|
+
def pretty_json(param)
|
65
|
+
Minitest::MatchJson.pretty_json(param)
|
66
|
+
rescue StandardError
|
67
|
+
flunk 'Parameter cannot be converted to JSON'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Spec style
|
47
72
|
module Expectations
|
48
73
|
#
|
49
74
|
# Fails unless the subject and parameter are equivalent JSON
|
50
75
|
#
|
51
|
-
|
52
|
-
Hash.infect_an_assertion :assert_match_json, :must_match_json
|
76
|
+
infect_an_assertion :assert_match_json, :must_match_json
|
53
77
|
end
|
54
|
-
|
55
78
|
end
|
data/minitest-match_json.gemspec
CHANGED
@@ -8,15 +8,16 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Minitest::MatchJson::VERSION
|
9
9
|
spec.authors = ['Gary Gordon']
|
10
10
|
spec.email = ['gfgordon@gmail.com']
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage = ''
|
11
|
+
spec.summary = 'Compare two JSON objects'
|
12
|
+
spec.description = 'Compare two JSON objects with pretty diffs'
|
13
|
+
spec.homepage = 'http://ggordon.github.io/minitest-match_json'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.executables = spec.files.grep(
|
18
|
-
spec.test_files = spec.files.grep(
|
17
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
19
19
|
spec.require_paths = ['lib']
|
20
|
+
spec.required_ruby_version = '>= 1.9'
|
20
21
|
|
21
22
|
spec.add_dependency 'diffy'
|
22
23
|
spec.add_dependency 'json'
|
@@ -24,4 +25,5 @@ Gem::Specification.new do |spec|
|
|
24
25
|
|
25
26
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
26
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'simplecov'
|
27
29
|
end
|
@@ -1,11 +1,117 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
1
4
|
gem 'minitest'
|
2
5
|
require 'minitest/autorun'
|
3
6
|
require 'minitest/match_json'
|
4
7
|
|
5
|
-
describe Minitest
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
describe Minitest do
|
9
|
+
describe 'Configuration' do
|
10
|
+
it '#configure will override defaults' do
|
11
|
+
Minitest::MatchJson.configure do |config|
|
12
|
+
config.format = :color
|
13
|
+
config.context = 1
|
14
|
+
end
|
15
|
+
Minitest::MatchJson.configuration.format.must_equal :color
|
16
|
+
Minitest::MatchJson.configuration.context.must_equal 1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'Basic tests' do
|
21
|
+
it 'compares some json' do
|
22
|
+
json = '{"a": 2, "b": 4}'
|
23
|
+
hash = { a: 2, b: 4 }
|
24
|
+
json.must_match_json hash
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'fails trying to compares a non json object' do
|
28
|
+
json = '{"a": 2, "b": 4}'
|
29
|
+
begin
|
30
|
+
json.must_match_json 2
|
31
|
+
rescue MiniTest::Assertion
|
32
|
+
pass
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns a default result when failing' do
|
37
|
+
Minitest::MatchJson.configure do |config|
|
38
|
+
config.format = :text
|
39
|
+
end
|
40
|
+
|
41
|
+
json = '{"b": 5}'
|
42
|
+
hash = { b: 4 }
|
43
|
+
|
44
|
+
out, _ = capture_io do
|
45
|
+
begin
|
46
|
+
json.must_match_json hash.to_json
|
47
|
+
rescue MiniTest::Assertion
|
48
|
+
pass
|
49
|
+
end
|
50
|
+
end
|
51
|
+
out.must_equal(<<STR
|
52
|
+
|
53
|
+
{
|
54
|
+
- \"b\": 4
|
55
|
+
+ \"b\": 5
|
56
|
+
}
|
57
|
+
\
|
58
|
+
|
59
|
+
STR
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'returns a default result with reduced context when failing' do
|
64
|
+
Minitest::MatchJson.configure do |config|
|
65
|
+
config.format = :text
|
66
|
+
config.context = 1
|
67
|
+
end
|
68
|
+
|
69
|
+
json = '{"a": 3, "b": 5, "c": 5}'
|
70
|
+
hash = { a: 3, b: 4, c: 5 }
|
71
|
+
|
72
|
+
out, _ = capture_io do
|
73
|
+
begin
|
74
|
+
json.must_match_json hash.to_json
|
75
|
+
rescue MiniTest::Assertion
|
76
|
+
pass
|
77
|
+
end
|
78
|
+
end
|
79
|
+
out.must_equal(<<STR
|
80
|
+
|
81
|
+
\"a\": 3,
|
82
|
+
- \"b\": 4,
|
83
|
+
+ \"b\": 5,
|
84
|
+
\"c\": 5
|
85
|
+
|
86
|
+
STR
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'returns a colorized result when failing' do
|
91
|
+
Minitest::MatchJson.configure do |config|
|
92
|
+
config.format = :color
|
93
|
+
end
|
94
|
+
|
95
|
+
json = '{"b": 5}'
|
96
|
+
hash = { b: 4 }
|
97
|
+
|
98
|
+
out, _ = capture_io do
|
99
|
+
begin
|
100
|
+
json.must_match_json hash.to_json
|
101
|
+
rescue MiniTest::Assertion
|
102
|
+
pass
|
103
|
+
end
|
104
|
+
end
|
105
|
+
out.must_equal(<<STR
|
106
|
+
|
107
|
+
{
|
108
|
+
\e[31m- \"b\": 4\e[0m
|
109
|
+
\e[32m+ \"b\": 5\e[0m
|
110
|
+
}
|
111
|
+
\
|
112
|
+
|
113
|
+
STR
|
114
|
+
)
|
115
|
+
end
|
10
116
|
end
|
11
117
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minitest-match_json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary Gordon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: diffy
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: Compare two JSON objects with pretty diffs
|
84
98
|
email:
|
85
99
|
- gfgordon@gmail.com
|
@@ -88,16 +102,18 @@ extensions: []
|
|
88
102
|
extra_rdoc_files: []
|
89
103
|
files:
|
90
104
|
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
91
106
|
- Gemfile
|
92
107
|
- LICENSE.txt
|
93
108
|
- README.md
|
94
109
|
- Rakefile
|
95
110
|
- lib/minitest-match_json.rb
|
96
111
|
- lib/minitest/match_json.rb
|
112
|
+
- lib/minitest/match_json/configuration.rb
|
97
113
|
- lib/minitest/match_json/version.rb
|
98
114
|
- minitest-match_json.gemspec
|
99
115
|
- test/minitest/match_json_test.rb
|
100
|
-
homepage:
|
116
|
+
homepage: http://ggordon.github.io/minitest-match_json
|
101
117
|
licenses:
|
102
118
|
- MIT
|
103
119
|
metadata: {}
|
@@ -109,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
125
|
requirements:
|
110
126
|
- - ">="
|
111
127
|
- !ruby/object:Gem::Version
|
112
|
-
version: '
|
128
|
+
version: '1.9'
|
113
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
130
|
requirements:
|
115
131
|
- - ">="
|