public
Description: Web-based translation and translation management tool
Homepage: http://translate.sf.net/
Clone URL: git://github.com/julen/pootle.git
pootle / test_pootlefile.py
100644 147 lines (124 sloc) 5.732 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
#!/usr/bin/env python
 
from Pootle import pootlefile
from Pootle import pootle
from Pootle import projects
from translate.storage import po
from translate.storage import test_po
from translate.filters import checks
from translate.misc import wStringIO
 
import os
 
class TestPootleFile(test_po.TestPOFile):
    class pootletestfile(pootlefile.pootlefile):
        def __init__(self):
            """wrapper constructor for pootlefile that uses temporary filename"""
            project = projects.DummyProject(self.testdir)
            return pootlefile.pootlefile.__init__(self, project, self.pofilename)
 
    StoreClass = pootletestfile
 
    def setup_method(self, method):
        """creates a clean test directory for the given method"""
        self.testdir = "%s_%s" % (self.__class__.__name__, method.__name__)
        self.filename = "%s_%s.po" % (self.__class__.__name__, method.__name__)
        self.pootletestfile.testdir = self.testdir
        self.pootletestfile.pofilename = self.filename
        self.cleardir()
        os.mkdir(self.testdir)
        self.rundir = os.path.abspath(os.getcwd())
        #potree.podirectory = self.testdir
        os.mkdir(os.path.join(self.testdir, 'unittest_project'))
        os.mkdir(os.path.join(self.testdir, 'unittest_project', 'xx'))
        posource = r'''#: test.c
msgid "test"
msgstr "rest"
 
#, fuzzy
msgid "tabel"
msgstr "tafel"
 
msgid "chair"
msgstr ""'''
        file(os.path.join(self.testdir, 'unittest_project', 'xx', 'test.po'), 'w').write(posource)
 
    def teardown_method(self, method):
        """removes the test directory for the given method"""
        os.chdir(self.rundir)
        self.cleardir()
 
    def cleardir(self):
        """removes the test directory"""
        if os.path.exists(self.testdir):
            for dirpath, subdirs, filenames in os.walk(self.testdir, topdown=False):
                for name in filenames:
                    os.remove(os.path.join(dirpath, name))
                for name in subdirs:
                    os.rmdir(os.path.join(dirpath, name))
        if os.path.exists(self.testdir): os.rmdir(self.testdir)
        assert not os.path.exists(self.testdir)
 
    def poparse(self, posource):
        """helper that parses po source without requiring files"""
        def filtererrorhandler(functionname, str1, str2, e):
            print "error in filter %s: %r, %r, %s" % (functionname, str1, str2, e)
            return False
 
        checkerclasses = [checks.StandardChecker, checks.StandardUnitChecker]
        stdchecker = checks.TeeChecker(checkerclasses=checkerclasses, errorhandler=filtererrorhandler)
        dummyproject = projects.DummyStatsProject(self.rundir, stdchecker, "unittest_project", "xx")
 
        pofile = pootlefile.pootlefile(dummyproject, "test.po", generatestats=False)
        pofile.parse(posource)
        return pofile
 
    def poregen(self, posource):
        """helper that converts po source to pofile object and back"""
        return str(self.poparse(posource))
 
    def test_simpleentry(self):
        """checks that a simple po entry is parsed correctly"""
        posource = '#: test.c\nmsgid "test"\nmsgstr "rest"\n'
        pofile = self.poparse(posource)
        assert len(pofile.units) == 1
        unit = pofile.units[0]
        assert unit.getlocations() == ["test.c"]
        assert unit.source == "test"
        assert unit.target == "rest"
 
    def test_classifyunits(self):
        "Tests basic use of classifyunits."
        posource = r'''#: test.c
msgid "test"
msgstr "rest"
 
#, fuzzy
msgid "tabel"
msgstr "tafel"
 
msgid "chair"
msgstr ""'''
        pofile = self.poparse(posource)
        pofile.savepofile()
        pofile.reset_statistics()
        assert pofile.statistics.getstats()['fuzzy'] == [1]
        assert pofile.statistics.getstats()['untranslated'] == [2]
        assert pofile.statistics.getstats()['translated'] == [0]
        assert pofile.statistics.getstats()['total'] == [0, 1, 2]
 
    def test_updateunit(self):
        """Test the updateunit() method."""
        posource = '#: test.c\nmsgid "upd"\nmsgstr "update"\n'
        testdir = os.path.join(self.testdir, 'unittest_project', 'xx')
        filename = self.filename
        filepath = os.path.join(testdir, filename)
        file(filepath, 'w').write(posource)
        dummy_project = projects.DummyProject(podir=testdir)
        pofile = pootlefile.pootlefile(project=dummy_project, pofilename=filename)
 
        newvalues = {}
        pofile.updateunit(0, newvalues, None, None)
        translation_unit = pofile.units[1]
        assert translation_unit.target == "update"
        assert not translation_unit.isfuzzy()
        assert str(translation_unit) == posource
 
        newvalues = {"target": "opdateer"}
        pofile.updateunit(0, newvalues, None, None)
        assert translation_unit.target == "opdateer"
        assert not translation_unit.isfuzzy()
        expected_posource = '#: test.c\nmsgid "upd"\nmsgstr "opdateer"\n'
        assert str(translation_unit) == expected_posource
 
        newvalues = {"fuzzy": True}
        pofile.updateunit(0, newvalues, None, None)
        assert translation_unit.target == "opdateer"
        assert translation_unit.isfuzzy()
        expected_posource = '#: test.c\n#, fuzzy\nmsgid "upd"\nmsgstr "opdateer"\n'
        assert str(translation_unit) == expected_posource
 
        newvalues = {"translator_comments": "Test comment."}
        pofile.updateunit(0, newvalues, None, None)
        assert translation_unit.target == "opdateer"
        assert translation_unit.isfuzzy()
        expected_posource = '# Test comment.\n#: test.c\n#, fuzzy\nmsgid "upd"\nmsgstr "opdateer"\n'
        assert str(translation_unit) == expected_posource