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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 7293cf1f1d54933dbfd6880e11c02e1d0a5abafa
4
- data.tar.gz: 9754ed84ab5d8ee38a3c6fbd6f8d91859fd56551
2
+ SHA256:
3
+ metadata.gz: 7791271c77095a624211ecf2b3f6bb5a9142f4d31ed43c287c2108ba84b9854d
4
+ data.tar.gz: 25fd3df8548e53ff229d18ed58c15a6d6a8837005e78910e0ec52a0252862ece
5
5
  SHA512:
6
- metadata.gz: d8546c81ea7609d908b0f752904533197d67eaa7d418e6470e1b2f6ae97790428cc4e86ad8e2eac0316ced9029d8cf3bbf8f9dc9ea8b28625277117fb784d54a
7
- data.tar.gz: 85819e21cafddb65dad53bb0652ebb57d94a5a0f3f671256458057fd02b5a4d5d1f5d72179711fed0695fda59e0aa31d76bd4a07aa96c4af93e962f102e07d99
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
@@ -3,7 +3,6 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
6
  InstalledFiles
8
7
  _yardoc
9
8
  coverage
data/CHANGELOG.md CHANGED
@@ -1,29 +1,59 @@
1
- ## 0.1.2
2
- * Fix method name for negative case
1
+ ## v0.2.0
3
2
 
4
- ## 0.1.2
5
- * Convert keys in expectation from Symbol to String
3
+ - Prefer amazing_print to awesome_print.
6
4
 
7
- ## 0.1.1
8
- * Suppress deprecation warnings for RSpec 3
5
+ ## v0.1.6
9
6
 
10
- ## 0.1.0
11
- * Change interface to be_json_including & be_json_as matchers
7
+ - Support composable matchers and matching arguments usecase
12
8
 
13
- ## 0.0.6
14
- * Fix a bug in comparison with Hash keys
9
+ ## v0.1.5
15
10
 
16
- ## 0.0.5
17
- * More strict comparison to detect different Hash keys
11
+ - Show reasons in failure case
18
12
 
19
- ## 0.0.4
20
- * Change indent style on failure message
13
+ ## v0.1.4
21
14
 
22
- ## 0.0.3
23
- * Bundler friendliness
15
+ - Support RSpec 3 style negative failure message
24
16
 
25
- ## 0.0.2
26
- * Tiny refactoring
17
+ ## v0.1.3
27
18
 
28
- ## 0.0.1
29
- * The first release on 2013-4-25
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
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rspec-json_matcher.gemspec
4
4
  gemspec
5
+
6
+ gem 'bundler'
7
+ gem 'rake'
8
+ gem 'rspec'
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 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: %i[spec]
@@ -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
- self.class.extract_keys(actual).sort == self.class.extract_keys(expected).sort
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
- def compare
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)].join("\n")
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
@@ -1,8 +1,8 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
3
  class ExactMatcher < AbstractMatcher
4
- def compare
5
- ExactComparer.compare(parsed, expected)
4
+ def compare(&reason)
5
+ ExactComparer.compare(parsed, expected, &reason)
6
6
  end
7
7
  end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
3
  class FuzzyMatcher < AbstractMatcher
4
- def compare
5
- FuzzyComparer.compare(parsed, expected)
4
+ def compare(&reason)
5
+ FuzzyComparer.compare(parsed, expected, &reason)
6
6
  end
7
7
  end
8
8
  end
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module JsonMatcher
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -6,7 +6,7 @@ require "rspec/json_matcher/abstract_matcher"
6
6
  require "rspec/json_matcher/exact_matcher"
7
7
  require "rspec/json_matcher/fuzzy_matcher"
8
8
  require "json"
9
- require "awesome_print"
9
+ require "amazing_print"
10
10
 
11
11
  module RSpec
12
12
  module JsonMatcher
@@ -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 "awesome_print"
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.1.3
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: 2014-02-06 00:00:00.000000000 Z
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: awesome_print
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
- - .gitignore
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
- rubyforge_project:
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