Class: RuboCop::Cop::RSpec::AggregateExamples

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
LineRangeHelpers, MatchersWithSideEffects, MetadataHelpers, NodeMatchers
Defined in:
lib/rubocop/cop/rspec/aggregate_examples.rb,
lib/rubocop/cop/rspec/aggregate_examples/language.rb,
lib/rubocop/cop/rspec/aggregate_examples/node_matchers.rb,
lib/rubocop/cop/rspec/aggregate_examples/metadata_helpers.rb,
lib/rubocop/cop/rspec/aggregate_examples/line_range_helpers.rb,
lib/rubocop/cop/rspec/aggregate_examples/matchers_with_side_effects.rb

Overview

RSpec public API methods that are commonly used in cops

Defined Under Namespace

Modules: Language, LineRangeHelpers, MatchersWithSideEffects, MetadataHelpers, NodeMatchers

Constant Summary collapse

MSG =
'Aggregate with the example at line %d.'

Constants included from Language

Language::RSPEC

Constants included from MatchersWithSideEffects

MatchersWithSideEffects::MSG_FOR_EXPECTATIONS_WITH_SIDE_EFFECTS

Instance Method Summary collapse

Instance Method Details

#autocorrect(corrector, clusters) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/rubocop/cop/rspec/aggregate_examples.rb', line 137

def autocorrect(corrector, clusters)
  clusters.each do |, examples|
    range = range_for_replace(examples)
    replacement = aggregated_example(examples, )
    corrector.replace(range, replacement)
    examples[1..].map { |example| drop_example(corrector, example) }
  end
end

#on_block(node) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rubocop/cop/rspec/aggregate_examples.rb', line 124

def on_block(node)
  example_group_with_several_examples(node) do |all_examples|
    example_clusters(all_examples).each_value do |examples|
      examples[1..].each do |example|
        add_offense(example, message: message_for(example, examples[0])) do |corrector|
          clusters = example_clusters_for_autocorrect(example)
          autocorrect(corrector, clusters)
        end
      end
    end
  end
end