Package translate :: Package storage :: Module test_mo

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.test_mo

      1  #!/usr/bin/env python
      2
      3  import StringIO
      4  import subprocess
      5  import os.path
      6
      7  from translate.storage import test_base
      8  from translate.storage import mo
      9  from translate.storage import factory
     10
     11 -class TestMOUnit(test_base.TestTranslationUnit):
     12      UnitClass = mo.mounit
     13
     14  posources = [
     15  r'''
     16  msgid ""
     17  msgstr ""
     18  "PO-Revision-Date: 2006-02-09 23:33+0200\n"
     19  "MIME-Version: 1.0\n"
     20  "Content-Type: text/plain; charset=UTF-8\n"
     21  "Content-Transfer-Encoding: 8-bit\n"
     22  ''',
     23  r'''
     24  msgid ""
     25  msgstr ""
     26  "PO-Revision-Date: 2006-02-09 23:33+0200\n"
     27  "MIME-Version: 1.0\n"
     28  "Content-Type: text/plain; charset=UTF-8\n"
     29  "Content-Transfer-Encoding: 8-bit\n"
     30
     31  msgid "plant"
     32  msgstr ""
     33  ''',
     34  # The following test is commented out, because the hash-size is different
     35  # compared to gettext, since we're not counting untranslated units.
     36  #r'''
     37  #msgid ""
     38  #msgstr ""
     39  #"PO-Revision-Date: 2006-02-09 23:33+0200\n"
     40  #"MIME-Version: 1.0\n"
     41  #"Content-Type: text/plain; charset=UTF-8\n"
     42  #"Content-Transfer-Encoding: 8-bit\n"
     43  #
     44  #msgid "plant"
     45  #msgstr ""
     46  #
     47  #msgid ""
     48  #"_: Noun\n"
     49  #"convert"
     50  #msgstr "bekeerling"
     51  #''',
     52  r'''
     53  msgid ""
     54  msgstr ""
     55  "PO-Revision-Date: 2006-02-09 23:33+0200\n"
     56  "MIME-Version: 1.0\n"
     57  "Content-Type: text/plain; charset=UTF-8\n"
     58  "Content-Transfer-Encoding: 8-bit\n"
     59
     60  msgid "plant"
     61  msgstr ""
     62
     63  msgid ""
     64  "_: Noun\n"
     65  "convert"
     66  msgstr "bekeerling"
     67
     68  msgctxt "verb"
     69  msgid ""
     70  "convert"
     71  msgstr "omskakel"
     72  ''',
     73  r'''
     74  msgid ""
     75  msgstr ""
     76  "PO-Revision-Date: 2006-02-09 23:33+0200\n"
     77  "MIME-Version: 1.0\n"
     78  "Content-Type: text/plain; charset=UTF-8\n"
     79  "Content-Transfer-Encoding: 8-bit\n"
     80
     81  msgid "plant"
     82  msgstr ""
     83
     84  msgid ""
     85  "_: Noun\n"
     86  "convert"
     87  msgstr "bekeerling"
     88
     89  msgctxt "verb"
     90  msgid ""
     91  "convert"
     92  msgstr "omskakel"
     93
     94  msgid "tree"
     95  msgid_plural "trees"
     96  msgstr[0] ""
     97  ''']
     98
     99 -class TestMOFile(test_base.TestTranslationStore):
    100      StoreClass = mo.mofile
    101
    102 -    def get_mo_and_po(self):
    103          return (os.path.abspath(self.filename + '.po'),
    104                  os.path.abspath(self.filename + '.msgfmt.mo'),
    105                  os.path.abspath(self.filename + '.pocompile.mo'))
    106
    107 -    def remove_po_and_mo(self):
    108          for file in self.get_mo_and_po():
    109              if os.path.exists(file):
    110                  os.remove(file)
    111
    112 -    def setup_method(self, method):
    113          test_base.TestTranslationStore.setup_method(self, method)
    114          self.remove_po_and_mo()
    115
    116 -    def teardown_method(self, method):
    117          test_base.TestTranslationStore.teardown_method(self, method)
    118          self.remove_po_and_mo()
    119
    120 -    def test_output(self):
    121          for posource in posources:
    122              print "PO source file"
    123              print posource
    124              PO_FILE, MO_MSGFMT, MO_POCOMPILE = self.get_mo_and_po()
    125
    126              out_file = open(PO_FILE, 'w')
    127              out_file.write(posource)
    128              out_file.close()
    129
    130              subprocess.call(['msgfmt', PO_FILE, '-o', MO_MSGFMT])
    131              subprocess.call(['pocompile', PO_FILE, MO_POCOMPILE])
    132
    133              store = factory.getobject(StringIO.StringIO(posource))
    134              if store.isempty() and not os.path.exists(MO_POCOMPILE):
    135                  # pocompile doesn't create MO files for empty PO files, so we
    136                  # can skip the checks here.
    137                  continue
    138
    139              mo_msgfmt_f = open(MO_MSGFMT)
    140              mo_pocompile_f = open(MO_POCOMPILE)
    141
    142              try:
    143                  mo_msgfmt = mo_msgfmt_f.read()
    144                  print "msgfmt output:"
    145                  print repr(mo_msgfmt)
    146                  mo_pocompile = mo_pocompile_f.read()
    147                  print "pocompile output:"
    148                  print repr(mo_pocompile)
    149                  assert mo_msgfmt == mo_pocompile
    150              finally:
    151                  mo_msgfmt_f.close()
    152                  mo_pocompile_f.close()
    153