diff options
Diffstat (limited to 'spec/lib/gitlab/i18n/translation_entry_spec.rb')
-rw-r--r-- | spec/lib/gitlab/i18n/translation_entry_spec.rb | 166 |
1 files changed, 150 insertions, 16 deletions
diff --git a/spec/lib/gitlab/i18n/translation_entry_spec.rb b/spec/lib/gitlab/i18n/translation_entry_spec.rb index 76879f75bec..2c95b0b0124 100644 --- a/spec/lib/gitlab/i18n/translation_entry_spec.rb +++ b/spec/lib/gitlab/i18n/translation_entry_spec.rb @@ -6,7 +6,7 @@ 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(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.singular_translation).to eq('Bonjour monde') end @@ -18,7 +18,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 'msgstr[0]' => 'Bonjour monde', 'msgstr[1]' => 'Bonjour mondes' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.singular_translation).to eq('Bonjour monde') end @@ -27,7 +27,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#all_translations' do it 'returns all translations for singular translations' do data = { msgid: 'Hello world', msgstr: 'Bonjour monde' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.all_translations).to eq(['Bonjour monde']) end @@ -39,7 +39,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 'msgstr[0]' => 'Bonjour monde', 'msgstr[1]' => 'Bonjour mondes' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.all_translations).to eq(['Bonjour monde', 'Bonjour mondes']) end @@ -52,7 +52,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do msgid_plural: 'Hello worlds', 'msgstr[0]' => 'Bonjour monde' } - entry = described_class.new(data, 1) + entry = described_class.new(entry_data: data, nplurals: 1, html_allowed: nil) expect(entry.plural_translations).to eq(['Bonjour monde']) end @@ -65,7 +65,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 'msgstr[1]' => 'Bonjour mondes', 'msgstr[2]' => 'Bonjour tous les mondes' } - entry = described_class.new(data, 3) + entry = described_class.new(entry_data: data, nplurals: 3, html_allowed: nil) expect(entry.plural_translations).to eq(['Bonjour mondes', 'Bonjour tous les mondes']) end @@ -77,7 +77,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do msgid: 'hello world', msgstr: 'hello' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to have_singular_translation end @@ -89,7 +89,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do "msgstr[0]" => 'hello world', "msgstr[1]" => 'hello worlds' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to have_singular_translation end @@ -100,7 +100,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do msgid_plural: 'hello worlds', "msgstr[0]" => 'hello worlds' } - entry = described_class.new(data, 1) + entry = described_class.new(entry_data: data, nplurals: 1, html_allowed: nil) expect(entry).not_to have_singular_translation end @@ -109,7 +109,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#msgid_contains_newlines' do it 'is true when the msgid is an array' do data = { msgid: %w(hello world) } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.msgid_has_multiple_lines?).to be_truthy end @@ -118,7 +118,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 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(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.plural_id_has_multiple_lines?).to be_truthy end @@ -127,7 +127,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#translations_contain_newlines' do it 'is true when the msgid is an array' do data = { msgstr: %w(hello world) } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.translations_have_multiple_lines?).to be_truthy end @@ -135,7 +135,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#contains_unescaped_chars' do let(:data) { { msgid: '' } } - let(:entry) { described_class.new(data, 2) } + let(:entry) { described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) } it 'is true when the msgid is an array' do string = '「100%確定」' @@ -177,7 +177,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#msgid_contains_unescaped_chars' do it 'is true when the msgid contains a `%`' do data = { msgid: '「100%確定」' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to receive(:contains_unescaped_chars?).and_call_original expect(entry.msgid_contains_unescaped_chars?).to be_truthy @@ -187,7 +187,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 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(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to receive(:contains_unescaped_chars?).and_call_original expect(entry.plural_id_contains_unescaped_chars?).to be_truthy @@ -197,10 +197,144 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#translations_contain_unescaped_chars' do it 'is true when the translation contains a `%`' do data = { msgstr: '「100%確定」' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) 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, 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 + + 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 + end end |