diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-08-25 16:23:51 +0300 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2017-08-31 22:13:01 +0300 |
commit | c6d969949ef98f1b4aebf38ca7f3ed1e59791d48 (patch) | |
tree | 88624cb8af3e4deb397ddb634b6d10a0579b79bc /spec | |
parent | cdaf1072daecd628a89f019b701bc0a2fa27c20e (diff) |
Validate the number of plurals in an entry
Diffstat (limited to 'spec')
-rw-r--r-- | spec/fixtures/missing_plurals.po | 22 | ||||
-rw-r--r-- | spec/fixtures/no_plurals.po | 24 | ||||
-rw-r--r-- | spec/lib/gitlab/i18n/po_entry_spec.rb | 67 | ||||
-rw-r--r-- | spec/lib/gitlab/i18n/po_linter_spec.rb | 45 |
4 files changed, 128 insertions, 30 deletions
diff --git a/spec/fixtures/missing_plurals.po b/spec/fixtures/missing_plurals.po new file mode 100644 index 00000000000..09ca0c82718 --- /dev/null +++ b/spec/fixtures/missing_plurals.po @@ -0,0 +1,22 @@ +# Spanish translations for gitlab package. +# Copyright (C) 2017 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the gitlab package. +# FIRST AUTHOR <EMAIL@ADDRESS>, 2017. +# +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 <bob@gitlab.com>\n" +"X-Generator: Poedit 2.0.2\n" + +msgid "%d commit" +msgid_plural "%d commits" +msgstr[0] "%d cambio" diff --git a/spec/fixtures/no_plurals.po b/spec/fixtures/no_plurals.po deleted file mode 100644 index 1bfc4ecbb04..00000000000 --- a/spec/fixtures/no_plurals.po +++ /dev/null @@ -1,24 +0,0 @@ -# Arthur Charron <arthur.charron@hotmail.fr>, 2017. #zanata -# Huang Tao <htve@outlook.com>, 2017. #zanata -# Kohei Ota <inductor@kela.jp>, 2017. #zanata -# Taisuke Inoue <taisuke.inoue.jp@gmail.com>, 2017. #zanata -# Takuya Noguchi <takninnovationresearch@gmail.com>, 2017. #zanata -# YANO Tethurou <tetuyano+zana@gmail.com>, 2017. #zanata -msgid "" -msgstr "" -"Project-Id-Version: gitlab 1.0.0\n" -"Report-Msgid-Bugs-To: \n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=UTF-8\n" -"Content-Transfer-Encoding: 8bit\n" -"PO-Revision-Date: 2017-08-06 11:23-0400\n" -"Last-Translator: Taisuke Inoue <taisuke.inoue.jp@gmail.com>\n" -"Language-Team: Japanese \"Language-Team: Russian (https://translate.zanata.org/" -"project/view/GitLab)\n" -"Language: ja\n" -"X-Generator: Zanata 3.9.6\n" -"Plural-Forms: nplurals=1; plural=0\n" - -msgid "%d commit" -msgid_plural "%d commits" -msgstr[0] "%d個のコミット" 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 <bob@gitlab.com>\\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' }) |