[Zope3-checkins] CVS: Zope3/src/zope/i18n/locales/tests - __init__.py:1.1 test_docstrings.py:1.1 test_locales.py:1.1 test_xmlfactory.py:1.1

Stephan Richter srichter at cosmos.phy.tufts.edu
Thu Feb 5 17:52:33 EST 2004


Update of /cvs-repository/Zope3/src/zope/i18n/locales/tests
In directory cvs.zope.org:/tmp/cvs-serv7674/src/zope/i18n/locales/tests

Added Files:
	__init__.py test_docstrings.py test_locales.py 
	test_xmlfactory.py 
Log Message:
ICU joined openi18n.org and developed a locale data XML standard called 
LDML. A result of this was that all locale files were changed to this new 
format. To stay up-to-date with the latest data, I converted the locale
parser to read the new format. While I was at it, I also changed the Locale
data structure a bit, since I wanted to keep the objects similar to the 
XML. Furthermore, the first time around I did not implement the inheritance
of object attributes and dictionaries correctly (I just faked it for the 
API calls), so I think I got it right this time. Finally I updated views 
and tests that relied on the old Locale API. Be aware that you might need
to change some of your product code as well. 


=== Added File Zope3/src/zope/i18n/locales/tests/__init__.py ===
# Test package for locales


=== Added File Zope3/src/zope/i18n/locales/tests/test_docstrings.py ===
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Tests for the ZCML Documentation Module

$Id: test_docstrings.py,v 1.1 2004/02/05 22:52:32 srichter Exp $
"""
import unittest
from zope.testing.doctestunit import DocTestSuite
from zope.i18n.locales.inheritance import AttributeInheritance, NoParentException

class LocaleInheritanceStub(AttributeInheritance):

    def __init__(self, nextLocale=None):
        self.__nextLocale__ = nextLocale

    def getInheritedSelf(self):
        if self.__nextLocale__ is None:
            raise NoParentException, 'No parent was specified.'
        return self.__nextLocale__


def test_suite():
    return unittest.TestSuite((
        DocTestSuite('zope.i18n.locales'),
        DocTestSuite('zope.i18n.locales.inheritance'),
        DocTestSuite('zope.i18n.locales.xmlfactory'),
        ))

if __name__ == '__main__':
    unittest.main()


=== Added File Zope3/src/zope/i18n/locales/tests/test_locales.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 LocaleProvider and everything that goes with it.

$Id: test_locales.py,v 1.1 2004/02/05 22:52:32 srichter Exp $
"""
import os
import datetime
from unittest import TestCase, TestSuite, makeSuite

from zope.i18n.interfaces.locales import ILocaleProvider
from zope.i18n.locales import locales
from zope.i18n.locales.provider import LocaleProvider, LoadLocaleError

import zope.i18n
datadir = os.path.join(os.path.dirname(zope.i18n.__file__), 'locales', 'data')

class TestILocaleProvider(TestCase):
    """Test the functionality of an implmentation of the ILocaleProvider
    interface."""

    def _makeNewProvider(self):
        raise NotImplemented

    def setUp(self):
        self.locales = self._makeNewProvider()

    def testInterfaceConformity(self):
        self.assert_(ILocaleProvider.isImplementedBy(self.locales))

    def test_getLocale(self):
        locale = self.locales.getLocale(None, None, None)
        self.assertEqual(locale.id.language, None)
        self.assertEqual(locale.id.territory, None)
        self.assertEqual(locale.id.variant, None)

        locale = self.locales.getLocale('en', None, None)
        self.assertEqual(locale.id.language, 'en')
        self.assertEqual(locale.id.territory, None)
        self.assertEqual(locale.id.variant, None)

        locale = self.locales.getLocale('en', 'US', None)
        self.assertEqual(locale.id.language, 'en')
        self.assertEqual(locale.id.territory, 'US')
        self.assertEqual(locale.id.variant, None)

        locale = self.locales.getLocale('en', 'US', 'POSIX')
        self.assertEqual(locale.id.language, 'en')
        self.assertEqual(locale.id.territory, 'US')
        self.assertEqual(locale.id.variant, 'POSIX')


class TestLocaleProvider(TestILocaleProvider):

    def _makeNewProvider(self):
        return LocaleProvider(datadir)

    def test_loadLocale(self):
        self.locales.loadLocale(None, None, None)
        self.assertEqual(self.locales._locales.keys(), [(None, None, None)])

        self.locales.loadLocale('en', None, None)
        self.assert_(('en', None, None) in self.locales._locales.keys())

    def test_loadLocaleFailure(self):
        self.assertRaises(LoadLocaleError, self.locales.loadLocale, 'xxx')


class TestLocaleAndProvider(TestCase):

    # Set the locale on the class so that test cases don't have
    # to pay to construct a new one each time.

    locales.loadLocale(None, None, None)
    locales.loadLocale('en', None, None)
    locales.loadLocale('en', 'US', None)
    locales.loadLocale('en', 'US', 'POSIX')
    locale = locales.getLocale('en', 'US', 'POSIX')

    def test_getTimeFormatter(self):
        formatter = self.locale.dates.getFormatter('time', 'medium')
        self.assertEqual(formatter.getPattern(), 'h:mm:ss a')
        self.assertEqual(formatter.format(datetime.time(12, 30, 10)),
                         '12:30:10 PM')
        self.assertEqual(formatter.parse('12:30:10 PM'),
                         datetime.time(12, 30, 10))

    def test_getDateFormatter(self):
        formatter = self.locale.dates.getFormatter('date', 'medium')
        self.assertEqual(formatter.getPattern(), 'MMM d, yyyy')
        self.assertEqual(formatter.format(datetime.date(2003, 01, 02)),
                         'Jan 2, 2003')
        self.assertEqual(formatter.parse('Jan 2, 2003'),
                         datetime.date(2003, 01, 02))

    def test_getDateTimeFormatter(self):
        formatter = self.locale.dates.getFormatter('dateTime', 'medium')
        self.assertEqual(formatter.getPattern(), 'MMM d, yyyy h:mm:ss a')
        self.assertEqual(
            formatter.format(datetime.datetime(2003, 01, 02, 12, 30)),
            'Jan 2, 2003 12:30:00 PM')
        self.assertEqual(formatter.parse('Jan 2, 2003 12:30:00 PM'),
                         datetime.datetime(2003, 01, 02, 12, 30))

    def test_getNumberFormatter(self):
        formatter = self.locale.numbers.getFormatter('decimal')
        self.assertEqual(formatter.getPattern(), '###0.###;-###0.###')
        self.assertEqual(formatter.format(1234.5678), '1234.567')
        self.assertEqual(formatter.format(-1234.5678), '-1234.567')
        self.assertEqual(formatter.parse('1234.567'), 1234.567)
        self.assertEqual(formatter.parse('-1234.567'), -1234.567)


class TestGlobalLocaleProvider(TestCase):

    def testLoading(self):
        locales.loadLocale(None, None, None)
        self.assert_(locales._locales.has_key((None, None, None)))
        locales.loadLocale('en', None, None)
        self.assert_(locales._locales.has_key(('en', None, None)))
        locales.loadLocale('en', 'US', None)
        self.assert_(locales._locales.has_key(('en', 'US', None)))
        locales.loadLocale('en', 'US', 'POSIX')
        self.assert_(locales._locales.has_key(('en', 'US', 'POSIX')))

    def test_getLocale(self):
        locale = locales.getLocale('en', 'GB')
        self.assertEqual(locale.id.language, 'en')
        self.assertEqual(locale.id.territory, 'GB')
        self.assertEqual(locale.id.variant, None)


def test_suite():
    return TestSuite((
        makeSuite(TestLocaleProvider),
        makeSuite(TestLocaleAndProvider),
        makeSuite(TestGlobalLocaleProvider),
        ))


=== Added File Zope3/src/zope/i18n/locales/tests/test_xmlfactory.py ===
##############################################################################
#
# Copyright (c) 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 PARTLAR PURPOSE.
#
##############################################################################
"""Testing all XML Locale functionality.

$Id: test_xmlfactory.py,v 1.1 2004/02/05 22:52:32 srichter Exp $
"""
import os
from unittest import TestCase, TestSuite, makeSuite

from zope.i18n.locales.xmlfactory import LocaleFactory
from zope.i18n.format import parseDateTimePattern, parseNumberPattern
import zope.i18n

class LocaleXMLFileTestCase(TestCase):
    """This test verifies that every locale XML file can be loaded."""

    def __init__(self, path):
        self.__path = path
        TestCase.__init__(self)
        
    def runTest(self):
        # Loading Locale object 
        locale = LocaleFactory(self.__path)()

        # Making sure all number format patterns parse
        for category in (u'decimal', u'scientific', u'percent', u'currency'):
            for length in getattr(locale.numbers, category+'Formats').values():
                for format in length.formats.values():
                    self.assert_(parseNumberPattern(format.pattern) is not None)

        # Making sure all datetime patterns parse
        for calendar in locale.dates.calendars.values():
            for category in ('date', 'time', 'dateTime'):
                for length in getattr(calendar, category+'Formats').values():
                    for format in length.formats.values():
                        self.assert_(
                            parseDateTimePattern(format.pattern) is not None)
                
                    

##def test_suite():
##    suite = TestSuite()
##    locale_dir = os.path.join(os.path.dirname(zope.i18n.__file__),
##                              'locales', 'data')
##    for path in os.listdir(locale_dir):
##        if not path.endswith(".xml"):
##            continue
##        path = os.path.join(locale_dir, path)
##        case = LocaleXMLFileTestCase(path)
##        suite.addTest(case)
##    return suite

# Note: These tests are disabled, just because they take a long time to run.
#       You should run these tests if you update the parsing code and/or
#       update the Locale XML Files.
def test_suite():
    return None




More information about the Zope3-Checkins mailing list