2020-04-22 19:07:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
require 'rubocop_spec_helper'
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
require 'rspec-parameterized'
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
require_relative '../../../rubocop/cop/static_translation_definition'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::StaticTranslationDefinition do
|
2020-04-22 19:07:51 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:msg) { described_class::MSG }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
shared_examples 'offense' do |code|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'registers an offense' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(code)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'no offense' do |code|
|
|
|
|
it 'does not register an offense' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_no_offenses(code)
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'offenses' do
|
2021-03-11 19:13:27 +05:30
|
|
|
where(:code) do
|
2020-04-22 19:07:51 +05:30
|
|
|
[
|
2021-03-11 19:13:27 +05:30
|
|
|
<<~CODE,
|
|
|
|
A = _("a")
|
|
|
|
^^^^^^ #{msg}
|
|
|
|
CODE
|
|
|
|
<<~CODE,
|
|
|
|
B = s_("b")
|
|
|
|
^^^^^^^ #{msg}
|
|
|
|
CODE
|
|
|
|
<<~CODE,
|
|
|
|
C = n_("c")
|
|
|
|
^^^^^^^ #{msg}
|
|
|
|
CODE
|
2022-11-25 23:54:43 +05:30
|
|
|
<<~'CODE',
|
|
|
|
A = _('a' \
|
|
|
|
^^^^^^^ [...]
|
|
|
|
'b')
|
|
|
|
CODE
|
|
|
|
<<~'CODE',
|
|
|
|
A = _("a#{s}")
|
|
|
|
^^^^^^^^^^ [...]
|
|
|
|
CODE
|
2021-03-11 19:13:27 +05:30
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
def self.translations
|
|
|
|
@cache ||= { hello: _("hello") }
|
|
|
|
^^^^^^^^^^ #{msg}
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
CODE
|
|
|
|
<<~CODE,
|
|
|
|
module MyModule
|
|
|
|
A = {
|
|
|
|
b: {
|
|
|
|
c: _("a")
|
|
|
|
^^^^^^ #{msg}
|
2020-04-22 19:07:51 +05:30
|
|
|
}
|
2021-03-11 19:13:27 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
CODE
|
2022-07-23 23:45:48 +05:30
|
|
|
<<~CODE,
|
2021-03-11 19:13:27 +05:30
|
|
|
class MyClass
|
|
|
|
B = [
|
|
|
|
[
|
|
|
|
s_("a")
|
|
|
|
^^^^^^^ #{msg}
|
2020-04-22 19:07:51 +05:30
|
|
|
]
|
2021-03-11 19:13:27 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
CODE
|
2022-07-23 23:45:48 +05:30
|
|
|
<<~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
|
2020-04-22 19:07:51 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
2021-03-11 19:13:27 +05:30
|
|
|
include_examples 'offense', params[:code]
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ignore' do
|
|
|
|
where(:code) do
|
|
|
|
[
|
|
|
|
'CONSTANT_1 = __("a")',
|
|
|
|
'CONSTANT_2 = s__("a")',
|
|
|
|
'CONSTANT_3 = n__("a")',
|
2022-11-25 23:54:43 +05:30
|
|
|
'CONSTANT_var = _(code)',
|
|
|
|
'CONSTANT_int = _(1)',
|
|
|
|
'CONSTANT_none = _()',
|
2020-11-24 15:15:51 +05:30
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
def self.method
|
|
|
|
@cache ||= { hello: -> { _("hello") } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2022-07-23 23:45:48 +05:30
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
def self.method
|
|
|
|
@cache ||= { hello: proc { _("hello") } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2020-11-24 15:15:51 +05:30
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
def method
|
|
|
|
@cache ||= { hello: _("hello") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2020-04-22 19:07:51 +05:30
|
|
|
<<~CODE,
|
|
|
|
def method
|
|
|
|
s_('a')
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
VALID = -> {
|
|
|
|
s_('hi')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
CODE
|
2022-01-26 12:08:38 +05:30
|
|
|
<<~CODE,
|
2020-04-22 19:07:51 +05:30
|
|
|
class MyClass
|
|
|
|
def hello
|
|
|
|
{
|
|
|
|
a: _('hi')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2022-01-26 12:08:38 +05:30
|
|
|
<<~CODE,
|
|
|
|
SomeClass = Struct.new do
|
|
|
|
def text
|
|
|
|
_('Some translated text')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2022-07-23 23:45:48 +05:30
|
|
|
<<~CODE,
|
2022-01-26 12:08:38 +05:30
|
|
|
Struct.new('SomeClass') do
|
|
|
|
def text
|
|
|
|
_('Some translated text')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2022-07-23 23:45:48 +05:30
|
|
|
<<~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
|
2020-04-22 19:07:51 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
include_examples 'no offense', params[:code]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|