2019-11-24 22:06:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-30 03:34:13 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Gitlab::I18n::TranslationEntry do
|
2017-08-30 03:34:13 -04:00
|
|
|
describe '#singular_translation' do
|
|
|
|
it 'returns the normal `msgstr` for translations without plural' do
|
|
|
|
data = { msgid: 'Hello world', msgstr: 'Bonjour monde' }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
|
|
|
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' }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 1, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 3, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
|
|
|
expect(entry.plural_translations).to eq(['Bonjour mondes', 'Bonjour tous les mondes'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
describe '#has_singular_translation?' do
|
2017-08-30 03:34:13 -04:00
|
|
|
it 'has a singular when the translation is not pluralized' do
|
|
|
|
data = {
|
|
|
|
msgid: 'hello world',
|
|
|
|
msgstr: 'hello'
|
|
|
|
}
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
expect(entry).to have_singular_translation
|
2017-08-30 03:34:13 -04:00
|
|
|
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'
|
|
|
|
}
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
expect(entry).to have_singular_translation
|
2017-08-30 03:34:13 -04:00
|
|
|
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'
|
|
|
|
}
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 1, html_allowed: nil)
|
2017-08-30 03:34:13 -04:00
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
expect(entry).not_to have_singular_translation
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-30 03:53:23 -04:00
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
describe '#msgid_contains_newlines' do
|
2017-08-30 03:53:23 -04:00
|
|
|
it 'is true when the msgid is an array' do
|
|
|
|
data = { msgid: %w(hello world) }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:53:23 -04:00
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
expect(entry.msgid_has_multiple_lines?).to be_truthy
|
2017-08-30 03:53:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
describe '#plural_id_contains_newlines' do
|
2017-08-30 03:53:23 -04:00
|
|
|
it 'is true when the msgid is an array' do
|
2017-08-31 07:39:41 -04:00
|
|
|
data = { msgid_plural: %w(hello world) }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:53:23 -04:00
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
expect(entry.plural_id_has_multiple_lines?).to be_truthy
|
2017-08-30 03:53:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-31 07:39:41 -04:00
|
|
|
describe '#translations_contain_newlines' do
|
2017-08-30 03:53:23 -04:00
|
|
|
it 'is true when the msgid is an array' do
|
|
|
|
data = { msgstr: %w(hello world) }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-30 03:53:23 -04:00
|
|
|
|
2018-05-14 09:28:39 -04:00
|
|
|
expect(entry.translations_have_multiple_lines?).to be_truthy
|
2017-08-30 03:53:23 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-31 14:11:22 -04:00
|
|
|
|
|
|
|
describe '#contains_unescaped_chars' do
|
|
|
|
let(:data) { { msgid: '' } }
|
2020-07-22 08:09:42 -04:00
|
|
|
let(:entry) { described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) }
|
2019-12-12 07:07:33 -05:00
|
|
|
|
2017-08-31 14:11:22 -04:00
|
|
|
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%確定」' }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-31 14:11:22 -04:00
|
|
|
|
|
|
|
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%確定」' }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-31 14:11:22 -04:00
|
|
|
|
|
|
|
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%確定」' }
|
2020-07-22 08:09:42 -04:00
|
|
|
entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil)
|
2017-08-31 14:11:22 -04:00
|
|
|
|
|
|
|
expect(entry).to receive(:contains_unescaped_chars?).and_call_original
|
|
|
|
expect(entry.translations_contain_unescaped_chars?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2020-07-22 08:09:42 -04:00
|
|
|
|
|
|
|
describe '#msgid_contains_potential_html?' do
|
|
|
|
subject(:entry) { described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) }
|
|
|
|
|
|
|
|
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, html_allowed: nil) }
|
|
|
|
|
|
|
|
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, html_allowed: nil) }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
describe '#msgid_html_allowed?' do
|
|
|
|
subject(:entry) do
|
|
|
|
described_class.new(entry_data: { msgid: 'String with a <strong>' }, nplurals: 2, html_allowed: html_todo)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the html in the string is in the todolist' do
|
|
|
|
let(:html_todo) { { 'plural_id' => nil, 'translations' => [] } }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(entry.msgid_html_allowed?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the html in the string is not in the todolist' do
|
|
|
|
let(:html_todo) { nil }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(entry.msgid_html_allowed?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#plural_id_html_allowed?' do
|
|
|
|
subject(:entry) do
|
|
|
|
described_class.new(entry_data: { msgid_plural: 'String with many <strong>' }, nplurals: 2, html_allowed: html_todo)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the html in the string is in the todolist' do
|
|
|
|
let(:html_todo) { { 'plural_id' => 'String with many <strong>', 'translations' => [] } }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(entry.plural_id_html_allowed?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the html in the string is not in the todolist' do
|
|
|
|
let(:html_todo) { { 'plural_id' => 'String with some <strong>', 'translations' => [] } }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(entry.plural_id_html_allowed?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#translations_html_allowed?' do
|
|
|
|
subject(:entry) do
|
|
|
|
described_class.new(entry_data: { msgstr: 'String with a <strong>' }, nplurals: 2, html_allowed: html_todo)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the html in the string is in the todolist' do
|
|
|
|
let(:html_todo) { { 'plural_id' => nil, 'translations' => ['String with a <strong>'] } }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(entry.translations_html_allowed?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the html in the string is not in the todolist' do
|
|
|
|
let(:html_todo) { { 'plural_id' => nil, 'translations' => ['String with a different <strong>'] } }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(entry.translations_html_allowed?).to be false
|
|
|
|
end
|
|
|
|
end
|
2020-08-06 14:09:41 -04:00
|
|
|
|
|
|
|
context 'when the todolist only has the msgid' do
|
|
|
|
let(:html_todo) { { 'plural_id' => nil, 'translations' => nil } }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(entry.translations_html_allowed?).to be false
|
|
|
|
end
|
|
|
|
end
|
2020-07-22 08:09:42 -04:00
|
|
|
end
|
2017-08-30 03:34:13 -04:00
|
|
|
end
|