Package translate :: Package convert :: Module test_oo2po
[hide private]
[frames] | no frames]

Source Code for Module translate.convert.test_oo2po

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  from translate.convert import oo2po 
  5  from translate.convert import po2oo 
  6  from translate.convert import test_convert 
  7  from translate.misc import wStringIO 
  8  from translate.storage import po 
  9  from translate.storage.poheader import poheader 
 10  from translate.storage import oo 
 11  import os 
 12   
13 -class TestOO2PO:
14 target_filetype = po.pofile 15 conversion_module = oo2po 16 conversion_class = oo2po.oo2po 17
18 - def convert(self, oosource, sourcelanguage='en-US', targetlanguage='af-ZA'):
19 """helper that converts oo source to po source without requiring files""" 20 inputoo = oo.oofile(oosource) 21 convertor = self.conversion_class(sourcelanguage, targetlanguage) 22 outputpo = convertor.convertstore(inputoo) 23 return outputpo
24
25 - def singleelement(self, pofile):
26 """checks that the pofile contains a single non-header element, and returns it""" 27 if isinstance(pofile, poheader): 28 assert len(pofile.units) == 2 29 assert pofile.units[0].isheader() 30 return pofile.units[1] 31 else: 32 assert len(pofile.units) == 1 33 return pofile.units[0]
34
35 - def roundtripstring(self, filename, entitystring):
36 """Convert the supplied string as part of an OpenOffice.org GSI file to po and back. 37 38 Return the string once it has been through all the conversions.""" 39 40 ootemplate = r'helpcontent2 %s 0 help par_id3150670 35 0 en-US %s 2002-02-02 02:02:02' 41 42 oosource = ootemplate % (filename, entitystring) 43 ooinputfile = wStringIO.StringIO(oosource) 44 ootemplatefile = wStringIO.StringIO(oosource) 45 pooutputfile = wStringIO.StringIO() 46 47 self.conversion_module.convertoo(ooinputfile, pooutputfile, ootemplatefile, targetlanguage='en-US') 48 posource = pooutputfile.getvalue() 49 50 poinputfile = wStringIO.StringIO(posource) 51 ootemplatefile = wStringIO.StringIO(oosource) 52 oooutputfile = wStringIO.StringIO() 53 po2oo.convertoo(poinputfile, oooutputfile, ootemplatefile, targetlanguage="en-US") 54 ooresult = oooutputfile.getvalue() 55 print "original oo:\n", oosource, "po version:\n", posource, "output oo:\n", ooresult 56 return ooresult.split('\t')[10]
57
58 - def check_roundtrip(self, filename, text):
59 """Checks that the text converted to po and back is the same as the original.""" 60 assert self.roundtripstring(filename, text) == text
61
62 - def test_simpleentity(self):
63 """checks that a simple oo entry converts properly to a po entry""" 64 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 65 pofile = self.convert(oosource) 66 pounit = self.singleelement(pofile) 67 assert pounit.source == "Character" 68 assert pounit.target == ""
69
70 - def test_escapes(self):
71 """checks that a simple oo entry converts escapes properly to a po entry""" 72 oosource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 en-US Newline \n Newline Tab \t Tab CR \r CR 20050924 09:13:58" 73 pofile = self.convert(oosource) 74 pounit = self.singleelement(pofile) 75 poelementsrc = str(pounit) 76 print poelementsrc 77 assert "Newline \n Newline" in pounit.source 78 assert "Tab \t Tab" in pounit.source 79 assert "CR \r CR" in pounit.source
80
81 - def test_roundtrip_escape(self):
82 self.check_roundtrip('strings.src', r'The given command is not a SELECT statement.\nOnly queries are allowed.') 83 self.check_roundtrip('source\ui\dlg\AutoControls_tmpl.hrc', r';\t59\t,\t44\t:\t58\t{Tab}\t9\t{Space}\t32') 84 self.check_roundtrip('inc_openoffice\windows\msi_languages\Nsis.ulf', r'The installation files must be unpacked and copied to your hard disk in preparation for the installation. After that, the %PRODUCTNAME installation will start automatically.\r\n\r\nClick \'Next\' to continue.') 85 self.check_roundtrip('file.xhp', r'\<asdf\>') 86 self.check_roundtrip('file.xhp', r'\<asdf prop=\"value\"\>') 87 self.check_roundtrip('file.xhp', r'\<asdf prop=\"value\"\>marked up text\</asdf\>') 88 self.check_roundtrip('file.xhp', r'\<asdf prop=\"value>>\"\>') 89 self.check_roundtrip('file.xhp', r'''\<asdf prop=\"value>>\"\>'Next'>> or "<<Previous"\</asdf\>''') 90 self.check_roundtrip('address_auto.xhp', r'''example, \<item type=\"literal\"\>'Harry\\'s Bar'.\</item\>''')
91
92 - def test_double_escapes(self):
93 oosource = r"helpcontent2 source\text\shared\01\02100001.xhp 0 help par_id3150670 35 0 en-US \\< 2002-02-02 02:02:02" 94 pofile = self.convert(oosource) 95 pounit = self.singleelement(pofile) 96 poelementsrc = str(pounit) 97 print poelementsrc 98 assert pounit.source == r"\<"
99
101 """checks that a helpcontent2 entry converts escapes properly to a po entry""" 102 oosource = r"helpcontent2 source\text\smath\guide\parentheses.xhp 0 help par_id3150344 4 0 en-US size *2 \\langle x \\rangle 2002-02-02 02:02:02" 103 pofile = self.convert(oosource) 104 pounit = self.singleelement(pofile) 105 poelementsrc = str(pounit) 106 print poelementsrc 107 assert pounit.source == r'size *2 \langle x \rangle'
108
110 """tests the we have the correct url for reporting msgid bugs""" 111 oosource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 en-US Newline \n Newline Tab \t Tab CR \r CR 20050924 09:13:58" 112 bug_url = '''http://qa.openoffice.org/issues/enter_bug.cgi''' + ('''?subcomponent=ui&comment=&short_desc=Localization issue in file: &component=l10n&form_name=enter_issue''').replace(" ", "%20").replace(":", "%3A") 113 pofile = self.convert(oosource) 114 assert pofile.units[0].isheader() 115 assert pofile.parseheader()["Report-Msgid-Bugs-To"] == bug_url
116
117 - def test_x_comment_inclusion(self):
118 """test that we can merge x-comment language entries into comment sections of the PO file""" 119 en_USsource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 en-US Text Quickhelp Title 20050924 09:13:58" 120 xcommentsource = r"wizards source\formwizard\dbwizres.src 0 string RID_DB_FORM_WIZARD_START + 19 0 x-comment %s %s %s 20050924 09:13:58" 121 # Real comment 122 comment = "Comment" 123 commentsource = en_USsource + '\n' + xcommentsource % (comment, comment, comment) 124 pofile = self.convert(commentsource) 125 if isinstance(pofile, poheader): 126 units = pofile.units[1:] 127 else: 128 units = pofile.units 129 textunit = units[0] 130 assert textunit.source == "Text" 131 assert comment in textunit.getnotes("developer") 132 quickhelpunit = units[1] 133 assert quickhelpunit.source == "Quickhelp" 134 assert comment in quickhelpunit.getnotes("developer") 135 titleunit = units[2] 136 assert titleunit.source == "Title" 137 assert comment in titleunit.getnotes("developer") 138 # Whitespace and blank 139 for comment in (" ", ""): 140 commentsource = en_USsource + '\n' + xcommentsource % (comment, comment, comment) 141 pofile = self.convert(commentsource) 142 if isinstance(pofile, poheader): 143 units = pofile.units[1:] 144 else: 145 units = pofile.units 146 textunit = units[0] 147 assert textunit.source == "Text" 148 assert textunit.getnotes("developer") == "" 149 quickhelpunit = units[1] 150 assert quickhelpunit.source == "Quickhelp" 151 assert quickhelpunit.getnotes("developer") == "" 152 titleunit = units[2] 153 assert titleunit.source == "Title" 154 assert titleunit.getnotes("developer") == ""
155
156 -class TestOO2POCommand(test_convert.TestConvertCommand, TestOO2PO):
157 """Tests running actual oo2po commands on files""" 158 convertmodule = oo2po 159
160 - def test_help(self):
161 """tests getting help""" 162 options = test_convert.TestConvertCommand.test_help(self) 163 options = self.help_check(options, "--source-language=LANG") 164 options = self.help_check(options, "--language=LANG") 165 options = self.help_check(options, "-P, --pot") 166 options = self.help_check(options, "--duplicates=DUPLICATESTYLE") 167 options = self.help_check(options, "--multifile=MULTIFILESTYLE") 168 options = self.help_check(options, "--nonrecursiveinput", last=True)
169
170 - def test_preserve_filename(self):
171 """Ensures that the filename is preserved.""" 172 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 173 self.create_testfile("snippet.sdf", oosource) 174 oofile = oo.oofile(self.open_testfile("snippet.sdf")) 175 assert oofile.filename.endswith("snippet.sdf") 176 oofile.parse(oosource) 177 assert oofile.filename.endswith("snippet.sdf")
178
179 - def test_simple_pot(self):
180 """tests the simplest possible conversion to a pot file""" 181 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 182 self.create_testfile("simple.oo", oosource) 183 self.run_command("simple.oo", "simple.pot", pot=True, nonrecursiveinput=True) 184 pofile = self.target_filetype(self.open_testfile("simple.pot")) 185 poelement = self.singleelement(pofile) 186 assert poelement.source == "Character" 187 assert poelement.target == ""
188
189 - def test_simple_po(self):
190 """tests the simplest possible conversion to a po file""" 191 oosource1 = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 192 oosource2 = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 ku Karakter 20050924 09:13:58' 193 self.create_testfile("simple.oo", oosource1 + "\n" + oosource2) 194 self.run_command("simple.oo", "simple.po", lang="ku", nonrecursiveinput=True) 195 pofile = self.target_filetype(self.open_testfile("simple.po")) 196 poelement = self.singleelement(pofile) 197 assert poelement.source == "Character" 198 assert poelement.target == "Karakter"
199
201 """tests the --multifile=onefile option and make sure it doesn't produce a directory""" 202 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 203 self.create_testfile("simple.oo", oosource) 204 self.run_command("simple.oo", "simple.pot", pot=True, multifile="onefile") 205 assert os.path.isfile(self.get_testfilename("simple.pot"))
206
207 - def test_remove_duplicates(self):
208 """test that removing of duplicates works correctly (bug 171)""" 209 oosource = r''' 210 sd source\ui\animations\SlideTransitionPane.src 0 checkbox DLG_SLIDE_TRANSITION_PANE CB_AUTO_PREVIEW HID_SD_SLIDETRANSITIONPANE_CB_AUTO_PREVIEW 1 en-US Automatic preview 20060725 03:26:42 211 sd source\ui\animations\AnimationSchemesPane.src 0 checkbox DLG_ANIMATION_SCHEMES_PANE CB_AUTO_PREVIEW HID_SD_ANIMATIONSCHEMESPANE_CB_AUTO_PREVIEW 1 en-US Automatic preview 20060725 03:26:42 212 sd source\ui\animations\CustomAnimationCreateDialog.src 0 checkbox RID_TP_CUSTOMANIMATION_ENTRANCE CBX_PREVIEW 143 en-US Automatic preview 20060725 03:26:42 213 sd source\ui\animations\CustomAnimationCreateDialog.src 0 checkbox RID_TP_CUSTOMANIMATION_ENTRANCE CBX_PREVIEW 143 fr Aperçu automatique 20060725 03:26:42 214 sd source\ui\animations\CustomAnimationSchemesPane.src 0 checkbox DLG_CUSTOMANIMATION_SCHEMES_PANE 4 0 en-US Automatic preview 20060725 03:26:42 215 sd source\ui\animations\CustomAnimationSchemesPane.src 0 checkbox DLG_CUSTOMANIMATION_SCHEMES_PANE 4 0 fr Aperçu automatique 20060725 03:26:42 216 ''' 217 self.create_testfile("simple.oo", oosource) 218 self.run_command("simple.oo", "simple.po", language="fr", multifile="onefile", error="traceback", duplicates="merge") 219 pofile = self.target_filetype(self.open_testfile("simple.po")) 220 assert len(pofile.units) == 2 221 assert pofile.units[1].target == u"Aperçu automatique"
222