Skip to content

Attributes matcher #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.lock
coverage
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require:
- rubocop-performance

Style/FrozenStringLiteralComment:
Enabled: false

Style/Documentation:
Enabled: false
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Available matchers:

* `expect(document['data']).to have_id('12')`
* `expect(document['data']).to have_type('users')`
* `expect(document['data']).to have_attributes(:name, :email)`
* `expect(document['data']).to have_jsonapi_attributes(:name, :email)`
* `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
* `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
* `expect(document['data']).to have_relationships(:posts, :comments)`
* `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'

RSpec::Core::RakeTask.new
RuboCop::RakeTask.new

task default: :spec
task default: %i[rubocop spec]
task test: :spec
5 changes: 3 additions & 2 deletions jsonapi-rspec.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = File.read(File.expand_path('../VERSION', __FILE__)).strip
version = File.read(File.expand_path('VERSION', __dir__)).strip

Gem::Specification.new do |spec|
spec.name = 'jsonapi-rspec'
Expand All @@ -15,7 +15,8 @@ Gem::Specification.new do |spec|

spec.add_dependency 'rspec-expectations'

spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rubocop-performance'
spec.add_development_dependency 'simplecov'
end
11 changes: 9 additions & 2 deletions lib/jsonapi/rspec/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ module Attributes
end
end

::RSpec::Matchers.define :have_attributes do |*attrs|
::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
match do |actual|
return false unless actual.key?('attributes')

attrs.all? { |attr| actual['attributes'].key?(attr.to_s) }
counted = (attrs.size == actual['attributes'].size) if @exactly

(attrs.map(&:to_s) - actual['attributes'].keys).empty? &&
(counted == @exactly)
end

chain :exactly do
@exactly = true
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/jsonapi/rspec/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Relationships
if !(actual['relationships'] || {}).key?(rel.to_s)
"expected #{actual} to have relationship #{rel}"
else
"expected #{actual['relationships'][rel.to_s]} to have data #{@data_val}"
"expected #{actual['relationships'][rel.to_s]} " \
"to have data #{@data_val}"
end
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/jsonapi/attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

RSpec.describe JSONAPI::RSpec do
let(:doc) do
{
'attributes' => {
'one' => 1,
'two' => 2,
'four' => 3
}
}
end

describe '#have_attribute' do
it { expect(doc).to have_attribute(:one) }
it { expect(doc).not_to have_attribute(:five) }
end

describe '#have_jsonapi_attributes' do
it { expect(doc).to have_jsonapi_attributes(:one, :two) }
it { expect(doc).not_to have_jsonapi_attributes(:two, :five) }
it { expect(doc).to have_jsonapi_attributes(:one, :two, :four).exactly }
it { expect(doc).not_to have_jsonapi_attributes(:one).exactly }
end
end
2 changes: 1 addition & 1 deletion spec/jsonapi/jsonapi_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe JSONAPI::RSpec, '#have_jsonapi_object' do
context 'when providing no value' do
it 'succeeds when jsonapi object is present' do
expect('jsonapi' => { 'version' => '1.0'}).to have_jsonapi_object
expect('jsonapi' => { 'version' => '1.0' }).to have_jsonapi_object
end

it 'fails when jsonapi object is absent' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
SimpleCov.start do
add_filter '/spec/'
end
SimpleCov.minimum_coverage 90

# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
Expand Down