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

Inherits:
Cop
  • Object
show all
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(example_node) ⇒ Object



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

def autocorrect(example_node)
  clusters = example_clusters_for_autocorrect(example_node)
  return if clusters.empty?

  lambda do |corrector|
    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
end

#on_block(node) ⇒ Object



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

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,
                    location: :expression,
                    message: message_for(example, examples[0]))
      end
    end
  end
end