Skip to content

Commit

Permalink
Merge pull request #9418 from dvandersluis/trailing-empty-lines-specs
Browse files Browse the repository at this point in the history
Fix `Layout/TrailingEmptyLines` spec to not need to use `raise_error`.
  • Loading branch information
dvandersluis authored Jan 26, 2021
2 parents fdf25f2 + fdcfe67 commit 68a75b3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
7 changes: 5 additions & 2 deletions lib/rubocop/rspec/expect_offense.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ def format_offense(source, **replacements)
source
end

def expect_offense(source, file = nil, severity: nil, **replacements)
def expect_offense(source, file = nil, severity: nil, chomp: false, **replacements)
expected_annotations = parse_annotations(source, **replacements)
@processed_source = parse_processed_source(expected_annotations.plain_source, file)
source = expected_annotations.plain_source
source = source.chomp if chomp

@processed_source = parse_processed_source(source, file)
@offenses = _investigate(cop, @processed_source)
actual_annotations =
expected_annotations.with_offense_annotations(@offenses)
Expand Down
50 changes: 23 additions & 27 deletions spec/rubocop/cop/layout/trailing_empty_lines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,24 @@
end

it 'registers an offense for no final newline after assignment' do
expect { expect_no_offenses('x = 0') }.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Final newline missing/
)
expect_offense(<<~RUBY, chomp: true)
x = 0
^{} Final newline missing.
RUBY
end

it 'registers an offense for no final newline after block comment' do
expect do
expect_no_offenses(<<~RUBY.chomp)
puts 'testing rubocop when final new line is missing
after block comments'
=begin
first line
second line
third line
=end
RUBY
end.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Final newline missing/
)
expect_offense(<<~RUBY, chomp: true)
puts 'testing rubocop when final new line is missing
after block comments'
=begin
first line
second line
third line
=end
^{} Final newline missing.
RUBY
end

it 'auto-corrects even if some lines have space' do
Expand All @@ -96,10 +92,10 @@
let(:cop_config) { { 'EnforcedStyle' => 'final_blank_line' } }

it 'registers an offense for final newline' do
expect { expect_no_offenses("x = 0\n") }.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Trailing blank line missing./
)
expect_offense(<<~RUBY, chomp: true)
x = 0\n
^{} Trailing blank line missing.
RUBY
end

it 'registers an offense for multiple trailing blank lines' do
Expand Down Expand Up @@ -133,10 +129,10 @@
end

it 'registers an offense for no final newline' do
expect { expect_no_offenses('x = 0') }.to raise_error(
RSpec::Expectations::ExpectationNotMetError,
/Final newline missing./
)
expect_offense(<<~RUBY, chomp: true)
x = 0
^{} Final newline missing.
RUBY
end

it 'accepts final blank line' do
Expand Down

0 comments on commit 68a75b3

Please sign in to comment.