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

static_translation_definition_spec.rb « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 10b4f16250417421e00444d32429cc011759ab44 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# frozen_string_literal: true

require 'rubocop_spec_helper'

require 'rspec-parameterized'

require_relative '../../../rubocop/cop/static_translation_definition'

RSpec.describe RuboCop::Cop::StaticTranslationDefinition do
  using RSpec::Parameterized::TableSyntax

  let(:msg) { described_class::MSG }

  shared_examples 'offense' do |code|
    it 'registers an offense' do
      expect_offense(code)
    end
  end

  shared_examples 'no offense' do |code|
    it 'does not register an offense' do
      expect_no_offenses(code)
    end
  end

  describe 'offenses' do
    where(:code) do
      [
        <<~CODE,
          A = _("a")
              ^^^^^^ #{msg}
        CODE
        <<~CODE,
          B = s_("b")
              ^^^^^^^ #{msg}
        CODE
        <<~CODE,
          C = n_("c")
              ^^^^^^^ #{msg}
        CODE
        <<~CODE,
          class MyClass
            def self.translations
              @cache ||= { hello: _("hello") }
                                  ^^^^^^^^^^ #{msg}
            end
          end
        CODE
        <<~CODE,
          module MyModule
            A = {
              b: {
                c: _("a")
                   ^^^^^^ #{msg}
              }
            }
          end
        CODE
        <<~CODE,
          class MyClass
            B = [
              [
                s_("a")
                ^^^^^^^ #{msg}
              ]
            ]
          end
        CODE
        <<~CODE,
          class MyClass
            field :foo, title: _('A title')
                               ^^^^^^^^^^^^ #{msg}
          end
        CODE
        <<~CODE
          included do
            _('a')
            ^^^^^^ #{msg}
          end
          prepended do
            self.var = _('a')
                       ^^^^^^ #{msg}
          end
          class_methods do
            _('a')
            ^^^^^^ #{msg}
          end
        CODE
      ]
    end

    with_them do
      include_examples 'offense', params[:code]
    end
  end

  describe 'ignore' do
    where(:code) do
      [
        'CONSTANT_1 = __("a")',
        'CONSTANT_2 = s__("a")',
        'CONSTANT_3 = n__("a")',
        <<~CODE,
          class MyClass
            def self.method
              @cache ||= { hello: -> { _("hello") } }
            end
          end
        CODE
        <<~CODE,
          class MyClass
            def self.method
              @cache ||= { hello: proc { _("hello") } }
            end
          end
        CODE
        <<~CODE,
          class MyClass
            def method
              @cache ||= { hello: _("hello") }
            end
          end
        CODE
        <<~CODE,
          def method
            s_('a')
          end
        CODE
        <<~CODE,
          class MyClass
            VALID = -> {
              s_('hi')
            }
          end
        CODE
        <<~CODE,
          class MyClass
            def hello
              {
                a: _('hi')
              }
            end
          end
        CODE
        <<~CODE,
          SomeClass = Struct.new do
            def text
              _('Some translated text')
            end
          end
        CODE
        <<~CODE,
          Struct.new('SomeClass') do
            def text
              _('Some translated text')
            end
          end
        CODE
        <<~CODE,
          class MyClass
            field :foo, title: -> { _('A title') }
          end
        CODE
        <<~CODE
          included do
            put do
              _('b')
            end
          end
          class_methods do
            expose do
              _('b')
            end
          end
        CODE
      ]
    end

    with_them do
      include_examples 'no offense', params[:code]
    end
  end
end