diff options
Diffstat (limited to 'spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb')
-rw-r--r-- | spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb | 67 |
1 files changed, 25 insertions, 42 deletions
diff --git a/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb b/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb index a6a44b3fa68..cacf0a1b67d 100644 --- a/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb +++ b/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb @@ -1,68 +1,51 @@ # frozen_string_literal: true require 'fast_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 RSpec.describe RuboCop::Cop::RubyInterpolationInTranslation do - subject(:cop) { described_class.new } + let(:msg) { "Don't use ruby interpolation \#{} inside translated strings, instead use %{}" } - it 'does not add an offence for a regular messages' do - inspect_source('_("Hello world")') + subject(:cop) { described_class.new } - expect(cop.offenses).to be_empty + it 'does not add an offense for a regular messages' do + expect_no_offenses('_("Hello world")') 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 %{}') + 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 - inspect_source('n_("Hello #{world}", "Hello world")') - - expect(cop.offenses).not_to be_empty + 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 - inspect_source('n_("Hello world", "Hello #{world}")') - - expect(cop.offenses).not_to be_empty + expect_offense(<<~CODE) + n_("Hello world", "Hello \#{world}") + ^^^^^ #{msg} + ^^^^^^^^ #{msg} + CODE 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 + expect_offense(<<~CODE) + s_("Hello|\#{world}") + ^^^^^ #{msg} + ^^^^^^^^ #{msg} + CODE end end # rubocop:enable Lint/InterpolationCheck |