fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_prop2po.py
blob06e040249ec4d97684d1596771ecb2d866027bf8
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 from translate.convert import prop2po
5 from translate.convert import test_convert
6 from translate.misc import wStringIO
7 from translate.storage import po
8 from translate.storage import properties
10 class TestProp2PO:
11 def prop2po(self, propsource, proptemplate=None):
12 """helper that converts .properties source to po source without requiring files"""
13 inputfile = wStringIO.StringIO(propsource)
14 inputprop = properties.propfile(inputfile)
15 convertor = prop2po.prop2po()
16 if proptemplate:
17 templatefile = wStringIO.StringIO(proptemplate)
18 templateprop = properties.propfile(templatefile)
19 outputpo = convertor.mergestore(templateprop, inputprop)
20 else:
21 outputpo = convertor.convertstore(inputprop)
22 return outputpo
24 def convertprop(self, propsource):
25 """call the convertprop, return the outputfile"""
26 inputfile = wStringIO.StringIO(propsource)
27 outputfile = wStringIO.StringIO()
28 templatefile = None
29 assert prop2po.convertprop(inputfile, outputfile, templatefile)
30 return outputfile.getvalue()
32 def singleelement(self, pofile):
33 """checks that the pofile contains a single non-header element, and returns it"""
34 assert len(pofile.units) == 2
35 assert pofile.units[0].isheader()
36 print pofile
37 return pofile.units[1]
39 def countelements(self, pofile):
40 """counts the number of non-header entries"""
41 assert pofile.units[0].isheader()
42 print pofile
43 return len(pofile.units) - 1
45 def test_simpleentry(self):
46 """checks that a simple properties entry converts properly to a po entry"""
47 propsource = 'SAVEENTRY=Save file\n'
48 pofile = self.prop2po(propsource)
49 pounit = self.singleelement(pofile)
50 assert pounit.source == "Save file"
51 assert pounit.target == ""
53 def test_convertprop(self):
54 """checks that the convertprop function is working"""
55 propsource = 'SAVEENTRY=Save file\n'
56 posource = self.convertprop(propsource)
57 pofile = po.pofile(wStringIO.StringIO(posource))
58 pounit = self.singleelement(pofile)
59 assert pounit.source == "Save file"
60 assert pounit.target == ""
62 def test_tab_at_end_of_string(self):
63 """check that we preserve tabs at the end of a string"""
64 propsource = r"TAB_AT_END=This setence has a tab at the end.\t"
65 pofile = self.prop2po(propsource)
66 pounit = self.singleelement(pofile)
67 assert pounit.source == "This setence has a tab at the end.\t"
69 propsource = r"SPACE_THEN_TAB_AT_END=This setence has a space then tab at the end. \t"
70 pofile = self.prop2po(propsource)
71 pounit = self.singleelement(pofile)
72 assert pounit.source == "This setence has a space then tab at the end. \t"
74 propsource = r"SPACE_AT_END=This setence will lose its 4 spaces at the end. "
75 pofile = self.prop2po(propsource)
76 pounit = self.singleelement(pofile)
77 assert pounit.source == "This setence will lose its 4 spaces at the end."
79 propsource = r"SPACE_AT_END_NO_TRIM=This setence will keep its 4 spaces at the end.\\ "
80 pofile = self.prop2po(propsource)
81 pounit = self.singleelement(pofile)
82 assert pounit.source == "This setence will keep its 4 spaces at the end. "
84 def test_tab_at_start_of_value(self):
85 """check that tabs in a property are ignored where appropriate"""
86 propsource = r"property = value"
87 pofile = self.prop2po(propsource)
88 pounit = self.singleelement(pofile)
89 assert pounit.getlocations()[0] == "property"
90 assert pounit.source == "value"
92 def test_unicode(self):
93 """checks that unicode entries convert properly"""
94 unistring = r'Norsk bokm\u00E5l'
95 propsource = 'nb = %s\n' % unistring
96 pofile = self.prop2po(propsource)
97 pounit = self.singleelement(pofile)
98 print repr(pofile.units[0].target)
99 print repr(pounit.source)
100 assert pounit.source == u'Norsk bokm\u00E5l'
102 def test_multiline_escaping(self):
103 """checks that multiline enties can be parsed"""
104 propsource = r"""5093=Unable to connect to your IMAP server. You may have exceeded the maximum number \
105 of connections to this server. If so, use the Advanced IMAP Server Settings dialog to \
106 reduce the number of cached connections."""
107 pofile = self.prop2po(propsource)
108 print repr(pofile.units[1].target)
109 assert self.countelements(pofile) == 1
111 def test_comments(self):
112 """test to ensure that we take comments from .properties and place them in .po"""
113 propsource = '''# Comment
114 prefPanel-smime=Security'''
115 pofile = self.prop2po(propsource)
116 pounit = self.singleelement(pofile)
117 assert pounit.getnotes("developer") == "# Comment"
119 def wtest_folding_accesskeys(self):
120 """check that we can fold various accesskeys into their associated label (bug #115)"""
121 propsource = r'''cmd_addEngine = Add Engines...
122 cmd_addEngine_accesskey = A'''
123 pofile = self.prop2po(propsource)
124 pounit = self.singleelement(pofile)
125 assert pounit.target == "&Add Engines..."
127 def test_dont_translate(self):
128 """check that we know how to ignore don't translate instructions in properties files (bug #116)"""
129 propsource = '''# LOCALIZATION NOTE (dont): DONT_TRANSLATE.
130 dont=don't translate me
131 do=translate me
133 pofile = self.prop2po(propsource)
134 assert self.countelements(pofile) == 1
136 def test_emptyproperty(self):
137 """checks that empty property definitions survive into po file, bug 15"""
138 propsource = '# comment\ncredit='
139 pofile = self.prop2po(propsource)
140 pounit = self.singleelement(pofile)
141 assert pounit.getlocations() == ["credit"]
142 assert pounit.getcontext() == "credit"
143 assert "#. # comment" in str(pofile)
144 assert pounit.source == ""
146 def test_emptyproperty_translated(self):
147 """checks that if we translate an empty property it makes it into the PO"""
148 proptemplate = 'credit='
149 propsource = 'credit=Translators Names'
150 pofile = self.prop2po(propsource, proptemplate)
151 pounit = self.singleelement(pofile)
152 assert pounit.getlocations() == ["credit"]
153 # FIXME we don't seem to get a _: comment but we should
154 #assert pounit.getcontext() == "credit"
155 assert pounit.source == ""
156 assert pounit.target == "Translators Names"
158 def test_newlines_in_value(self):
159 """check that we can carry newlines that appear in the property value into the PO"""
160 propsource = '''prop=\\nvalue\\n\n'''
161 pofile = self.prop2po(propsource)
162 unit = self.singleelement(pofile)
163 assert unit.source == "\nvalue\n"
165 def test_unassociated_comments(self):
166 """check that we can handle comments not directly associated with a property"""
167 propsource = '''# Header comment\n\n# Comment\n\nprop=value\n'''
168 pofile = self.prop2po(propsource)
169 unit = self.singleelement(pofile)
170 assert unit.source == "value"
171 assert unit.getnotes("developer") == "# Comment"
173 def test_unassociated_comment_order(self):
174 """check that we can handle the order of unassociated comments"""
175 propsource = '''# Header comment\n\n# 1st Unassociated comment\n\n# 2nd Connected comment\nprop=value\n'''
176 pofile = self.prop2po(propsource)
177 unit = self.singleelement(pofile)
178 assert unit.source == "value"
179 assert unit.getnotes("developer") == "# 1st Unassociated comment\n# 2nd Connected comment"
181 class TestProp2POCommand(test_convert.TestConvertCommand, TestProp2PO):
182 """Tests running actual prop2po commands on files"""
183 convertmodule = prop2po
184 defaultoptions = {"progress": "none"}
186 def test_help(self):
187 """tests getting help"""
188 options = test_convert.TestConvertCommand.test_help(self)
189 options = self.help_check(options, "-P, --pot")
190 options = self.help_check(options, "-t TEMPLATE, --template=TEMPLATE")
191 options = self.help_check(options, "--duplicates=DUPLICATESTYLE", last=True)