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
|
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from pytest import importorskip
import pytest
import sys
pytestmark = pytest.mark.skipif(sys.version_info > (3,0),
reason="Not implemented for Python 3 yet")
from translate.convert import po2ini, test_convert
from translate.misc import wStringIO
from translate.storage import po
importorskip("iniparse")
class TestPO2Ini(object):
def po2ini(self, posource):
"""helper that converts po source to .ini source without requiring files"""
inputfile = wStringIO.StringIO(posource)
inputpo = po.pofile(inputfile)
convertor = po2ini.reini()
outputini = convertor.convertstore(inputpo)
return outputini
def merge2ini(self, inisource, posource, dialect="default"):
"""helper that merges po translations to .ini source without requiring files"""
inputfile = wStringIO.StringIO(posource)
inputpo = po.pofile(inputfile)
templatefile = wStringIO.StringIO(inisource)
convertor = po2ini.reini(templatefile, inputpo, dialect=dialect)
outputini = convertor.convertstore()
print(outputini)
return outputini
def test_merging_simple(self):
"""check the simplest case of merging a translation"""
posource = '''#: [section]prop\nmsgid "value"\nmsgstr "waarde"\n'''
initemplate = '''[section]\nprop=value\n'''
iniexpected = '''[section]\nprop=waarde\n'''
inifile = self.merge2ini(initemplate, posource)
print(inifile)
assert inifile == iniexpected
def test_space_preservation(self):
"""check that we preserve any spacing in ini files when merging"""
posource = '''#: [section]prop\nmsgid "value"\nmsgstr "waarde"\n'''
initemplate = '''[section]\nprop = value\n'''
iniexpected = '''[section]\nprop = waarde\n'''
inifile = self.merge2ini(initemplate, posource)
print(inifile)
assert inifile == iniexpected
def test_merging_blank_entries(self):
"""check that we can correctly merge entries that are blank in the template"""
posource = r'''#: [section]accesskey-accept
msgid ""
"_: accesskey-accept\n"
""
msgstr ""'''
initemplate = '[section]\naccesskey-accept=\n'
iniexpected = '[section]\naccesskey-accept=\n'
inifile = self.merge2ini(initemplate, posource)
print(inifile)
assert inifile == iniexpected
def test_merging_fuzzy(self):
"""check merging a fuzzy translation"""
posource = '''#: [section]prop\n#, fuzzy\nmsgid "value"\nmsgstr "waarde"\n'''
initemplate = '''[section]\nprop=value\n'''
iniexpected = '''[section]\nprop=value\n'''
inifile = self.merge2ini(initemplate, posource)
print(inifile)
assert inifile == iniexpected
def test_merging_propertyless_template(self):
"""check that when merging with a template with no ini values that we copy the template"""
posource = ""
initemplate = "# A comment\n"
iniexpected = initemplate
inifile = self.merge2ini(initemplate, posource)
print(inifile)
assert inifile == iniexpected
def test_empty_value(self):
"""test that we handle an value in translation that is missing in the template"""
posource = '''#: [section]key
msgctxt "key"
msgid ""
msgstr "translated"
'''
initemplate = '''[section]\nkey =\n'''
iniexpected = '''[section]\nkey =translated\n'''
inifile = self.merge2ini(initemplate, posource)
print(inifile)
assert inifile == iniexpected
def test_dialects_inno(self):
"""test that we output correctly for Inno files."""
posource = r'''#: [section]prop
msgid "value\tvalue2\n"
msgstr "ṽḁḽṻḝ\tṽḁḽṻḝ2\n"
'''
initemplate = '''[section]\nprop = value%tvalue%n\n'''
iniexpected = '''[section]\nprop = ṽḁḽṻḝ%tṽḁḽṻḝ2%n\n'''
inifile = self.merge2ini(initemplate, posource, "inno").decode('utf-8')
print(inifile)
assert inifile == iniexpected
class TestPO2IniCommand(test_convert.TestConvertCommand, TestPO2Ini):
"""Tests running actual po2ini commands on files"""
convertmodule = po2ini
defaultoptions = {"progress": "none"}
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, "--threshold=PERCENT")
options = self.help_check(options, "--fuzzy")
options = self.help_check(options, "--nofuzzy", last=True)
|