rspec-json_matcher 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +24 -0
- data/.gitignore +0 -1
- data/CHANGELOG.md +50 -20
- data/Gemfile +4 -0
- data/Gemfile.lock +39 -0
- data/README.md +3 -0
- data/Rakefile +8 -1
- data/lib/rspec/json_matcher/abstract_comparer.rb +19 -7
- data/lib/rspec/json_matcher/abstract_matcher.rb +12 -4
- data/lib/rspec/json_matcher/exact_matcher.rb +2 -2
- data/lib/rspec/json_matcher/fuzzy_matcher.rb +2 -2
- data/lib/rspec/json_matcher/version.rb +1 -1
- data/lib/rspec/json_matcher.rb +1 -1
- data/rspec-json_matcher.gemspec +1 -4
- data/spec/rspec/json_matcher_spec.rb +24 -0
- metadata +13 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7791271c77095a624211ecf2b3f6bb5a9142f4d31ed43c287c2108ba84b9854d
|
4
|
+
data.tar.gz: 25fd3df8548e53ff229d18ed58c15a6d6a8837005e78910e0ec52a0252862ece
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf3fb8482b30cc606f67a40113303a2487d8043fabb7fbf5ec5f2810f5697ed89fffdfba4f045192d6dd54f6f5e123744a2be321d3e341b8703a93e857541a3b
|
7
|
+
data.tar.gz: 4237facf43539c1e51efa83fca24c336cd5b8bc5729da61df8263dfb06c06ff4733935c5fbc120ddf2b44866e37ab59f5935d2f8d0e0894b25c90c7a51ed40a2
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: test
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
push:
|
6
|
+
branches:
|
7
|
+
- main
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
ruby:
|
15
|
+
- 2.7.2
|
16
|
+
steps:
|
17
|
+
- uses: actions/checkout@v2
|
18
|
+
- name: Set up Ruby
|
19
|
+
uses: ruby/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
ruby-version: ${{ matrix.ruby }}
|
22
|
+
bundler-cache: true
|
23
|
+
- name: Run the default task
|
24
|
+
run: bundle exec rake
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,29 +1,59 @@
|
|
1
|
-
##
|
2
|
-
* Fix method name for negative case
|
1
|
+
## v0.2.0
|
3
2
|
|
4
|
-
|
5
|
-
* Convert keys in expectation from Symbol to String
|
3
|
+
- Prefer amazing_print to awesome_print.
|
6
4
|
|
7
|
-
##
|
8
|
-
* Suppress deprecation warnings for RSpec 3
|
5
|
+
## v0.1.6
|
9
6
|
|
10
|
-
|
11
|
-
* Change interface to be_json_including & be_json_as matchers
|
7
|
+
- Support composable matchers and matching arguments usecase
|
12
8
|
|
13
|
-
##
|
14
|
-
* Fix a bug in comparison with Hash keys
|
9
|
+
## v0.1.5
|
15
10
|
|
16
|
-
|
17
|
-
* More strict comparison to detect different Hash keys
|
11
|
+
- Show reasons in failure case
|
18
12
|
|
19
|
-
##
|
20
|
-
* Change indent style on failure message
|
13
|
+
## v0.1.4
|
21
14
|
|
22
|
-
|
23
|
-
* Bundler friendliness
|
15
|
+
- Support RSpec 3 style negative failure message
|
24
16
|
|
25
|
-
##
|
26
|
-
* Tiny refactoring
|
17
|
+
## v0.1.3
|
27
18
|
|
28
|
-
|
29
|
-
|
19
|
+
- Fix method name for negative case
|
20
|
+
|
21
|
+
## v0.1.2
|
22
|
+
|
23
|
+
- Convert keys in expectation from Symbol to String
|
24
|
+
|
25
|
+
## v0.1.2
|
26
|
+
|
27
|
+
- Convert keys in expectation from Symbol to String
|
28
|
+
|
29
|
+
## v0.1.1
|
30
|
+
|
31
|
+
- Suppress deprecation warnings for RSpec 3
|
32
|
+
|
33
|
+
## v0.1.0
|
34
|
+
|
35
|
+
- Change interface to be_json_including & be_json_as matchers
|
36
|
+
|
37
|
+
## v0.0.6
|
38
|
+
|
39
|
+
- Fix a bug in comparison with Hash keys
|
40
|
+
|
41
|
+
## v0.0.5
|
42
|
+
|
43
|
+
- More strict comparison to detect different Hash keys
|
44
|
+
|
45
|
+
## v0.0.4
|
46
|
+
|
47
|
+
- Change indent style on failure message
|
48
|
+
|
49
|
+
## v0.0.3
|
50
|
+
|
51
|
+
- Bundler friendliness
|
52
|
+
|
53
|
+
## v0.0.2
|
54
|
+
|
55
|
+
- Tiny refactoring
|
56
|
+
|
57
|
+
## v0.0.1
|
58
|
+
|
59
|
+
- The first release on 2013-4-25
|
data/Gemfile
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rspec-json_matcher (0.2.0)
|
5
|
+
amazing_print
|
6
|
+
json
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
amazing_print (1.4.0)
|
12
|
+
diff-lcs (1.5.0)
|
13
|
+
json (2.6.1)
|
14
|
+
rake (13.0.6)
|
15
|
+
rspec (3.11.0)
|
16
|
+
rspec-core (~> 3.11.0)
|
17
|
+
rspec-expectations (~> 3.11.0)
|
18
|
+
rspec-mocks (~> 3.11.0)
|
19
|
+
rspec-core (3.11.0)
|
20
|
+
rspec-support (~> 3.11.0)
|
21
|
+
rspec-expectations (3.11.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.11.0)
|
24
|
+
rspec-mocks (3.11.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.11.0)
|
27
|
+
rspec-support (3.11.0)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
x86_64-linux
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
bundler
|
34
|
+
rake
|
35
|
+
rspec
|
36
|
+
rspec-json_matcher!
|
37
|
+
|
38
|
+
BUNDLED WITH
|
39
|
+
2.3.6
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# RSpec::JsonMatcher
|
2
|
+
|
3
|
+
[![test](https://github.com/r7kamura/rspec-json_matcher/actions/workflows/test.yml/badge.svg)](https://github.com/r7kamura/rspec-json_matcher/actions/workflows/test.yml)
|
4
|
+
|
2
5
|
This library provides RSpec matcher for testing JSON string.
|
3
6
|
|
4
7
|
* parsing a given value as JSON string
|
data/Rakefile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module RSpec
|
2
2
|
module JsonMatcher
|
3
3
|
class AbstractComparer
|
4
|
-
attr_reader :actual, :expected
|
4
|
+
attr_reader :actual, :expected, :reason
|
5
5
|
|
6
|
-
def self.compare(*args)
|
7
|
-
new(*args).compare
|
6
|
+
def self.compare(*args, &reason)
|
7
|
+
new(*args, &reason).compare
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.extract_keys(array_or_hash)
|
@@ -15,9 +15,10 @@ module RSpec
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def initialize(actual, expected)
|
18
|
+
def initialize(actual, expected, &reason)
|
19
19
|
@actual = actual
|
20
20
|
@expected = expected
|
21
|
+
@reason = reason
|
21
22
|
end
|
22
23
|
|
23
24
|
def compare
|
@@ -31,7 +32,14 @@ module RSpec
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def has_same_keys?
|
34
|
-
|
35
|
+
actual_keys = self.class.extract_keys(actual).sort
|
36
|
+
expected_keys = self.class.extract_keys(expected).sort
|
37
|
+
(actual_keys == expected_keys).tap do |success|
|
38
|
+
unless success
|
39
|
+
diff_keys = (actual_keys - expected_keys) + (expected_keys - actual_keys)
|
40
|
+
reason[diff_keys.ai]
|
41
|
+
end
|
42
|
+
end
|
35
43
|
end
|
36
44
|
|
37
45
|
def has_same_value?
|
@@ -49,11 +57,15 @@ module RSpec
|
|
49
57
|
def has_same_values?
|
50
58
|
if expected.is_a?(Array)
|
51
59
|
expected.each_index.all? do |index|
|
52
|
-
index < actual.size && self.class.compare(actual[index], expected[index])
|
60
|
+
(index < actual.size && self.class.compare(actual[index], expected[index], &reason)).tap do |success|
|
61
|
+
reason["[#{index}]"] unless success
|
62
|
+
end
|
53
63
|
end
|
54
64
|
else
|
55
65
|
expected.keys.all? do |key|
|
56
|
-
actual.has_key?(key.to_s) && self.class.compare(actual[key.to_s], expected[key])
|
66
|
+
(actual.has_key?(key.to_s) && self.class.compare(actual[key.to_s], expected[key], &reason)).tap do |success|
|
67
|
+
reason[key.to_s] unless success
|
68
|
+
end
|
57
69
|
end
|
58
70
|
end
|
59
71
|
end
|
@@ -1,16 +1,19 @@
|
|
1
1
|
module RSpec
|
2
2
|
module JsonMatcher
|
3
3
|
class AbstractMatcher
|
4
|
-
attr_reader :expected, :parsed
|
4
|
+
attr_reader :expected, :parsed, :reasons
|
5
5
|
|
6
6
|
def initialize(expected = nil)
|
7
7
|
@expected = expected
|
8
|
+
@reasons = []
|
8
9
|
end
|
9
10
|
|
10
11
|
def matches?(json)
|
11
12
|
@parsed = JSON.parse(json)
|
12
13
|
if has_expectation?
|
13
|
-
compare
|
14
|
+
compare do |reason|
|
15
|
+
@reasons << reason
|
16
|
+
end
|
14
17
|
else
|
15
18
|
true
|
16
19
|
end
|
@@ -19,7 +22,9 @@ module RSpec
|
|
19
22
|
false
|
20
23
|
end
|
21
24
|
|
22
|
-
|
25
|
+
alias_method :===, :matches?
|
26
|
+
|
27
|
+
def compare(&reason)
|
23
28
|
raise NotImplementedError, "You must implement #{self.class}#compare"
|
24
29
|
end
|
25
30
|
|
@@ -42,6 +47,7 @@ module RSpec
|
|
42
47
|
inspection("not ")
|
43
48
|
end
|
44
49
|
end
|
50
|
+
alias :failure_message_when_negated :negative_failure_message
|
45
51
|
|
46
52
|
private
|
47
53
|
|
@@ -54,7 +60,9 @@ module RSpec
|
|
54
60
|
end
|
55
61
|
|
56
62
|
def inspection(prefix = nil)
|
57
|
-
["expected #{prefix}to match:", expected.ai(indent: -2), "", "actual:", parsed.ai(indent: -2)
|
63
|
+
messages = ["expected #{prefix}to match:", expected.ai(indent: -2), "", "actual:", parsed.ai(indent: -2), ""]
|
64
|
+
messages.push "reason: #{reasons.reverse.join(".")}" unless reasons.empty?
|
65
|
+
messages.join("\n")
|
58
66
|
end
|
59
67
|
end
|
60
68
|
end
|
data/lib/rspec/json_matcher.rb
CHANGED
data/rspec-json_matcher.gemspec
CHANGED
@@ -19,8 +19,5 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "json"
|
22
|
-
spec.add_dependency "
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
-
spec.add_development_dependency "rake"
|
25
|
-
spec.add_development_dependency "rspec"
|
22
|
+
spec.add_dependency "amazing_print"
|
26
23
|
end
|
@@ -13,6 +13,14 @@ describe RSpec::JsonMatcher do
|
|
13
13
|
{}.to_json.should be_json
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
context 'when used with a composable matcher' do
|
18
|
+
it 'matches' do
|
19
|
+
{
|
20
|
+
"a" => {"b" => 1}.to_json
|
21
|
+
}.should match("a" => be_json)
|
22
|
+
end
|
23
|
+
end
|
16
24
|
end
|
17
25
|
|
18
26
|
describe "#be_json_as" do
|
@@ -174,6 +182,14 @@ describe RSpec::JsonMatcher do
|
|
174
182
|
{ "foo" => "bar" }.to_json.should be_json_as(foo: "bar")
|
175
183
|
end
|
176
184
|
end
|
185
|
+
|
186
|
+
context 'when used with a composable matcher' do
|
187
|
+
it 'matches' do
|
188
|
+
{
|
189
|
+
"a" => {"b" => 1}.to_json
|
190
|
+
}.should match("a" => be_json_as("b" => 1))
|
191
|
+
end
|
192
|
+
end
|
177
193
|
end
|
178
194
|
|
179
195
|
describe "#be_json_including" do
|
@@ -293,5 +309,13 @@ describe RSpec::JsonMatcher do
|
|
293
309
|
)
|
294
310
|
end
|
295
311
|
end
|
312
|
+
|
313
|
+
context 'when used with a composable matcher' do
|
314
|
+
it 'matches' do
|
315
|
+
{
|
316
|
+
"a" => {"b" => 1, "c" => 2}.to_json
|
317
|
+
}.should match("a" => be_json_including("b" => 1))
|
318
|
+
end
|
319
|
+
end
|
296
320
|
end
|
297
321
|
end
|
metadata
CHANGED
@@ -1,83 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-json_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: amazing_print
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: bundler
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ~>
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1.3'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '1.3'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rake
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - '>='
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rspec
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - '>='
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - '>='
|
38
|
+
- - ">="
|
81
39
|
- !ruby/object:Gem::Version
|
82
40
|
version: '0'
|
83
41
|
description: This library provides RSpec matcher for testing JSON string.
|
@@ -87,9 +45,11 @@ executables: []
|
|
87
45
|
extensions: []
|
88
46
|
extra_rdoc_files: []
|
89
47
|
files:
|
90
|
-
- .
|
48
|
+
- ".github/workflows/test.yml"
|
49
|
+
- ".gitignore"
|
91
50
|
- CHANGELOG.md
|
92
51
|
- Gemfile
|
52
|
+
- Gemfile.lock
|
93
53
|
- LICENSE.txt
|
94
54
|
- README.md
|
95
55
|
- Rakefile
|
@@ -115,17 +75,16 @@ require_paths:
|
|
115
75
|
- lib
|
116
76
|
required_ruby_version: !ruby/object:Gem::Requirement
|
117
77
|
requirements:
|
118
|
-
- -
|
78
|
+
- - ">="
|
119
79
|
- !ruby/object:Gem::Version
|
120
80
|
version: '0'
|
121
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
82
|
requirements:
|
123
|
-
- -
|
83
|
+
- - ">="
|
124
84
|
- !ruby/object:Gem::Version
|
125
85
|
version: '0'
|
126
86
|
requirements: []
|
127
|
-
|
128
|
-
rubygems_version: 2.0.3
|
87
|
+
rubygems_version: 3.1.4
|
129
88
|
signing_key:
|
130
89
|
specification_version: 4
|
131
90
|
summary: RSpec matcher for testing JSON string
|