translateorgza / translate forked from egparedes/translate

Toolkit assisting in the localization of software

This URL has Read+Write access

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