2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-30 05:08:37 -04:00
|
|
|
require 'fast_spec_helper'
|
2020-04-21 11:21:10 -04:00
|
|
|
|
2020-06-30 05:08:37 -04:00
|
|
|
require 'rspec-parameterized'
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
require_relative '../../../rubocop/cop/static_translation_definition'
|
|
|
|
|
2021-01-07 13:10:38 -05:00
|
|
|
RSpec.describe RuboCop::Cop::StaticTranslationDefinition do
|
2020-04-21 11:21:10 -04:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2021-01-26 13:09:30 -05:00
|
|
|
let(:msg) do
|
|
|
|
"The text you're translating will be already in the translated form when it's assigned to the constant. " \
|
|
|
|
"When a users changes the locale, these texts won't be translated again. " \
|
|
|
|
"Consider moving the translation logic to a method."
|
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
2021-01-26 13:09:30 -05:00
|
|
|
shared_examples 'offense' do |code|
|
2020-04-21 11:21:10 -04:00
|
|
|
it 'registers an offense' do
|
2021-01-26 13:09:30 -05:00
|
|
|
expect_offense(code)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'no offense' do |code|
|
|
|
|
it 'does not register an offense' do
|
2021-01-26 13:09:30 -05:00
|
|
|
expect_no_offenses(code)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'offenses' do
|
2021-01-26 13:09:30 -05:00
|
|
|
where(:code) do
|
2020-04-21 11:21:10 -04:00
|
|
|
[
|
2021-01-26 13:09:30 -05:00
|
|
|
<<~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}
|
2020-09-11 05:08:44 -04:00
|
|
|
end
|
2021-01-26 13:09:30 -05:00
|
|
|
end
|
|
|
|
CODE
|
|
|
|
<<~CODE,
|
|
|
|
module MyModule
|
|
|
|
A = {
|
|
|
|
b: {
|
|
|
|
c: _("a")
|
|
|
|
^^^^^^ #{msg}
|
2020-04-21 11:21:10 -04:00
|
|
|
}
|
2021-01-26 13:09:30 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
<<~CODE
|
|
|
|
class MyClass
|
|
|
|
B = [
|
|
|
|
[
|
|
|
|
s_("a")
|
|
|
|
^^^^^^^ #{msg}
|
2020-04-21 11:21:10 -04:00
|
|
|
]
|
2021-01-26 13:09:30 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
CODE
|
2020-04-21 11:21:10 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
2021-01-26 13:09:30 -05:00
|
|
|
include_examples 'offense', params[:code]
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'ignore' do
|
|
|
|
where(:code) do
|
|
|
|
[
|
|
|
|
'CONSTANT_1 = __("a")',
|
|
|
|
'CONSTANT_2 = s__("a")',
|
|
|
|
'CONSTANT_3 = n__("a")',
|
2020-09-11 05:08:44 -04:00
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
def self.method
|
|
|
|
@cache ||= { hello: -> { _("hello") } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
def method
|
|
|
|
@cache ||= { hello: _("hello") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2020-04-21 11:21:10 -04:00
|
|
|
<<~CODE,
|
|
|
|
def method
|
|
|
|
s_('a')
|
|
|
|
end
|
|
|
|
CODE
|
|
|
|
<<~CODE,
|
|
|
|
class MyClass
|
|
|
|
VALID = -> {
|
|
|
|
s_('hi')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
CODE
|
2021-11-19 19:14:01 -05:00
|
|
|
<<~CODE,
|
2020-04-21 11:21:10 -04:00
|
|
|
class MyClass
|
|
|
|
def hello
|
|
|
|
{
|
|
|
|
a: _('hi')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CODE
|
2021-11-19 19:14:01 -05:00
|
|
|
<<~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
|
2020-04-21 11:21:10 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
include_examples 'no offense', params[:code]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|