From c6d969949ef98f1b4aebf38ca7f3ed1e59791d48 Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Fri, 25 Aug 2017 15:23:51 +0200 Subject: Validate the number of plurals in an entry --- spec/lib/gitlab/i18n/po_entry_spec.rb | 67 ++++++++++++++++++++++++++++++++++ spec/lib/gitlab/i18n/po_linter_spec.rb | 45 ++++++++++++++++++++--- 2 files changed, 106 insertions(+), 6 deletions(-) (limited to 'spec/lib/gitlab/i18n') diff --git a/spec/lib/gitlab/i18n/po_entry_spec.rb b/spec/lib/gitlab/i18n/po_entry_spec.rb index 0317caedbff..e671f3c17a1 100644 --- a/spec/lib/gitlab/i18n/po_entry_spec.rb +++ b/spec/lib/gitlab/i18n/po_entry_spec.rb @@ -68,4 +68,71 @@ describe Gitlab::I18n::PoEntry do expect(entry.plural_translations).to eq(['Bonjour mondes', 'Bonjour tous les mondes']) end end + + describe '#expected_plurals' do + it 'returns nil when the entry is an actual translation' do + data = { msgid: 'Hello world', msgstr: 'Bonjour monde' } + entry = described_class.new(data) + + expect(entry.expected_plurals).to be_nil + end + + it 'returns the number of plurals' do + data = { + msgid: "", + msgstr: [ + "", + "Project-Id-Version: gitlab 1.0.0\\n", + "Report-Msgid-Bugs-To: \\n", + "PO-Revision-Date: 2017-07-13 12:10-0500\\n", + "Language-Team: Spanish\\n", + "Language: es\\n", + "MIME-Version: 1.0\\n", + "Content-Type: text/plain; charset=UTF-8\\n", + "Content-Transfer-Encoding: 8bit\\n", + "Plural-Forms: nplurals=2; plural=n != 1;\\n", + "Last-Translator: Bob Van Landuyt \\n", + "X-Generator: Poedit 2.0.2\\n" + ] + } + entry = described_class.new(data) + + expect(entry.expected_plurals).to eq(2) + end + end + + describe '#has_singular?' do + it 'has a singular when the translation is not pluralized' do + data = { + msgid: 'hello world', + msgstr: 'hello' + } + entry = described_class.new(data) + + expect(entry).to have_singular + 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(data) + + expect(entry).to have_singular + 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(data) + + expect(entry).not_to have_singular + end + end end diff --git a/spec/lib/gitlab/i18n/po_linter_spec.rb b/spec/lib/gitlab/i18n/po_linter_spec.rb index 2e155511076..307ea8b2640 100644 --- a/spec/lib/gitlab/i18n/po_linter_spec.rb +++ b/spec/lib/gitlab/i18n/po_linter_spec.rb @@ -28,21 +28,21 @@ describe Gitlab::I18n::PoLinter do it 'has an error for a normal string' do message_id = "You are going to remove %{group_name}.\\nRemoved groups CANNOT be restored!\\nAre you ABSOLUTELY sure?" - expected_message = "<#{message_id}> is defined over multiple lines, this breaks some tooling." + expected_message = "is defined over multiple lines, this breaks some tooling." expect(errors[message_id]).to include(expected_message) end it 'has an error when a translation is defined over multiple lines' do message_id = "You are going to remove %{group_name}.\\nRemoved groups CANNOT be restored!\\nAre you ABSOLUTELY sure?" - expected_message = "<#{message_id}> has translations defined over multiple lines, this breaks some tooling." + expected_message = "has translations defined over multiple lines, this breaks some tooling." expect(errors[message_id]).to include(expected_message) end it 'raises an error when a plural translation is defined over multiple lines' do message_id = 'With plural' - expected_message = "<#{message_id}> has translations defined over multiple lines, this breaks some tooling." + expected_message = "has translations defined over multiple lines, this breaks some tooling." expect(errors[message_id]).to include(expected_message) end @@ -81,10 +81,10 @@ describe Gitlab::I18n::PoLinter do end context 'with missing plurals' do - let(:po_path) { 'spec/fixtures/no_plurals.po' } + let(:po_path) { 'spec/fixtures/missing_plurals.po' } it 'has no errors' do - is_expected.to be_empty + is_expected.not_to be_empty end end @@ -151,13 +151,33 @@ describe Gitlab::I18n::PoLinter do expect(linter).to receive(:validate_flags).with([], fake_entry) expect(linter).to receive(:validate_variables).with([], fake_entry) expect(linter).to receive(:validate_newlines).with([], fake_entry) + expect(linter).to receive(:validate_number_of_plurals).with([], fake_entry) linter.validate_entry(fake_entry) end end + describe '#validate_number_of_plurals' do + it 'validates when there are an incorrect number of translations' do + fake_metadata = double + allow(fake_metadata).to receive(:expected_plurals).and_return(2) + allow(linter).to receive(:metadata).and_return(fake_metadata) + + fake_entry = Gitlab::I18n::PoEntry.new( + msgid: 'the singular', + msgid_plural: 'the plural', + 'msgstr[0]' => 'the singular' + ) + errors = [] + + linter.validate_number_of_plurals(errors, fake_entry) + + expect(errors).to include('should have 2 translations') + end + end + describe '#validate_variables' do - it 'validates both signular and plural in a pluralized string' do + it 'validates both signular and plural in a pluralized string when the entry has a singular' do pluralized_entry = Gitlab::I18n::PoEntry.new({ msgid: 'Hello %{world}', msgid_plural: 'Hello all %{world}', @@ -173,6 +193,19 @@ describe Gitlab::I18n::PoLinter do linter.validate_variables([], pluralized_entry) end + it 'only validates plural when there is no separate singular' do + pluralized_entry = Gitlab::I18n::PoEntry.new({ + msgid: 'Hello %{world}', + msgid_plural: 'Hello all %{world}', + 'msgstr[0]' => 'Bonjour %{world}' + }) + + expect(linter).to receive(:validate_variables_in_message) + .with([], 'Hello all %{world}', 'Bonjour %{world}') + + linter.validate_variables([], pluralized_entry) + end + it 'validates the message variables' do entry = Gitlab::I18n::PoEntry.new({ msgid: 'Hello', msgstr: 'Bonjour' }) -- cgit v1.2.3