[Zope3-checkins] CVS: Zope3/src/zope/app/services/translation/tests - __init__.py:1.1.2.1 test_gettextexportimport.py:1.1.2.1 test_messagecatalog.py:1.1.2.1 test_translationservice.py:1.1.2.1

Jim Fulton jim@zope.com
Mon, 23 Dec 2002 14:32:30 -0500


Update of /cvs-repository/Zope3/src/zope/app/services/translation/tests
In directory cvs.zope.org:/tmp/cvs-serv19908/zope/app/services/translation/tests

Added Files:
      Tag: NameGeddon-branch
	__init__.py test_gettextexportimport.py test_messagecatalog.py 
	test_translationservice.py 
Log Message:
Initial renaming before debugging

=== Added File Zope3/src/zope/app/services/translation/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/app/services/translation/tests/test_gettextexportimport.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""This module tests the Gettext Export and Import funciotnality of the
Translation Service.

$Id: test_gettextexportimport.py,v 1.1.2.1 2002/12/23 19:32:29 jim Exp $
"""
import unittest, time

from cStringIO import StringIO

from zope.app.tests.placelesssetup import PlacelessSetup
from zope.app.component.metaconfigure import \
     provideService, managerHandler
from zope.app.component.metaconfigure import handler

from zope.app.services.translation.messagecatalog import \
     MessageCatalog 
from zope.i18n.negotiator import negotiator
from zope.interfaces.i18n import INegotiator
from zope.interfaces.i18n import IUserPreferredLanguages

from zope.app.services.translation.translationservice import \
     TranslationService
from zope.app.services.translation.gettextimportfilter import \
     GettextImportFilter
from zope.app.services.translation.gettextexportfilter import \
     GettextExportFilter



class Environment:

    __implements__ = IUserPreferredLanguages

    def __init__(self, langs=()):
        self.langs = langs

    def getPreferredLanguages(self):
        return self.langs


class TestGettextExportImport(PlacelessSetup, unittest.TestCase):


    _data = '''msgid ""
msgstr ""
"Project-Id-Version: Zope 3\\n"
"PO-Revision-Date: %s\\n"
"Last-Translator: Zope 3 Gettext Export Filter\\n"
"Zope-Language: de\\n"
"Zope-Domain: default\\n" 
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=UTF-8\\n"
"Content-Transfer-Encoding: 8bit\\n"

msgid "Choose"
msgstr "Ausw\xc3\xa4hlen!"

msgid "greeting"
msgstr "hallo"
'''

    def setUp(self):
        PlacelessSetup.setUp(self)
        # Setup the negotiator service registry entry
        managerHandler('defineService', 'LanguageNegotiation', INegotiator) 
        provideService('LanguageNegotiation', negotiator, 'Zope.Public')
        self._service = TranslationService('default') 
        handler('Factories', 'provideFactory', 'Message Catalog',
                MessageCatalog)


    def testImportExport(self):
        service = self._service

        imp = GettextImportFilter(service)
        imp.importMessages(['default'], ['de'],
                           StringIO(self._data %'2002/02/02 02:02'))

        exp = GettextExportFilter(service)
        result = exp.exportMessages(['default'], ['de'])
        
        dt = time.time()
        dt = time.localtime(dt)
        dt = time.strftime('%Y/%m/%d %H:%M', dt)

        self.assertEqual(result.strip(), (self._data %dt).strip())


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(TestGettextExportImport)


if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())



=== Added File Zope3/src/zope/app/services/translation/tests/test_messagecatalog.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""Test the generic persistent Message Catalog.

$Id: test_messagecatalog.py,v 1.1.2.1 2002/12/23 19:32:29 jim Exp $
"""
import unittest

from zope.app.services.translation.messagecatalog import \
     MessageCatalog
from zope.i18n.tests.test_ireadmessagecatalog import TestIReadMessageCatalog
from zope.i18n.tests.test_iwritemessagecatalog import TestIWriteMessageCatalog


class MessageCatalogTest(TestIReadMessageCatalog, TestIWriteMessageCatalog):


    def startUp(self):
        TestIReadMessageCatalog.startUp(self)
        TestIWriteMessageCatalog.startUp(self)


    def _getMessageCatalog(self):
        catalog = MessageCatalog('en', 'default')
        catalog.setMessage('short_greeting', 'Hello!', 0)
        catalog.setMessage('greeting', 'Hello $name, how are you?', 0)
        return catalog
    
    def _getUniqueIndentifier(self):
        return ('en', 'default')


def test_suite():
    loader=unittest.TestLoader()
    return loader.loadTestsFromTestCase(MessageCatalogTest)

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/app/services/translation/tests/test_translationservice.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# 
##############################################################################
"""This module tests the regular persistent Translation Service.

$Id: test_translationservice.py,v 1.1.2.1 2002/12/23 19:32:29 jim Exp $
"""
import unittest, sys

from zope.app.component.metaconfigure import handler

from zope.app.services.translation.translationservice import \
     TranslationService
from zope.app.services.translation.messagecatalog import \
     MessageCatalog 
from zope.i18n.tests.test_ireadtranslationservice import \
     TestIReadTranslationService
from zope.i18n.tests.test_iwritetranslationservice import \
     TestIWriteTranslationService
from zope.i18n.tests.test_isynctranslationservice import \
     TestISyncTranslationService


class TestTranslationService(TestIReadTranslationService,
                             TestIWriteTranslationService,
                             TestISyncTranslationService):


    def setUp(self):
        TestISyncTranslationService.setUp(self)
        TestIReadTranslationService.setUp(self)
        TestIWriteTranslationService.setUp(self)
        handler('Factories', 'provideFactory', 'Message Catalog',
                MessageCatalog)


    def _getTranslationService(self):
        service = TranslationService('default') 

        en_catalog = MessageCatalog('en', 'default')
        de_catalog = MessageCatalog('de', 'default')
        # Populate the catalogs with translations of a message id
        en_catalog.setMessage('short_greeting', 'Hello!', 10)
        de_catalog.setMessage('short_greeting', 'Hallo!', 10)
        # And another message id with interpolation placeholders
        en_catalog.setMessage('greeting', 'Hello $name, how are you?', 0)
        de_catalog.setMessage('greeting', 'Hallo $name, wie geht es Dir?', 0)

        service.setObject('en-default-1', en_catalog)
        service.setObject('de-default-1', de_catalog)

        return service

    def testParameterNames(self):
        service = self._service
        translate = service.translate
        eq = self.assertEqual
        raises = self.assertRaises
        # Test that the second argument is called `msgid'
        eq(translate('default', msgid='short_greeting', target_language='en'),
           'Hello!')
        # This is what the argument used to be called
        raises(TypeError, translate, 'default', source='short_greeting',
               target_language='en')


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(TestTranslationService)


if __name__ == '__main__':
    unittest.TextTestRunner().run(test_suite())