diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-02 12:18:33 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-05-02 12:18:33 +0300 |
commit | 74da249f7e22c20e144ba3c044c6bdeb5df86cd4 (patch) | |
tree | 0db7396e87d999b13cc61dff61e4f323e0a4a5d2 /spec/rubocop | |
parent | e14edb3ce5fd3162d3cf10b88f58333066109f53 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/rubocop')
-rw-r--r-- | spec/rubocop/cop/gettext/static_identifier_spec.rb | 174 | ||||
-rw-r--r-- | spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb | 48 |
2 files changed, 174 insertions, 48 deletions
diff --git a/spec/rubocop/cop/gettext/static_identifier_spec.rb b/spec/rubocop/cop/gettext/static_identifier_spec.rb new file mode 100644 index 00000000000..a0c15d8ad48 --- /dev/null +++ b/spec/rubocop/cop/gettext/static_identifier_spec.rb @@ -0,0 +1,174 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require 'rspec-parameterized' + +require_relative '../../../../rubocop/cop/gettext/static_identifier' + +RSpec.describe RuboCop::Cop::Gettext::StaticIdentifier, feature_category: :internationalization do + describe '#_()' do + it 'does not flag correct use' do + expect_no_offenses(<<~'RUBY') + _('Hello') + _('Hello #{name}') + + _('Hello %{name}') % { name: name } + format(_('Hello %{name}') % { name: name }) + + _('Hello' \ + 'Multiline') + _('Hello' \ + 'Multiline %{name}') % { name: name } + + var = "Hello" + _(var) + _(method_name) + list.each { |item| _(item) } + _(CONST) + RUBY + end + + it 'flags incorrect use' do + expect_offense(<<~'RUBY') + _('Hello' + ' concat') + ^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `_(...)`. + _('Hello'.concat(' concat')) + ^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `_(...)`. + _("Hello #{name}") + ^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `_(...)`. + _('Hello %{name}' % { name: name }) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `_(...)`. + _(format('Hello %{name}') % { name: name }) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `_(...)`. + RUBY + end + end + + describe '#N_()' do + it 'does not flag correct use' do + expect_no_offenses(<<~'RUBY') + N_('Hello') + N_('Hello #{name}') + N_('Hello %{name}') % { name: name } + format(_('Hello %{name}') % { name: name }) + + N_('Hello' \ + 'Multiline') + + var = "Hello" + N_(var) + N_(method_name) + list.each { |item| N_(item) } + N_(CONST) + RUBY + end + + it 'flags incorrect use' do + expect_offense(<<~'RUBY') + N_('Hello' + ' concat') + ^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `N_(...)`. + N_("Hello #{name}") + ^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `N_(...)`. + N_('Hello %{name}' % { name: name }) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `N_(...)`. + N_('Hello' \ + ^^^^^^^^^ Ensure to pass static strings to translation method `N_(...)`. + 'Multiline %{name}' % { name: name }) + N_(format('Hello %{name}') % { name: name }) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `N_(...)`. + RUBY + end + end + + describe '#s_()' do + it 'does not flag correct use' do + expect_no_offenses(<<~'RUBY') + s_('World|Hello') + s_('World|Hello #{name}') + s_('World|Hello %{name}') % { name: name } + format(s_('World|Hello %{name}') % { name: name }) + + s_('World|Hello' \ + 'Multiline') + + var = "Hello" + s_(var) + s_(method_name) + list.each { |item| s_(item) } + s_(CONST) + RUBY + end + + it 'flags incorrect use' do + expect_offense(<<~'RUBY') + s_("World|Hello #{name}") + ^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `s_(...)`. + s_('World|Hello' + ' concat') + ^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `s_(...)`. + s_('World|Hello %{name}' % { name: name }) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `s_(...)`. + s_('World|Hello' \ + ^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `s_(...)`. + 'Multiline %{name}' % { name: name }) + s_(format('World|Hello %{name}') % { name: name }) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `s_(...)`. + RUBY + end + end + + describe '#n_()' do + it 'does not flag correct use' do + expect_no_offenses(<<~'RUBY') + n_('Hello', 'Hellos', 2) + n_('Hello', 'Hellos', count) + + n_('Hello' ' concat', 'Hellos', 2) + n_('Hello', 'Hello' 's', 2) + + n_('Hello %{name}', 'Hellos %{name}', 2) % { name: name } + format(n_('Hello %{name}', 'Hellos %{name}', count) % { name: name }) + + n_('Hello', 'Hellos' \ + 'Multiline', 2) + + n_('Hello' \ + 'Multiline', 'Hellos', 2) + + n_('Hello' \ + 'Multiline %{name}', 'Hellos %{name}', 2) % { name: name } + + var = "Hello" + n_(var, var, 1) + n_(method_name, method_name, count) + list.each { |item| n_(item, item, 2) } + n_(CONST, CONST, 2) + RUBY + end + + it 'flags incorrect use' do + expect_offense(<<~'RUBY') + n_('Hello' + ' concat', 'Hellos', 2) + ^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `n_(...)`. + n_('Hello', 'Hello' + 's', 2) + ^^^^^^^^^^^^^ Ensure to pass static strings to translation method `n_(...)`. + n_("Hello #{name}", "Hellos", 2) + ^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `n_(...)`. + n_('Hello %{name}' % { name: name }, 'Hellos', 2) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `n_(...)`. + n_('Hello' \ + ^^^^^^^^^ Ensure to pass static strings to translation method `n_(...)`. + 'Multiline %{name}' % { name: name }, 'Hellos %{name}', 2) + n_('Hello', format('Hellos %{name}') % { name: name }, count) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ensure to pass static strings to translation method `n_(...)`. + RUBY + end + end + + describe 'edge cases' do + it 'does not flag' do + expect_no_offenses(<<~RUBY) + n_(s_('World|Hello'), s_('World|Hellos'), 2) + RUBY + end + end +end diff --git a/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb b/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb deleted file mode 100644 index b687e91601c..00000000000 --- a/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -# frozen_string_literal: true - -require 'rubocop_spec_helper' - -require_relative '../../../rubocop/cop/ruby_interpolation_in_translation' - -# Disabling interpolation check as we deliberately want to have #{} in strings. -# rubocop:disable Lint/InterpolationCheck -RSpec.describe RuboCop::Cop::RubyInterpolationInTranslation do - let(:msg) { "Don't use ruby interpolation \#{} inside translated strings, instead use %{}" } - - it 'does not add an offense for a regular messages' do - expect_no_offenses('_("Hello world")') - end - - it 'adds the correct offense when using interpolation in a string' do - expect_offense(<<~CODE) - _("Hello \#{world}") - ^^^^^ #{msg} - ^^^^^^^^ #{msg} - CODE - end - - it 'detects when using a ruby interpolation in the first argument of a pluralized string' do - expect_offense(<<~CODE) - n_("Hello \#{world}", "Hello world") - ^^^^^ #{msg} - ^^^^^^^^ #{msg} - CODE - end - - it 'detects when using a ruby interpolation in the second argument of a pluralized string' do - expect_offense(<<~CODE) - n_("Hello world", "Hello \#{world}") - ^^^^^ #{msg} - ^^^^^^^^ #{msg} - CODE - end - - it 'detects when using interpolation in a namespaced translation' do - expect_offense(<<~CODE) - s_("Hello|\#{world}") - ^^^^^ #{msg} - ^^^^^^^^ #{msg} - CODE - end -end -# rubocop:enable Lint/InterpolationCheck |