summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/ruby_interpolation_in_translation_spec.rb
blob: 7bd50866577bc9f16d10663826c33489a78771c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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