devtools/test/unittest_i18n.py
branchstable
changeset 9153 bc1b8e77d6ce
child 9200 1ba5961b19dd
equal deleted inserted replaced
9152:b0155bfd4e17 9153:bc1b8e77d6ce
       
     1 # -*- coding: iso-8859-1 -*-
       
     2 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     4 #
       
     5 # This file is part of CubicWeb.
       
     6 #
       
     7 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     8 # terms of the GNU Lesser General Public License as published by the Free
       
     9 # Software Foundation, either version 2.1 of the License, or (at your option)
       
    10 # any later version.
       
    11 #
       
    12 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    14 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    15 # details.
       
    16 #
       
    17 # You should have received a copy of the GNU Lesser General Public License along
       
    18 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    19 """unit tests for i18n messages generator"""
       
    20 
       
    21 import os, os.path as osp
       
    22 import sys
       
    23 
       
    24 from logilab.common.testlib import TestCase, unittest_main
       
    25 
       
    26 from cubicweb.cwconfig import CubicWebNoAppConfiguration
       
    27 
       
    28 DATADIR = osp.join(osp.abspath(osp.dirname(__file__)), 'data')
       
    29 
       
    30 def load_po(fname):
       
    31     msgs = []
       
    32     msgid = None
       
    33     msgctxt = None
       
    34     for line in open(fname):
       
    35         if line.strip() in ('', '#'):
       
    36             continue
       
    37         if line.startswith('msgstr'):
       
    38             msgs.append((msgid, msgctxt))
       
    39             msgid = None
       
    40             msgctxt = None
       
    41         elif line.startswith('msgid'):
       
    42             msgid = line.split(' ', 1)[1][1:-1]
       
    43         elif line.startswith('msgctx'):
       
    44             msgctxt = line.split(' ', 1)[1][1: -1]
       
    45 
       
    46         else:
       
    47             if msgctxt is not None:
       
    48                 msgctxt += line[1:-1]
       
    49             elif msgid is not None:
       
    50                 msgid += line[1:-1]
       
    51     return set(msgs)
       
    52 
       
    53 class cubePotGeneratorTC(TestCase):
       
    54     """test case for i18n pot file generator"""
       
    55     def setUp(self):
       
    56         self._CUBES_PATH = CubicWebNoAppConfiguration.CUBES_PATH[:]
       
    57         CubicWebNoAppConfiguration.CUBES_PATH.append(osp.join(DATADIR, 'cubes'))
       
    58         CubicWebNoAppConfiguration.cls_adjust_sys_path()
       
    59 
       
    60     def tearDown(self):
       
    61         CubicWebNoAppConfiguration.CUBES_PATH[:] = self._CUBES_PATH
       
    62 
       
    63     def test_i18ncube(self):
       
    64         # MUST import here to make, since the import statement fire
       
    65         # the cube paths setup (and then must occur after the setUp)
       
    66         from cubicweb.devtools.devctl import update_cube_catalogs
       
    67         cube = osp.join(DATADIR, 'cubes', 'i18ntestcube')
       
    68         msgs = load_po(osp.join(cube, 'i18n', 'en.po.ref'))
       
    69         update_cube_catalogs(cube)
       
    70         newmsgs = load_po(osp.join(cube, 'i18n', 'en.po'))
       
    71         self.assertEqual(msgs, newmsgs)
       
    72 
       
    73 if __name__ == '__main__':
       
    74     unittest_main()