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
93 """check items that are only special instances of whitespce""" 94 # FIXME We can't roundtrip this yet because of some problems in the oo class handling 95 self.check_roundtrip('choose_chart_type.xhp', r' ') 96 self.check_roundtrip('choose_chart_type.xhp', '\xc2\xa0')
97
98 - def test_double_escapes(self):
99 oosource = r"helpcontent2 source\text\shared\01\02100001.xhp 0 help par_id3150670 35 0 en-US \\< 2002-02-02 02:02:02" 100 pofile = self.convert(oosource) 101 pounit = self.singleelement(pofile) 102 poelementsrc = str(pounit) 103 print poelementsrc 104 assert pounit.source == r"\<"
105
107 """checks that a helpcontent2 entry converts escapes properly to a po entry""" 108 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" 109 pofile = self.convert(oosource) 110 pounit = self.singleelement(pofile) 111 poelementsrc = str(pounit) 112 print poelementsrc 113 assert pounit.source == r'size *2 \langle x \rangle'
114
116 """tests the we have the correct url for reporting msgid bugs""" 117 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" 118 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") 119 pofile = self.convert(oosource) 120 assert pofile.units[0].isheader() 121 assert pofile.parseheader()["Report-Msgid-Bugs-To"] == bug_url
122
123 - def test_x_comment_inclusion(self):
124 """test that we can merge x-comment language entries into comment sections of the PO file""" 125 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" 126 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" 127 # Real comment 128 comment = "Comment" 129 commentsource = en_USsource + '\n' + xcommentsource % (comment, comment, comment) 130 pofile = self.convert(commentsource) 131 if isinstance(pofile, poheader): 132 units = pofile.units[1:] 133 else: 134 units = pofile.units 135 textunit = units[0] 136 assert textunit.source == "Text" 137 assert comment in textunit.getnotes("developer") 138 quickhelpunit = units[1] 139 assert quickhelpunit.source == "Quickhelp" 140 assert comment in quickhelpunit.getnotes("developer") 141 titleunit = units[2] 142 assert titleunit.source == "Title" 143 assert comment in titleunit.getnotes("developer") 144 # Whitespace and blank 145 for comment in (" ", ""): 146 commentsource = en_USsource + '\n' + xcommentsource % (comment, comment, comment) 147 pofile = self.convert(commentsource) 148 if isinstance(pofile, poheader): 149 units = pofile.units[1:] 150 else: 151 units = pofile.units 152 textunit = units[0] 153 assert textunit.source == "Text" 154 assert textunit.getnotes("developer") == "" 155 quickhelpunit = units[1] 156 assert quickhelpunit.source == "Quickhelp" 157 assert quickhelpunit.getnotes("developer") == "" 158 titleunit = units[2] 159 assert titleunit.source == "Title" 160 assert titleunit.getnotes("developer") == ""
161
162 -class TestOO2POCommand(test_convert.TestConvertCommand, TestOO2PO):
163 """Tests running actual oo2po commands on files""" 164 convertmodule = oo2po 165
166 - def test_help(self):
167 """tests getting help""" 168 options = test_convert.TestConvertCommand.test_help(self) 169 options = self.help_check(options, "--source-language=LANG") 170 options = self.help_check(options, "--language=LANG") 171 options = self.help_check(options, "-P, --pot") 172 options = self.help_check(options, "--duplicates=DUPLICATESTYLE") 173 options = self.help_check(options, "--multifile=MULTIFILESTYLE") 174 options = self.help_check(options, "--nonrecursiveinput", last=True)
175
176 - def test_preserve_filename(self):
177 """Ensures that the filename is preserved.""" 178 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 179 self.create_testfile("snippet.sdf", oosource) 180 oofile = oo.oofile(self.open_testfile("snippet.sdf")) 181 assert oofile.filename.endswith("snippet.sdf") 182 oofile.parse(oosource) 183 assert oofile.filename.endswith("snippet.sdf")
184
185 - def test_simple_pot(self):
186 """tests the simplest possible conversion to a pot file""" 187 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 188 self.create_testfile("simple.oo", oosource) 189 self.run_command("simple.oo", "simple.pot", pot=True, nonrecursiveinput=True) 190 pofile = self.target_filetype(self.open_testfile("simple.pot")) 191 poelement = self.singleelement(pofile) 192 assert poelement.source == "Character" 193 assert poelement.target == ""
194
195 - def test_simple_po(self):
196 """tests the simplest possible conversion to a po file""" 197 oosource1 = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 198 oosource2 = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 ku Karakter 20050924 09:13:58' 199 self.create_testfile("simple.oo", oosource1 + "\n" + oosource2) 200 self.run_command("simple.oo", "simple.po", lang="ku", nonrecursiveinput=True) 201 pofile = self.target_filetype(self.open_testfile("simple.po")) 202 poelement = self.singleelement(pofile) 203 assert poelement.source == "Character" 204 assert poelement.target == "Karakter"
205
207 """tests the --multifile=onefile option and make sure it doesn't produce a directory""" 208 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Character 20050924 09:13:58' 209 self.create_testfile("simple.oo", oosource) 210 self.run_command("simple.oo", "simple.pot", pot=True, multifile="onefile") 211 assert os.path.isfile(self.get_testfilename("simple.pot"))
212
213 - def test_remove_duplicates(self):
214 """test that removing of duplicates works correctly (bug 171)""" 215 oosource = r''' 216 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 217 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 218 sd source\ui\animations\CustomAnimationCreateDialog.src 0 checkbox RID_TP_CUSTOMANIMATION_ENTRANCE CBX_PREVIEW 143 en-US Automatic preview 20060725 03:26:42 219 sd source\ui\animations\CustomAnimationCreateDialog.src 0 checkbox RID_TP_CUSTOMANIMATION_ENTRANCE CBX_PREVIEW 143 fr Aperçu automatique 20060725 03:26:42 220 sd source\ui\animations\CustomAnimationSchemesPane.src 0 checkbox DLG_CUSTOMANIMATION_SCHEMES_PANE 4 0 en-US Automatic preview 20060725 03:26:42 221 sd source\ui\animations\CustomAnimationSchemesPane.src 0 checkbox DLG_CUSTOMANIMATION_SCHEMES_PANE 4 0 fr Aperçu automatique 20060725 03:26:42 222 ''' 223 self.create_testfile("simple.oo", oosource) 224 self.run_command("simple.oo", "simple.po", language="fr", multifile="onefile", error="traceback", duplicates="merge") 225 pofile = self.target_filetype(self.open_testfile("simple.po")) 226 assert len(pofile.units) == 2 227 assert pofile.units[1].target == u"Aperçu automatique"
228