summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/i18n/translation_entry_spec.rb
blob: f05346d07d3a46f0f8bd1d0ccc119d952cee2aaf (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::I18n::TranslationEntry do
  describe '#singular_translation' do
    it 'returns the normal `msgstr` for translations without plural' do
      data = { msgid: 'Hello world', msgstr: 'Bonjour monde' }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry.singular_translation).to eq('Bonjour monde')
    end

    it 'returns the first string for entries with plurals' do
      data = {
        msgid: 'Hello world',
        msgid_plural: 'Hello worlds',
        'msgstr[0]' => 'Bonjour monde',
        'msgstr[1]' => 'Bonjour mondes'
      }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry.singular_translation).to eq('Bonjour monde')
    end
  end

  describe '#all_translations' do
    it 'returns all translations for singular translations' do
      data = { msgid: 'Hello world', msgstr: 'Bonjour monde' }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry.all_translations).to eq(['Bonjour monde'])
    end

    it 'returns all translations when including plural translations' do
      data = {
        msgid: 'Hello world',
        msgid_plural: 'Hello worlds',
        'msgstr[0]' => 'Bonjour monde',
        'msgstr[1]' => 'Bonjour mondes'
      }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry.all_translations).to eq(['Bonjour monde', 'Bonjour mondes'])
    end
  end

  describe '#plural_translations' do
    it 'returns all translations if there is only one plural' do
      data = {
        msgid: 'Hello world',
        msgid_plural: 'Hello worlds',
        'msgstr[0]' => 'Bonjour monde'
      }
      entry = described_class.new(entry_data: data, nplurals: 1)

      expect(entry.plural_translations).to eq(['Bonjour monde'])
    end

    it 'returns all translations except for the first one if there are multiple' do
      data = {
        msgid: 'Hello world',
        msgid_plural: 'Hello worlds',
        'msgstr[0]' => 'Bonjour monde',
        'msgstr[1]' => 'Bonjour mondes',
        'msgstr[2]' => 'Bonjour tous les mondes'
      }
      entry = described_class.new(entry_data: data, nplurals: 3)

      expect(entry.plural_translations).to eq(['Bonjour mondes', 'Bonjour tous les mondes'])
    end
  end

  describe '#has_singular_translation?' do
    it 'has a singular when the translation is not pluralized' do
      data = {
        msgid: 'hello world',
        msgstr: 'hello'
      }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry).to have_singular_translation
    end

    it 'has a singular when plural and singular are separately defined' do
      data = {
        msgid: 'hello world',
        msgid_plural: 'hello worlds',
        "msgstr[0]" => 'hello world',
        "msgstr[1]" => 'hello worlds'
      }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry).to have_singular_translation
    end

    it 'does not have a separate singular if the plural string only has one translation' do
      data = {
        msgid: 'hello world',
        msgid_plural: 'hello worlds',
        "msgstr[0]" => 'hello worlds'
      }
      entry = described_class.new(entry_data: data, nplurals: 1)

      expect(entry).not_to have_singular_translation
    end
  end

  describe '#msgid_contains_newlines' do
    it 'is true when the msgid is an array' do
      data = { msgid: %w(hello world) }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry.msgid_has_multiple_lines?).to be_truthy
    end
  end

  describe '#plural_id_contains_newlines' do
    it 'is true when the msgid is an array' do
      data = { msgid_plural: %w(hello world) }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry.plural_id_has_multiple_lines?).to be_truthy
    end
  end

  describe '#translations_contain_newlines' do
    it 'is true when the msgid is an array' do
      data = { msgstr: %w(hello world) }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry.translations_have_multiple_lines?).to be_truthy
    end
  end

  describe '#contains_unescaped_chars' do
    let(:data) { { msgid: '' } }
    let(:entry) { described_class.new(entry_data: data, nplurals: 2) }

    it 'is true when the msgid is an array' do
      string = '「100%確定」'

      expect(entry.contains_unescaped_chars?(string)).to be_truthy
    end

    it 'is false when the `%` char is escaped' do
      string = '「100%%確定」'

      expect(entry.contains_unescaped_chars?(string)).to be_falsy
    end

    it 'is false when using an unnamed variable' do
      string = '「100%d確定」'

      expect(entry.contains_unescaped_chars?(string)).to be_falsy
    end

    it 'is false when using a named variable' do
      string = '「100%{named}確定」'

      expect(entry.contains_unescaped_chars?(string)).to be_falsy
    end

    it 'is true when an unnamed variable is not closed' do
      string = '「100%{named確定」'

      expect(entry.contains_unescaped_chars?(string)).to be_truthy
    end

    it 'is true when the string starts with a `%`' do
      string = '%10'

      expect(entry.contains_unescaped_chars?(string)).to be_truthy
    end
  end

  describe '#msgid_contains_unescaped_chars' do
    it 'is true when the msgid contains a `%`' do
      data = { msgid: '「100%確定」' }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry).to receive(:contains_unescaped_chars?).and_call_original
      expect(entry.msgid_contains_unescaped_chars?).to be_truthy
    end
  end

  describe '#plural_id_contains_unescaped_chars' do
    it 'is true when the plural msgid contains a `%`' do
      data = { msgid_plural: '「100%確定」' }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry).to receive(:contains_unescaped_chars?).and_call_original
      expect(entry.plural_id_contains_unescaped_chars?).to be_truthy
    end
  end

  describe '#translations_contain_unescaped_chars' do
    it 'is true when the translation contains a `%`' do
      data = { msgstr: '「100%確定」' }
      entry = described_class.new(entry_data: data, nplurals: 2)

      expect(entry).to receive(:contains_unescaped_chars?).and_call_original
      expect(entry.translations_contain_unescaped_chars?).to be_truthy
    end
  end

  describe '#msgid_contains_potential_html?' do
    subject(:entry) { described_class.new(entry_data: data, nplurals: 2) }

    context 'when there are no angle brackets in the msgid' do
      let(:data) { { msgid: 'String with no brackets' } }

      it 'returns false' do
        expect(entry.msgid_contains_potential_html?).to be_falsey
      end
    end

    context 'when there are angle brackets in the msgid' do
      let(:data) { { msgid: 'String with <strong> tag' } }

      it 'returns true' do
        expect(entry.msgid_contains_potential_html?).to be_truthy
      end
    end
  end

  describe '#plural_id_contains_potential_html?' do
    subject(:entry) { described_class.new(entry_data: data, nplurals: 2) }

    context 'when there are no angle brackets in the plural_id' do
      let(:data) { { msgid_plural: 'String with no brackets' } }

      it 'returns false' do
        expect(entry.plural_id_contains_potential_html?).to be_falsey
      end
    end

    context 'when there are angle brackets in the plural_id' do
      let(:data) { { msgid_plural: 'This string has a <strong>' } }

      it 'returns true' do
        expect(entry.plural_id_contains_potential_html?).to be_truthy
      end
    end
  end

  describe '#translations_contain_potential_html?' do
    subject(:entry) { described_class.new(entry_data: data, nplurals: 2) }

    context 'when there are no angle brackets in the translations' do
      let(:data) { { msgstr: 'This string has no angle brackets' } }

      it 'returns false' do
        expect(entry.translations_contain_potential_html?).to be_falsey
      end
    end

    context 'when there are angle brackets in the translations' do
      let(:data) { { msgstr: 'This string has a <strong>' } }

      it 'returns true' do
        expect(entry.translations_contain_potential_html?).to be_truthy
      end
    end
  end
end