Package translate :: Package storage :: Module test_tmx

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.test_tmx

     1  #!/usr/bin/env python
     2
     3  from translate.storage import tmx
     4  from translate.storage import test_base
     5  from translate.misc import wStringIO
     6
     7 -class TestTMXUnit(test_base.TestTranslationUnit):
     8      UnitClass = tmx.tmxunit
     9
    10 -class TestTMXUnitFromParsedString(TestTMXUnit):
    11      tmxsource = '''<?xml version="1.0" encoding="utf-8"?>
    12  <!DOCTYPE tmx
    13    SYSTEM 'tmx14.dtd'>
    14  <tmx version="1.4">
    15          <header adminlang="en" creationtool="Translate Toolkit - po2tmx" creationtoolversion="1.0beta" datatype="PlainText" o-tmf="UTF-8" segtype="sentence" srclang="en"/>
    16          <body>
    17                  <tu>
    18                          <tuv xml:lang="en">
    19                                  <seg>Test String</seg>
    20                          </tuv>
    21                  </tu>
    22          </body>
    23  </tmx>'''
    24
    25 -    def setup_method(self, method):
    26          store = tmx.tmxfile.parsestring(self.tmxsource)
    27          self.unit = store.units[0]
    28
    29 -class TestTMXfile(test_base.TestTranslationStore):
    30      StoreClass = tmx.tmxfile
    31
    32 -    def tmxparse(self, tmxsource):
    33          """helper that parses tmx source without requiring files"""
    34          dummyfile = wStringIO.StringIO(tmxsource)
    35          print tmxsource
    36          tmxfile = tmx.tmxfile(dummyfile)
    37          return tmxfile
    38
    39 -    def test_translate(self):
    40          tmxfile = tmx.tmxfile()
    41          assert tmxfile.translate("Anything") is None
    42          tmxfile.addtranslation("A string of characters", "en", "'n String karakters", "af")
    43          assert tmxfile.translate("A string of characters") == "'n String karakters"
    44
    45 -    def test_addtranslation(self):
    46          """tests that addtranslation() stores strings correctly"""
    47          tmxfile = tmx.tmxfile()
    48          tmxfile.addtranslation("A string of characters", "en", "'n String karakters", "af")
    49          newfile = self.tmxparse(str(tmxfile))
    50          print str(tmxfile)
    51          assert newfile.translate("A string of characters") == "'n String karakters"
    52
    53 -    def test_withnewlines(self):
    54          """test addtranslation() with newlines"""
    55          tmxfile = tmx.tmxfile()
    56          tmxfile.addtranslation("First line\nSecond line", "en", "Eerste lyn\nTweede lyn", "af")
    57          newfile = self.tmxparse(str(tmxfile))
    58          print str(tmxfile)
    59          assert newfile.translate("First line\nSecond line") == "Eerste lyn\nTweede lyn"
    60
    61 -    def test_xmlentities(self):
    62          """Test that the xml entities '&' and '<'  are escaped correctly"""
    63          tmxfile = tmx.tmxfile()
    64          tmxfile.addtranslation("Mail & News", "en", "Nuus & pos", "af")
    65          tmxfile.addtranslation("Five < ten", "en", "Vyf < tien", "af")
    66          xmltext = str(tmxfile)
    67          print "The generated xml:"
    68          print xmltext
    69          assert tmxfile.translate('Mail & News') == 'Nuus & pos'
    70          assert xmltext.index('Mail &amp; News')
    71          assert xmltext.find('Mail & News') == -1
    72          assert tmxfile.translate('Five < ten') == 'Vyf < tien'
    73          assert xmltext.index('Five &lt; ten')
    74          assert xmltext.find('Five < ten') == -1
    75