Package translate :: Package filters :: Module test_pofilter
[hide private]
[frames] | no frames]

Source Code for Module translate.filters.test_pofilter

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  from translate.storage import factory 
  5  from translate.filters import pofilter 
  6  from translate.filters import checks 
  7  from translate.misc import wStringIO 
  8   
9 -class BaseTestFilter(object):
10 """Base class for filter tests.""" 11 12 filename = "" 13
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
20
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
32
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
37
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')
43
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
50
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') 58 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')
64
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
70
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
76
77 - def test_test_against_fuzzy(self):
78 """test whether to run tests against fuzzy translations""" 79 self.unit.markfuzzy() 80 81 filter_result = self.filter(self.translationstore, cmdlineoptions=["--fuzzy"]) 82 assert filter_result.units[0].geterrors().has_key('isfuzzy') 83 84 filter_result = self.filter(self.translationstore, cmdlineoptions=["--nofuzzy"]) 85 assert len(filter_result.units) == 0 86 87 # Re-initialize the translation store object in order to get an unfuzzy unit 88 # with no filter notes. 89 self.setup_method(self) 90 91 filter_result = self.filter(self.translationstore, cmdlineoptions=["--fuzzy"]) 92 assert len(filter_result.units) == 0 93 94 filter_result = self.filter(self.translationstore, cmdlineoptions=["--nofuzzy"]) 95 assert len(filter_result.units) == 0
96
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() 102 103 filter_result = self.filter(self.translationstore, cmdlineoptions=["--noreview"]) 104 assert len(filter_result.units) == 0 105 106 # Re-initialize the translation store object. 107 self.setup_method(self) 108 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
113
114 - def test_isfuzzy(self):
115 """tests the extraction of items marked fuzzy""" 116 self.unit.markfuzzy() 117 118 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=isfuzzy"]) 119 assert filter_result.units[0].geterrors().has_key('isfuzzy') 120 121 self.unit.markfuzzy(False) 122 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=isfuzzy"]) 123 assert len(filter_result.units) == 0
124
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 129 130 self.unit.markreviewneeded() 131 filter_result = self.filter(self.translationstore, cmdlineoptions=["--test=isreview"]) 132 assert filter_result.units[0].isreview()
133
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
140
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
150
151 -class TestPOFilter(BaseTestFilter):
152 """Test class for po-specific tests.""" 153 filetext = '#: test.c\nmsgid "test"\nmsgstr "rest"\n' 154 filename = 'test.po' 155
156 - def setup_method(self, method):
157 self.translationstore = self.parse_text(self.filetext) 158 self.unit = self.translationstore.units[0]
159
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
168
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" 183
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>''' 196 197 self.translationstore = self.parse_text(self.filetext) 198 self.unit = self.translationstore.units[0]
199
200 - def setup_method(self, method):
201 self.translationstore = self.parse_text(self.filetext) 202 self.unit = self.translationstore.units[0]
203
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" 222
223 - def setup_method(self, method):
224 self.translationstore = self.parse_text(self.filetext) 225 self.unit = self.translationstore.units[0]
226
227 - def test_test_against_fuzzy(self):
228 """TMX doesn't support fuzzy""" 229 pass
230
231 - def test_test_against_review(self):
232 """TMX doesn't support review""" 233 pass
234
235 - def test_isfuzzy(self):
236 """TMX doesn't support fuzzy""" 237 pass
238
239 - def test_isreview(self):
240 """TMX doesn't support review""" 241 pass
242