|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::EmptyLineAfterExample, :config do |
| 4 | + subject(:cop) { described_class.new(config) } |
| 5 | + |
| 6 | + let(:cop_config) { { 'AllowConsecutiveOneLiners' => true } } |
| 7 | + |
| 8 | + it 'flags a missing empty line after `it`' do |
| 9 | + expect_offense(<<-RUBY) |
| 10 | + RSpec.describe Foo do |
| 11 | + it 'does this' do |
| 12 | + end |
| 13 | + ^^^ Add an empty line after `it`. |
| 14 | + it 'does that' do |
| 15 | + end |
| 16 | + end |
| 17 | + RUBY |
| 18 | + |
| 19 | + expect_correction(<<-RUBY) |
| 20 | + RSpec.describe Foo do |
| 21 | + it 'does this' do |
| 22 | + end |
| 23 | +
|
| 24 | + it 'does that' do |
| 25 | + end |
| 26 | + end |
| 27 | + RUBY |
| 28 | + end |
| 29 | + |
| 30 | + it 'flags one-line examples' do |
| 31 | + expect_offense(<<-RUBY) |
| 32 | + RSpec.describe Foo do |
| 33 | + it { } |
| 34 | + ^^^^^^ Add an empty line after `it`. |
| 35 | + it 'does that' do |
| 36 | + end |
| 37 | + end |
| 38 | + RUBY |
| 39 | + |
| 40 | + expect_correction(<<-RUBY) |
| 41 | + RSpec.describe Foo do |
| 42 | + it { } |
| 43 | +
|
| 44 | + it 'does that' do |
| 45 | + end |
| 46 | + end |
| 47 | + RUBY |
| 48 | + end |
| 49 | + |
| 50 | + it 'flags a missing empty line after `specify`' do |
| 51 | + expect_offense(<<-RUBY) |
| 52 | + RSpec.context 'foo' do |
| 53 | + specify do |
| 54 | + end |
| 55 | + ^^^ Add an empty line after `specify`. |
| 56 | + specify 'something gets done' do |
| 57 | + end |
| 58 | + end |
| 59 | + RUBY |
| 60 | + |
| 61 | + expect_correction(<<-RUBY) |
| 62 | + RSpec.context 'foo' do |
| 63 | + specify do |
| 64 | + end |
| 65 | +
|
| 66 | + specify 'something gets done' do |
| 67 | + end |
| 68 | + end |
| 69 | + RUBY |
| 70 | + end |
| 71 | + |
| 72 | + it 'ignores when an empty line is present' do |
| 73 | + expect_no_offenses(<<-RUBY) |
| 74 | + RSpec.describe Foo do |
| 75 | + it 'does this' do |
| 76 | + end |
| 77 | +
|
| 78 | + it 'does that' do |
| 79 | + end |
| 80 | + end |
| 81 | + RUBY |
| 82 | + end |
| 83 | + |
| 84 | + it 'ignores consecutive one-liners' do |
| 85 | + expect_no_offenses(<<-RUBY) |
| 86 | + RSpec.describe Foo do |
| 87 | + it { one } |
| 88 | + it { two } |
| 89 | + end |
| 90 | + RUBY |
| 91 | + end |
| 92 | + |
| 93 | + it 'flags mixed one-line and multi-line examples' do |
| 94 | + expect_offense(<<-RUBY) |
| 95 | + RSpec.context 'foo' do |
| 96 | + it { } |
| 97 | + it { } |
| 98 | + ^^^^^^ Add an empty line after `it`. |
| 99 | + it 'does this' do |
| 100 | + end |
| 101 | + ^^^ Add an empty line after `it`. |
| 102 | + it { } |
| 103 | + it { } |
| 104 | + end |
| 105 | + RUBY |
| 106 | + end |
| 107 | + |
| 108 | + context 'when AllowConsecutiveOneLiners is false' do |
| 109 | + let(:cop_config) { { 'AllowConsecutiveOneLiners' => false } } |
| 110 | + |
| 111 | + it 'ignores consecutive one-liners' do |
| 112 | + expect_offense(<<-RUBY) |
| 113 | + RSpec.describe Foo do |
| 114 | + it { one } |
| 115 | + ^^^^^^^^^^ Add an empty line after `it`. |
| 116 | + it { two } |
| 117 | + end |
| 118 | + RUBY |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments