Package translate :: Package convert :: Module test_po2tmx

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.test_po2tmx

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3
      4  from translate.convert import po2tmx
      5  from translate.convert import test_convert
      6  from translate.misc import wStringIO
      7  from translate.storage import tmx
      8  from translate.storage import lisa
      9
     10 -class TestPO2TMX:
     11
     12 -    def po2tmx(self, posource, sourcelanguage='en', targetlanguage='af'):
     13          """helper that converts po source to tmx source without requiring files"""
     14          inputfile = wStringIO.StringIO(posource)
     15          outputfile = wStringIO.StringIO()
     16          outputfile.tmxfile = tmx.tmxfile(inputfile=None, sourcelanguage=sourcelanguage)
     17          po2tmx.convertpo(inputfile, outputfile, templatefile=None, sourcelanguage=sourcelanguage, targetlanguage=targetlanguage)
     18          return outputfile.tmxfile
     19
     20 -    def test_basic(self):
     21          minipo = """# Afrikaans translation of program ABC
     22  #
     23  msgid ""
     24  msgstr ""
     25  "Project-Id-Version: program 2.1-branch\n"
     26  "Report-Msgid-Bugs-To: \n"
     27  "POT-Creation-Date: 2006-01-09 07:15+0100\n"
     28  "PO-Revision-Date: 2004-03-30 17:02+0200\n"
     29  "Last-Translator: Zuza Software Foundation <xxx@translate.org.za>\n"
     30  "Language-Team: Afrikaans <translate-discuss-xxx@lists.sourceforge.net>\n"
     31  "MIME-Version: 1.0\n"
     32  "Content-Type: text/plain; charset=UTF-8\n"
     33  "Content-Transfer-Encoding: 8bit\n"
     34
     35  # Please remember to do something
     36  #: ../dir/file.xml.in.h:1 ../dir/file2.xml.in.h:4
     37  msgid "Applications"
     38  msgstr "Toepassings"
     39  """
     40          tmx = self.po2tmx(minipo)
     41          print "The generated xml:"
     42          print str(tmx)
     43          assert tmx.translate("Applications") == "Toepassings"
     44          assert tmx.translate("bla") is None
     45          xmltext = str(tmx)
     46          assert xmltext.index('creationtool="Translate Toolkit - po2tmx"')
     47          assert xmltext.index('adminlang')
     48          assert xmltext.index('creationtoolversion')
     49          assert xmltext.index('datatype')
     50          assert xmltext.index('o-tmf')
     51          assert xmltext.index('segtype')
     52          assert xmltext.index('srclang')
     53
     54 -    def test_sourcelanguage(self):
     55          minipo = 'msgid "String"\nmsgstr "String"\n'
     56          tmx = self.po2tmx(minipo, sourcelanguage="xh")
     57          print "The generated xml:"
     58          print str(tmx)
     59          header = tmx.document.find("header")
     60          assert header.get("srclang") == "xh"
     61
     62 -    def test_targetlanguage(self):
     63          minipo = 'msgid "String"\nmsgstr "String"\n'
     64          tmx = self.po2tmx(minipo, targetlanguage="xh")
     65          print "The generated xml:"
     66          print str(tmx)
     67          tuv = tmx.document.findall(".//%s" % tmx.namespaced("tuv"))[1]
     68          #tag[0] will be the source, we want the target tuv
     69          assert tuv.get("{%s}lang" % lisa.XML_NS) == "xh"
     70
     71 -    def test_multiline(self):
     72          """Test multiline po entry"""
     73          minipo = r'''msgid "First part "
     74  "and extra"
     75  msgstr "Eerste deel "
     76  "en ekstra"'''
     77          tmx = self.po2tmx(minipo)
     78          print "The generated xml:"
     79          print str(tmx)
     80          assert tmx.translate('First part and extra') == 'Eerste deel en ekstra'
     81
     82
     83 -    def test_escapednewlines(self):
     84          """Test the escaping of newlines"""
     85          minipo = r'''msgid "First line\nSecond line"
     86  msgstr "Eerste lyn\nTweede lyn"
     87  '''
     88          tmx = self.po2tmx(minipo)
     89          print "The generated xml:"
     90          print str(tmx)
     91          assert tmx.translate("First line\nSecond line") == "Eerste lyn\nTweede lyn"
     92
     93 -    def test_escapedtabs(self):
     94          """Test the escaping of tabs"""
     95          minipo = r'''msgid "First column\tSecond column"
     96  msgstr "Eerste kolom\tTweede kolom"
     97  '''
     98          tmx = self.po2tmx(minipo)
     99          print "The generated xml:"
    100          print str(tmx)
    101          assert tmx.translate("First column\tSecond column") == "Eerste kolom\tTweede kolom"
    102
    103 -    def test_escapedquotes(self):
    104          """Test the escaping of quotes (and slash)"""
    105          minipo = r'''msgid "Hello \"Everyone\""
    106  msgstr "Good day \"All\""
    107
    108  msgid "Use \\\"."
    109  msgstr "Gebruik \\\"."
    110  '''
    111          tmx = self.po2tmx(minipo)
    112          print "The generated xml:"
    113          print str(tmx)
    114          assert tmx.translate('Hello "Everyone"') == 'Good day "All"'
    115          assert tmx.translate(r'Use \".') == r'Gebruik \".'
    116
    117 -    def test_exclusions(self):
    118          """Test that empty and fuzzy messages are excluded"""
    119          minipo = r'''#, fuzzy
    120  msgid "One"
    121  msgstr "Een"
    122
    123  msgid "Two"
    124  msgstr ""
    125
    126  msgid ""
    127  msgstr "Drie"
    128  '''
    129          tmx = self.po2tmx(minipo)
    130          print "The generated xml:"
    131          print str(tmx)
    132          assert "<tu" not in str(tmx)
    133          assert len(tmx.units) == 0
    134
    135 -    def test_nonascii(self):
    136          """Tests that non-ascii conversion works."""
    137          minipo = r'''msgid "Bézier curve"
    138  msgstr "Bézier-kurwe"
    139  '''
    140          tmx = self.po2tmx(minipo)
    141          print str(tmx)
    142          assert tmx.translate(u"Bézier curve") == u"Bézier-kurwe"
    143
    144
    145 -class TestPO2TMXCommand(test_convert.TestConvertCommand, TestPO2TMX):
    146      """Tests running actual po2tmx commands on files"""
    147      convertmodule = po2tmx
    148
    149 -    def test_help(self):
    150          """tests getting help"""
    151          options = test_convert.TestConvertCommand.test_help(self)
    152          options = self.help_check(options, "-l LANG, --language=LANG")
    153          options = self.help_check(options, "--source-language=LANG", last=True)
    154