json_rspec_match_maker 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 +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.rubocop.yml +4 -0
- data/.rubocop_todo.yml +7 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +56 -0
- data/LICENSE.txt +21 -0
- data/README.md +275 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/json_rspec_match_maker.gemspec +29 -0
- data/lib/json_rspec_match_maker.rb +8 -0
- data/lib/json_rspec_match_maker/base.rb +130 -0
- data/lib/json_rspec_match_maker/expected_value.rb +14 -0
- data/lib/json_rspec_match_maker/target_value.rb +34 -0
- data/lib/json_rspec_match_maker/version.rb +3 -0
- metadata +147 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e36976f311c6c78fe7f6bfb0339d808d93551d27d315c1dc94969817b1d62f6e
|
|
4
|
+
data.tar.gz: b4d671f19e23039cbb97d07894fdd7840fa423bad54f49b9207f762cba970d21
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9f1124d99d8857124c4e05615ef647965ad9712857801da4362fa483b68c171c38e8deb8c474487bfc5467837e5d19efffbb30a195399731f4ab1855d431bc52
|
|
7
|
+
data.tar.gz: d4881b94eb0f429af38d720cf60d9e398594bea46963f501e24bb3588d506c7dbe18f5d218f85d874cc3baf78b7dc53a54e6409a7630dce822423ed8af1fd666
|
data/.gitignore
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
data/.rubocop_todo.yml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# This configuration was generated by
|
|
2
|
+
# `rubocop --auto-gen-config`
|
|
3
|
+
# on 2018-03-10 16:28:20 -0500 using RuboCop version 0.52.1.
|
|
4
|
+
# The point is for the user to remove these configuration records
|
|
5
|
+
# one by one as the offenses are removed from the code base.
|
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at patmcgee331@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
json_rspec_match_maker (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.0)
|
|
10
|
+
diff-lcs (1.3)
|
|
11
|
+
parallel (1.12.1)
|
|
12
|
+
parser (2.5.0.3)
|
|
13
|
+
ast (~> 2.4.0)
|
|
14
|
+
powerpack (0.1.1)
|
|
15
|
+
rainbow (3.0.0)
|
|
16
|
+
rake (10.5.0)
|
|
17
|
+
rspec (3.7.0)
|
|
18
|
+
rspec-core (~> 3.7.0)
|
|
19
|
+
rspec-expectations (~> 3.7.0)
|
|
20
|
+
rspec-mocks (~> 3.7.0)
|
|
21
|
+
rspec-core (3.7.1)
|
|
22
|
+
rspec-support (~> 3.7.0)
|
|
23
|
+
rspec-expectations (3.7.0)
|
|
24
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
25
|
+
rspec-support (~> 3.7.0)
|
|
26
|
+
rspec-mocks (3.7.0)
|
|
27
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
28
|
+
rspec-support (~> 3.7.0)
|
|
29
|
+
rspec-support (3.7.1)
|
|
30
|
+
rubocop (0.52.1)
|
|
31
|
+
parallel (~> 1.10)
|
|
32
|
+
parser (>= 2.4.0.2, < 3.0)
|
|
33
|
+
powerpack (~> 0.1)
|
|
34
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
35
|
+
ruby-progressbar (~> 1.7)
|
|
36
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
|
37
|
+
ruby-progressbar (1.9.0)
|
|
38
|
+
unicode-display_width (1.3.0)
|
|
39
|
+
yard (0.9.12)
|
|
40
|
+
yardstick (0.9.9)
|
|
41
|
+
yard (~> 0.8, >= 0.8.7.2)
|
|
42
|
+
|
|
43
|
+
PLATFORMS
|
|
44
|
+
ruby
|
|
45
|
+
|
|
46
|
+
DEPENDENCIES
|
|
47
|
+
bundler (~> 1.16)
|
|
48
|
+
json_rspec_match_maker!
|
|
49
|
+
rake (~> 10.0)
|
|
50
|
+
rspec (~> 3.7.0)
|
|
51
|
+
rubocop (~> 0.52.1)
|
|
52
|
+
yard (~> 0.9.12)
|
|
53
|
+
yardstick (~> 0.9.9)
|
|
54
|
+
|
|
55
|
+
BUNDLED WITH
|
|
56
|
+
1.16.1
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Patrick McGee
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
# JsonRspecMatchMaker
|
|
2
|
+
|
|
3
|
+
Write RSpec matchers for JSON api endpoints using a simple data structure.
|
|
4
|
+
DRY up API expectations, without losing the specificity sacrificed by a
|
|
5
|
+
schema-based approach to JSON expectations.
|
|
6
|
+
|
|
7
|
+
## Why?
|
|
8
|
+
|
|
9
|
+
As pointed out by Thoughbot in their blog post [Validating JSON Schemas with an
|
|
10
|
+
RSpec
|
|
11
|
+
Matcher](https://robots.thoughtbot.com/validating-json-schemas-with-an-rspec-matcher)
|
|
12
|
+
the naive pattern for writing request specs for a JSON API in rails tends to
|
|
13
|
+
look something like:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
describe "Fetching the current user" do
|
|
17
|
+
context "with valid auth token" do
|
|
18
|
+
it "returns the current user" do
|
|
19
|
+
user = create(:user)
|
|
20
|
+
auth_header = { "Auth-Token" => user.auth_token }
|
|
21
|
+
|
|
22
|
+
get v1_current_user_url, {}, auth_header
|
|
23
|
+
|
|
24
|
+
current_user = response_body["user"]
|
|
25
|
+
expect(response.status).to eq 200
|
|
26
|
+
expect(current_user["auth_token"]).to eq user.auth_token
|
|
27
|
+
expect(current_user["email"]).to eq user.email
|
|
28
|
+
expect(current_user["first_name"]).to eq user.first_name
|
|
29
|
+
expect(current_user["last_name"]).to eq user.last_name
|
|
30
|
+
expect(current_user["id"]).to eq user.id
|
|
31
|
+
expect(current_user["phone_number"]).to eq user.phone_number
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def response_body
|
|
36
|
+
JSON.parse(response.body)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Tedious to write and just as tedious to read.
|
|
42
|
+
|
|
43
|
+
In that post, they talk about one alternative for making tests around JSON
|
|
44
|
+
better - JSON Schema. This is an interesting approach, but I'd like to be more
|
|
45
|
+
specific about my specs than validating that the shape of the data and the types
|
|
46
|
+
of the values are correct.
|
|
47
|
+
|
|
48
|
+
However, I do like the way that the JSON schema is written because it is very
|
|
49
|
+
similar to the JSON that's generated. So that's the goal - enable writing spec
|
|
50
|
+
matchers similar to a JSON schema definition but with greater specificity about values.
|
|
51
|
+
|
|
52
|
+
I think this gem accomplishes that. As an example, here is a JSON Schema
|
|
53
|
+
defintion (also pulled from the Thoughbot blog post)
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"type": "object",
|
|
58
|
+
"required": ["user"],
|
|
59
|
+
"properties": {
|
|
60
|
+
"user" : {
|
|
61
|
+
"type" : "object",
|
|
62
|
+
"required" : [
|
|
63
|
+
"auth_token",
|
|
64
|
+
"email",
|
|
65
|
+
"first_name",
|
|
66
|
+
"id",
|
|
67
|
+
"last_name",
|
|
68
|
+
"phone_number"
|
|
69
|
+
],
|
|
70
|
+
"properties" : {
|
|
71
|
+
"auth_token" : { "type" : "string" },
|
|
72
|
+
"created_at" : { "type" : "string", "format": "date-time" },
|
|
73
|
+
"email" : { "type" : "string" },
|
|
74
|
+
"first_name" : { "type" : "string" },
|
|
75
|
+
"id" : { "type" : "integer" },
|
|
76
|
+
"last_name" : { "type" : "string" },
|
|
77
|
+
"phone_number" : { "type" : "string" },
|
|
78
|
+
"updated_at" : { "type" : "string", "format": "date-time" }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
And here is what the interesting bits of a matcher using this gem would look
|
|
86
|
+
like for the same case:
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
{
|
|
90
|
+
'user.auth_token' => ->(user) { user.auth_token },
|
|
91
|
+
'user.created_at' => ->(user) { user.created_at },
|
|
92
|
+
'user.email' => ->(user) { user.email },
|
|
93
|
+
'user.first_name' => ->(user) { user.first_name },
|
|
94
|
+
'user.id' => ->(user) { user.id },
|
|
95
|
+
'user.last_name' => ->(user) { user.last_name }
|
|
96
|
+
'user.phone_number' => ->(user) { user.phone_number },
|
|
97
|
+
'user.updated_at' => ->(user) { user.updated_at }
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Then that matcher can be used to make your specs:
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
describe "Fetching the current user" do
|
|
105
|
+
context "with valid auth token" do
|
|
106
|
+
it "returns the current user" do
|
|
107
|
+
user = create(:user)
|
|
108
|
+
auth_header = { "Auth-Token" => user.auth_token }
|
|
109
|
+
|
|
110
|
+
get v1_current_user_url, {}, auth_header
|
|
111
|
+
|
|
112
|
+
expect(response_body).to be_valid_json_for_user(user)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def response_body
|
|
117
|
+
JSON.parse(response.body)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Installation with Rails
|
|
123
|
+
|
|
124
|
+
Add this line to your application's Gemfile:
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
gem 'json_rspec_match_maker', require: false
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
And then execute:
|
|
131
|
+
|
|
132
|
+
$ bundle
|
|
133
|
+
|
|
134
|
+
Update your `rails_helper.rb` with:
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
# require the gem
|
|
138
|
+
require 'json_rspec_match_maker'
|
|
139
|
+
|
|
140
|
+
# require your custom matchers you'll be writing
|
|
141
|
+
Dir[Rails.root.join('spec/support/matchers/json_matchers/**/*.rb')].each do |f|
|
|
142
|
+
require f
|
|
143
|
+
end
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Usage
|
|
147
|
+
|
|
148
|
+
Create a new matcher that interhits from the base class:
|
|
149
|
+
|
|
150
|
+
```ruby
|
|
151
|
+
class AddressMatcher < JsonRspecMatchMaker::Base
|
|
152
|
+
end
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
A child class just needs to define and set the `@match_definition`
|
|
156
|
+
|
|
157
|
+
```ruby
|
|
158
|
+
class AddressMatcher < JsonRspecMatchMaker::Base
|
|
159
|
+
def initialize(address)
|
|
160
|
+
@match_definition = set_match_def
|
|
161
|
+
super
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Matchers need to be wrapped in a module we can include in our specs:
|
|
167
|
+
|
|
168
|
+
```ruby
|
|
169
|
+
module JsonMatchers
|
|
170
|
+
class AddressMatcher < JsonRspecMatchMaker::Base
|
|
171
|
+
...
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
That module defines our match method:
|
|
177
|
+
|
|
178
|
+
```ruby
|
|
179
|
+
module JsonMatchers
|
|
180
|
+
# class defined up here...
|
|
181
|
+
|
|
182
|
+
def be_valid_json_for_address(address)
|
|
183
|
+
AddressMatcher.new(address)
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Which we can then use in RSpec like:
|
|
189
|
+
|
|
190
|
+
```ruby
|
|
191
|
+
RSpec.describe 'Address serialization' do
|
|
192
|
+
include JsonMatchers
|
|
193
|
+
|
|
194
|
+
let(:address) { Address.new }
|
|
195
|
+
let(:address_json) { address.to_json }
|
|
196
|
+
|
|
197
|
+
it 'serializes the address' do
|
|
198
|
+
expect(address_json).to be_valid_json_for_address(address)
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Here is an example of a complete matcher class:
|
|
204
|
+
|
|
205
|
+
```ruby
|
|
206
|
+
class AddressMatcher < JsonRspecMatchMaker::Base
|
|
207
|
+
MATCH_DEF = {
|
|
208
|
+
'id' => ->(instance) { instance.id },
|
|
209
|
+
'description' => ->(instance) { instance.description },
|
|
210
|
+
'street_line_one' => ->(instance) { instance.street_line_one },
|
|
211
|
+
'street_line_two' => ->(instance) { instance.street_line_two },
|
|
212
|
+
'city' => ->(instance) { instance.city },
|
|
213
|
+
'state' => ->(instance) { instance.state.abbreviation },
|
|
214
|
+
'postal_code' => ->(instance) { instance.postal_code },
|
|
215
|
+
}.freeze
|
|
216
|
+
|
|
217
|
+
def initialize(address)
|
|
218
|
+
@match_definition = MATCH_DEF
|
|
219
|
+
super
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
In that cause, our expectations are static so we can define the match definition
|
|
225
|
+
as a constant.
|
|
226
|
+
|
|
227
|
+
In other cases, we might want our matchers to be more dynamic so we could do
|
|
228
|
+
something like:
|
|
229
|
+
|
|
230
|
+
```ruby
|
|
231
|
+
class AddressMatcher < JsonRspecMatchMaker::Base
|
|
232
|
+
def initialize(address, state_format)
|
|
233
|
+
@match_definition = set_match_def(state_format)
|
|
234
|
+
super(address)
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def set_match_def(state_format)
|
|
238
|
+
{
|
|
239
|
+
'state' => ->(instance) { instance.state.formatted(state_format) }
|
|
240
|
+
}.merge(MATCH_DEF)
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Arrays are defined very similary to single objects:
|
|
246
|
+
|
|
247
|
+
```ruby
|
|
248
|
+
{
|
|
249
|
+
'answers' => {
|
|
250
|
+
each: ->(instance) { instance.answers },
|
|
251
|
+
attributes: {
|
|
252
|
+
'id' => ->(answer) { answer.id },
|
|
253
|
+
'question' => ->(answer) { answer.question.text },
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
## Development
|
|
260
|
+
|
|
261
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
262
|
+
|
|
263
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
264
|
+
|
|
265
|
+
## Contributing
|
|
266
|
+
|
|
267
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_rspec_match_maker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
268
|
+
|
|
269
|
+
## License
|
|
270
|
+
|
|
271
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
272
|
+
|
|
273
|
+
## Code of Conduct
|
|
274
|
+
|
|
275
|
+
Everyone interacting in the JsonRspecMatchMaker project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/json_rspec_match_maker/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
require 'json_rspec_match_maker'
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require 'irb'
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path('lib', __dir__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'json_rspec_match_maker/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'json_rspec_match_maker'
|
|
8
|
+
spec.version = JsonRspecMatchMaker::VERSION
|
|
9
|
+
spec.authors = ['Patrick McGee']
|
|
10
|
+
spec.email = ['patmcgee@dogparklabs.com']
|
|
11
|
+
|
|
12
|
+
spec.summary = 'Utility class for building JSON RSpec matchers.'
|
|
13
|
+
spec.homepage = 'http://www.github.com/dogparklabs/json_rspec_match_maker'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
17
|
+
f.match(%r{^(spec)/})
|
|
18
|
+
end
|
|
19
|
+
spec.bindir = 'exe'
|
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
21
|
+
spec.require_paths = ['lib']
|
|
22
|
+
|
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.7.0'
|
|
26
|
+
spec.add_development_dependency 'rubocop', '~> 0.52.1'
|
|
27
|
+
spec.add_development_dependency 'yard', '~> 0.9.12'
|
|
28
|
+
spec.add_development_dependency 'yardstick', '~> 0.9.9'
|
|
29
|
+
end
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
require 'json_rspec_match_maker/version'
|
|
2
|
+
require 'json_rspec_match_maker/expected_value'
|
|
3
|
+
require 'json_rspec_match_maker/target_value'
|
|
4
|
+
require 'json_rspec_match_maker/base'
|
|
5
|
+
|
|
6
|
+
# Main point of interest is that this module includes a utility
|
|
7
|
+
# class for building JSON RSpec matchers
|
|
8
|
+
module JsonRspecMatchMaker; end
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
module JsonRspecMatchMaker
|
|
2
|
+
# Error raised when the child class has failed to set @match_definition
|
|
3
|
+
# @api private
|
|
4
|
+
class MatchDefinitionNotFound < StandardError
|
|
5
|
+
# Create an error message for a child class
|
|
6
|
+
# @param class_name [String] the name of the matcher class
|
|
7
|
+
def initialize(class_name)
|
|
8
|
+
super("Expected instance variable @match_defintion to be set for #{class_name}")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Base class that abstracts away all of the work of using the @match_definition
|
|
13
|
+
class Base
|
|
14
|
+
# The object being expected against
|
|
15
|
+
# @api private
|
|
16
|
+
# @return [Object]
|
|
17
|
+
attr_reader :expected
|
|
18
|
+
|
|
19
|
+
# The json being tested
|
|
20
|
+
# @api private
|
|
21
|
+
# @return [Hash]
|
|
22
|
+
attr_reader :target
|
|
23
|
+
|
|
24
|
+
# Data structure that specifies how instance values relate to JSON values
|
|
25
|
+
# @api private
|
|
26
|
+
# @return [Hash]
|
|
27
|
+
attr_reader :match_definition
|
|
28
|
+
|
|
29
|
+
# Create a new JSON matcher
|
|
30
|
+
# @api public
|
|
31
|
+
# @param expected [Object] The object being serialized into JSON
|
|
32
|
+
# @example
|
|
33
|
+
# JsonRspecMatchMaker.new(active_record_model)
|
|
34
|
+
# JsonRspecMatchMaker.new(presenter_instance)
|
|
35
|
+
def initialize(expected)
|
|
36
|
+
@expected = expected
|
|
37
|
+
@errors = {}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Match method called by RSpec
|
|
41
|
+
# @api public
|
|
42
|
+
# @return [Bool]
|
|
43
|
+
# @example
|
|
44
|
+
# JsonRspecMatchMaker.new(user).matches?(user.to_json) #=> true
|
|
45
|
+
# JsonRspecMatchMaker.new(dog).matches?(cat.to_json) #=> false
|
|
46
|
+
def matches?(target)
|
|
47
|
+
@target = target
|
|
48
|
+
check_target_against_expected
|
|
49
|
+
@errors.empty?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Error reporting method called by RSpec
|
|
53
|
+
# @api public
|
|
54
|
+
# @return [String]
|
|
55
|
+
# @example
|
|
56
|
+
# match_maker.failure_message #=> 'Mismatch in field name: expected (Freddy) got (Harold)'
|
|
57
|
+
def failure_message
|
|
58
|
+
@errors.values.join('\n')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
# Walks through the match definition, collecting errors for each field
|
|
64
|
+
# @api private
|
|
65
|
+
# @raise [MatchDefinitionNotFound] if child class does not set @match_definition
|
|
66
|
+
# @return [nil] returns nothing, adds to error list as side effect
|
|
67
|
+
def check_target_against_expected
|
|
68
|
+
raise MatchDefinitionNotFound, self.class.name unless @match_definition
|
|
69
|
+
@match_definition.each do |error_key, match_def|
|
|
70
|
+
if match_def.respond_to? :call
|
|
71
|
+
check_values(error_key, match_def)
|
|
72
|
+
else
|
|
73
|
+
check_each(error_key, match_def)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Iterates through a list of objects while checking fields
|
|
79
|
+
# @api private
|
|
80
|
+
# @param error_key [String]
|
|
81
|
+
# the first name of the field reported in the error
|
|
82
|
+
# each errors are reported #{error_key}[#{idx}].#{each_key}
|
|
83
|
+
# @param each_definition [Hash]
|
|
84
|
+
# :each is a function that returns the list of items
|
|
85
|
+
# :attributes is a hash with the same structure as the top-level match_def hash
|
|
86
|
+
# @return [nil] returns nothing, adds to error list as side effect
|
|
87
|
+
def check_each(error_key, each_definition)
|
|
88
|
+
enumerable = each_definition[:each].call(expected)
|
|
89
|
+
enumerable.each_with_index do |each_instance, idx|
|
|
90
|
+
each_definition[:attributes].each do |attr_error_key, match_function|
|
|
91
|
+
each_opts = { idx: idx, error_key: attr_error_key }
|
|
92
|
+
check_values(error_key, match_function, each_instance, each_opts)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Checks fields on a single instance
|
|
98
|
+
# @api private
|
|
99
|
+
# @param error_key [String] the name of the field reported in the error
|
|
100
|
+
# @param match_function [Hash]
|
|
101
|
+
# a function returning the value for the key for the object being serialized
|
|
102
|
+
# @param expected_instance [Object]
|
|
103
|
+
# the top level instance, or an each instance from #check_each
|
|
104
|
+
# @param each_opts [nil, Hash]
|
|
105
|
+
# nil if checking a top level value
|
|
106
|
+
# Hash if iterating through a list
|
|
107
|
+
# :idx the current index
|
|
108
|
+
# :error_key the subfield reported in the error
|
|
109
|
+
# the index if iterating through a list, otherwise nil
|
|
110
|
+
# @return [nil] returns nothing, adds to error list as side effect
|
|
111
|
+
def check_values(error_key, match_function, expected_instance = expected, each_opts = nil)
|
|
112
|
+
expected_value = ExpectedValue.new(match_function, expected_instance)
|
|
113
|
+
target_value = TargetValue.new(error_key, each_opts, target)
|
|
114
|
+
add_error(expected_value, target_value) unless expected_value == target_value
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Adds an erorr to the list when a mismatch is detected
|
|
118
|
+
# @api private
|
|
119
|
+
# @return [String] the error message
|
|
120
|
+
def add_error(expected_value, target_value)
|
|
121
|
+
@errors[target_value.error_key] = <<-MSG
|
|
122
|
+
|
|
123
|
+
Mismatch in field: '#{target_value.error_key}'
|
|
124
|
+
expected: '#{expected_value.value}'
|
|
125
|
+
received: '#{target_value.value}'
|
|
126
|
+
|
|
127
|
+
MSG
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module JsonRspecMatchMaker
|
|
2
|
+
# Handles fetching the expected value from the expected instance
|
|
3
|
+
class ExpectedValue
|
|
4
|
+
attr_reader :value
|
|
5
|
+
def initialize(match_function, expected_instance)
|
|
6
|
+
@value = match_function.call(expected_instance)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def ==(other)
|
|
10
|
+
raise ArgumentError unless other.is_a? TargetValue
|
|
11
|
+
other.value == value
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module JsonRspecMatchMaker
|
|
2
|
+
# Handles fetching the target value from the target object
|
|
3
|
+
class TargetValue
|
|
4
|
+
attr_reader :error_key, :value
|
|
5
|
+
|
|
6
|
+
def initialize(key, each_opts, target)
|
|
7
|
+
@error_key = full_error_key(key, each_opts)
|
|
8
|
+
@value = fetch_value(key, each_opts, target)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def full_error_key(key, each_opts)
|
|
12
|
+
return key if each_opts.nil?
|
|
13
|
+
|
|
14
|
+
"#{key}[#{each_opts[:idx]}].#{each_opts[:error_key]}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def fetch_value(key, each_opts, target)
|
|
18
|
+
return value_for_key(key, target) if each_opts.nil?
|
|
19
|
+
|
|
20
|
+
targets = value_for_key(key, target)
|
|
21
|
+
specific_target = targets[each_opts[:idx]]
|
|
22
|
+
value_for_key(each_opts[:error_key], specific_target)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def value_for_key(key, json)
|
|
26
|
+
key.split('.').reduce(json) { |j, k| j[k] }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def ==(other)
|
|
30
|
+
raise ArgumentError unless other.is_a? ExpectedValue
|
|
31
|
+
other.value == value
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: json_rspec_match_maker
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Patrick McGee
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-03-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.7.0
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 3.7.0
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rubocop
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 0.52.1
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 0.52.1
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: yard
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 0.9.12
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: 0.9.12
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: yardstick
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 0.9.9
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 0.9.9
|
|
97
|
+
description:
|
|
98
|
+
email:
|
|
99
|
+
- patmcgee@dogparklabs.com
|
|
100
|
+
executables: []
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files: []
|
|
103
|
+
files:
|
|
104
|
+
- ".gitignore"
|
|
105
|
+
- ".rspec"
|
|
106
|
+
- ".rubocop.yml"
|
|
107
|
+
- ".rubocop_todo.yml"
|
|
108
|
+
- ".travis.yml"
|
|
109
|
+
- CODE_OF_CONDUCT.md
|
|
110
|
+
- Gemfile
|
|
111
|
+
- Gemfile.lock
|
|
112
|
+
- LICENSE.txt
|
|
113
|
+
- README.md
|
|
114
|
+
- Rakefile
|
|
115
|
+
- bin/console
|
|
116
|
+
- bin/setup
|
|
117
|
+
- json_rspec_match_maker.gemspec
|
|
118
|
+
- lib/json_rspec_match_maker.rb
|
|
119
|
+
- lib/json_rspec_match_maker/base.rb
|
|
120
|
+
- lib/json_rspec_match_maker/expected_value.rb
|
|
121
|
+
- lib/json_rspec_match_maker/target_value.rb
|
|
122
|
+
- lib/json_rspec_match_maker/version.rb
|
|
123
|
+
homepage: http://www.github.com/dogparklabs/json_rspec_match_maker
|
|
124
|
+
licenses:
|
|
125
|
+
- MIT
|
|
126
|
+
metadata: {}
|
|
127
|
+
post_install_message:
|
|
128
|
+
rdoc_options: []
|
|
129
|
+
require_paths:
|
|
130
|
+
- lib
|
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
|
+
requirements:
|
|
133
|
+
- - ">="
|
|
134
|
+
- !ruby/object:Gem::Version
|
|
135
|
+
version: '0'
|
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
|
+
requirements:
|
|
138
|
+
- - ">="
|
|
139
|
+
- !ruby/object:Gem::Version
|
|
140
|
+
version: '0'
|
|
141
|
+
requirements: []
|
|
142
|
+
rubyforge_project:
|
|
143
|
+
rubygems_version: 2.7.3
|
|
144
|
+
signing_key:
|
|
145
|
+
specification_version: 4
|
|
146
|
+
summary: Utility class for building JSON RSpec matchers.
|
|
147
|
+
test_files: []
|