lib/rubocop/cop/rspec/nested_groups.rb in rubocop-rspec-2.12.1 vs lib/rubocop/cop/rspec/nested_groups.rb in rubocop-rspec-2.13.0
- old
+ new
@@ -34,11 +34,11 @@
# it 'yada yada'
# end
# end
# end
#
- # # better
+ # # good
# context 'using some feature as an admin' do
# let(:some) { :various }
# let(:feature) { :setup }
#
# let(:user) do
@@ -51,38 +51,44 @@
#
# it 'blah blah'
# it 'yada yada'
# end
#
- # @example configuration
+ # @example `Max: 3` (default)
+ # # bad
+ # describe Foo do
+ # context 'foo' do
+ # context 'bar' do
+ # context 'baz' do # flagged by rubocop
+ # end
+ # end
+ # end
+ # end
#
- # # .rubocop.yml
- # # RSpec/NestedGroups:
- # # Max: 2
- #
- # context 'when using some feature' do
- # let(:some) { :various }
- # let(:feature) { :setup }
- #
- # context 'when user is signed in' do
- # let(:user) do
- # UserCreate.call(user_attributes)
+ # @example `Max: 2`
+ # # bad
+ # describe Foo do
+ # context 'foo' do
+ # context 'bar' do # flagged by rubocop
+ # context 'baz' do # flagged by rubocop
+ # end
# end
+ # end
+ # end
#
- # let(:user_attributes) do
- # {
- # name: 'John',
- # age: 22,
- # role: role
- # }
+ # @example `AllowedGroups: [] (default)`
+ # describe Foo do # <-- nested groups 1
+ # context 'foo' do # <-- nested groups 2
+ # context 'bar' do # <-- nested groups 3
# end
+ # end
+ # end
#
- # context 'when user is an admin' do # flagged by rubocop
- # let(:role) { 'admin' }
- #
- # it 'blah blah'
- # it 'yada yada'
+ # @example `AllowedGroups: [path]`
+ # describe Foo do # <-- nested groups 1
+ # path '/foo' do # <-- nested groups 1 (not counted)
+ # context 'bar' do # <-- nested groups 2
# end
# end
# end
#
class NestedGroups < Base
@@ -111,17 +117,27 @@
def find_nested_example_groups(node, nesting: 1, &block)
example_group = example_group?(node)
yield node, nesting if example_group && nesting > max_nesting
- next_nesting = example_group ? nesting + 1 : nesting
+ next_nesting = if count_up_nesting?(node, example_group)
+ nesting + 1
+ else
+ nesting
+ end
node.each_child_node(:block, :begin) do |child|
find_nested_example_groups(child, nesting: next_nesting, &block)
end
end
+ def count_up_nesting?(node, example_group)
+ example_group &&
+ (node.block_type? &&
+ !allowed_groups.include?(node.method_name))
+ end
+
def message(nesting)
format(MSG, total: nesting, max: max_nesting)
end
def max_nesting
@@ -133,9 +149,13 @@
warn DEPRECATION_WARNING
cop_config.fetch(DEPRECATED_MAX_KEY)
else
cop_config.fetch('Max', 3)
end
+ end
+
+ def allowed_groups
+ @allowed_groups ||= cop_config.fetch('AllowedGroups', [])
end
end
end
end
end