summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/gitlab/const_get_inherit_false_spec.rb
blob: 97ed6b743f94836b575e2bfe91f5aba50c285cf7 (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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/gitlab/const_get_inherit_false'

RSpec.describe RuboCop::Cop::Gitlab::ConstGetInheritFalse, type: :rubocop do
  include CopHelper

  subject(:cop) { described_class.new }

  context 'Object.const_get' do
    it 'registers an offense with no 2nd argument' do
      expect_offense(<<~PATTERN)
        Object.const_get(:CONSTANT)
               ^^^^^^^^^ Use inherit=false when using const_get.
      PATTERN
    end

    it 'autocorrects' do
      expect(autocorrect_source('Object.const_get(:CONSTANT)')).to eq('Object.const_get(:CONSTANT, false)')
    end

    context 'inherit=false' do
      it 'does not register an offense' do
        expect_no_offenses(<<~PATTERN)
        Object.const_get(:CONSTANT, false)
        PATTERN
      end
    end

    context 'inherit=true' do
      it 'registers an offense' do
        expect_offense(<<~PATTERN)
        Object.const_get(:CONSTANT, true)
               ^^^^^^^^^ Use inherit=false when using const_get.
        PATTERN
      end

      it 'autocorrects' do
        expect(autocorrect_source('Object.const_get(:CONSTANT, true)')).to eq('Object.const_get(:CONSTANT, false)')
      end
    end
  end

  context 'const_get for a nested class' do
    it 'registers an offense on reload usage' do
      expect_offense(<<~PATTERN)
        Nested::Blog.const_get(:CONSTANT)
                     ^^^^^^^^^ Use inherit=false when using const_get.
      PATTERN
    end

    it 'autocorrects' do
      expect(autocorrect_source('Nested::Blag.const_get(:CONSTANT)')).to eq('Nested::Blag.const_get(:CONSTANT, false)')
    end

    context 'inherit=false' do
      it 'does not register an offense' do
        expect_no_offenses(<<~PATTERN)
        Nested::Blog.const_get(:CONSTANT, false)
        PATTERN
      end
    end

    context 'inherit=true' do
      it 'registers an offense if inherit is true' do
        expect_offense(<<~PATTERN)
        Nested::Blog.const_get(:CONSTANT, true)
                     ^^^^^^^^^ Use inherit=false when using const_get.
        PATTERN
      end

      it 'autocorrects' do
        expect(autocorrect_source('Nested::Blag.const_get(:CONSTANT, true)')).to eq('Nested::Blag.const_get(:CONSTANT, false)')
      end
    end
  end
end