3 from Pootle
import pootlefile
4 from Pootle
import pootle
5 from Pootle
import projects
6 from translate
.storage
import po
7 from translate
.storage
import test_po
8 from translate
.filters
import checks
9 from translate
.misc
import wStringIO
13 class TestPootleFile(test_po
.TestPOFile
):
14 class pootletestfile(pootlefile
.pootlefile
):
16 """wrapper constructor for pootlefile that uses temporary filename"""
17 project
= projects
.DummyProject(self
.testdir
)
18 return pootlefile
.pootlefile
.__init
__(self
, project
, self
.pofilename
)
20 StoreClass
= pootletestfile
22 def setup_method(self
, method
):
23 """creates a clean test directory for the given method"""
24 self
.testdir
= "%s_%s" % (self
.__class
__.__name
__, method
.__name
__)
25 self
.filename
= "%s_%s.po" % (self
.__class
__.__name
__, method
.__name
__)
26 self
.pootletestfile
.testdir
= self
.testdir
27 self
.pootletestfile
.pofilename
= self
.filename
29 os
.mkdir(self
.testdir
)
30 self
.rundir
= os
.path
.abspath(os
.getcwd())
31 #potree.podirectory = self.testdir
32 os
.mkdir(os
.path
.join(self
.testdir
, 'unittest_project'))
33 os
.mkdir(os
.path
.join(self
.testdir
, 'unittest_project', 'xx'))
34 posource
= r
'''#: test.c
44 file(os
.path
.join(self
.testdir
, 'unittest_project', 'xx', 'test.po'), 'w').write(posource
)
46 def teardown_method(self
, method
):
47 """removes the test directory for the given method"""
52 """removes the test directory"""
53 if os
.path
.exists(self
.testdir
):
54 for dirpath
, subdirs
, filenames
in os
.walk(self
.testdir
, topdown
=False):
55 for name
in filenames
:
56 os
.remove(os
.path
.join(dirpath
, name
))
58 os
.rmdir(os
.path
.join(dirpath
, name
))
59 if os
.path
.exists(self
.testdir
): os
.rmdir(self
.testdir
)
60 assert not os
.path
.exists(self
.testdir
)
62 def poparse(self
, posource
):
63 """helper that parses po source without requiring files"""
64 def filtererrorhandler(functionname
, str1
, str2
, e
):
65 print "error in filter %s: %r, %r, %s" % (functionname
, str1
, str2
, e
)
68 checkerclasses
= [checks
.StandardChecker
, checks
.StandardUnitChecker
]
69 stdchecker
= checks
.TeeChecker(checkerclasses
=checkerclasses
, errorhandler
=filtererrorhandler
)
70 dummyproject
= projects
.DummyStatsProject(self
.rundir
, stdchecker
, "unittest_project", "xx")
72 pofile
= pootlefile
.pootlefile(dummyproject
, "test.po", generatestats
=False)
73 pofile
.parse(posource
)
76 def poregen(self
, posource
):
77 """helper that converts po source to pofile object and back"""
78 return str(self
.poparse(posource
))
80 def test_simpleentry(self
):
81 """checks that a simple po entry is parsed correctly"""
82 posource
= '#: test.c\nmsgid "test"\nmsgstr "rest"\n'
83 pofile
= self
.poparse(posource
)
84 assert len(pofile
.units
) == 1
85 unit
= pofile
.units
[0]
86 assert unit
.getlocations() == ["test.c"]
87 assert unit
.source
== "test"
88 assert unit
.target
== "rest"
90 def test_classifyunits(self
):
91 "Tests basic use of classifyunits."
92 posource
= r
'''#: test.c
102 pofile
= self
.poparse(posource
)
104 assert pofile
.statistics
.getstats()['fuzzy'] == [1]
105 assert pofile
.statistics
.getstats()['untranslated'] == [2]
106 assert pofile
.statistics
.getstats()['translated'] == [0]
107 assert pofile
.statistics
.getstats()['total'] == [0, 1, 2]
109 def test_updateunit(self
):
110 """Test the updateunit() method."""
111 posource
= '#: test.c\nmsgid "upd"\nmsgstr "update"\n'
112 testdir
= os
.path
.join(self
.testdir
, 'unittest_project', 'xx')
113 filename
= self
.filename
114 filepath
= os
.path
.join(testdir
, filename
)
115 file(filepath
, 'w').write(posource
)
116 dummy_project
= projects
.DummyProject(podir
=testdir
)
117 pofile
= pootlefile
.pootlefile(project
=dummy_project
, pofilename
=filename
)
120 pofile
.updateunit(0, newvalues
, None, None)
121 translation_unit
= pofile
.units
[1]
122 assert translation_unit
.target
== "update"
123 assert not translation_unit
.isfuzzy()
124 assert str(translation_unit
) == posource
126 newvalues
= {"target": "opdateer"}
127 pofile
.updateunit(0, newvalues
, None, None)
128 assert translation_unit
.target
== "opdateer"
129 assert not translation_unit
.isfuzzy()
130 expected_posource
= '#: test.c\nmsgid "upd"\nmsgstr "opdateer"\n'
131 assert str(translation_unit
) == expected_posource
133 newvalues
= {"fuzzy": True}
134 pofile
.updateunit(0, newvalues
, None, None)
135 assert translation_unit
.target
== "opdateer"
136 assert translation_unit
.isfuzzy()
137 expected_posource
= '#: test.c\n#, fuzzy\nmsgid "upd"\nmsgstr "opdateer"\n'
138 assert str(translation_unit
) == expected_posource
140 newvalues
= {"translator_comments": "Test comment."}
141 pofile
.updateunit(0, newvalues
, None, None)
142 assert translation_unit
.target
== "opdateer"
143 assert translation_unit
.isfuzzy()
144 expected_posource
= '# Test comment.\n#: test.c\n#, fuzzy\nmsgid "upd"\nmsgstr "opdateer"\n'
145 assert str(translation_unit
) == expected_posource