summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/rspec/single_line_hook_spec.rb
blob: 6cf0831d3ad51dae8b6732a85dfdf1b3d94e5b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'spec_helper'

require 'rubocop'
require 'rubocop/rspec/support'

require_relative '../../../../rubocop/cop/rspec/single_line_hook'

describe RuboCop::Cop::RSpec::SingleLineHook do
  include CopHelper

  subject(:cop) { described_class.new }

  # Override `CopHelper#inspect_source` to always appear to be in a spec file,
  # so that our RSpec-only cop actually runs
  def inspect_source(*args)
    super(*args, 'foo_spec.rb')
  end

  it 'registers an offense for a single-line `before` block' do
    inspect_source(cop, 'before { do_something }')

    expect(cop.offenses.size).to eq(1)
    expect(cop.offenses.map(&:line)).to eq([1])
    expect(cop.highlights).to eq(['before { do_something }'])
  end

  it 'registers an offense for a single-line `after` block' do
    inspect_source(cop, 'after(:each) { undo_something }')

    expect(cop.offenses.size).to eq(1)
    expect(cop.offenses.map(&:line)).to eq([1])
    expect(cop.highlights).to eq(['after(:each) { undo_something }'])
  end

  it 'registers an offense for a single-line `around` block' do
    inspect_source(cop, 'around { |ex| do_something_else }')

    expect(cop.offenses.size).to eq(1)
    expect(cop.offenses.map(&:line)).to eq([1])
    expect(cop.highlights).to eq(['around { |ex| do_something_else }'])
  end

  it 'ignores a multi-line `before` block' do
    inspect_source(cop, ['before do',
                         '  do_something',
                         'end'])

    expect(cop.offenses.size).to eq(0)
  end

  it 'ignores a multi-line `after` block' do
    inspect_source(cop, ['after(:each) do',
                         '  undo_something',
                         'end'])

    expect(cop.offenses.size).to eq(0)
  end

  it 'ignores a multi-line `around` block' do
    inspect_source(cop, ['around do |ex|',
                         '  do_something_else',
                         'end'])

    expect(cop.offenses.size).to eq(0)
  end
end