2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
require 'fast_spec_helper'
|
2019-12-21 20:55:43 +05:30
|
|
|
require_relative '../../../../rubocop/cop/gitlab/const_get_inherit_false'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Gitlab::ConstGetInheritFalse do
|
2019-12-21 20:55:43 +05:30
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
|
|
|
context 'Object.const_get' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'registers an offense with no 2nd argument and corrects' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_offense(<<~PATTERN)
|
2019-12-21 20:55:43 +05:30
|
|
|
Object.const_get(:CONSTANT)
|
|
|
|
^^^^^^^^^ Use inherit=false when using const_get.
|
|
|
|
PATTERN
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_correction(<<~PATTERN)
|
|
|
|
Object.const_get(:CONSTANT, false)
|
|
|
|
PATTERN
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'inherit=false' do
|
|
|
|
it 'does not register an offense' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_no_offenses(<<~PATTERN)
|
2021-03-11 19:13:27 +05:30
|
|
|
Object.const_get(:CONSTANT, false)
|
2019-12-21 20:55:43 +05:30
|
|
|
PATTERN
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'inherit=true' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'registers an offense and corrects' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_offense(<<~PATTERN)
|
2021-03-11 19:13:27 +05:30
|
|
|
Object.const_get(:CONSTANT, true)
|
|
|
|
^^^^^^^^^ Use inherit=false when using const_get.
|
2019-12-21 20:55:43 +05:30
|
|
|
PATTERN
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_correction(<<~PATTERN)
|
|
|
|
Object.const_get(:CONSTANT, false)
|
|
|
|
PATTERN
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'const_get for a nested class' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'registers an offense on reload usage and corrects' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_offense(<<~PATTERN)
|
2019-12-21 20:55:43 +05:30
|
|
|
Nested::Blog.const_get(:CONSTANT)
|
|
|
|
^^^^^^^^^ Use inherit=false when using const_get.
|
|
|
|
PATTERN
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_correction(<<~PATTERN)
|
|
|
|
Nested::Blog.const_get(:CONSTANT, false)
|
|
|
|
PATTERN
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'inherit=false' do
|
|
|
|
it 'does not register an offense' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_no_offenses(<<~PATTERN)
|
2021-03-11 19:13:27 +05:30
|
|
|
Nested::Blog.const_get(:CONSTANT, false)
|
2019-12-21 20:55:43 +05:30
|
|
|
PATTERN
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'inherit=true' do
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'registers an offense if inherit is true and corrects' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect_offense(<<~PATTERN)
|
2021-03-11 19:13:27 +05:30
|
|
|
Nested::Blog.const_get(:CONSTANT, true)
|
|
|
|
^^^^^^^^^ Use inherit=false when using const_get.
|
2019-12-21 20:55:43 +05:30
|
|
|
PATTERN
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_correction(<<~PATTERN)
|
|
|
|
Nested::Blog.const_get(:CONSTANT, false)
|
|
|
|
PATTERN
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|