2019-10-29 12:06:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-17 06:08:43 +00:00
|
|
|
require 'fast_spec_helper'
|
2017-11-22 07:50:36 +00:00
|
|
|
require_relative '../../../../rubocop/cop/gitlab/module_with_instance_variables'
|
2017-07-11 18:29:33 +00:00
|
|
|
|
2021-01-07 18:10:38 +00:00
|
|
|
RSpec.describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do
|
2021-01-26 18:09:30 +00:00
|
|
|
let(:msg) { "Do not use instance variables in a module. [...]" }
|
2017-07-11 18:29:33 +00:00
|
|
|
|
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
2021-01-26 18:09:30 +00:00
|
|
|
shared_examples('registering offense') do
|
2017-07-11 18:29:33 +00:00
|
|
|
it 'registers an offense when instance variable is used in a module' do
|
2021-01-26 18:09:30 +00:00
|
|
|
expect_offense(source)
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-18 17:25:23 +00:00
|
|
|
shared_examples('not registering offense') do
|
|
|
|
it 'does not register offenses' do
|
2021-01-26 18:09:30 +00:00
|
|
|
expect_no_offenses(source)
|
2017-09-18 17:25:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-11 18:29:33 +00:00
|
|
|
context 'when source is a regular module' do
|
2021-01-26 18:09:30 +00:00
|
|
|
it_behaves_like 'registering offense' do
|
2017-11-21 15:15:24 +00:00
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module M
|
|
|
|
def f
|
|
|
|
@f = true
|
2021-01-26 18:09:30 +00:00
|
|
|
^^^^^^^^^ #{msg}
|
2017-11-21 15:15:24 +00:00
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source is a nested module' do
|
2021-01-26 18:09:30 +00:00
|
|
|
it_behaves_like 'registering offense' do
|
2017-11-21 15:15:24 +00:00
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module N
|
|
|
|
module M
|
|
|
|
def f
|
|
|
|
@f = true
|
2021-01-26 18:09:30 +00:00
|
|
|
^^^^^^^^^ #{msg}
|
2017-11-21 15:15:24 +00:00
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source is a nested module with multiple offenses' do
|
2021-01-26 18:09:30 +00:00
|
|
|
it_behaves_like 'registering offense' do
|
2017-11-21 15:15:24 +00:00
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module N
|
|
|
|
module M
|
|
|
|
def f
|
|
|
|
@f = true
|
2021-01-26 18:09:30 +00:00
|
|
|
^^^^^^^^^ #{msg}
|
2017-11-21 15:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def g
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def h
|
|
|
|
@h = true
|
2021-01-26 18:09:30 +00:00
|
|
|
^^^^^^^^^ #{msg}
|
2017-11-21 15:15:24 +00:00
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-18 17:25:23 +00:00
|
|
|
context 'when source is using simple or ivar assignment' do
|
2017-11-21 15:15:24 +00:00
|
|
|
it_behaves_like 'not registering offense' do
|
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module M
|
|
|
|
def f
|
|
|
|
@f ||= true
|
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-17 13:25:49 +00:00
|
|
|
context 'when source is using simple ivar' do
|
2017-11-21 15:15:24 +00:00
|
|
|
it_behaves_like 'not registering offense' do
|
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module M
|
|
|
|
def f?
|
|
|
|
@f
|
|
|
|
end
|
2017-11-17 13:25:49 +00:00
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-11-17 13:25:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source is defining initialize' do
|
2017-11-21 15:15:24 +00:00
|
|
|
it_behaves_like 'not registering offense' do
|
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module M
|
|
|
|
def initialize
|
|
|
|
@a = 1
|
|
|
|
@b = 2
|
|
|
|
end
|
2017-11-17 13:25:49 +00:00
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-11-17 13:25:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-18 17:25:23 +00:00
|
|
|
context 'when source is using simple or ivar assignment with other ivar' do
|
2021-01-26 18:09:30 +00:00
|
|
|
it_behaves_like 'registering offense' do
|
2017-11-21 15:15:24 +00:00
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module M
|
|
|
|
def f
|
|
|
|
@f ||= g(@g)
|
2021-01-26 18:09:30 +00:00
|
|
|
^^ #{msg}
|
2017-11-21 15:15:24 +00:00
|
|
|
end
|
2017-09-18 17:25:23 +00:00
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
2017-09-18 17:25:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source is using or ivar assignment with something else' do
|
2021-01-26 18:09:30 +00:00
|
|
|
it_behaves_like 'registering offense' do
|
2017-11-21 15:15:24 +00:00
|
|
|
let(:source) do
|
|
|
|
<<~RUBY
|
|
|
|
module M
|
|
|
|
def f
|
|
|
|
@f ||= true
|
2021-01-26 18:09:30 +00:00
|
|
|
^^ #{msg}
|
2017-11-21 15:15:24 +00:00
|
|
|
@f.to_s
|
2021-01-26 18:09:30 +00:00
|
|
|
^^ #{msg}
|
2017-11-21 15:15:24 +00:00
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
2017-11-21 15:15:24 +00:00
|
|
|
RUBY
|
|
|
|
end
|
2017-07-11 18:29:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|