Skip to content

Commit

Permalink
Run autocorrect rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz committed Dec 15, 2023
1 parent 5af29dc commit 3451675
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 125 deletions.
14 changes: 7 additions & 7 deletions lib/rubocop/cop/rspec/aggregate_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ class AggregateExamples < ::RuboCop::Cop::Cop
prepend Its
prepend MatchersWithSideEffects

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

def on_block(node)
example_group_with_several_examples(node) do |all_examples|
example_clusters(all_examples).each do |_, examples|
examples[1..-1].each do |example|
examples[1..].each do |example|
add_offense(example,
location: :expression,
message: message_for(example, examples[0]))
location: :expression,
message: message_for(example, examples[0]))
end
end
end
Expand All @@ -143,7 +143,7 @@ def autocorrect(example_node)
range = range_for_replace(examples)
replacement = aggregated_example(examples, metadata)
corrector.replace(range, replacement)
examples[1..-1].map { |example| drop_example(corrector, example) }
examples[1..].map { |example| drop_example(corrector, example) }
end
end
end
Expand Down Expand Up @@ -173,12 +173,12 @@ def message_for(_example, first_example)

def drop_example(corrector, example)
aggregated_range = range_by_whole_lines(example.source_range,
include_final_newline: true)
include_final_newline: true)
corrector.remove(aggregated_range)
end

def aggregated_example(examples, metadata)
base_indent = " " * examples.first.source_range.column
base_indent = ' ' * examples.first.source_range.column
metadata = metadata_for_aggregated_example(metadata)
[
"#{base_indent}specify#{metadata} do",
Expand Down
14 changes: 7 additions & 7 deletions lib/rubocop/cop/rspec/aggregate_examples/its.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def transform_its(body, arguments)
argument = arguments.first
replacement =
case argument.type
when :array
key = argument.values.first
"expect(subject[#{key.source}])"
else
property = argument.value
"expect(subject.#{property})"
when :array
key = argument.values.first
"expect(subject[#{key.source}])"
else
property = argument.value
"expect(subject.#{property})"
end
body.source.gsub(/is_expected|are_expected/, replacement)
end
Expand All @@ -64,7 +64,7 @@ def example_metadata(example)
return super unless its?(example.send_node)

# First parameter to `its` is not metadata.
example.send_node.arguments[1..-1]
example.send_node.arguments[1..]
end

def its?(node)
Expand Down
92 changes: 45 additions & 47 deletions lib/rubocop/cop/rspec/aggregate_examples/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,68 @@ module Cop
module RSpec
# RSpec public API methods that are commonly used in cops
class AggregateExamples < ::RuboCop::Cop::Cop
module Language
RSPEC = "{(const {nil? cbase} :RSpec) nil?}"
module Language
RSPEC = '{(const {nil? cbase} :RSpec) nil?}'

# Set of method selectors
class SelectorSet
def initialize(selectors)
@selectors = selectors
end
# Set of method selectors
class SelectorSet
def initialize(selectors)
@selectors = selectors
end

def ==(other)
selectors.eql?(other.selectors)
end
def ==(other)
selectors.eql?(other.selectors)
end

def +(other)
self.class.new(selectors + other.selectors)
end
def +(other)
self.class.new(selectors + other.selectors)
end

def include?(selector)
selectors.include?(selector)
end
delegate :include?, to: :selectors

def block_pattern
"(block #{send_pattern} ...)"
end
def block_pattern
"(block #{send_pattern} ...)"
end

def send_pattern
"(send #{RSPEC} #{node_pattern_union} ...)"
end
def send_pattern
"(send #{RSPEC} #{node_pattern_union} ...)"
end

def node_pattern_union
"{#{node_pattern}}"
end
def node_pattern_union
"{#{node_pattern}}"
end

def node_pattern
selectors.map(&:inspect).join(" ")
end
def node_pattern
selectors.map(&:inspect).join(' ')
end

protected
protected

attr_reader :selectors
end
attr_reader :selectors
end

module ExampleGroups
GROUPS = SelectorSet.new(%i[describe context feature example_group])
SKIPPED = SelectorSet.new(%i[xdescribe xcontext xfeature])
FOCUSED = SelectorSet.new(%i[fdescribe fcontext ffeature])
module ExampleGroups
GROUPS = SelectorSet.new(%i[describe context feature example_group])
SKIPPED = SelectorSet.new(%i[xdescribe xcontext xfeature])
FOCUSED = SelectorSet.new(%i[fdescribe fcontext ffeature])

ALL = GROUPS + SKIPPED + FOCUSED
end
ALL = GROUPS + SKIPPED + FOCUSED
end

module Examples
EXAMPLES = SelectorSet.new(%i[it specify example scenario its])
FOCUSED = SelectorSet.new(%i[fit fspecify fexample fscenario focus])
SKIPPED = SelectorSet.new(%i[xit xspecify xexample xscenario skip])
PENDING = SelectorSet.new(%i[pending])
module Examples
EXAMPLES = SelectorSet.new(%i[it specify example scenario its])
FOCUSED = SelectorSet.new(%i[fit fspecify fexample fscenario focus])
SKIPPED = SelectorSet.new(%i[xit xspecify xexample xscenario skip])
PENDING = SelectorSet.new(%i[pending])

ALL = EXAMPLES + FOCUSED + SKIPPED + PENDING
end
ALL = EXAMPLES + FOCUSED + SKIPPED + PENDING
end

module Runners
ALL = SelectorSet.new(%i[to to_not not_to])
module Runners
ALL = SelectorSet.new(%i[to to_not not_to])
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module LineRangeHelpers

def range_for_replace(examples)
range = range_by_whole_lines(examples.first.source_range,
include_final_newline: true)
include_final_newline: true)
next_range = range_by_whole_lines(examples[1].source_range)
if adjacent?(range, next_range)
range.resize(range.length + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ module MatchersWithSideEffects
include Language

MSG_FOR_EXPECTATIONS_WITH_SIDE_EFFECTS =
"Aggregate with the example at line %d. IMPORTANT! Pay attention " \
"to the expectation order, some of the matchers have side effects."
'Aggregate with the example at line %d. IMPORTANT! Pay attention ' \
'to the expectation order, some of the matchers have side effects.'

private

Expand All @@ -48,7 +48,7 @@ def message_for(example, first_example)
end

def matcher_with_side_effects_names
cop_config.fetch("MatchersWithSideEffects", [])
cop_config.fetch('MatchersWithSideEffects', [])
.map(&:to_sym)
end

Expand Down
10 changes: 4 additions & 6 deletions lib/rubocop/cop/rspec/aggregate_examples/metadata_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ module MetadataHelpers

def metadata_for_aggregated_example(metadata)
metadata_to_add = metadata.compact.map(&:source)
if add_aggregate_failures_metadata?
metadata_to_add.unshift(":aggregate_failures")
end
metadata_to_add.unshift(':aggregate_failures') if add_aggregate_failures_metadata?
if metadata_to_add.any?
"(#{metadata_to_add.join(", ")})"
"(#{metadata_to_add.join(', ')})"
else
""
''
end
end

Expand Down Expand Up @@ -58,7 +56,7 @@ def metadata_pairs_without_aggegate_failures(metadata)
end

def add_aggregate_failures_metadata?
cop_config.fetch("AddAggregateFailuresMetadata", false)
cop_config.fetch('AddAggregateFailuresMetadata', false)
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions spec/rubocop/cop/rspec/aggregate_examples/its_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

# require "cop_helper"

RSpec.describe RuboCop::Cop::RSpec::AggregateExamples, ".its", :config do
RSpec.describe RuboCop::Cop::RSpec::AggregateExamples, '.its', :config do
subject(:cop) { described_class.new(config) }

let(:all_cops_config) do
{"DisplayCopNames" => false}
{ 'DisplayCopNames' => false }
end

let(:cop_config) do
{"AddAggregateFailuresMetadata" => false}
{ 'AddAggregateFailuresMetadata' => false }
end

subject(:cop) { described_class.new(config) }

# Regular `its` call with an attribute/method name, or a chain of methods
# expressed as a string with dots.
it "flags `its`" do
it 'flags `its`' do
expect_offense(<<~RUBY)
describe do
its(:one) { is_expected.to be(true) }
Expand All @@ -42,7 +42,7 @@

# For single-element array argument, it's possible to make a proper
# correction for `its`.
it "flags `its` with single element array syntax" do
it 'flags `its` with single element array syntax' do
expect_offense(<<~RUBY)
describe do
its([:one]) { is_expected.to be(true) }
Expand All @@ -66,7 +66,7 @@
# - a Hash: `hash[element1][element2]...`
# - and arbitrary type: `hash[element1, element2, ...]`
# It is impossible to infer the type to propose a proper correction.
it "flags `its` with multiple element array syntax, but does not autocorrect" do
it 'flags `its` with multiple element array syntax, but does not autocorrect' do
expect_offense(<<~RUBY)
describe do
its([:one, :two]) { is_expected.to be(true) }
Expand All @@ -79,7 +79,7 @@
end

# Supports single-element `its` array argument with metadata.
it "flags `its` with metadata" do
it 'flags `its` with metadata' do
expect_offense(<<~RUBY)
describe do
its([:one], night_mode: true) { is_expected.to be(true) }
Expand Down Expand Up @@ -112,7 +112,7 @@
#
# NOTE: The same applies to method calls, instance, class, global vars and
# constants.
it "flags `its` with a send node, but does not autocorrect" do
it 'flags `its` with a send node, but does not autocorrect' do
expect_offense(<<~RUBY)
describe do
its(one) { is_expected.to be(false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
# require "test_prof/cops/rspec/aggregate_examples"

RSpec.describe RuboCop::Cop::RSpec::AggregateExamples,
".matchers_with_side_effects", :config do
'.matchers_with_side_effects',
:config do
subject(:cop) { described_class.new(config) }

let(:all_cops_config) do
{"DisplayCopNames" => false}
{ 'DisplayCopNames' => false }
end

subject(:cop) { described_class.new(config) }

context "without side effect matchers defined in configuration" do
context 'without side effect matchers defined in configuration' do
let(:cop_config) do
{"MatchersWithSideEffects" => []}
{ 'MatchersWithSideEffects' => [] }
end

it "flags all examples" do
it 'flags all examples' do
expect_offense(<<~RUBY)
describe do
it { expect(entry).to validate_absence_of(:comment) }
Expand All @@ -36,10 +37,10 @@
end
end

context "with default configuration" do
context 'with default configuration' do
let(:cop_config) { {} }

it "flags without qualifiers, but does not autocorrect" do
it 'flags without qualifiers, but does not autocorrect' do
expect_offense(<<~RUBY)
describe 'with and without side effects' do
it { expect(fruit).to be_good }
Expand All @@ -51,7 +52,7 @@
expect_no_corrections
end

it "flags with qualifiers, but does not autocorrect" do
it 'flags with qualifiers, but does not autocorrect' do
expect_offense(<<~RUBY)
describe 'with and without side effects' do
it { expect(fruit).to be_good }
Expand Down
Loading

0 comments on commit 3451675

Please sign in to comment.