Welcome to mirror list, hosted at ThFree Co, Russian Federation.

instrumentation_superclass_spec.rb « usage_data « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a55f0852f3580ff1357a058be84b87d117bfa581 (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
# frozen_string_literal: true

require 'rubocop_spec_helper'

require_relative '../../../../rubocop/cop/usage_data/instrumentation_superclass'

RSpec.describe RuboCop::Cop::UsageData::InstrumentationSuperclass do
  let(:allowed_classes) { %i[GenericMetric DatabaseMetric RedisHllMetric] }
  let(:msg) { "Instrumentation classes should subclass one of the following: #{allowed_classes.join(', ')}." }

  let(:config) do
    RuboCop::Config.new('UsageData/InstrumentationSuperclass' => {
                          'AllowedClasses' => allowed_classes
                        })
  end

  context 'with class definition' do
    context 'when inheriting from allowed superclass' do
      it 'does not register an offense' do
        expect_no_offenses('class NewMetric < GenericMetric; end')
      end
    end

    context 'when inheriting from some other superclass' do
      it 'registers an offense' do
        expect_offense(<<~CODE)
          class NewMetric < BaseMetric; end
                            ^^^^^^^^^^ #{msg}
        CODE
      end
    end

    context 'when not inheriting' do
      it 'does not register an offense' do
        expect_no_offenses('class NewMetric; end')
      end
    end
  end

  context 'with dynamic class definition' do
    context 'when inheriting from allowed superclass' do
      it 'does not register an offense' do
        expect_no_offenses('NewMetric = Class.new(GenericMetric)')
      end
    end

    context 'when inheriting from some other superclass' do
      it 'registers an offense' do
        expect_offense(<<~CODE)
          NewMetric = Class.new(BaseMetric)
                                ^^^^^^^^^^ #{msg}
        CODE
      end
    end

    context 'when not inheriting' do
      it 'does not register an offense' do
        expect_no_offenses('NewMetric = Class.new')
      end
    end
  end
end