Skip to content

Commit

Permalink
fixup! Detect all kinds of example groups in MultipleDescribes
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Jul 23, 2020
1 parent 32188de commit 82099fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
47 changes: 14 additions & 33 deletions lib/rubocop/cop/rspec/describe_class.rb
Expand Up @@ -27,52 +27,33 @@ class DescribeClass < Base
MSG = 'The first argument to describe should be '\
'the class or module being tested.'

def_node_matcher :valid_describe?, <<-PATTERN
{
(send #{RSPEC} :describe const ...)
(send #{RSPEC} :describe)
}
PATTERN

def_node_matcher :describe_with_rails_metadata?, <<-PATTERN
(send #{RSPEC} :describe !const ...
(hash <#rails_metadata? ...>)
)
PATTERN

def_node_matcher :rails_metadata?, <<-PATTERN
(pair
(sym :type)
(sym {
:channel :controller :helper :job :mailer :model :request
:routing :view :feature :system :mailbox
}
)
(sym { :channel :controller :helper :job :mailer :model :request
:routing :view :feature :system :mailbox })
)
PATTERN

def_node_matcher :described, <<~PATTERN
(block $(send #{RSPEC} :describe $_ ...) ...)
def_node_matcher :not_a_const_described, <<~PATTERN
(block
[ (send #{RSPEC} :describe $[!const !#string_constant?] ...)
!(send #{RSPEC} :describe ... (hash <#rails_metadata? ...>)) ]
...
)
PATTERN

def on_top_level_group(top_level_node)
node, described = described(top_level_node)

return unless described

return if shared_group?(node) ||
valid_describe?(node) ||
describe_with_rails_metadata?(node) ||
string_constant_describe?(described)

add_offense(described)
not_a_const_described(top_level_node) do |described|
add_offense(described)
end
end

private

def string_constant_describe?(described_value)
described_value.str_type? &&
described_value.value.match?(/^(?:(?:::)?[A-Z]\w*)+$/)
def string_constant?(described)
described.str_type? &&
described.value.match?(/^(?:(?:::)?[A-Z]\w*)+$/)
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions spec/rubocop/cop/rspec/multiple_describes_spec.rb
Expand Up @@ -26,10 +26,9 @@
RUBY
end

it 'flags shared example groups' do
expect_offense(<<-RUBY)
it 'ignores shared example groups' do
expect_no_offenses(<<-RUBY)
shared_examples_for 'behaves' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use multiple top-level example groups - try to nest them.
end
shared_examples_for 'misbehaves' do
end
Expand Down

0 comments on commit 82099fd

Please sign in to comment.