test/tools/test_msgmerge.rb in gettext-2.3.9 vs test/tools/test_msgmerge.rb in gettext-3.0.0
- old
+ new
@@ -19,288 +19,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
require 'gettext/tools/msgmerge'
class TestToolsMsgMerge < Test::Unit::TestCase
- class TestPoData < self
- def setup
- @po_data = GetText::Tools::MsgMerge::PoData.new
- end
-
- def test_msgids
- @po_data["hello"] = "bonjour"
- @po_data["he"] = "il"
-
- assert_equal(["he", "hello"], @po_data.msgids.sort)
- end
-
- def test_each_msgids
- @po_data["hello"] = "bonjour"
- @po_data["he"] = "il"
- @po_data[""] = "header"
- @po_data[:last] = ""
-
- msgids = []
- @po_data.each_msgid do |msgid|
- msgids << msgid
- end
- assert_equal(["he", "hello"], msgids.sort)
- end
-
- class TestAddToExistingEntry < self
- def test_msgctxt
- msgctxt = "msgctxt"
- msgid = "msgid"
- original_string = "#{msgctxt}\004#{msgid}"
- msgstr = "msgstr"
-
- po_data = GetText::Tools::MsgMerge::PoData.new
- po_data[original_string] = msgstr
-
- assert_equal(msgctxt, po_data.po[msgctxt, msgid].msgctxt)
- end
-
- def test_msgid_plural
- msgid = "msgid"
- msgid_plural = "msgid_plural"
- original_string = "#{msgid}\000#{msgid_plural}"
- msgstr = "msgstr"
-
- po_data = GetText::Tools::MsgMerge::PoData.new
- po_data[msgid] = msgstr
- assert_equal(nil, po_data.po[msgid].msgid_plural)
-
- po_data[original_string] = msgstr
- assert_equal(msgid_plural, po_data.po[msgid].msgid_plural)
- end
-
- def test_msgctxt_and_msgid_plural
- msgctxt = "msgctxt"
- msgid = "msgid"
- msgid_plural = "msgid_plural"
- original_string = "#{msgctxt}\004#{msgid}\000#{msgid_plural}"
- msgstr = "msgstr"
-
- po_data = GetText::Tools::MsgMerge::PoData.new
- po_data[original_string] = msgstr
-
- assert_equal(msgctxt, po_data.po[msgctxt, msgid].msgctxt)
- assert_equal(msgid_plural, po_data.po[msgctxt, msgid].msgid_plural)
- end
- end
-
- class TestGeneratePo < self
- def test_comment_and_msgid_and_msgstr
- header_entry_comment = "# header entry comment."
- header_entry = "header\nentry\n"
- comment = "#: test.rb:10"
- msgid = "Hello"
- msgstr = "Salut"
-
- po = GetText::Tools::MsgMerge::PoData.new
- po.set_comment("", header_entry_comment)
- po[""] = header_entry
- po[msgid] = msgstr
- po.set_comment(msgid, comment)
-
- expected_header_entry = ""
- header_entry.each_line do |line|
- expected_header_entry << "\"#{line.chomp}\\n\"\n"
- end
- expected_header_entry = expected_header_entry.chomp
- expected_po = <<EOP
-#{header_entry_comment}
-msgid \"\"
-msgstr \"\"
-#{expected_header_entry}
-
-#{comment}
-msgid \"#{msgid}\"
-msgstr \"#{msgstr}\"
-EOP
- assert_equal(expected_po, po.generate_po)
- end
-
- def test_obsolete_comment
- obsolete_comment =<<EOC
-# test.rb:10
-msgid \"Hello\"
-msgstr \"Salut\"
-EOC
- header_entry_comment = "# header entry comment."
- header_entry = "header entry"
-
- po = GetText::Tools::MsgMerge::PoData.new
- po.set_comment("", header_entry_comment)
- po[""] = header_entry
- po.set_comment(:last, obsolete_comment)
-
- expected_obsolete_comment = <<-EOC
-# test.rb:10
-#~ msgid "Hello"
-#~ msgstr "Salut"
-EOC
- expected_obsolete_comment = expected_obsolete_comment.chomp
-
- expected_po = <<EOP
-#{header_entry_comment}
-msgid \"\"
-msgstr \"#{header_entry}\"
-
-#{expected_obsolete_comment}
-EOP
- assert_equal(expected_po, po.generate_po)
- end
-
- def test_msgid_plural_and_empty_msgstr
- msgid = "Singular message\000Plural message"
-
- @po_data[""] = "Plural-Forms: nplurals=2; plural=n != 1;\\n"
- @po_data[msgid] = "\000"
- @po_data.set_comment(msgid, "# plural message")
- actual_po = @po_data.generate_po_entry(msgid)
- expected_po = <<'EOE'
-# plural message
-msgid "Singular message"
-msgid_plural "Plural message"
-msgstr[0] ""
-msgstr[1] ""
-EOE
- assert_equal(expected_po, actual_po)
- end
- end
-
- class TestGeneratePOEntry < self
- def test_msgid_plural
- msgid = "Singular message\000Plural message"
-
- @po_data[msgid] = "Singular translation\000Plural translation"
- @po_data.set_comment(msgid, "# plural message")
- actual_po = @po_data.generate_po_entry(msgid)
- expected_po = <<'EOE'
-# plural message
-msgid "Singular message"
-msgid_plural "Plural message"
-msgstr[0] "Singular translation"
-msgstr[1] "Plural translation"
-EOE
- assert_equal(expected_po, actual_po)
- end
-
- def test_msgctxt
- msg_id = "Context\004Translation"
- @po_data[msg_id] = "Translated"
- @po_data.set_comment(msg_id, "# no comment")
-
- entry = @po_data.generate_po_entry(msg_id)
- assert_equal(<<-'EOE', entry)
-# no comment
-msgctxt "Context"
-msgid "Translation"
-msgstr "Translated"
-EOE
- end
- end
- end
-
- class TestSplitMsgid < self
- def test_existed_msgctxt_and_msgid_plural
- msgctxt = "msgctxt"
- msgid = "msgid"
- msgid_plural = "msgid_plural"
-
- assert_equal([msgctxt, msgid, msgid_plural],
- split_msgid("#{msgctxt}\004#{msgid}\000#{msgid_plural}"))
- end
-
- def test_existed_msgctxt_only
- msgctxt = "msgctxt"
- msgid = "msgid"
-
- assert_equal([msgctxt, msgid, nil],
- split_msgid("#{msgctxt}\004#{msgid}"))
- end
-
- def test_existed_msgid_plural_only
- msgid = "msgid"
- msgid_plural = "msgid_plural"
-
- assert_equal([nil, msgid, msgid_plural],
- split_msgid("#{msgid}\000#{msgid_plural}"))
- end
-
- def test_not_existed
- msgid = "msgid"
-
- assert_equal([nil, msgid, nil], split_msgid(msgid))
- end
-
- def test_empty_msgid
- msgid = ""
-
- assert_equal([nil, msgid, nil], split_msgid(msgid))
- end
-
- def test_last_symbol_msgid
- msgid = :last
-
- assert_equal([nil, msgid, nil], split_msgid(msgid))
- end
-
- private
- def split_msgid(msgid)
- po_data = GetText::Tools::MsgMerge::PoData.new
- po_data.send(:split_msgid, msgid)
- end
- end
-
- class TestParseComment < self
- def setup
- @po_data = GetText::Tools::MsgMerge::PoData.new
- @entry = @po_data.send(:generate_entry, "test")
- end
-
- def test_translator_comment
- comment = "# translator comment"
- parsed_comment = parse_comment(comment).translator_comment
- assert_equal("translator comment\n", parsed_comment)
- end
-
- def test_extracted_comment
- comment = "#. extracted comment"
- parsed_comment = parse_comment(comment).extracted_comment
- assert_equal("extracted comment\n", parsed_comment)
- end
-
- def test_references
- comment = "#: reference.rb:10"
- parsed_comment = parse_comment(comment).references
- assert_equal(["reference.rb:10"], parsed_comment)
- end
-
- def test_flag
- comment = "#, fuzzy"
- parsed_comment = parse_comment(comment).flag
- assert_equal("fuzzy\n", parsed_comment)
- end
-
- def test_previous
- comment = "#| msgid the previous msgid"
- parsed_comment = parse_comment(comment).previous
- assert_equal("msgid the previous msgid\n", parsed_comment)
- end
-
- private
- def parse_comment(comment)
- @po_data.send(:parse_comment, comment, @entry)
- end
- end
-
class TestMerger < self
def setup
@merger = GetText::Tools::MsgMerge::Merger.new
- @po_data = GetText::Tools::MsgMerge::PoData.new
@po = GetText::PO.new
@pot = GetText::PO.new
end
def test_add_entry
@@ -519,11 +244,11 @@
private
def generate_entry(options)
msgctxt = options[:msgctxt]
msgid_plural = options[:msgid_plural]
- type = @po_data.send(:detect_entry_type, msgctxt, msgid_plural)
+ type = detect_entry_type(msgctxt, msgid_plural)
entry = GetText::POEntry.new(type)
entry.translator_comment = options[:translator_comment]
entry.extracted_comment = options[:extracted_comment]
entry.references = options[:references] || []
@@ -533,9 +258,25 @@
entry.msgid = options[:msgid]
entry.msgid_plural = msgid_plural
entry.msgstr = options[:msgstr]
entry.comment = options[:comment]
entry
+ end
+
+ def detect_entry_type(msgctxt, msgid_plural)
+ if msgctxt.nil?
+ if msgid_plural.nil?
+ :normal
+ else
+ :plural
+ end
+ else
+ if msgid_plural.nil?
+ :msgctxt
+ else
+ :msgctxt_plural
+ end
+ end
end
end
class TestMerge < self
include GetTextTestUtils