fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / convert / test_csv2po.py
blob4ae26c9d7b0e903596d41846543bc825891a19b6
1 #!/usr/bin/env python
3 from translate.convert import csv2po
4 from translate.convert import test_convert
5 from translate.misc import wStringIO
6 from translate.storage import po
7 from translate.storage import csvl10n
9 class TestCSV2PO:
10 def csv2po(self, csvsource, template=None):
11 """helper that converts csv source to po source without requiring files"""
12 inputfile = wStringIO.StringIO(csvsource)
13 inputcsv = csvl10n.csvfile(inputfile)
14 if template:
15 templatefile = wStringIO.StringIO(template)
16 inputpot = po.pofile(templatefile)
17 else:
18 inputpot = None
19 convertor = csv2po.csv2po(templatepo=inputpot)
20 outputpo = convertor.convertstore(inputcsv)
21 return outputpo
23 def singleelement(self, storage):
24 """checks that the pofile contains a single non-header element, and returns it"""
25 print str(storage)
26 assert len(storage.units) == 1
27 return storage.units[0]
29 def test_simpleentity(self):
30 """checks that a simple csv entry definition converts properly to a po entry"""
31 csvheader = 'comment,original,translation\n'
32 csvsource = 'intl.charset.default,ISO-8859-1,UTF-16'
33 # Headerless
34 pofile = self.csv2po(csvsource)
35 pounit = self.singleelement(pofile)
36 # With header
37 pofile = self.csv2po(csvheader + csvsource)
38 pounit = self.singleelement(pofile)
39 assert pounit.getlocations() == ['intl.charset.default']
40 assert pounit.source == "ISO-8859-1"
41 assert pounit.target == "UTF-16"
43 def test_simpleentity_with_template(self):
44 """checks that a simple csv entry definition converts properly to a po entry"""
45 csvsource = '''source,original,translation
46 intl.charset.default,ISO-8859-1,UTF-16'''
47 potsource = '''#: intl.charset.default
48 msgid "ISO-8859-1"
49 msgstr ""
50 '''
51 pofile = self.csv2po(csvsource, potsource)
52 pounit = self.singleelement(pofile)
53 assert pounit.getlocations() == ['intl.charset.default']
54 assert pounit.source == "ISO-8859-1"
55 assert pounit.target == "UTF-16"
57 def test_newlines(self):
58 """tests multiline po entries"""
59 minicsv = r'''"Random comment
60 with continuation","Original text","Langdradige teks
61 wat lank aanhou"
62 '''
63 pofile = self.csv2po(minicsv)
64 unit = self.singleelement(pofile)
65 assert unit.getlocations() == ['Random', 'comment', 'with', 'continuation']
66 assert unit.source == "Original text"
67 print unit.target
68 assert unit.target == "Langdradige teks\nwat lank aanhou"
70 def test_tabs(self):
71 """Test the escaping of tabs"""
72 minicsv = ',"First column\tSecond column","Twee kolomme gesky met \t"'
73 pofile = self.csv2po(minicsv)
74 unit = self.singleelement(pofile)
75 print unit.source
76 assert unit.source == "First column\tSecond column"
77 assert not pofile.findunit("First column\tSecond column").target == "Twee kolomme gesky met \\t"
79 def test_quotes(self):
80 """Test the escaping of quotes (and slash)"""
81 minicsv = r''',"Hello ""Everyone""","Good day ""All"""
82 ,"Use \"".","Gebruik \""."'''
83 print minicsv
84 csvfile = csvl10n.csvfile(wStringIO.StringIO(minicsv))
85 print str(csvfile)
86 pofile = self.csv2po(minicsv)
87 unit = pofile.units[0]
88 assert unit.source == 'Hello "Everyone"'
89 assert pofile.findunit('Hello "Everyone"').target == 'Good day "All"'
90 print str(pofile)
91 for unit in pofile.units:
92 print unit.source
93 print unit.target
94 print
95 # assert pofile.findunit('Use \\".').target == 'Gebruik \\".'
97 def test_empties(self):
98 """Tests that things keep working with empty entries"""
99 minicsv = ',Source,'
100 pofile = self.csv2po(minicsv)
101 assert pofile.findunit("Source") is not None
102 assert pofile.findunit("Source").target == ""
103 assert len(pofile.units) == 1
105 def test_kdecomment(self):
106 """checks that we can merge into KDE comment entries"""
107 csvsource = '''comment,original,translation
108 simple.c,Source,Target'''
109 potsource = r'''#: simple.c
110 msgid "_: KDE comment\n"
111 "Source"
112 msgstr ""
113 '''
114 pofile = self.csv2po(csvsource, potsource)
115 pounit = self.singleelement(pofile)
116 assert pounit._extract_msgidcomments() == 'KDE comment'
117 assert pounit.source == "Source"
118 assert pounit.target == "Target"
120 class TestCSV2POCommand(test_convert.TestConvertCommand, TestCSV2PO):
121 """Tests running actual csv2po commands on files"""
122 convertmodule = csv2po
124 def test_help(self):
125 """tests getting help"""
126 options = test_convert.TestConvertCommand.test_help(self)
127 options = self.help_check(options, "-t TEMPLATE, --template=TEMPLATE")
128 options = self.help_check(options, "--charset=CHARSET")
129 options = self.help_check(options, "--columnorder=COLUMNORDER")
130 options = self.help_check(options, "--duplicates=DUPLICATESTYLE", last=True)