fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / filters / test_pofilter.py
blobd77652b921927dc7ba4efe762adbecaf368994d6
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 from translate.storage import factory
5 from translate.filters import pofilter
6 from translate.filters import checks
7 from translate.misc import wStringIO
9 class BaseTestFilter(object):
10 """Base class for filter tests."""
12 filename = ""
14 def parse_text(self, filetext):
15 """helper that parses xliff file content without requiring files"""
16 dummyfile = wStringIO.StringIO(filetext)
17 dummyfile.name = self.filename
18 store = factory.getobject(dummyfile)
19 return store
21 def filter(self, translationstore, checkerconfig=None, cmdlineoptions=None):
22 """Helper that passes a translations store through a filter, and returns the resulting store."""
23 if cmdlineoptions is None:
24 cmdlineoptions = []
25 options, args = pofilter.cmdlineparser().parse_args([self.filename] + cmdlineoptions)
26 checkerclasses = [checks.StandardChecker, checks.StandardUnitChecker]
27 if checkerconfig is None:
28 checkerconfig = checks.CheckerConfig()
29 checkfilter = pofilter.pocheckfilter(options, checkerclasses, checkerconfig)
30 tofile = checkfilter.filterfile(translationstore)
31 return tofile
33 def test_simplepass(self):
34 """checks that an obviously correct string passes"""
35 filter_result = self.filter(self.translationstore)
36 assert len(filter_result.units) == 0
38 def test_simplefail(self):
39 """checks that an obviously wrong string fails"""
40 self.unit.target = "REST"
41 filter_result = self.filter(self.translationstore)
42 assert filter_result.units[0].geterrors().has_key('startcaps')
44 def test_variables_across_lines(self):
45 """Test that variables can span lines and still fail/pass"""
46 self.unit.source = '"At &timeBombURL."\n"label;."'
47 self.unit.target = '"Tydens &tydBombURL."\n"etiket;."'
48 filter_result = self.filter(self.translationstore)
49 assert len(filter_result.units) == 0
51 def test_ignore_if_already_marked(self):
52 """check that we don't add another failing marker if the message is already marked as failed"""
53 self.unit.target = ''
54 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=untranslated"])
55 errors = filter_result.units[0].geterrors()
56 assert len(errors) == 1
57 assert errors.has_key('untranslated')
59 # Run a filter test on the result, to check that it doesn't mark the same error twice.
60 filter_result2 = self.filter(filter_result, cmdlineoptions=["--test=untranslated"])
61 errors = filter_result2.units[0].geterrors()
62 assert len(errors) == 1
63 assert errors.has_key('untranslated')
65 def test_non_existant_check(self):
66 """check that we report an error if a user tries to run a non-existant test"""
67 filter_result = self.filter(self.translationstore, cmdlineoptions=["-t nonexistant"])
68 # TODO Not sure how to check for the stderror result of: warning: could not find filter nonexistant
69 assert len(filter_result.units) == 0
71 def test_list_all_tests(self):
72 """lists all available tests"""
73 filter_result = self.filter(self.translationstore, cmdlineoptions=["-l"])
74 # TODO again not sure how to check the stderror output
75 assert len(filter_result.units) == 0
77 def test_test_against_fuzzy(self):
78 """test whether to run tests against fuzzy translations"""
79 self.unit.markfuzzy()
81 filter_result = self.filter(self.translationstore, cmdlineoptions=["--fuzzy"])
82 assert filter_result.units[0].geterrors().has_key('isfuzzy')
84 filter_result = self.filter(self.translationstore, cmdlineoptions=["--nofuzzy"])
85 assert len(filter_result.units) == 0
87 # Re-initialize the translation store object in order to get an unfuzzy unit
88 # with no filter notes.
89 self.setup_method(self)
91 filter_result = self.filter(self.translationstore, cmdlineoptions=["--fuzzy"])
92 assert len(filter_result.units) == 0
94 filter_result = self.filter(self.translationstore, cmdlineoptions=["--nofuzzy"])
95 assert len(filter_result.units) == 0
97 def test_test_against_review(self):
98 """test whether to run tests against translations marked for review"""
99 self.unit.markreviewneeded()
100 filter_result = self.filter(self.translationstore, cmdlineoptions=["--review"])
101 assert filter_result.units[0].isreview()
103 filter_result = self.filter(self.translationstore, cmdlineoptions=["--noreview"])
104 assert len(filter_result.units) == 0
106 # Re-initialize the translation store object.
107 self.setup_method(self)
109 filter_result = self.filter(self.translationstore, cmdlineoptions=["--review"])
110 assert len(filter_result.units) == 0
111 filter_result = self.filter(self.translationstore, cmdlineoptions=["--noreview"])
112 assert len(filter_result.units) == 0
114 def test_isfuzzy(self):
115 """tests the extraction of items marked fuzzy"""
116 self.unit.markfuzzy()
118 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=isfuzzy"])
119 assert filter_result.units[0].geterrors().has_key('isfuzzy')
121 self.unit.markfuzzy(False)
122 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=isfuzzy"])
123 assert len(filter_result.units) == 0
125 def test_isreview(self):
126 """tests the extraction of items marked review"""
127 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=isreview"])
128 assert len(filter_result.units) == 0
130 self.unit.markreviewneeded()
131 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=isreview"])
132 assert filter_result.units[0].isreview()
134 def test_unicode(self):
135 """tests that we can handle UTF-8 encoded characters when there is no known header specified encoding"""
136 self.unit.source = u'Bézier curve'
137 self.unit.target = u'Bézier-kurwe'
138 filter_result = self.filter(self.translationstore)
139 assert len(filter_result.units) == 0
141 def test_preconditions(self):
142 """tests that the preconditions work correctly"""
143 self.unit.source = "File"
144 self.unit.target = ""
145 filter_result= self.filter(self.translationstore)
146 # We should only get one error (untranslated), and nothing else
147 assert len(filter_result.units) == 1
148 unit = filter_result.units[0]
149 assert len(unit.geterrors()) == 1
151 class TestPOFilter(BaseTestFilter):
152 """Test class for po-specific tests."""
153 filetext = '#: test.c\nmsgid "test"\nmsgstr "rest"\n'
154 filename = 'test.po'
156 def setup_method(self, method):
157 self.translationstore = self.parse_text(self.filetext)
158 self.unit = self.translationstore.units[0]
160 def test_msgid_comments(self):
161 """Tests that msgid comments don't feature anywhere."""
162 posource = 'msgid "_: Capital. ACRONYMN. (msgid) comment 3. %d Extra sentence.\\n"\n"cow"\nmsgstr "koei"\n'
163 pofile = self.parse_text(posource)
164 filter_result = self.filter(pofile)
165 if len(filter_result.units):
166 print filter_result.units[0]
167 assert len(filter_result.units) == 0
169 class TestXliffFilter(BaseTestFilter):
170 """Test class for xliff-specific tests."""
171 filetext = '''<?xml version="1.0" encoding="utf-8"?>
172 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
173 <file original='NoName' source-language="en" datatype="plaintext">
174 <body>
175 <trans-unit approved="yes">
176 <source>test</source>
177 <target>rest</target>
178 </trans-unit>
179 </body>
180 </file>
181 </xliff>'''
182 filename = "test.xlf"
184 def set_store_review(review=True):
185 self.filetext = '''<?xml version="1.0" encoding="utf-8"?>
186 <xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
187 <file datatype="po" original="example.po" source-language="en-US">
188 <body>
189 <trans-unit approved="yes">
190 <source>test</source>
191 <target>rest</target>
192 </trans-unit>
193 </body>
194 </file>
195 </xliff>'''
197 self.translationstore = self.parse_text(self.filetext)
198 self.unit = self.translationstore.units[0]
200 def setup_method(self, method):
201 self.translationstore = self.parse_text(self.filetext)
202 self.unit = self.translationstore.units[0]
204 class TestTMXFilter(BaseTestFilter):
205 """Test class for TMX-specific tests."""
206 filetext = '''<!DOCTYPE tmx SYSTEM "tmx14.dtd">
207 <tmx version="1.4">
208 <header creationtool="Translate Toolkit - po2tmx" creationtoolversion="1.1.1rc1" segtype="sentence" o-tmf="UTF-8" adminlang="en" srclang="en
209 " datatype="PlainText"/>
210 <body>
211 <tu>
212 <tuv xml:lang="en">
213 <seg>test</seg>
214 </tuv>
215 <tuv xml:lang="af">
216 <seg>rest</seg>
217 </tuv>
218 </tu>
219 </body>
220 </tmx>'''
221 filename = "test.tmx"
223 def setup_method(self, method):
224 self.translationstore = self.parse_text(self.filetext)
225 self.unit = self.translationstore.units[0]
227 def test_test_against_fuzzy(self):
228 """TMX doesn't support fuzzy"""
229 pass
231 def test_test_against_review(self):
232 """TMX doesn't support review"""
233 pass
235 def test_isfuzzy(self):
236 """TMX doesn't support fuzzy"""
237 pass
239 def test_isreview(self):
240 """TMX doesn't support review"""
241 pass