class RuboCop::Cop::RSpec::EmptyExampleGroup
Checks if an example group does not include any tests.
@example usage
# bad describe Bacon do let(:bacon) { Bacon.new(chunkiness) } let(:chunkiness) { false } context 'extra chunky' do # flagged by rubocop let(:chunkiness) { true } end it 'is chunky' do expect(bacon.chunky?).to be_truthy end end # good describe Bacon do let(:bacon) { Bacon.new(chunkiness) } let(:chunkiness) { false } it 'is chunky' do expect(bacon.chunky?).to be_truthy end end # good describe Bacon do pending 'will add tests later' end
Constants
- MSG
Public Instance Methods
Source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 139 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler return if node.each_ancestor(:any_def).any? return if node.each_ancestor(:block).any? { |block| example?(block) } example_group_body(node) do |body| next unless offensive?(body) add_offense(node.send_node) do |corrector| corrector.remove(removed_range(node)) end end end
Private Instance Methods
Source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 165 def conditionals_with_examples?(body) return false unless body.type?(:begin, :case) body.each_descendant(:if, :case).any? do |condition_node| examples_in_branches?(condition_node) end end
Source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 173 def examples_in_branches?(condition_node) return false unless condition_node return false unless condition_node.type?(:if, :case) condition_node.branches.any? { |branch| examples?(branch) } end
Source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 154 def offensive?(body) return true unless body return false if conditionals_with_examples?(body) if body.type?(:if, :case) !examples_in_branches?(body) else !examples?(body) end end
Source
# File lib/rubocop/cop/rspec/empty_example_group.rb, line 180 def removed_range(node) range_by_whole_lines( node.source_range, include_final_newline: true ) end