Skip to content

Commit

Permalink
Consistent multi-line string concatenation
Browse files Browse the repository at this point in the history
I have suggested some rules for consistent formatting of these strings
in rubocop/rubocop#6420, but it seems to be
too complicated to have RuboCop enforce them.
  • Loading branch information
bquorning committed Nov 14, 2020
1 parent c9d3b12 commit f62085f
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/around_block.rb
Expand Up @@ -27,7 +27,7 @@ module RSpec
# end
class AroundBlock < Base
MSG_NO_ARG = 'Test object should be passed to around block.'
MSG_UNUSED_ARG = 'You should call `%<arg>s.call` '\
MSG_UNUSED_ARG = 'You should call `%<arg>s.call` ' \
'or `%<arg>s.run`.'

def_node_matcher :hook, <<-PATTERN
Expand Down
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rspec/before_after_all.rb
Expand Up @@ -24,9 +24,9 @@ module RSpec
# after(:each) { Widget.delete_all }
# end
class BeforeAfterAll < Base
MSG = 'Beware of using `%<hook>s` as it may cause state to leak '\
'between tests. If you are using `rspec-rails`, and '\
'`use_transactional_fixtures` is enabled, then records created '\
MSG = 'Beware of using `%<hook>s` as it may cause state to leak ' \
'between tests. If you are using `rspec-rails`, and ' \
'`use_transactional_fixtures` is enabled, then records created ' \
'in `%<hook>s` are not automatically rolled back.'

def_node_matcher :before_or_after_all, <<-PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/describe_class.rb
Expand Up @@ -37,7 +37,7 @@ module RSpec
class DescribeClass < Base
include TopLevelGroup

MSG = 'The first argument to describe should be '\
MSG = 'The first argument to describe should be ' \
'the class or module being tested.'

def_node_matcher :example_group_with_ignored_metadata?, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/describe_method.rb
Expand Up @@ -19,7 +19,7 @@ module RSpec
class DescribeMethod < Base
include TopLevelGroup

MSG = 'The second argument to describe should be the method '\
MSG = 'The second argument to describe should be the method ' \
"being tested. '#instance' or '.class'."

def_node_matcher :second_argument, <<~PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/expect_output.rb
Expand Up @@ -15,7 +15,7 @@ module RSpec
# # good
# expect { my_app.print_report }.to output('Hello World').to_stdout
class ExpectOutput < Base
MSG = 'Use `expect { ... }.to output(...).to_%<name>s` '\
MSG = 'Use `expect { ... }.to output(...).to_%<name>s` ' \
'instead of mutating $%<name>s.'

def on_gvasgn(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/instance_spy.rb
Expand Up @@ -21,7 +21,7 @@ module RSpec
class InstanceSpy < Base
extend AutoCorrector

MSG = 'Use `instance_spy` when you check your double '\
MSG = 'Use `instance_spy` when you check your double ' \
'with `have_received`.'

def_node_search :null_double, <<-PATTERN
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/it_behaves_like.rb
Expand Up @@ -22,7 +22,7 @@ class ItBehavesLike < Base
extend AutoCorrector
include ConfigurableEnforcedStyle

MSG = 'Prefer `%<replacement>s` over `%<original>s` when including '\
MSG = 'Prefer `%<replacement>s` over `%<original>s` when including ' \
'examples in a nested context.'

def_node_matcher :example_inclusion_offense, '(send _ % ...)'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/iterated_expectation.rb
Expand Up @@ -17,7 +17,7 @@ module RSpec
# end
class IteratedExpectation < Base
MSG = 'Prefer using the `all` matcher instead ' \
'of iterating over an array.'
'of iterating over an array.'

def_node_matcher :each?, <<-PATTERN
(block
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rspec/message_spies.rb
Expand Up @@ -29,8 +29,8 @@ class MessageSpies < Base

MSG_RECEIVE = 'Prefer `receive` for setting message expectations.'

MSG_HAVE_RECEIVED = 'Prefer `have_received` for setting message '\
'expectations. Setup `%<source>s` as a spy using '\
MSG_HAVE_RECEIVED = 'Prefer `have_received` for setting message ' \
'expectations. Setup `%<source>s` as a spy using ' \
'`allow` or `instance_spy`.'

SUPPORTED_STYLES = %w[have_received receive].freeze
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/rspec/multiple_describes.rb
Expand Up @@ -25,8 +25,7 @@ module RSpec
class MultipleDescribes < Base
include TopLevelGroup

MSG = 'Do not use multiple top-level example groups - '\
'try to nest them.'
MSG = 'Do not use multiple top-level example groups - try to nest them.'

def on_top_level_group(node)
top_level_example_groups =
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/scattered_setup.rb
Expand Up @@ -23,7 +23,7 @@ module RSpec
# end
#
class ScatteredSetup < Base
MSG = 'Do not define multiple `%<hook_name>s` hooks in the same '\
MSG = 'Do not define multiple `%<hook_name>s` hooks in the same ' \
'example group (also defined on %<lines>s).'

def on_block(node)
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/rspec/shared_context.rb
Expand Up @@ -53,11 +53,8 @@ module RSpec
class SharedContext < Base
extend AutoCorrector

MSG_EXAMPLES = "Use `shared_examples` when you don't "\
'define context.'

MSG_CONTEXT = "Use `shared_context` when you don't "\
'define examples.'
MSG_EXAMPLES = "Use `shared_examples` when you don't define context."
MSG_CONTEXT = "Use `shared_context` when you don't define examples."

def_node_search :examples?,
send_pattern('{#Includes.examples #Examples.all}')
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/single_argument_message_chain.rb
Expand Up @@ -19,7 +19,7 @@ module RSpec
class SingleArgumentMessageChain < Base
extend AutoCorrector

MSG = 'Use `%<recommended>s` instead of calling '\
MSG = 'Use `%<recommended>s` instead of calling ' \
'`%<called>s` with a single argument.'

def_node_matcher :message_chain, <<-PATTERN
Expand Down
6 changes: 3 additions & 3 deletions spec/rubocop/cop/rspec/before_after_all_spec.rb
Expand Up @@ -2,9 +2,9 @@

RSpec.describe RuboCop::Cop::RSpec::BeforeAfterAll do
def message(hook)
"Beware of using `#{hook}` as it may cause state to leak between tests. "\
'If you are using `rspec-rails`, and `use_transactional_fixtures` is '\
"enabled, then records created in `#{hook}` are not automatically rolled "\
"Beware of using `#{hook}` as it may cause state to leak between tests. " \
'If you are using `rspec-rails`, and `use_transactional_fixtures` is ' \
"enabled, then records created in `#{hook}` are not automatically rolled " \
'back.'
end

Expand Down
14 changes: 7 additions & 7 deletions spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb
Expand Up @@ -29,37 +29,37 @@

include_examples 'autocorrect',
'expect(current_path).to eq expected_path',
'expect(page).to have_current_path expected_path, '\
'expect(page).to have_current_path expected_path, ' \
'ignore_query: true'

include_examples 'autocorrect',
'expect(current_path).to eq(expected_path)',
'expect(page).to have_current_path(expected_path, '\
'expect(page).to have_current_path(expected_path, ' \
'ignore_query: true)'

include_examples 'autocorrect',
'expect(current_path).to(eq(expected_path))',
'expect(page).to(have_current_path(expected_path, '\
'expect(page).to(have_current_path(expected_path, ' \
'ignore_query: true))'

include_examples 'autocorrect',
'expect(current_path).to eq(expected_path(f: :b))',
'expect(page).to have_current_path(expected_path(f: :b), '\
'expect(page).to have_current_path(expected_path(f: :b), ' \
'ignore_query: true)'

include_examples 'autocorrect',
'expect(page.current_path).to eq(foo(bar).path)',
'expect(page).to have_current_path(foo(bar).path, '\
'expect(page).to have_current_path(foo(bar).path, ' \
'ignore_query: true)'

include_examples 'autocorrect',
'expect(current_path).not_to eq expected_path',
'expect(page).to have_no_current_path expected_path, '\
'expect(page).to have_no_current_path expected_path, ' \
'ignore_query: true'

include_examples 'autocorrect',
'expect(current_path).to_not eq expected_path',
'expect(page).to have_no_current_path expected_path, '\
'expect(page).to have_no_current_path expected_path, ' \
'ignore_query: true'

include_examples 'autocorrect',
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/rspec/example_wording_spec.rb
Expand Up @@ -152,7 +152,7 @@

it 'flags \-separated multiline strings' do
expect_offense(<<-RUBY)
it 'should do something '\\
it 'should do something ' \\
^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
'and correctly fix' do
end
Expand All @@ -166,7 +166,7 @@

it 'flags \-separated multiline interpolated strings' do
expect_offense(<<-'RUBY')
it "should do something "\
it 'should do something ' \
^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
"with #{object}" do
end
Expand Down

0 comments on commit f62085f

Please sign in to comment.