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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

124 lines
3.1 KiB
Ruby
Raw Normal View History

2021-03-11 19:13:27 +05:30
# frozen_string_literal: true
2022-10-11 01:57:18 +05:30
require 'rubocop_spec_helper'
2021-03-11 19:13:27 +05:30
require_relative '../../../../rubocop/cop/gitlab/namespaced_class'
RSpec.describe RuboCop::Cop::Gitlab::NamespacedClass do
2022-07-16 23:28:13 +05:30
shared_examples 'enforces namespaced classes' do
def namespaced(code)
return code unless namespace
2021-03-11 19:13:27 +05:30
2022-07-16 23:28:13 +05:30
<<~SOURCE
module #{namespace}
#{code}
2021-03-11 19:13:27 +05:30
end
2022-07-16 23:28:13 +05:30
SOURCE
end
it 'flags a class definition without additional namespace' do
expect_offense(namespaced(<<~SOURCE))
class MyClass
2022-08-13 15:12:31 +05:30
^^^^^^^ #{described_class::MSG}
2022-07-16 23:28:13 +05:30
end
SOURCE
end
2021-03-11 19:13:27 +05:30
2022-07-16 23:28:13 +05:30
it 'flags a compact class definition without additional namespace' do
expect_offense(<<~SOURCE, namespace: namespace)
class %{namespace}::MyClass
2022-08-13 15:12:31 +05:30
^{namespace}^^^^^^^^^ #{described_class::MSG}
2022-07-16 23:28:13 +05:30
end
SOURCE
end
it 'flags a class definition with inheritance without additional namespace' do
expect_offense(namespaced(<<~SOURCE))
2021-03-11 19:13:27 +05:30
class MyClass < ApplicationRecord
2022-08-13 15:12:31 +05:30
^^^^^^^ #{described_class::MSG}
2022-07-16 23:28:13 +05:30
def some_method
true
end
2021-03-11 19:13:27 +05:30
end
2022-07-16 23:28:13 +05:30
SOURCE
end
2021-03-11 19:13:27 +05:30
2022-07-16 23:28:13 +05:30
it 'does not flag the class definition with namespace in separate lines' do
expect_no_offenses(namespaced(<<~SOURCE))
module MyModule
class MyClass < ApplicationRecord
end
class MyOtherClass
def other_method
1 + 1
end
2021-03-11 19:13:27 +05:30
end
end
2022-07-16 23:28:13 +05:30
SOURCE
end
2021-03-11 19:13:27 +05:30
2022-07-16 23:28:13 +05:30
it 'does not flag the class definition with nested namespace in separate lines' do
expect_no_offenses(namespaced(<<~SOURCE))
module TopLevelModule
module NestedModule
class MyClass
end
2021-03-11 19:13:27 +05:30
end
end
2022-07-16 23:28:13 +05:30
SOURCE
end
it 'does not flag the class definition nested inside namespaced class' do
expect_no_offenses(namespaced(<<~SOURCE))
module TopLevelModule
class TopLevelClass
class MyClass
end
end
end
SOURCE
end
2021-03-11 19:13:27 +05:30
2022-07-16 23:28:13 +05:30
it 'does not flag the class definition nested inside compact namespace' do
expect_no_offenses(<<~SOURCE)
module #{namespace}::TopLevelModule
2021-03-11 19:13:27 +05:30
class MyClass
end
end
2022-07-16 23:28:13 +05:30
SOURCE
end
it 'does not flag a compact namespaced class definition' do
expect_no_offenses(namespaced(<<~SOURCE))
class MyModule::MyClass < ApplicationRecord
end
SOURCE
end
it 'does not flag a truly compact namespaced class definition' do
expect_no_offenses(<<~SOURCE, namespace: namespace)
class %{namespace}::MyModule::MyClass < ApplicationRecord
end
SOURCE
end
2021-03-11 19:13:27 +05:30
end
2022-07-16 23:28:13 +05:30
context 'without top-level namespace' do
let(:namespace) { nil }
it_behaves_like 'enforces namespaced classes'
end
context 'with Gitlab namespace' do
let(:namespace) { 'Gitlab' }
it_behaves_like 'enforces namespaced classes'
end
context 'with ::Gitlab namespace' do
let(:namespace) { '::Gitlab' }
it_behaves_like 'enforces namespaced classes'
2021-03-11 19:13:27 +05:30
end
end