#!/usr/bin/env python from translate.convert import dtd2po from translate.convert import test_convert from translate.misc import wStringIO from translate.storage import po from translate.storage import dtd class TestDTD2PO: def dtd2po(self, dtdsource, dtdtemplate=None): """helper that converts dtd source to po source without requiring files""" inputfile = wStringIO.StringIO(dtdsource) inputdtd = dtd.dtdfile(inputfile) convertor = dtd2po.dtd2po() if not dtdtemplate: outputpo = convertor.convertfile(inputdtd) else: templatefile = wStringIO.StringIO(dtdtemplate) templatedtd = dtd.dtdfile(templatefile) outputpo = convertor.mergefiles(templatedtd, inputdtd) return outputpo def convertdtd(self, dtdsource): """call the convertdtd, return the outputfile""" inputfile = wStringIO.StringIO(dtdsource) outputfile = wStringIO.StringIO() templatefile = None assert dtd2po.convertdtd(inputfile, outputfile, templatefile) return outputfile.getvalue() def singleelement(self, pofile): """checks that the pofile contains a single non-header element, and returns it""" assert len(pofile.units) == 2 assert pofile.units[0].isheader() return pofile.units[1] def countelements(self, pofile): """returns the number of non-header items""" if pofile.units[0].isheader(): return len(pofile.units) - 1 else: return len(pofile.units) def test_simpleentity(self): """checks that a simple dtd entity definition converts properly to a po entry""" dtdsource = '\n' pofile = self.dtd2po(dtdsource) pounit = self.singleelement(pofile) assert po.unquotefrompo(pounit.msgid) == "bananas for sale" assert po.unquotefrompo(pounit.msgstr) == "" # Now with a template language dtdtemplate = '\n' dtdtranslated = '\n' pofile = self.dtd2po(dtdtranslated, dtdtemplate) pounit = self.singleelement(pofile) assert po.unquotefrompo(pounit.msgid) == "bananas for sale" assert po.unquotefrompo(pounit.msgstr) == "piesangs te koop" def test_convertdtd(self): """checks that the convertdtd function is working""" dtdsource = '\n' posource = self.convertdtd(dtdsource) pofile = po.pofile(wStringIO.StringIO(posource)) unit = self.singleelement(pofile) assert po.unquotefrompo(unit.msgid) == "Save As..." assert po.unquotefrompo(unit.msgstr) == "" def test_apos(self): """apostrophe should not break a single-quoted entity definition, bug 69""" dtdsource = "\n" pofile = self.dtd2po(dtdsource) pounit = self.singleelement(pofile) assert po.unquotefrompo(pounit.msgid) == "bananas ' for sale" def test_quotes(self): """quotes should be handled in a single-quoted entity definition""" dtdsource = """\n""" pofile = self.dtd2po(dtdsource) pounit = self.singleelement(pofile) print str(pounit) assert po.unquotefrompo(pounit.msgid) == '"Bananas" for sale' def test_emptyentity(self): """checks that empty entity definitions survive into po file, bug 15""" dtdsource = '\n' pofile = self.dtd2po(dtdsource) pounit = self.singleelement(pofile) assert "credit.translation" in str(pounit) def test_emptyentity_translated(self): """checks that if we translate an empty entity it makes it into the PO, bug 101""" dtdtemplate = '\n' dtdsource = '\n' pofile = self.dtd2po(dtdsource, dtdtemplate) unit = self.singleelement(pofile) print unit assert "credit.translation" in str(unit) assert po.unquotefrompo(unit.msgstr) == "Translators Names" def test_kdecomment_merge(self): """test that LOCALIZATION NOTES are added properly as KDE comments and merged with duplicate comments""" dtdtemplate = '\n' + \ '\n' dtdsource = dtdtemplate % ("note1.label", "note1.label") + dtdtemplate % ("note2.label", "note2.label") pofile = self.dtd2po(dtdsource) pofile.units = pofile.units[1:] posource = str(pofile) print posource assert posource.count('"_:') == 2 assert posource.count('\\n') == 2 def test_donttranslate_simple(self): """check that we handle DONT_TRANSLATE messages properly""" dtdsource = ''' ''' pofile = self.dtd2po(dtdsource) assert self.countelements(pofile) == 0 dtdsource = ''' ''' pofile = self.dtd2po(dtdsource) assert self.countelements(pofile) == 0 dtdsource = ''' ''' pofile = self.dtd2po(dtdsource) assert self.countelements(pofile) == 1 def test_donttranslate_label(self): """test strangeness when label entity is marked DONT_TRANSLATE and accesskey is not, bug 30""" dtdsource = '\n' + \ '\n\n' pofile = self.dtd2po(dtdsource) posource = str(pofile) # we need to decided what we're going to do here - see the comments in bug 30 # this tests the current implementation which is that the DONT_TRANSLATE string is removed, but the other remains assert 'editorCheck.label' not in posource assert 'editorCheck.accesskey' in posource def test_donttranslate_onlyentity(self): """if the entity is itself just another entity then it shouldn't appear in the output PO file""" dtdsource = ''' ''' pofile = self.dtd2po(dtdsource) assert self.countelements(pofile) == 0 def test_donttranslate_commentedout(self): """check that we don't process messages in : bug 102""" dtdsource = '''''' pofile = self.dtd2po(dtdsource) assert self.countelements(pofile) == 0 def test_spaces_at_start_of_dtd_lines(self): """test that pretty print spaces at the start of subsequent DTD element lines are removed from the PO file, bug 79""" # Space at the end of the line dtdsource = '\n' pofile = self.dtd2po(dtdsource) pounit = self.singleelement(pofile) # We still need to decide how we handle line line breaks in the DTD entities. It seems that we should actually # drop the line break but this has not been implemented yet. assert po.unquotefrompo(pounit.msgid, True) == "First line then \nnext lines." # No space at the end of the line dtdsource = '\n' pofile = self.dtd2po(dtdsource) unit = self.singleelement(pofile) assert po.unquotefrompo(unit.msgid, True) == "First line then \nnext lines." def test_accesskeys_folding(self): """test that we fold accesskeys into message strings""" dtdsource_template = '\n\n' lang_template = '\n\n' for label in ("label", "title"): for accesskey in ("accesskey", "accessKey", "akey"): pofile = self.dtd2po(dtdsource_template % (label, accesskey)) pounit = self.singleelement(pofile) assert pounit.source == "&Save As..." # Test with template (bug 155) pofile = self.dtd2po(lang_template % (label, accesskey), dtdsource_template % (label, accesskey)) pounit = self.singleelement(pofile) assert pounit.source == "&Save As..." assert pounit.target == "&Gcina ka..." def test_accesskeys_mismatch(self): """check that we can handle accesskeys that don't match and thus can't be folded into the .label entry""" dtdsource = '\n' + \ '\n' pofile = self.dtd2po(dtdsource) assert self.countelements(pofile) == 2 def test_carriage_return_in_multiline_dtd(self): """test that we create nice PO files when we find a \r\n in a multiline DTD element""" dtdsource = '\n' pofile = self.dtd2po(dtdsource) unit = self.singleelement(pofile) assert po.unquotefrompo(unit.msgid, True) == "First line then \nnext lines." def test_preserving_spaces(self): """test that we preserve space that appear at the start of the first line of a DTD entity""" # Space before first character dtdsource = '' pofile = self.dtd2po(dtdsource) unit = self.singleelement(pofile) assert po.unquotefrompo(unit.msgid) == " - " # Double line and spaces dtdsource = '' pofile = self.dtd2po(dtdsource) unit = self.singleelement(pofile) assert po.unquotefrompo(unit.msgid, True) == " - with a newline \nand more text" def test_escaping_newline_tabs(self): """test that we handle all kinds of newline permutations""" dtdsource = '\n' converter = dtd2po.dtd2po() thedtd = dtd.dtdunit() thedtd.parse(dtdsource) thepo = po.pounit() converter.convertstrings(thedtd, thepo) print thedtd print thepo.msgid # \n in a dtd should also appear as \n in the PO file assert po.unquotefrompo(thepo.msgid) == r"A hard coded newline.\nAnd tab\t and a \r carriage return." def test_abandoned_accelerator(self): """test that when a language DTD has an accelerator but the template DTD does not that we abandon the accelerator""" dtdtemplate = '\n' dtdlanguage = '\n\n' pofile = self.dtd2po(dtdlanguage, dtdtemplate) unit = self.singleelement(pofile) assert po.unquotefrompo(unit.msgid) == "Test" assert po.unquotefrompo(unit.msgstr) == "Toets" def test_unassociable_accelerator(self): """test to see that we can handle accelerator keys that cannot be associated correctly""" dtdsource ='\n' pofile = self.dtd2po(dtdsource) assert pofile.units[1].source == "Manage Certificates..." assert pofile.units[2].source == "M" pofile = self.dtd2po(dtdsource, dtdsource) assert pofile.units[1].target == "Manage Certificates..." assert pofile.units[2].target == "M" def test_changed_labels_and_accelerators(self): """test to ensure that when the template changes an entity name we can still manage the accelerators""" dtdtemplate = ''' ''' dtdlanguage = ''' ''' pofile = self.dtd2po(dtdlanguage, dtdtemplate) print pofile assert pofile.units[3].source == "Manage Certificates..." assert pofile.units[3].target == "ﺇﺩﺍﺭﺓ ﺎﻠﺸﻫﺍﺩﺎﺗ..." assert pofile.units[4].source == "M" assert pofile.units[4].target == "ﺩ" def wtest_accelerator_keys_not_in_sentence(self): """tests to ensure that we can manage accelerator keys that are not part of the transated sentence eg in Chinese""" dtdtemplate = ''' ''' dtdlanguage = ''' ''' pofile = self.dtd2po(dtdlanguage, dtdtemplate) print pofile assert pofile.units[1].target == "使用自動捲動(&Autoscrolling)" # We assume that accesskeys with no associated key should be done as follows "XXXX (&A)" # TODO - check that we can unfold this from PO -> DTD dtdlanguage = ''' ''' pofile = self.dtd2po(dtdlanguage, dtdtemplate) print pofile assert pofile.units[1].target == "使用自動捲動 (&A)" def test_exclude_entity_includes(self): """test that we don't turn an include into a translatable string""" dtdsource = '' pofile = self.dtd2po(dtdsource) assert self.countelements(pofile) == 0 def test_linewraps(self): """check that empty line wraps are not placed in po file""" dtdsource = '''Test me.
">''' pofile = self.dtd2po(dtdsource) pounit = self.singleelement(pofile) assert po.unquotefrompo(pounit.msgid, True) == "Test me.
" def test_merging_with_new_untranslated(self): """test that when we merge in new untranslated strings with existing translations we manage the encodings properly""" # This should probably be in test_po.py but was easier to do here dtdtemplate = '''\n\n''' dtdlanguage = '''\n''' pofile = self.dtd2po(dtdlanguage, dtdtemplate) print pofile assert pofile.units[1].source == "Unread" def test_merge_without_template(self): """test that we we manage the case where we merge and their is no template file""" # If we supply a template file we should fail if the template file does not exist or is blank. We should # not put the translation in as the source. dtdtemplate = '' dtdsource = '' pofile = self.dtd2po(dtdsource, dtdtemplate) print pofile assert self.countelements(pofile) == 0 class TestDTD2POCommand(test_convert.TestConvertCommand, TestDTD2PO): """Tests running actual dtd2po commands on files""" convertmodule = dtd2po defaultoptions = {"progress": "none"} def test_help(self): """tests getting help""" options = test_convert.TestConvertCommand.test_help(self) options = self.help_check(options, "-P, --pot") options = self.help_check(options, "-tTEMPLATE, --template=TEMPLATE") options = self.help_check(options, "--duplicates=DUPLICATESTYLE", last=True)