json_matchers 0.6.0 → 0.6.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: 7837005397def477004c328fc25ebf08f3442338
4
- data.tar.gz: 6eabcabe3f0317d35ed02980b67d615e609933cd
3
+ metadata.gz: f804bef576b5eb020767f2b84e25692f5e14225e
4
+ data.tar.gz: d940ccbb14d096153d58bba091b0f663c155732a
5
5
  SHA512:
6
- metadata.gz: 463f19a601e24f35a43ca210636c9e68f80c6f33b5c3e402d4116a5e3bd9c60748a4df8755da1edbde4d570b48453a15bbcd76dccc42d3a643a8d26fe87f5820
7
- data.tar.gz: fba3346e93283c75f917bfa3ee9fd49a1680f1767ef97a701a439eb9aed4596400b929c78b95c0effffc59547eede894a2d1ae67710297ee2e8eef49632f7640
6
+ metadata.gz: 1616399a1d6caf634f18f4a59682fdf56b2fd9a1e257851fc06a904b7741b9cedd18f09e7b758ef8ed3ebb3df88bb1adf7f6b4d216902365a4bf334437810fb0
7
+ data.tar.gz: 5ed37b4b4c32cfe7ff89561690ba347b8f42827a58e32931643a69d23ebfadcd8f414abb255ed34e1a66e5b0f4910e4f4bb334fea2c0937e3997d3bba1cb60ea
data/NEWS.md CHANGED
@@ -1,6 +1,14 @@
1
1
  master
2
2
  ======
3
3
 
4
+ 0.6.1
5
+ =====
6
+
7
+ * Configure default options for all matchers.
8
+ * Use `JSON.pretty_generate` to format error messages. [#44]
9
+
10
+ [#44]: https://github.com/thoughtbot/json_matchers/pull/44
11
+
4
12
  0.6.0
5
13
  =====
6
14
 
data/README.md CHANGED
@@ -103,7 +103,24 @@ describe "GET /posts" do
103
103
  end
104
104
  ```
105
105
 
106
- A list of available options can be found [here][options]
106
+ A list of available options can be found [here][options].
107
+
108
+ [options]: https://github.com/ruby-json-schema/json-schema/blob/2.2.4/lib/json-schema/validator.rb#L160-L162
109
+
110
+ ### Default matcher options
111
+
112
+ To configure the default options passed to *all* matchers, call
113
+ `JsonMatchers.configure`:
114
+
115
+ ```rb
116
+ # spec/support/json_matchers.rb
117
+
118
+ JsonMatchers.configure do |config|
119
+ config.options[:strict] = true
120
+ end
121
+ ```
122
+
123
+ A list of available options can be found [here][options].
107
124
 
108
125
  [options]: https://github.com/ruby-json-schema/json-schema/blob/2.2.4/lib/json-schema/validator.rb#L160-L162
109
126
 
@@ -0,0 +1,17 @@
1
+ module JsonMatchers
2
+ def self.configuration
3
+ @configuration ||= Configuration.new
4
+ end
5
+
6
+ def self.configure
7
+ yield(configuration)
8
+ end
9
+
10
+ class Configuration
11
+ attr_reader :options
12
+
13
+ def initialize
14
+ @options = {}
15
+ end
16
+ end
17
+ end
@@ -2,9 +2,9 @@ require "json-schema"
2
2
 
3
3
  module JsonMatchers
4
4
  class Matcher
5
- def initialize(schema_path, **options)
5
+ def initialize(schema_path, options = {})
6
6
  @schema_path = schema_path
7
- @options = options
7
+ @options = default_options.merge(options)
8
8
  end
9
9
 
10
10
  def matches?(response)
@@ -35,5 +35,9 @@ module JsonMatchers
35
35
  response
36
36
  end
37
37
  end
38
+
39
+ def default_options
40
+ JsonMatchers.configuration.options || {}
41
+ end
38
42
  end
39
43
  end
@@ -11,39 +11,43 @@ module JsonMatchers
11
11
  end
12
12
 
13
13
  def failure_message(response)
14
- <<-FAIL.strip_heredoc
15
- expected
14
+ <<-FAIL
15
+ #{validation_failure_message}
16
16
 
17
- #{response.body}
17
+ ---
18
18
 
19
- to match schema "#{schema_name}":
19
+ expected
20
20
 
21
- #{schema_body}
21
+ #{pretty_json(response.body)}
22
22
 
23
- ---
23
+ to match schema "#{schema_name}":
24
24
 
25
- #{validation_failure_message}
25
+ #{pretty_json(schema_body)}
26
26
 
27
27
  FAIL
28
28
  end
29
29
 
30
30
  def failure_message_when_negated(response)
31
- <<-FAIL.strip_heredoc
32
- expected
31
+ <<-FAIL
32
+ #{validation_failure_message}
33
33
 
34
- #{response.body}
34
+ ---
35
35
 
36
- not to match schema "#{schema_name}":
36
+ expected
37
37
 
38
- #{schema_body}
38
+ #{pretty_json(response.body)}
39
39
 
40
- ---
40
+ not to match schema "#{schema_name}":
41
41
 
42
- #{validation_failure_message}
42
+ #{pretty_json(schema_body)}
43
43
 
44
44
  FAIL
45
45
  end
46
46
 
47
+ def pretty_json(json_string)
48
+ JSON.pretty_generate(JSON.parse(json_string.to_s))
49
+ end
50
+
47
51
  def schema_path
48
52
  JsonMatchers.path_to_schema(schema_name)
49
53
  end
@@ -1,3 +1,3 @@
1
1
  module JsonMatchers
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.1"
3
3
  end
data/lib/json_matchers.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "json_matchers/version"
2
+ require "json_matchers/configuration"
2
3
  require "json_matchers/matcher"
3
4
  require "json_matchers/errors"
4
5
  require "active_support/all"
@@ -22,23 +22,6 @@ describe JsonMatchers, "#match_response_schema" do
22
22
  expect(response_for({})).not_to match_response_schema("foo_schema")
23
23
  end
24
24
 
25
- it "accepts options for the validator" do
26
- create_schema("foo_schema", {
27
- "type" => "object",
28
- "required" => [
29
- "id",
30
- ],
31
- "properties" => {
32
- "id" => { "type" => "number" },
33
- "title" => {"type" => "string"},
34
- },
35
- "additionalProperties" => false,
36
- })
37
-
38
- expect(response_for({ "id" => 1, "title" => "bar" })).
39
- to match_response_schema("foo_schema", strict: false)
40
- end
41
-
42
25
  it "validates a JSON string" do
43
26
  create_schema("foo_schema", {
44
27
  "type" => "object",
@@ -72,7 +55,7 @@ describe JsonMatchers, "#match_response_schema" do
72
55
 
73
56
  expect {
74
57
  expect(response_for("bar" => 5)).to match_response_schema("foo")
75
- }.to raise_error(/{"bar":5}/)
58
+ }.to raise_formatted_error(%{{ "bar": 5 }})
76
59
  end
77
60
 
78
61
  it "contains the body in the failure message when negated" do
@@ -80,7 +63,7 @@ describe JsonMatchers, "#match_response_schema" do
80
63
 
81
64
  expect {
82
65
  expect(response_for([])).not_to match_response_schema("foo")
83
- }.to raise_error(/\[\]/)
66
+ }.to raise_formatted_error("[ ]")
84
67
  end
85
68
 
86
69
  it "contains the schema in the failure message" do
@@ -89,7 +72,7 @@ describe JsonMatchers, "#match_response_schema" do
89
72
 
90
73
  expect {
91
74
  expect(response_for("bar" => 5)).to match_response_schema("foo")
92
- }.to raise_error(/#{schema.to_json}/)
75
+ }.to raise_formatted_error(%{{ "type": "array" }})
93
76
  end
94
77
 
95
78
  it "contains the schema in the failure message when negated" do
@@ -98,7 +81,7 @@ describe JsonMatchers, "#match_response_schema" do
98
81
 
99
82
  expect {
100
83
  expect(response_for([])).not_to match_response_schema("foo")
101
- }.to raise_error(/#{schema.to_json}/)
84
+ }.to raise_formatted_error(%{{ "type": "array" }})
102
85
  end
103
86
 
104
87
  it "does not fail when the schema matches" do
@@ -129,4 +112,54 @@ describe JsonMatchers, "#match_response_schema" do
129
112
  expect(valid_response).to match_response_schema("collection")
130
113
  expect(invalid_response).not_to match_response_schema("collection")
131
114
  end
115
+
116
+ context "when options are passed directly to the matcher" do
117
+ it "forwards options to the validator" do
118
+ create_schema("foo_schema", {
119
+ "type" => "object",
120
+ "properties" => {
121
+ "id" => { "type" => "number" },
122
+ "title" => { "type" => "string" },
123
+ },
124
+ })
125
+
126
+ expect(response_for({ "id" => 1, "title" => "bar" })).
127
+ to match_response_schema("foo_schema", strict: true)
128
+ expect(response_for({ "id" => 1 })).
129
+ not_to match_response_schema("foo_schema", strict: true)
130
+ end
131
+ end
132
+
133
+ context "when options are configured globally" do
134
+ it "forwards them to the validator" do
135
+ create_schema("foo_schema", {
136
+ "type" => "object",
137
+ "properties" => {
138
+ "id" => { "type" => "number" },
139
+ "title" => { "type" => "string" },
140
+ },
141
+ })
142
+
143
+ JsonMatchers.configure do |config|
144
+ config.options[:strict] = true
145
+ end
146
+
147
+ expect(response_for({ "id" => 1, "title" => "bar" })).
148
+ to match_response_schema("foo_schema")
149
+ expect(response_for({ "id" => 1 })).
150
+ not_to match_response_schema("foo_schema")
151
+ end
152
+
153
+ after do
154
+ JsonMatchers.configure do |config|
155
+ config.options.delete(:strict)
156
+ end
157
+ end
158
+ end
159
+
160
+ def raise_formatted_error(error_message)
161
+ raise_error do |error|
162
+ expect(error.message.squish).to include(error_message)
163
+ end
164
+ end
132
165
  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.6.0
4
+ version: 0.6.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: 2016-10-12 00:00:00.000000000 Z
11
+ date: 2016-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -113,6 +113,7 @@ files:
113
113
  - Rakefile
114
114
  - json_matchers.gemspec
115
115
  - lib/json_matchers.rb
116
+ - lib/json_matchers/configuration.rb
116
117
  - lib/json_matchers/errors.rb
117
118
  - lib/json_matchers/matcher.rb
118
119
  - lib/json_matchers/rspec.rb
@@ -148,4 +149,3 @@ test_files:
148
149
  - spec/json_matchers/match_response_schema_spec.rb
149
150
  - spec/spec_helper.rb
150
151
  - spec/support/file_helpers.rb
151
- has_rdoc: