Skip to content

Commit

Permalink
Use expect_correction with loop: true
Browse files Browse the repository at this point in the history
  • Loading branch information
bquorning authored and bbatsov committed Jun 1, 2020
1 parent e4af299 commit 14a96ea
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 66 deletions.
30 changes: 2 additions & 28 deletions spec/rubocop/cop/bundler/ordered_gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,6 @@
end

context 'When each individual group of line is not sorted' do
let(:source) { <<~RUBY }
gem "d"
gem "b"
gem "e"
gem "a"
gem "c"
gem "h"
gem "g"
gem "j"
gem "f"
gem "i"
RUBY

it 'registers some offenses' do
expect_offense(<<~RUBY)
gem "d"
Expand All @@ -118,11 +104,8 @@
^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `f` should appear before `j`.
gem "i"
RUBY
end

it 'autocorrects' do
new_source = autocorrect_source_with_loop(source)
expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY, loop: true)
gem "a"
gem "b"
gem "c"
Expand Down Expand Up @@ -176,24 +159,15 @@
end

context 'When gems have an inline comment, and not sorted' do
let(:source) { <<~RUBY }
gem 'rubocop' # For code quality
gem 'pry'
gem 'rspec' # For test
RUBY

it 'registers an offense' do
expect_offense(<<~RUBY)
gem 'rubocop' # For code quality
gem 'pry'
^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `pry` should appear before `rubocop`.
gem 'rspec' # For test
RUBY
end

it 'autocorrects' do
new_source = autocorrect_source_with_loop(source)
expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY, loop: true)
gem 'pry'
gem 'rspec' # For test
gem 'rubocop' # For code quality
Expand Down
15 changes: 3 additions & 12 deletions spec/rubocop/cop/layout/class_structure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,14 @@ class Person
extend SomeModule
end
RUBY
end

specify do
expect(autocorrect_source_with_loop(<<~RUBY))
class Example
CONST = 1
expect_correction(<<~RUBY, loop: true)
class Person
include AnotherModule
extend SomeModule
CONST = 'wrong place'
end
RUBY
.to eq(<<~RUBY)
class Example
include AnotherModule
extend SomeModule
CONST = 1
end
RUBY
end
end

Expand Down
20 changes: 4 additions & 16 deletions spec/rubocop/cop/layout/heredoc_indentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,14 @@ def foo
end

it 'registers an offense for not indented, without `~`' do
annotated_source = <<~RUBY
expect_offense(<<~RUBY)
<<#{quote}RUBY2#{quote}
foo
^^^ Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<`.
RUBY2
RUBY

expect_offense(annotated_source)

corrected = looped_autocorrect_from_annotated_source(annotated_source)
expect(corrected).to eq(<<~CORRECTION)
expect_correction(<<~CORRECTION, loop: true)
<<~#{quote}RUBY2#{quote}
foo
RUBY2
Expand All @@ -154,7 +151,7 @@ def foo
end

it 'registers an offense for first line minus-level indented, with `-`' do
annotated_source = <<~RUBY
expect_offense(<<~RUBY)
puts <<-#{quote}RUBY2#{quote}
def foo
^^^^^^^ Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<-`.
Expand All @@ -163,10 +160,7 @@ def foo
RUBY2
RUBY

expect_offense(annotated_source)

corrected = looped_autocorrect_from_annotated_source(annotated_source)
expect(corrected).to eq(<<-CORRECTION)
expect_correction(<<-CORRECTION, loop: true)
puts <<~#{quote}RUBY2#{quote}
def foo
bar
Expand Down Expand Up @@ -243,10 +237,4 @@ def baz
[nil, "'", '"', '`'].each do |quote|
include_examples 'all heredoc type', quote
end

def looped_autocorrect_from_annotated_source(annotated_source)
source =
annotated_source.lines.reject { |line| line =~ /^ *\^/ }.join
autocorrect_source_with_loop(source)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@
end

it 'autocorrects the outer offense later' do
source =
annotated_source.lines.reject { |line| line =~ /^ *\^/ }.join
corrected = autocorrect_source_with_loop(source)
expect_offense(annotated_source)

expect(corrected).to eq(<<~RUBY)
expect_correction(<<~RUBY, loop: true)
@errors << if var.any?(:prob_a_check)
'Problem A'
elsif var.any?(:prob_a_check)
Expand Down Expand Up @@ -2228,11 +2226,9 @@
end

it 'eventually autocorrects all branches' do
source =
annotated_source.lines.reject { |line| line =~ /^ *\^/ }.join
corrected = autocorrect_source_with_loop(source)
expect_offense(annotated_source)

expect(corrected).to eq(<<~RUBY)
expect_correction(<<~RUBY, loop: true)
bar = if outer
1
else
Expand Down
6 changes: 4 additions & 2 deletions spec/rubocop/cop/style/rescue_modifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ def foo
end

it 'corrects doubled rescue modifiers' do
new_source = autocorrect_source_with_loop(<<~RUBY)
expect_offense(<<~RUBY)
blah rescue 1 rescue 2
^^^^^^^^^^^^^ Avoid using `rescue` in its modifier form.
^^^^^^^^^^^^^^^^^^^^^^ Avoid using `rescue` in its modifier form.
RUBY

expect(new_source).to eq(<<~RUBY)
expect_correction(<<~RUBY, loop: true)
begin
begin
blah
Expand Down

0 comments on commit 14a96ea

Please sign in to comment.