diff options
author | Stan Hu <stanhu@gmail.com> | 2018-08-29 22:54:12 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-08-29 22:54:12 -0700 |
commit | 69eddc14b11b63429b8f2511a1127616c692b94c (patch) | |
tree | a94482be144cef60a8ee1b590857ca24f49f418a /spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb | |
parent | bc7a4eedf9fa6681465b622af52c34d49ffb5d0e (diff) | |
parent | f981d4febbbb5103262f4daa858236d9c4ed9d67 (diff) | |
download | gitlab-ce-69eddc14b11b63429b8f2511a1127616c692b94c.tar.gz |
Merge branch 'master' into sh-test-ldap-clones-via-gitlab-qa
Diffstat (limited to 'spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb')
-rw-r--r-- | spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb b/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb new file mode 100644 index 00000000000..7bd50866577 --- /dev/null +++ b/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' + +require 'rubocop' +require 'rubocop/rspec/support' + +require_relative '../../../rubocop/cop/ruby_interpolation_in_translation' + +# Disabling interpolation check as we deliberately want to have #{} in strings. +# rubocop:disable Lint/InterpolationCheck +describe RuboCop::Cop::RubyInterpolationInTranslation do + subject(:cop) { described_class.new } + + it 'does not add an offence for a regular messages' do + inspect_source('_("Hello world")') + + expect(cop.offenses).to be_empty + end + + it 'adds the correct offence when using interpolation in a string' do + inspect_source('_("Hello #{world}")') + + offense = cop.offenses.first + + expect(offense.location.source).to eq('#{world}') + expect(offense.message).to eq('Don\'t use ruby interpolation #{} inside translated strings, instead use %{}') + end + + it 'detects when using a ruby interpolation in the first argument of a pluralized string' do + inspect_source('n_("Hello #{world}", "Hello world")') + + expect(cop.offenses).not_to be_empty + end + + it 'detects when using a ruby interpolation in the second argument of a pluralized string' do + inspect_source('n_("Hello world", "Hello #{world}")') + + expect(cop.offenses).not_to be_empty + end + + it 'detects when using interpolation in a namespaced translation' do + inspect_source('s_("Hello|#{world}")') + + expect(cop.offenses).not_to be_empty + end + + it 'does not add an offence for messages defined over multiple lines' do + source = <<~SRC + _("Hello "\ + "world ") + SRC + + inspect_source(source) + expect(cop.offenses).to be_empty + end + + it 'adds an offence for violations in a message defined over multiple lines' do + source = <<~SRC + _("Hello "\ + "\#{world} ") + SRC + + inspect_source(source) + expect(cop.offenses).not_to be_empty + end +end +# rubocop:enable Lint/InterpolationCheck |