fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_po2oo.py
blob15fcb45a3aa156e99b2303f65c2b84b4f70665c4
1 #!/usr/bin/env python
3 from translate.convert import po2oo
4 from translate.convert import oo2po
5 from translate.convert import test_convert
6 from translate.misc import wStringIO
7 from translate.storage import po
8 import warnings
9 import os
11 class TestPO2OO:
12 def setup_method(self, method):
13 warnings.resetwarnings()
15 def teardown_method(self, method):
16 warnings.resetwarnings()
18 def convertoo(self, posource, ootemplate, language="en-US"):
19 """helper to exercise the command line function"""
20 inputfile = wStringIO.StringIO(posource)
21 outputfile = wStringIO.StringIO()
22 templatefile = wStringIO.StringIO(ootemplate)
23 assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage=language, timestamp=0)
24 return outputfile.getvalue()
26 def roundtripstring(self, entitystring):
27 oointro, oooutro = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US ', ' 2002-02-02 02:02:02' + '\r\n'
28 oosource = oointro + entitystring + oooutro
29 ooinputfile = wStringIO.StringIO(oosource)
30 ooinputfile2 = wStringIO.StringIO(oosource)
31 pooutputfile = wStringIO.StringIO()
32 oo2po.convertoo(ooinputfile, pooutputfile, ooinputfile2, targetlanguage='en-US')
33 posource = pooutputfile.getvalue()
34 poinputfile = wStringIO.StringIO(posource)
35 ootemplatefile = wStringIO.StringIO(oosource)
36 oooutputfile = wStringIO.StringIO()
37 po2oo.convertoo(poinputfile, oooutputfile, ootemplatefile, targetlanguage="en-US")
38 ooresult = oooutputfile.getvalue()
39 print "original oo:\n", oosource, "po version:\n", posource, "output oo:\n", ooresult
40 assert ooresult.startswith(oointro) and ooresult.endswith(oooutro)
41 return ooresult[len(oointro):-len(oooutro)]
43 def check_roundtrip(self, oosource):
44 """Checks that the round-tripped string is the same as the original"""
45 assert self.roundtripstring(oosource) == oosource
47 def test_convertoo(self):
48 """checks that the convertoo function is working"""
49 oobase = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 %s %s 20050924 09:13:58' + '\r\n'
50 posource = '''#: numpages.src#RID_SVXPAGE_NUM_OPTIONS.STR_BULLET.string.text\nmsgid "Simple String"\nmsgstr "Dimpled Ring"\n'''
51 ootemplate = oobase % ('en-US', 'Simple String')
52 ooexpected = oobase % ('zu', 'Dimpled Ring')
53 newoo = self.convertoo(posource, ootemplate, language="zu")
54 assert newoo == ootemplate + ooexpected
56 def test_pofilter(self):
57 """Tests integration with pofilter"""
58 #Some bad po with a few errors:
59 posource = '#: sourcefile.bla#ID_NUMBER.txet.gnirts\nmsgid "<tag cow=\\"3\\">Mistake."\nmsgstr " <etiket koei=\\"3\\">(fout) "'
60 filter = po2oo.filter
61 pofile = po.pofile()
62 pofile.parse(posource)
63 assert not filter.validelement(pofile.units[0], "dummyname.po", "exclude-all")
65 def test_roundtrip_simple(self):
66 """checks that simple strings make it through a oo->po->oo roundtrip"""
67 self.check_roundtrip('Hello')
68 self.check_roundtrip('"Hello"')
69 self.check_roundtrip('"Hello Everybody"')
71 def test_roundtrip_escape(self):
72 """checks that escapes in strings make it through a oo->po->oo roundtrip"""
73 self.check_roundtrip(r'"Simple Escape \ \n \\ \: \t \r "')
74 self.check_roundtrip(r'"More escapes \\n \\t \\r \\: "')
75 self.check_roundtrip(r'"More escapes \\\n \\\t \\\r \\\: "')
76 self.check_roundtrip(r'"More escapes \\\\n \\\\t \\\\r \\\\: "')
77 self.check_roundtrip(r'"End Line Escape \"')
78 self.check_roundtrip(r'"\\rangle \\langle')
79 self.check_roundtrip(r'\\\\<')
80 self.check_roundtrip(r'\\\<')
81 self.check_roundtrip(r'\\<')
82 self.check_roundtrip(r'\<')
84 def test_roundtrip_quotes(self):
85 """checks that (escaped) quotes in strings make it through a oo->po->oo roundtrip"""
86 self.check_roundtrip(r"""'Quote Escape "" '""")
87 self.check_roundtrip(r'''"Single-Quote ' "''')
88 self.check_roundtrip(r'''"Single-Quote Escape \' "''')
89 self.check_roundtrip(r"""'Both Quotes "" '' '""")
91 def xtest_roundtrip_spaces(self):
92 # FIXME: this test fails because the resultant PO file returns as po.isempty since .isblank returns true
93 # which is caused by isblankmsgtr returning True. Its a complete mess which would mean unravelling lots
94 # of yuch in pypo. Until we have time to do that unravelling we're diabling this test. You can reenable
95 # once we've fixed that.
96 """checks that (escaped) quotes in strings make it through a oo->po->oo roundtrip"""
97 self.check_roundtrip(" ")
98 self.check_roundtrip(u"\u00a0")
100 def test_default_timestamp(self):
101 """test to ensure that we revert to the default timestamp"""
102 oointro, oooutro = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Text ', '\r\n'
103 posource = '''#: numpages.src#RID_SVXPAGE_NUM_OPTIONS.STR_BULLET.string.text\nmsgid "Text"\nmsgstr "Text"\n'''
104 inputfile = wStringIO.StringIO(posource)
105 outputfile = wStringIO.StringIO()
106 templatefile = wStringIO.StringIO(oointro + '20050924 09:13:58' + oooutro)
107 assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage="en-US")
108 assert outputfile.getvalue() == oointro + '2002-02-02 02:02:02' + oooutro
110 def test_escape_conversion(self):
111 """test to ensure that we convert escapes correctly"""
112 oosource = r'svx source\dialog\numpages.src 0 string RID_SVXPAGE_NUM_OPTIONS STR_BULLET 0 en-US Column1\tColumn2\r\n 2002-02-02 02:02:02' + '\r\n'
113 posource = '''#: numpages.src#RID_SVXPAGE_NUM_OPTIONS.STR_BULLET.string.text\nmsgid "Column1\\tColumn2\\r\\n"\nmsgstr "Kolom1\\tKolom2\\r\\n"\n'''
114 inputfile = wStringIO.StringIO(posource)
115 outputfile = wStringIO.StringIO()
116 templatefile = wStringIO.StringIO(oosource)
117 assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage="af-ZA")
118 assert "\tKolom1\\tKolom2\\r\\n\t" in outputfile.getvalue()
120 def test_helpcontent_escapes(self):
121 """test to ensure that we convert helpcontent escapes correctly"""
122 # Note how this test specifically uses incorrect spacing in the
123 # translation. The extra space before 'hid' and an extra space before
124 # the closing tag should not confuse us.
125 oosource = r'helpcontent2 source\text\shared\3dsettings_toolbar.xhp 0 help par_idN1056A 0 en-US \<ahelp hid=\".\"\>The 3D-Settings toolbar controls properties of selected 3D objects.\</ahelp\> 2002-02-02 02:02:02' + '\r\n'
126 posource = r'''#: 3dsettings_toolbar.xhp#par_idN1056A.help.text
127 msgid ""
128 "<ahelp hid=\".\">The 3D-Settings toolbar controls properties of selected 3D "
129 "ob jects.</ahelp>"
130 msgstr ""
131 "<ahelp hid=\".\" >Zeee 3DDDD-Settings toolbar controls properties of selected 3D "
132 "objects.</ahelp>"
134 inputfile = wStringIO.StringIO(posource)
135 outputfile = wStringIO.StringIO()
136 templatefile = wStringIO.StringIO(oosource)
137 assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage="af-ZA")
138 assert r"\<ahelp hid=\".\" \>Zeee 3DDDD-Settings toolbar controls properties of selected 3D objects.\</ahelp\>" in outputfile.getvalue()
140 def test_helpcontent_escapes2(self):
141 """test to ensure that we convert helpcontent escapes correctly"""
142 oosource = r'helpcontent2 source\text\scalc\05\empty_cells.xhp 0 help par_id2629474 0 en-US A1: <empty> 2002-02-02 02:02:02' + '\r\n'
143 posource = r'''#: empty_cells.xhp#par_id2629474.help.text
144 msgid "A1: <empty>"
145 msgstr "Aa1: <empty>"
147 inputfile = wStringIO.StringIO(posource)
148 outputfile = wStringIO.StringIO()
149 templatefile = wStringIO.StringIO(oosource)
150 assert po2oo.convertoo(inputfile, outputfile, templatefile, targetlanguage="af-ZA")
151 assert r"Aa1: <empty>" in outputfile.getvalue()
153 class TestPO2OOCommand(test_convert.TestConvertCommand, TestPO2OO):
154 """Tests running actual po2oo commands on files"""
155 convertmodule = po2oo
157 def test_help(self):
158 """tests getting help"""
159 options = test_convert.TestConvertCommand.test_help(self)
160 options = self.help_check(options, "--source-language=LANG")
161 options = self.help_check(options, "--language=LANG")
162 options = self.help_check(options, "-T, --keeptimestamp")
163 options = self.help_check(options, "--nonrecursiveoutput")
164 options = self.help_check(options, "--nonrecursivetemplate")
165 options = self.help_check(options, "--filteraction")
166 options = self.help_check(options, "--fuzzy")
167 options = self.help_check(options, "--nofuzzy")
168 options = self.help_check(options, "-t TEMPLATE, --template=TEMPLATE")
169 options = self.help_check(options, "--multifile=MULTIFILESTYLE", last=True)
171 def merge2oo(self, oosource, posource):
172 """helper that merges po translations to oo source through files"""
173 outputoo = convertor.convertstore(inputpo)
174 return outputoo
176 def convertoo(self, posource, ootemplate, language="en-US"):
177 """helper to exercise the command line function"""
178 self.create_testfile(os.path.join("input", "svx", "source", "dialog.po"), posource)
179 self.create_testfile("input.oo", ootemplate)
180 self.run_command("input", "output.oo", template="input.oo", language=language, keeptimestamp=True)
181 return self.read_testfile("output.oo")