debian-mirror-gitlab/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

51 lines
1.5 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2020-07-28 23:09:34 +05:30
require 'fast_spec_helper'
2018-11-20 20:47:30 +05:30
require_relative '../../../rubocop/cop/ruby_interpolation_in_translation'
# Disabling interpolation check as we deliberately want to have #{} in strings.
# rubocop:disable Lint/InterpolationCheck
2021-03-08 18:12:59 +05:30
RSpec.describe RuboCop::Cop::RubyInterpolationInTranslation do
2021-03-11 19:13:27 +05:30
let(:msg) { "Don't use ruby interpolation \#{} inside translated strings, instead use %{}" }
2018-11-20 20:47:30 +05:30
2021-03-11 19:13:27 +05:30
subject(:cop) { described_class.new }
2018-11-20 20:47:30 +05:30
2021-03-11 19:13:27 +05:30
it 'does not add an offense for a regular messages' do
expect_no_offenses('_("Hello world")')
2018-11-20 20:47:30 +05:30
end
2021-03-11 19:13:27 +05:30
it 'adds the correct offense when using interpolation in a string' do
expect_offense(<<~CODE)
_("Hello \#{world}")
^^^^^ #{msg}
^^^^^^^^ #{msg}
CODE
2018-11-20 20:47:30 +05:30
end
it 'detects when using a ruby interpolation in the first argument of a pluralized string' do
2021-03-11 19:13:27 +05:30
expect_offense(<<~CODE)
n_("Hello \#{world}", "Hello world")
^^^^^ #{msg}
^^^^^^^^ #{msg}
CODE
2018-11-20 20:47:30 +05:30
end
it 'detects when using a ruby interpolation in the second argument of a pluralized string' do
2021-03-11 19:13:27 +05:30
expect_offense(<<~CODE)
n_("Hello world", "Hello \#{world}")
^^^^^ #{msg}
^^^^^^^^ #{msg}
CODE
2018-11-20 20:47:30 +05:30
end
it 'detects when using interpolation in a namespaced translation' do
2021-03-11 19:13:27 +05:30
expect_offense(<<~CODE)
s_("Hello|\#{world}")
^^^^^ #{msg}
^^^^^^^^ #{msg}
CODE
2018-11-20 20:47:30 +05:30
end
end
# rubocop:enable Lint/InterpolationCheck