summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/static_translation_definition_spec.rb
blob: b6c9f6a25df1231c56eafbb4a8296d53edfd2226 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true

require 'fast_spec_helper'

require 'rubocop'
require 'rspec-parameterized'

require_relative '../../../rubocop/cop/static_translation_definition'

RSpec.describe RuboCop::Cop::StaticTranslationDefinition, type: :rubocop do
  include CopHelper

  using RSpec::Parameterized::TableSyntax

  subject(:cop) { described_class.new }

  shared_examples 'offense' do |code, highlight, line|
    it 'registers an offense' do
      inspect_source(code)

      expect(cop.offenses.size).to eq(1)
      expect(cop.offenses.map(&:line)).to eq([line])
      expect(cop.highlights).to eq([highlight])
    end
  end

  shared_examples 'no offense' do |code|
    it 'does not register an offense' do
      inspect_source(code)

      expect(cop.offenses).to be_empty
    end
  end

  describe 'offenses' do
    where(:code, :highlight, :line) do
      [
        ['A = _("a")', '_("a")', 1],
        ['B = s_("b")', 's_("b")', 1],
        ['C = n_("c")', 'n_("c")', 1],
        [
          <<~CODE,
            module MyModule
              A = {
                b: {
                  c: _("a")
                }
              }
            end
          CODE
          '_("a")',
          4
        ],
        [
          <<~CODE,
            class MyClass
              B = [
                [
                  s_("a")
                ]
              ]
            end
          CODE
          's_("a")',
          4
        ]
      ]
    end

    with_them do
      include_examples 'offense', params[:code], params[:highlight], params[:line]
    end
  end

  describe 'ignore' do
    where(:code) do
      [
        'CONSTANT_1 = __("a")',
        'CONSTANT_2 = s__("a")',
        'CONSTANT_3 = n__("a")',
        <<~CODE,
          def method
            s_('a')
          end
        CODE
        <<~CODE,
          class MyClass
            VALID = -> {
              s_('hi')
            }
          end
        CODE
        <<~CODE
          class MyClass
            def hello
              {
                a: _('hi')
              }
            end
          end
        CODE
      ]
    end

    with_them do
      include_examples 'no offense', params[:code]
    end
  end
end