fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_po2csv.py
blobeca289622910ed920e02fd319e31124f0a766daf
1 #!/usr/bin/env python
3 from translate.convert import po2csv
4 from translate.convert import csv2po
5 from translate.convert import test_convert
6 from translate.misc import wStringIO
7 from translate.storage import po
8 from translate.storage import csvl10n
10 class TestPO2CSV:
11 def po2csv(self, posource):
12 """helper that converts po source to csv source without requiring files"""
13 inputfile = wStringIO.StringIO(posource)
14 inputpo = po.pofile(inputfile)
15 convertor = po2csv.po2csv()
16 outputcsv = convertor.convertstore(inputpo)
17 return outputcsv
19 def csv2po(self, csvsource, template=None):
20 """helper that converts csv source to po source without requiring files"""
21 inputfile = wStringIO.StringIO(csvsource)
22 inputcsv = csvl10n.csvfile(inputfile)
23 if template:
24 templatefile = wStringIO.StringIO(template)
25 inputpot = po.pofile(templatefile)
26 else:
27 inputpot = None
28 convertor = csv2po.csv2po(templatepo=inputpot)
29 outputpo = convertor.convertstore(inputcsv)
30 return outputpo
32 def singleelement(self, storage):
33 """checks that the pofile contains a single non-header element, and returns it"""
34 assert len(storage.units) == 1
35 return storage.units[0]
37 def test_simpleentity(self):
38 """checks that a simple csv entry definition converts properly to a po entry"""
39 minipo = r'''#: term.cpp
40 msgid "Term"
41 msgstr "asdf"'''
42 csvfile = self.po2csv(minipo)
43 unit = self.singleelement(csvfile)
44 assert unit.comment == "term.cpp"
45 assert unit.source == "Term"
46 assert unit.target == "asdf"
48 def test_multiline(self):
49 """tests multiline po entries"""
50 minipo = r'''msgid "First part "
51 "and extra"
52 msgstr "Eerste deel "
53 "en ekstra"'''
54 csvfile = self.po2csv(minipo)
55 unit = self.singleelement(csvfile)
56 assert unit.source == "First part and extra"
57 assert unit.target == "Eerste deel en ekstra"
59 def test_escapednewlines(self):
60 """Test the escaping of newlines"""
61 minipo = r'''msgid "First line\nSecond line"
62 msgstr "Eerste lyn\nTweede lyn"
63 '''
64 csvfile = self.po2csv(minipo)
65 unit = self.singleelement(csvfile)
66 assert unit.source == "First line\nSecond line"
67 assert unit.target == "Eerste lyn\nTweede lyn"
68 pofile = self.csv2po(str(csvfile))
69 unit = self.singleelement(pofile)
70 assert unit.source == "First line\nSecond line"
71 assert unit.target == "Eerste lyn\nTweede lyn"
73 def test_escapedtabs(self):
74 """Test the escaping of tabs"""
75 minipo = r'''msgid "First column\tSecond column"
76 msgstr "Eerste kolom\tTweede kolom"
77 '''
78 csvfile = self.po2csv(minipo)
79 unit = self.singleelement(csvfile)
80 assert unit.source == "First column\tSecond column"
81 assert unit.target == "Eerste kolom\tTweede kolom"
82 assert csvfile.findunit("First column\tSecond column").target == "Eerste kolom\tTweede kolom"
84 def test_escapedquotes(self):
85 """Test the escaping of quotes (and slash)"""
86 minipo = r'''msgid "Hello \"Everyone\""
87 msgstr "Good day \"All\""
89 msgid "Use \\\"."
90 msgstr "Gebruik \\\"."
91 '''
92 csvfile = self.po2csv(minipo)
93 assert csvfile.findunit('Hello "Everyone"').target == 'Good day "All"'
94 assert csvfile.findunit('Use \\".').target == 'Gebruik \\".'
96 def test_escapedescape(self):
97 """Test the escaping of pure escapes is unaffected"""
98 minipo = r'''msgid "Find\\Options"
99 msgstr "Vind\\Opsies"
101 csvfile = self.po2csv(minipo)
102 print minipo
103 print csvfile
104 assert csvfile.findunit(r'Find\Options').target == r'Vind\Opsies'
106 def test_singlequotes(self):
107 """Tests that single quotes are preserved correctly"""
108 minipo = '''msgid "source 'source'"\nmsgstr "target 'target'"\n'''
109 csvfile = self.po2csv(minipo)
110 print str(csvfile)
111 assert csvfile.findunit("source 'source'").target == "target 'target'"
112 # Make sure we don't mess with start quotes until writing
113 minipo = '''msgid "'source'"\nmsgstr "'target'"\n'''
114 csvfile = self.po2csv(minipo)
115 print str(csvfile)
116 assert csvfile.findunit(r"'source'").target == r"'target'"
117 # TODO check that we escape on writing not in the internal representation
119 def test_empties(self):
120 """Tests that things keep working with empty entries"""
121 minipo = 'msgid "Source"\nmsgstr ""\n\nmsgid ""\nmsgstr ""'
122 csvfile = self.po2csv(minipo)
123 assert csvfile.findunit("Source") is not None
124 assert csvfile.findunit("Source").target == ""
125 assert len(csvfile.units) == 1
127 def test_kdecomments(self):
128 """test that we don't carry KDE comments to CSV"""
129 minipo = '#: simple.c\nmsgid "_: KDE comment\\n"\n"Same"\nmsgstr "Same"\n'
130 csvfile = self.po2csv(minipo)
131 unit = self.singleelement(csvfile)
132 assert unit.source == "Same"
133 assert unit.target == "Same"
135 class TestPO2CSVCommand(test_convert.TestConvertCommand, TestPO2CSV):
136 """Tests running actual po2csv commands on files"""
137 convertmodule = po2csv
139 def test_help(self):
140 """tests getting help"""
141 options = test_convert.TestConvertCommand.test_help(self)
142 options = self.help_check(options, "-P, --pot")
143 options = self.help_check(options, "--columnorder=COLUMNORDER", last=True)