summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/custom_error_class_spec.rb
blob: 381d7871a40c13259db603b6f3667c1286ea9851 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'spec_helper'

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

require_relative '../../../rubocop/cop/custom_error_class'

describe RuboCop::Cop::CustomErrorClass do
  include CopHelper

  subject(:cop) { described_class.new }

  context 'when a class has a body' do
    it 'does nothing' do
      inspect_source(cop, 'class CustomError < StandardError; def foo; end; end')

      expect(cop.offenses).to be_empty
    end
  end

  context 'when a class has no explicit superclass' do
    it 'does nothing' do
      inspect_source(cop, 'class CustomError; end')

      expect(cop.offenses).to be_empty
    end
  end

  context 'when a class has a superclass that does not end in Error' do
    it 'does nothing' do
      inspect_source(cop, 'class CustomError < BasicObject; end')

      expect(cop.offenses).to be_empty
    end
  end

  context 'when a class is empty and inherits from a class ending in Error' do
    context 'when the class is on a single line' do
      let(:source) do
        <<-SOURCE
          module Foo
            class CustomError < Bar::Baz::BaseError; end
          end
        SOURCE
      end

      let(:expected) do
        <<-EXPECTED
          module Foo
            CustomError = Class.new(Bar::Baz::BaseError)
          end
        EXPECTED
      end

      it 'registers an offense' do
        expected_highlights = source.split("\n")[1].strip

        inspect_source(cop, source)

        aggregate_failures do
          expect(cop.offenses.size).to eq(1)
          expect(cop.offenses.map(&:line)).to eq([2])
          expect(cop.highlights).to contain_exactly(expected_highlights)
        end
      end

      it 'autocorrects to the right version' do
        autocorrected = autocorrect_source(cop, source, 'foo/custom_error.rb')

        expect(autocorrected).to eq(expected)
      end
    end

    context 'when the class is on multiple lines' do
      let(:source) do
        <<-SOURCE
          module Foo
            class CustomError < Bar::Baz::BaseError
            end
          end
        SOURCE
      end

      let(:expected) do
        <<-EXPECTED
          module Foo
            CustomError = Class.new(Bar::Baz::BaseError)
          end
        EXPECTED
      end

      it 'registers an offense' do
        expected_highlights = source.split("\n")[1..2].join("\n").strip

        inspect_source(cop, source)

        aggregate_failures do
          expect(cop.offenses.size).to eq(1)
          expect(cop.offenses.map(&:line)).to eq([2])
          expect(cop.highlights).to contain_exactly(expected_highlights)
        end
      end

      it 'autocorrects to the right version' do
        autocorrected = autocorrect_source(cop, source, 'foo/custom_error.rb')

        expect(autocorrected).to eq(expected)
      end
    end
  end
end