Skip to content
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

Resembles any matches handles empty array #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
.rubocop-*
96 changes: 96 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Rails:
Enabled: true

AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
TargetRailsVersion: 5.1
TargetRubyVersion: 2.5

Include:
- "**/Rakefile"
- "**/config.ru"
Exclude:
- "vendor/**/*"
- "spec/fixtures/**/*"
- "bin/**/*"
- "script/**/*"
- "node_modules/**/*"

inherit_from:
- http://relaxed.ruby.style/rubocop.yml
# Rules subject to debate
Metrics/LineLength:
Max: 120

Metrics/BlockLength:
ExcludedMethods:
- describe
- context
- it
- specify
Exclude:
- "lib/tasks/**/*"
- "config/routes.rb"
Rails/Date:
Enabled: false
Rails/TimeZone:
Enabled: false
Rails/RelativeDateConstant:
Enabled: false
Style/AndOr:
EnforcedStyle: conditionals
Layout/CaseIndentation:
IndentOneStep: true
Style/Documentation:
Enabled: false
Style/EachWithObject:
Enabled: false
Layout/ExtraSpacing:
Exclude:
- "config/routes.rb"
Style/FrozenStringLiteralComment:
Enabled: true
Style/HashSyntax:
Exclude:
- "lib/tasks/**/*"
Layout/MultilineOperationIndentation:
EnforcedStyle: indented
Style/NumericLiterals:
Enabled: true
Exclude:
- "spec/**/*"
Layout/EmptyLinesAroundClassBody:
EnforcedStyle: empty_lines_special
Layout/EmptyLinesAroundModuleBody:
EnforcedStyle: empty_lines_special
Style/SingleLineBlockParams:
Enabled: false
Style/StringLiterals:
EnforcedStyle: double_quotes
Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes
Layout/MultilineMethodCallIndentation:
Exclude:
- "spec/**/*.rb"
Style/FormatString:
Enabled: false
Style/FormatStringToken:
Enabled: false
Style/SymbolArray:
MinSize: 4
Style/WordArray:
MinSize: 4
Lint/UnusedBlockArgument:
Enabled: false
Lint/UnusedMethodArgument:
Enabled: false
Performance/Casecmp:
Enabled: false
Performance/RedundantMerge:
MaxKeyValuePairs: 1
Style/ClassCheck:
Enabled: false
Style/SignalException:
Enabled: false

8 changes: 6 additions & 2 deletions lib/rspec/resembles_json_matchers/resembles_any_of_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ def initialize(expected)

def matches?(actual)
@actual = Array.wrap(actual)
@actual.all? do |a|
expected_matchers.any? { |m| attempted_matchers << m; m.matches? a }
if actual.empty?
expected_matchers.empty?
else
actual.all? do |a|
expected_matchers.any? { |m| attempted_matchers << m; m.matches? a }
end
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/matchers/resembles_any_of_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@

subject(:matcher) { RSpec::ResemblesJsonMatchers::ResemblesAnyOfMatcher.new(matcher_candidates) }

describe "empty array" do
context "when expected has items but actual does not" do
let(:given) { [ ] }
let(:matcher_candidates) { [ 1 ] }

specify { expect(matcher.matches?(given)).to be_falsey }
end

context "when expected is an empty array and actual is not" do
let(:given) { [ 1 ] }
let(:matcher_candidates) { [ ] }

specify { expect(matcher.matches?(given)).to be_falsey }
end

context "when expected and actual are both empty" do
let(:given) { [ ] }
let(:matcher_candidates) { [ ] }

specify { expect(matcher.matches?(given)).to be_truthy }
end
end

context "when every item in the given array matches one of the matchers" do
let(:given) { [ 1, 2, "foo" ] }
let(:matcher_candidates) { [ be_kind_of(Integer), "foo" ] }
Expand Down