fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_po2ts.py
blob7213a484f481ac10bec21050a08c027bd62607a7
1 #!/usr/bin/env python
3 from translate.convert import po2ts
4 from translate.convert import test_convert
5 from translate.misc import wStringIO
6 from translate.storage import po
8 class TestPO2TS:
9 def po2ts(self, posource):
10 """helper that converts po source to ts source without requiring files"""
11 inputfile = wStringIO.StringIO(posource)
12 inputpo = po.pofile(inputfile)
13 convertor = po2ts.po2ts()
14 outputts = convertor.convertstore(inputpo)
15 return outputts
17 def singleelement(self, storage):
18 """checks that the pofile contains a single non-header element, and returns it"""
19 assert len(storage.units) == 1
20 return storage.units[0]
22 def test_simpleunit(self):
23 """checks that a simple po entry definition converts properly to a ts entry"""
24 minipo = r'''#: term.cpp
25 msgid "Term"
26 msgstr "asdf"'''
27 tsfile = self.po2ts(minipo)
28 print tsfile
29 assert "<name>term.cpp</name>" in tsfile
30 assert "<source>Term</source>" in tsfile
31 assert "<translation>asdf</translation>" in tsfile
32 assert "<comment>" not in tsfile
34 def test_fullunit(self):
35 """check that an entry with various settings is converted correctly"""
36 posource = '''# Translator comment
37 #. Automatic comment
38 #: location.cpp:100
39 msgid "Source"
40 msgstr "Target"
41 '''
42 tsfile = self.po2ts(posource)
43 print tsfile
44 # The other section are a duplicate of test_simplentry
45 # FIXME need to think about auto vs trans comments maybe in TS v1.1
46 assert "<comment>Translator comment</comment>" in tsfile
48 def test_fuzzyunit(self):
49 """check that we handle fuzzy units correctly"""
50 posource = '''#: term.cpp
51 #, fuzzy
52 msgid "Source"
53 msgstr "Target"'''
54 tsfile = self.po2ts(posource)
55 print tsfile
56 assert '''<translation type="unfinished">Target</translation>''' in tsfile
58 def test_obsolete(self):
59 """test that we can take back obsolete messages"""
60 posource = '''#. (obsolete)
61 #: term.cpp
62 msgid "Source"
63 msgstr "Target"'''
64 tsfile = self.po2ts(posource)
65 print tsfile
66 assert '''<translation type="obsolete">Target</translation>''' in tsfile
68 def test_duplicates(self):
69 """test that we can handle duplicates in the same context block"""
70 posource = '''#: @@@#1
71 msgid "English"
72 msgstr "a"
74 #: @@@#3
75 msgid "English"
76 msgstr "b"
77 '''
78 tsfile = self.po2ts(posource)
79 print tsfile
80 assert tsfile.find("English") != tsfile.rfind("English")
83 class TestPO2TSCommand(test_convert.TestConvertCommand, TestPO2TS):
84 """Tests running actual po2ts commands on files"""
85 convertmodule = po2ts
87 def test_help(self):
88 """tests getting help"""
89 options = test_convert.TestConvertCommand.test_help(self)
90 options = self.help_check(options, "-c CONTEXT, --context=CONTEXT")
91 options = self.help_check(options, "-t TEMPLATE, --template=TEMPLATE", last=True)