[Python-checkins] python/dist/src/Lib/test test_gettext.py,1.10,1.11

loewis@users.sourceforge.net loewis@users.sourceforge.net
Thu, 21 Nov 2002 13:45:34 -0800


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv11193/Lib/test

Modified Files:
	test_gettext.py 
Log Message:
Patch #633547: Support plural forms. Do TODOs in test suite.


Index: test_gettext.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_gettext.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** test_gettext.py	20 Jul 2002 00:36:38 -0000	1.10
--- test_gettext.py	21 Nov 2002 21:45:32 -0000	1.11
***************
*** 3,154 ****
  import gettext
  
  
! def test_api_1(localedir, mofile):
!     print 'test api 1'
  
-     # Test basic interface
  
!     print 'installing gettext'
!     gettext.install('gettext', localedir)
  
-     # test some translations
-     print _('albatross')
-     print _(u'mullusk')
-     print _(r'Raymond Luxury Yach-t')
-     print _(ur'nudge nudge')
  
!     # double quotes
!     print _("albatross")
!     print _(u"mullusk")
!     print _(r"Raymond Luxury Yach-t")
!     print _(ur"nudge nudge")
  
!     # triple single quotes
!     print _('''albatross''')
!     print _(u'''mullusk''')
!     print _(r'''Raymond Luxury Yach-t''')
!     print _(ur'''nudge nudge''')
  
!     # triple double quotes
!     print _("""albatross""")
!     print _(u"""mullusk""")
!     print _(r"""Raymond Luxury Yach-t""")
!     print _(ur"""nudge nudge""")
  
!     # multiline strings
!     print _('''This module provides internationalization and localization
  support for your Python programs by providing an interface to the GNU
! gettext message catalog library.''')
  
-     # test the alternative interface
-     fp = open(os.path.join(mofile), 'rb')
-     t = gettext.GNUTranslations(fp)
-     fp.close()
  
!     t.install()
  
!     print _('nudge nudge')
  
!     # try unicode return type
!     t.install(unicode=1)
  
!     print _('mullusk')
  
  
  
! def test_api_2(localedir, mofile):
!     print 'test api 2'
  
!     gettext.bindtextdomain('gettext', localedir)
!     print gettext.bindtextdomain('gettext') == localedir
  
!     gettext.textdomain('gettext')
!     # should return 'gettext'
!     print gettext.textdomain()
  
-     # local function override builtin
-     _ = gettext.gettext
  
!     # test some translations
!     print _('albatross')
!     print _(u'mullusk')
!     print _(r'Raymond Luxury Yach-t')
!     print _(ur'nudge nudge')
  
-     # double quotes
-     print _("albatross")
-     print _(u"mullusk")
-     print _(r"Raymond Luxury Yach-t")
-     print _(ur"nudge nudge")
  
!     # triple single quotes
!     print _('''albatross''')
!     print _(u'''mullusk''')
!     print _(r'''Raymond Luxury Yach-t''')
!     print _(ur'''nudge nudge''')
  
-     # triple double quotes
-     print _("""albatross""")
-     print _(u"""mullusk""")
-     print _(r"""Raymond Luxury Yach-t""")
-     print _(ur"""nudge nudge""")
  
!     # multiline strings
!     print _('''This module provides internationalization and localization
  support for your Python programs by providing an interface to the GNU
! gettext message catalog library.''')
  
-     # Now test dgettext()
-     def _(message):
-         return gettext.dgettext('gettext')
  
  
  
! GNU_MO_DATA = '''\
! 3hIElQAAAAAFAAAAHAAAAEQAAAAHAAAAbAAAAAAAAACIAAAAFQAAAIkAAAChAAAAnwAAAAcAAABB
! AQAACwAAAEkBAAAZAQAAVQEAABYAAABvAgAAoQAAAIYCAAAFAAAAKAMAAAkAAAAuAwAAAQAAAAQA
! AAACAAAAAAAAAAUAAAAAAAAAAwAAAABSYXltb25kIEx1eHVyeSBZYWNoLXQAVGhpcyBtb2R1bGUg
! cHJvdmlkZXMgaW50ZXJuYXRpb25hbGl6YXRpb24gYW5kIGxvY2FsaXphdGlvbgpzdXBwb3J0IGZv
! ciB5b3VyIFB5dGhvbiBwcm9ncmFtcyBieSBwcm92aWRpbmcgYW4gaW50ZXJmYWNlIHRvIHRoZSBH
! TlUKZ2V0dGV4dCBtZXNzYWdlIGNhdGFsb2cgbGlicmFyeS4AbXVsbHVzawBudWRnZSBudWRnZQBQ
! cm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1SZXZpc2lvbi1EYXRlOiAyMDAwLTA4LTI5IDEyOjE5
! LTA0OjAwCkxhc3QtVHJhbnNsYXRvcjogQmFycnkgQS4gV2Fyc2F3IDxiYXJyeUBweXRob24ub3Jn
! PgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpNSU1FLVZlcnNpb246
! IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9a29pOF9yCkNvbnRlbnQtVHJh
! bnNmZXItRW5jb2Rpbmc6IG5vbmUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4xCgBUaHJv
! YXR3b2JibGVyIE1hbmdyb3ZlAEd1dmYgemJxaHlyIGNlYml2cXJmIHZhZ3JlYW5ndmJhbnl2bW5n
! dmJhIG5hcSB5YnBueXZtbmd2YmEKZmhjY2JlZyBzYmUgbGJoZSBDbGd1YmEgY2VidGVuemYgb2wg
! Y2ViaXZxdmF0IG5hIHZhZ3Jlc25wciBnYiBndXIgVEFICnRyZ2dya2cgenJmZm50ciBwbmdueWJ0
! IHl2b2VuZWwuAGJhY29uAHdpbmsgd2luawA=
! '''
  
  
! LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
! MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
  
- def setup():
-     os.makedirs(LOCALEDIR)
-     fp = open(MOFILE, 'wb')
-     fp.write(base64.decodestring(GNU_MO_DATA))
-     fp.close()
-     os.environ['LANGUAGE'] = 'xx'
  
! def teardown():
!     os.environ['LANGUAGE'] = 'en'
!     os.unlink(MOFILE)
!     os.removedirs(LOCALEDIR)
  
  
- try:
-     setup()
-     test_api_1(os.curdir, MOFILE)
-     test_api_2(os.curdir, MOFILE)
- finally:
-     teardown()
-     pass
  
  
  
  # For reference, here's the .po file used to created the .mo data above.
  
  '''
--- 3,283 ----
  import gettext
  
+ import unittest
+ from unittest import TestCase
  
! # TODO:
! #  - Add new tests, for example for "dgettext"
! #  - Remove dummy tests, for example testing for single and double quotes
! #    has no sense, it would have if we were testing a parser (i.e. pygett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ciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
! eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
! IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
! ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
! '''
  
  
! LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
! MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
  
! def setup():
!     os.makedirs(LOCALEDIR)
!     fp = open(MOFILE, 'wb')
!     fp.write(base64.decodestring(GNU_MO_DATA))
!     fp.close()
!     os.environ['LANGUAGE'] = 'xx'
  
! def teardown():
!     os.environ['LANGUAGE'] = 'en'
!     os.unlink(MOFILE)
!     os.removedirs(LOCALEDIR)
  
! 
! class GettextTestCase1(TestCase):
!     def setUp(self):
!         self.localedir = os.curdir
!         self.mofile = MOFILE
! 
!         gettext.install('gettext', self.localedir)
! 
! 
!     def test_some_translations(self):
!         # test some translations
!         assert _('albatross') == 'albatross'
!         assert _(u'mullusk') == 'bacon'
!         assert _(r'Raymond Luxury Yach-t') == 'Throatwobbler Mangrove'
!         assert _(ur'nudge nudge') == 'wink wink'
! 
! 
!     def test_double_quotes(self):
!         # double quotes
!         assert _("albatross") == 'albatross'
!         assert _(u"mullusk") == 'bacon'
!         assert _(r"Raymond Luxury Yach-t") == 'Throatwobbler Mangrove'
!         assert _(ur"nudge nudge") == 'wink wink'
! 
! 
!     def test_triple_single_quotes(self):
!         # triple single quotes
!         assert _('''albatross''') == 'albatross'
!         assert _(u'''mullusk''') == 'bacon'
!         assert _(r'''Raymond Luxury Yach-t''') == 'Throatwobbler Mangrove'
!         assert _(ur'''nudge nudge''') == 'wink wink'
! 
! 
!     def test_triple_double_quotes(self):
!         # triple double quotes
!         assert _("""albatross""") == 'albatross'
!         assert _(u"""mullusk""") == 'bacon'
!         assert _(r"""Raymond Luxury Yach-t""") == 'Throatwobbler Mangrove'
!         assert _(ur"""nudge nudge""") == 'wink wink'
! 
! 
!     def test_multiline_strings(self):
!         # multiline strings
!         assert _('''This module provides internationalization and localization
  support for your Python programs by providing an interface to the GNU
! gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
! fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
! trggrkg zrffntr pngnybt yvoenel.'''
  
  
!     def test_the_alternative_interface(self):
!         # test the alternative interface
!         fp = open(os.path.join(self.mofile), 'rb')
!         t = gettext.GNUTranslations(fp)
!         fp.close()
  
!         t.install()
  
!         assert _('nudge nudge') == 'wink wink'
  
!         # try unicode return type
!         t.install(unicode=1)
  
+         assert _('mullusk') == 'bacon'
  
  
! class GettextTestCase2(TestCase):
!     def setUp(self):
!         self.localedir = os.curdir
  
!         gettext.bindtextdomain('gettext', self.localedir)
!         gettext.textdomain('gettext')
  
!         self._ = gettext.gettext
  
  
!     def test_bindtextdomain(self):
!         assert gettext.bindtextdomain('gettext') == self.localedir
  
  
!     def test_textdomain(self):
!         assert gettext.textdomain() == 'gettext'
  
  
!     def test_some_translations(self):
!         # test some translations
!         assert self._('albatross') == 'albatross'
!         assert self._(u'mullusk') == 'bacon'
!         assert self._(r'Raymond Luxury Yach-t') == 'Throatwobbler Mangrove'
!         assert self._(ur'nudge nudge') == 'wink wink'
! 
! 
!     def test_double_quotes(self):
!         # double quotes
!         assert self._("albatross") == 'albatross'
!         assert self._(u"mullusk") == 'bacon'
!         assert self._(r"Raymond Luxury Yach-t") == 'Throatwobbler Mangrove'
!         assert self._(ur"nudge nudge") == 'wink wink'
! 
! 
!     def test_triple_single_quotes(self):
!         # triple single quotes
!         assert self._('''albatross''') == 'albatross'
!         assert self._(u'''mullusk''') == 'bacon'
!         assert self._(r'''Raymond Luxury Yach-t''') == 'Throatwobbler Mangrove'
!         assert self._(ur'''nudge nudge''') == 'wink wink'
! 
! 
!     def test_triple_double_quotes(self):
!         # triple double quotes
!         assert self._("""albatross""") == 'albatross'
!         assert self._(u"""mullusk""") == 'bacon'
!         assert self._(r"""Raymond Luxury Yach-t""") == 'Throatwobbler Mangrove'
!         assert self._(ur"""nudge nudge""") == 'wink wink'
! 
! 
!     def test_multiline_strings(self):
!         # multiline strings
!         assert self._('''This module provides internationalization and localization
  support for your Python programs by providing an interface to the GNU
! gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
! fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
! trggrkg zrffntr pngnybt yvoenel.'''
  
  
  
  
! class PluralFormsTestCase(TestCase):
!     def setUp(self):
!         self.mofile = MOFILE
  
+     def test_plural_forms1(self):
+         x = gettext.ngettext('There is %s file', 'There are %s files', 1)
+         assert x == 'Hay %s fichero'
  
!         x = gettext.ngettext('There is %s file', 'There are %s files', 2)
!         assert x == 'Hay %s ficheros'
  
  
!     def test_plural_forms2(self):
!         fp = open(os.path.join(self.mofile), 'rb')
!         t = gettext.GNUTranslations(fp)
!         fp.close()
  
+         x = t.ngettext('There is %s file', 'There are %s files', 1)
+         assert x == 'Hay %s fichero'
+ 
+         x = t.ngettext('There is %s file', 'There are %s files', 2)
+         assert x == 'Hay %s ficheros'
+ 
+ 
+     def test_hu(self):
+         f = gettext.c2py('0')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ 
+ 
+     def test_de(self):
+         f = gettext.c2py('n != 1')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
+ 
+ 
+     def test_fr(self):
+         f = gettext.c2py('n>1')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
+ 
+ 
+     def test_gd(self):
+         f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"
+ 
+ 
+     def test_gd2(self):
+         # Tests the combination of parentheses and "?:"
+         f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"
+ 
+ 
+     def test_lt(self):
+         f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111"
+ 
+ 
+     def test_ru(self):
+         f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222"
+ 
+ 
+     def test_pl(self):
+         f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222"
+ 
+ 
+     def test_sl(self):
+         f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
+         s = ''.join([ str(f(x)) for x in range(200) ])
+         assert s == "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"
+ 
+ 
+     def test_security(self):
+         # Test for a dangerous expression
+         try:
+             gettext.c2py("os.chmod('/etc/passwd',0777)")
+         except ValueError:
+             pass
+         else:
+             raise AssertionError
+ 
+ 
+ 
+ if __name__ == '__main__':
+     try:
+         setup()
+         unittest.main()
+     finally:
+         teardown()
  
  
  
  
  # For reference, here's the .po file used to created the .mo data above.
+ #
+ # The original version was automatically generated from the sources with
+ # pygettext. Later it was manually modified to add plural forms support.
  
  '''
***************
*** 161,170 ****
  "Project-Id-Version: 2.0\n"
  "PO-Revision-Date: 2000-08-29 12:19-04:00\n"
! "Last-Translator: Barry A. Warsaw <barry@python.org>\n"
  "Language-Team: XX <python-dev@python.org>\n"
  "MIME-Version: 1.0\n"
! "Content-Type: text/plain; charset=koi8_r\n"
! "Content-Transfer-Encoding: none\n"
  "Generated-By: pygettext.py 1.1\n"
  
  #: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
--- 290,300 ----
  "Project-Id-Version: 2.0\n"
  "PO-Revision-Date: 2000-08-29 12:19-04:00\n"
! "Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
  "Language-Team: XX <python-dev@python.org>\n"
  "MIME-Version: 1.0\n"
! "Content-Type: text/plain; charset=iso-8859-1\n"
! "Content-Transfer-Encoding: 8bit\n"
  "Generated-By: pygettext.py 1.1\n"
+ "Plural-Forms: nplurals=2; plural=n!=1;\n"
  
  #: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
***************
*** 199,201 ****
--- 329,338 ----
  "fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
  "trggrkg zrffntr pngnybt yvoenel."
+ 
+ # Manually added, as neither pygettext nor xgettext support plural forms
+ # in Python.
+ msgid "There is %s file"
+ msgid_plural "There are %s files"
+ msgstr[0] "Hay %s fichero"
+ msgstr[1] "Hay %s ficheros"
  '''