File size: 5,514 Bytes
1512437 | 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 | #!/usr/bin/env python
from translate.convert import csv2po, test_convert
from translate.misc import wStringIO
from translate.storage import csvl10n, po
from translate.storage.test_base import first_translatable, headerless_len
def test_replacestrings():
"""Test the _replacestring function"""
assert csv2po.replacestrings("Test one two three",
("one", "een"),
("two", "twee")) == "Test een twee three"
class TestCSV2PO:
def csv2po(self, csvsource, template=None):
"""helper that converts csv source to po source without requiring files"""
inputfile = wStringIO.StringIO(csvsource)
inputcsv = csvl10n.csvfile(inputfile)
if template:
templatefile = wStringIO.StringIO(template)
inputpot = po.pofile(templatefile)
else:
inputpot = None
convertor = csv2po.csv2po(templatepo=inputpot)
outputpo = convertor.convertstore(inputcsv)
return outputpo
def singleelement(self, storage):
"""checks that the pofile contains a single non-header element, and returns it"""
print(storage.serialize())
assert headerless_len(storage.units) == 1
return first_translatable(storage)
def test_simpleentity(self):
"""checks that a simple csv entry definition converts properly to a po entry"""
csvheader = 'location,source,target\n'
csvsource = 'intl.charset.default,ISO-8859-1,UTF-16'
# Headerless
pofile = self.csv2po(csvsource)
pounit = self.singleelement(pofile)
# With header
pofile = self.csv2po(csvheader + csvsource)
pounit = self.singleelement(pofile)
assert pounit.getlocations() == ['intl.charset.default']
assert pounit.source == "ISO-8859-1"
assert pounit.target == "UTF-16"
def test_simpleentity_with_template(self):
"""checks that a simple csv entry definition converts properly to a po entry"""
csvsource = '''source,original,translation
intl.charset.default,ISO-8859-1,UTF-16'''
potsource = '''#: intl.charset.default
msgid "ISO-8859-1"
msgstr ""
'''
pofile = self.csv2po(csvsource, potsource)
pounit = self.singleelement(pofile)
assert pounit.getlocations() == ['intl.charset.default']
assert pounit.source == "ISO-8859-1"
assert pounit.target == "UTF-16"
def test_newlines(self):
"""tests multiline po entries"""
minicsv = r'''"Random comment
with continuation","Original text","Langdradige teks
wat lank aanhou"
'''
pofile = self.csv2po(minicsv)
unit = self.singleelement(pofile)
assert unit.getlocations() == ['Random comment\nwith continuation']
assert unit.source == "Original text"
print(unit.target)
assert unit.target == "Langdradige teks\nwat lank aanhou"
def test_tabs(self):
"""Test the escaping of tabs"""
minicsv = ',"First column\tSecond column","Twee kolomme gesky met \t"'
pofile = self.csv2po(minicsv)
unit = self.singleelement(pofile)
print(unit.source)
assert unit.source == "First column\tSecond column"
assert not pofile.findunit("First column\tSecond column").target == "Twee kolomme gesky met \\t"
def test_quotes(self):
"""Test the escaping of quotes (and slash)"""
minicsv = r''',"Hello ""Everyone""","Good day ""All"""
,"Use \"".","Gebruik \""."'''
print(minicsv)
csvfile = csvl10n.csvfile(wStringIO.StringIO(minicsv))
print(csvfile.serialize())
pofile = self.csv2po(minicsv)
unit = first_translatable(pofile)
assert unit.source == 'Hello "Everyone"'
assert pofile.findunit('Hello "Everyone"').target == 'Good day "All"'
print(pofile.serialize())
for unit in pofile.units:
print(unit.source)
print(unit.target)
print()
# assert pofile.findunit('Use \\".').target == 'Gebruik \\".'
def test_empties(self):
"""Tests that things keep working with empty entries"""
minicsv = ',SomeSource,'
pofile = self.csv2po(minicsv)
assert pofile.findunit("SomeSource") is not None
assert pofile.findunit("SomeSource").target == ""
assert headerless_len(pofile.units) == 1
def test_kdecomment(self):
"""checks that we can merge into KDE comment entries"""
csvsource = '''location,source,target
simple.c,Source,Target'''
potsource = r'''#: simple.c
msgid "_: KDE comment\n"
"Source"
msgstr ""
'''
pofile = self.csv2po(csvsource, potsource)
pounit = self.singleelement(pofile)
assert pounit._extract_msgidcomments() == 'KDE comment'
assert pounit.source == "Source"
assert pounit.target == "Target"
class TestCSV2POCommand(test_convert.TestConvertCommand, TestCSV2PO):
"""Tests running actual csv2po commands on files"""
convertmodule = csv2po
def test_help(self):
"""tests getting help"""
options = test_convert.TestConvertCommand.test_help(self)
options = self.help_check(options, "-t TEMPLATE, --template=TEMPLATE")
options = self.help_check(options, "-P, --pot")
options = self.help_check(options, "--charset=CHARSET")
options = self.help_check(options, "--columnorder=COLUMNORDER")
options = self.help_check(options, "--duplicates=DUPLICATESTYLE", last=True)
|