json-matchers 0.3.0 → 0.3.1

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
2
  SHA1:
3
- metadata.gz: b2bc10af4131d3f0ed42c314a9a9647547051e6f
4
- data.tar.gz: bd823542ce64cab597d9f9689a7b49bce21494e8
3
+ metadata.gz: 2f5663f28a62b7036c41b7648cc84db8be036445
4
+ data.tar.gz: 4dc11e206e1010683b3b92277bd61f314fdcb95c
5
5
  SHA512:
6
- metadata.gz: 8ce7b02e972556b4d961a245b4766e537d21b7adef732c4f0cab6aecc990ca01258a062acbb73a6773fc051769f374ead55a474aeb402a4d8311fdc2f52a4a30
7
- data.tar.gz: e8b8f5c80dfe722447f5bea3cf95c369b23c1eab49439383e5a31a87d2ab27126a5ac1d6b38d685f31883629aa4e558e4d4bce06d3589c384323b419b6ee19ac
6
+ metadata.gz: d710ea1267974d7dd9e7bba6181f2d39206d7552ec3c2613f17f05239ec546ee85ab27ef580d817b7ce434849d201c6e32b06a57beb50821e5a8b961fd067f9f
7
+ data.tar.gz: 85d2db20bde72739562656cf0b651a9d5df49319cfdd3244b661d623dfc5ec84b231a965268c799c083d938af4732183a510d2abae470091cce4ec8e473bbc57
@@ -1,6 +1,17 @@
1
1
  master
2
2
  ------
3
3
 
4
+ 0.3.1
5
+ -----
6
+
7
+ * BREAKING CHANGE: Rename module to `JsonMatchers`. This resolves clashing with
8
+ gems like `oj` / `oj_mimic_json` that take control of the standard library's
9
+ `json` module. As a result, the file to require is now `json_matchers`,
10
+ instead of `json/matchers`.
11
+
12
+ * No longer condone auto-loading RSpec. Add documentation around adding `require
13
+ "json_matchers/rspec"` to consumers' `spec/spec_helper.rb`
14
+
4
15
  0.3.0
5
16
  -----
6
17
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # JSON::Matchers
1
+ # JsonMatchers
2
2
 
3
3
  Validate the JSON returned by your Rails JSON APIs
4
4
 
@@ -7,7 +7,9 @@ Validate the JSON returned by your Rails JSON APIs
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ruby
10
- gem 'json-matchers', require: false
10
+ group :test do
11
+ gem "json-matchers", require: false
12
+ end
11
13
  ```
12
14
 
13
15
  And then execute:
@@ -27,7 +29,7 @@ First, include it in your `spec_helper`:
27
29
  ```ruby
28
30
  # spec/spec_helper.rb
29
31
 
30
- require "json/matchers"
32
+ require "json_matchers/rspec"
31
33
  ```
32
34
 
33
35
  Define your [JSON Schema](http://json-schema.org/example1.html) in the schema directory:
@@ -134,13 +136,13 @@ To learn more about `$ref`, check out [Understanding JSON Schema Structuring](ht
134
136
 
135
137
  By default, the schema directory is `spec/support/api/schemas`.
136
138
 
137
- This can be configured via `JSON::Matchers.schema_root`.
139
+ This can be configured via `JsonMatchers.schema_root`.
138
140
 
139
141
 
140
142
  ```ruby
141
143
  # spec/support/json-matchers.rb
142
144
 
143
- JSON::Matchers.schema_root = "docs/api/schemas"
145
+ JsonMatchers.schema_root = "docs/api/schemas"
144
146
  ```
145
147
 
146
148
  ## Contributing
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "json/matchers/version"
4
+ require "json_matchers/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "json-matchers"
8
- spec.version = JSON::Matchers::VERSION
8
+ spec.version = JsonMatchers::VERSION
9
9
  spec.authors = ["Sean Doyle"]
10
10
  spec.email = ["sean.p.doyle24@gmail.com"]
11
11
  spec.summary = %q{Validate your Rails JSON API's JSON}
@@ -0,0 +1,12 @@
1
+ warn <<-WARNING
2
+ DEPRECATED: The `json-matchers` gem has been deprecated.
3
+
4
+ Please replace change your Gemfile's reference from `json-matchers` to
5
+ `json_matchers`.
6
+ WARNING
7
+
8
+ require "json_matchers"
9
+
10
+ if defined?(RSpec)
11
+ require "json_matchers/rspec"
12
+ end
@@ -1,20 +1,12 @@
1
- require "json/matchers/version"
2
- require "json/matchers/matcher"
3
- require "json/matchers/errors"
4
- require "active_support/all"
1
+ warn <<-WARNING
2
+ DEPRECATED: requiring the library via `require "json/matchers"` is deprecated.
5
3
 
6
- module JSON
7
- module Matchers
8
- mattr_accessor :schema_root
4
+ To include the library, please add `require "json_matchers/rspec"` to your
5
+ `spec/spec_helper.rb`.
6
+ WARNING
9
7
 
10
- self.schema_root = "#{Dir.pwd}/spec/support/api/schemas"
11
-
12
- def self.path_to_schema(schema_name)
13
- Pathname(schema_root).join("#{schema_name}.json")
14
- end
15
- end
16
- end
8
+ require "json_matchers"
17
9
 
18
10
  if defined?(RSpec)
19
- require "json/matchers/rspec"
11
+ require "json_matchers/rspec"
20
12
  end
@@ -0,0 +1,14 @@
1
+ require "json_matchers/version"
2
+ require "json_matchers/matcher"
3
+ require "json_matchers/errors"
4
+ require "active_support/all"
5
+
6
+ module JsonMatchers
7
+ mattr_accessor :schema_root
8
+
9
+ self.schema_root = "#{Dir.pwd}/spec/support/api/schemas"
10
+
11
+ def self.path_to_schema(schema_name)
12
+ Pathname(schema_root).join("#{schema_name}.json")
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module JsonMatchers
2
+ InvalidSchemaError = Class.new(StandardError)
3
+ MissingSchema = Class.new(Errno::ENOENT)
4
+ end
@@ -0,0 +1,37 @@
1
+ require "json-schema"
2
+
3
+ module JsonMatchers
4
+ class Matcher
5
+ def initialize(schema_path, **options)
6
+ @schema_path = schema_path
7
+ @options = options
8
+ end
9
+
10
+ def matches?(response)
11
+ @response = response
12
+
13
+ validator_options = {
14
+ strict: true,
15
+ }.merge(options)
16
+
17
+ JSON::Validator.validate!(
18
+ schema_path.to_s,
19
+ response.body,
20
+ validator_options,
21
+ )
22
+ rescue JSON::Schema::ValidationError => ex
23
+ @validation_failure_message = ex.message
24
+ false
25
+ rescue JSON::ParserError
26
+ raise InvalidSchemaError
27
+ end
28
+
29
+ def validation_failure_message
30
+ @validation_failure_message.to_s
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :schema_path, :options
36
+ end
37
+ end
@@ -0,0 +1,83 @@
1
+ require "json_matchers"
2
+
3
+ module JsonMatchers
4
+ class RSpec < SimpleDelegator
5
+ attr_reader :schema_name
6
+
7
+ def initialize(schema_name, **options)
8
+ @schema_name = schema_name
9
+
10
+ super(JsonMatchers::Matcher.new(schema_path, options))
11
+ end
12
+
13
+ def failure_message(response)
14
+ <<-FAIL.strip_heredoc
15
+ expected
16
+
17
+ #{response.body}
18
+
19
+ to match schema "#{schema_name}":
20
+
21
+ #{schema_body}
22
+
23
+ ---
24
+
25
+ #{validation_failure_message}
26
+
27
+ FAIL
28
+ end
29
+
30
+ def failure_message_when_negated(response)
31
+ <<-FAIL.strip_heredoc
32
+ expected
33
+
34
+ #{response.body}
35
+
36
+ not to match schema "#{schema_name}":
37
+
38
+ #{schema_body}
39
+
40
+ ---
41
+
42
+ #{validation_failure_message}
43
+
44
+ FAIL
45
+ end
46
+
47
+ def schema_path
48
+ JsonMatchers.path_to_schema(schema_name)
49
+ end
50
+
51
+ def schema_body
52
+ File.read(schema_path)
53
+ end
54
+ end
55
+ end
56
+
57
+ if RSpec.respond_to?(:configure)
58
+ RSpec::Matchers.define :match_response_schema do |schema_name, **options|
59
+ matcher = JsonMatchers::RSpec.new(schema_name, options)
60
+
61
+ match do |response|
62
+ matcher.matches?(response)
63
+ end
64
+
65
+ if respond_to?(:failure_message)
66
+ failure_message do |response|
67
+ matcher.failure_message(response)
68
+ end
69
+
70
+ failure_message_when_negated do |response|
71
+ matcher.failure_message_when_negated(response)
72
+ end
73
+ else
74
+ failure_message_for_should do |response|
75
+ matcher.failure_message(response)
76
+ end
77
+
78
+ failure_message_for_should_not do |response|
79
+ matcher.failure_message_when_negated(response)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module JsonMatchers
2
+ VERSION = "0.3.1"
3
+ end
@@ -1,10 +1,10 @@
1
- describe JSON::Matchers, "#match_response_schema" do
1
+ describe JsonMatchers, "#match_response_schema" do
2
2
  it "fails with an invalid JSON body" do
3
3
  create_schema("foo", "")
4
4
 
5
5
  expect {
6
6
  expect(response_for("")).to match_response_schema("foo")
7
- }.to raise_error(JSON::Matchers::InvalidSchemaError)
7
+ }.to raise_error(JsonMatchers::InvalidSchemaError)
8
8
  end
9
9
 
10
10
  it "does not fail with an empty JSON body" do
@@ -1,4 +1,4 @@
1
- require "json/matchers"
1
+ require "json_matchers/rspec"
2
2
 
3
3
  Dir["./spec/support/*"].each { |file| require file }
4
4
 
@@ -1,5 +1,5 @@
1
1
  module FileHelpers
2
- ORIGINAL_SCHEMA_ROOT = JSON::Matchers.schema_root
2
+ ORIGINAL_SCHEMA_ROOT = JsonMatchers.schema_root
3
3
 
4
4
  def create_schema(name, json)
5
5
  File.open("#{schema_root}/#{name}.json", "w") do |file|
@@ -23,7 +23,7 @@ module FileHelpers
23
23
  end
24
24
 
25
25
  def schema_root
26
- JSON::Matchers.schema_root
26
+ JsonMatchers.schema_root
27
27
  end
28
28
  end
29
29
 
@@ -31,12 +31,12 @@ RSpec.configure do |config|
31
31
  config.include FileHelpers
32
32
 
33
33
  config.around do |example|
34
- JSON::Matchers.schema_root = File.join(Dir.pwd, "spec", "fixtures", "schemas")
35
- FileUtils.mkdir_p(JSON::Matchers.schema_root)
34
+ JsonMatchers.schema_root = File.join(Dir.pwd, "spec", "fixtures", "schemas")
35
+ FileUtils.mkdir_p(JsonMatchers.schema_root)
36
36
 
37
37
  example.run
38
38
 
39
- FileUtils.rm_rf(JSON::Matchers.schema_root)
40
- JSON::Matchers.schema_root = FileHelpers::ORIGINAL_SCHEMA_ROOT
39
+ FileUtils.rm_rf(JsonMatchers.schema_root)
40
+ JsonMatchers.schema_root = FileHelpers::ORIGINAL_SCHEMA_ROOT
41
41
  end
42
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Doyle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -112,12 +112,14 @@ files:
112
112
  - README.md
113
113
  - Rakefile
114
114
  - json-matchers.gemspec
115
+ - lib/json-matchers.rb
115
116
  - lib/json/matchers.rb
116
- - lib/json/matchers/errors.rb
117
- - lib/json/matchers/matcher.rb
118
- - lib/json/matchers/rspec.rb
119
- - lib/json/matchers/version.rb
120
- - spec/json/matchers/match_response_schema_spec.rb
117
+ - lib/json_matchers.rb
118
+ - lib/json_matchers/errors.rb
119
+ - lib/json_matchers/matcher.rb
120
+ - lib/json_matchers/rspec.rb
121
+ - lib/json_matchers/version.rb
122
+ - spec/json_matchers/match_response_schema_spec.rb
121
123
  - spec/spec_helper.rb
122
124
  - spec/support/file_helpers.rb
123
125
  homepage: https://github.com/thoughtbot/json-matchers
@@ -145,7 +147,7 @@ signing_key:
145
147
  specification_version: 4
146
148
  summary: Validate your Rails JSON API's JSON
147
149
  test_files:
148
- - spec/json/matchers/match_response_schema_spec.rb
150
+ - spec/json_matchers/match_response_schema_spec.rb
149
151
  - spec/spec_helper.rb
150
152
  - spec/support/file_helpers.rb
151
153
  has_rdoc:
@@ -1,6 +0,0 @@
1
- module JSON
2
- module Matchers
3
- InvalidSchemaError = Class.new(StandardError)
4
- MissingSchema = Class.new(Errno::ENOENT)
5
- end
6
- end
@@ -1,39 +0,0 @@
1
- require "json-schema"
2
-
3
- module JSON
4
- module Matchers
5
- class Matcher
6
- def initialize(schema_path, **options)
7
- @schema_path = schema_path
8
- @options = options
9
- end
10
-
11
- def matches?(response)
12
- @response = response
13
-
14
- validator_options = {
15
- strict: true,
16
- }.merge(options)
17
-
18
- JSON::Validator.validate!(
19
- schema_path.to_s,
20
- response.body,
21
- validator_options,
22
- )
23
- rescue JSON::Schema::ValidationError => ex
24
- @validation_failure_message = ex.message
25
- false
26
- rescue JSON::ParserError
27
- raise InvalidSchemaError
28
- end
29
-
30
- def validation_failure_message
31
- @validation_failure_message.to_s
32
- end
33
-
34
- private
35
-
36
- attr_reader :schema_path, :options
37
- end
38
- end
39
- end
@@ -1,83 +0,0 @@
1
- module JSON
2
- module Matchers
3
- class RSpec < SimpleDelegator
4
- attr_reader :schema_name
5
-
6
- def initialize(schema_name, **options)
7
- @schema_name = schema_name
8
-
9
- super(JSON::Matchers::Matcher.new(schema_path, options))
10
- end
11
-
12
- def failure_message(response)
13
- <<-FAIL.strip_heredoc
14
- expected
15
-
16
- #{response.body}
17
-
18
- to match schema "#{schema_name}":
19
-
20
- #{schema_body}
21
-
22
- ---
23
-
24
- #{validation_failure_message}
25
-
26
- FAIL
27
- end
28
-
29
- def failure_message_when_negated(response)
30
- <<-FAIL.strip_heredoc
31
- expected
32
-
33
- #{response.body}
34
-
35
- not to match schema "#{schema_name}":
36
-
37
- #{schema_body}
38
-
39
- ---
40
-
41
- #{validation_failure_message}
42
-
43
- FAIL
44
- end
45
-
46
- def schema_path
47
- JSON::Matchers.path_to_schema(schema_name)
48
- end
49
-
50
- def schema_body
51
- File.read(schema_path)
52
- end
53
- end
54
- end
55
- end
56
-
57
- if RSpec.respond_to?(:configure)
58
- RSpec::Matchers.define :match_response_schema do |schema_name, **options|
59
- matcher = JSON::Matchers::RSpec.new(schema_name, options)
60
-
61
- match do |response|
62
- matcher.matches?(response)
63
- end
64
-
65
- if respond_to?(:failure_message)
66
- failure_message do |response|
67
- matcher.failure_message(response)
68
- end
69
-
70
- failure_message_when_negated do |response|
71
- matcher.failure_message_when_negated(response)
72
- end
73
- else
74
- failure_message_for_should do |response|
75
- matcher.failure_message(response)
76
- end
77
-
78
- failure_message_for_should_not do |response|
79
- matcher.failure_message_when_negated(response)
80
- end
81
- end
82
- end
83
- end
@@ -1,5 +0,0 @@
1
- module JSON
2
- module Matchers
3
- VERSION = "0.3.0"
4
- end
5
- end