debian-mirror-gitlab/spec/rubocop/cop/gitlab/const_get_inherit_false_spec.rb

81 lines
2.3 KiB
Ruby
Raw Normal View History

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 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/gitlab/const_get_inherit_false'
2020-07-28 23:09:34 +05:30
RSpec.describe RuboCop::Cop::Gitlab::ConstGetInheritFalse, type: :rubocop do
2019-12-21 20:55:43 +05:30
include CopHelper
subject(:cop) { described_class.new }
context 'Object.const_get' do
it 'registers an offense with no 2nd argument' 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
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
2020-06-23 00:09:42 +05:30
expect_no_offenses(<<~PATTERN)
2019-12-21 20:55:43 +05:30
Object.const_get(:CONSTANT, false)
PATTERN
end
end
context 'inherit=true' do
it 'registers an offense' do
2020-06-23 00:09:42 +05:30
expect_offense(<<~PATTERN)
2019-12-21 20:55:43 +05:30
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
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
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
2020-06-23 00:09:42 +05:30
expect_no_offenses(<<~PATTERN)
2019-12-21 20:55:43 +05:30
Nested::Blog.const_get(:CONSTANT, false)
PATTERN
end
end
context 'inherit=true' do
it 'registers an offense if inherit is true' 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, 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