blob: d667819ec2a079594be177ceaabf1c31fa8b8524 [file] [log] [blame]
Barry Warsaw9e035922000-08-25 19:14:25 +00001import os
Barry Warsawff6d8132000-08-25 19:50:38 +00002import base64
Barry Warsawa1ce93f2003-04-11 18:36:43 +00003import shutil
Barry Warsaw9e035922000-08-25 19:14:25 +00004import gettext
Martin v. Löwisd8996052002-11-21 21:45:32 +00005import unittest
Barry Warsawa1ce93f2003-04-11 18:36:43 +00006
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Barry Warsawa1ce93f2003-04-11 18:36:43 +00008
Fred Drake004d5e62000-10-23 17:22:08 +00009
Martin v. Löwisd8996052002-11-21 21:45:32 +000010# TODO:
11# - Add new tests, for example for "dgettext"
12# - Remove dummy tests, for example testing for single and double quotes
13# has no sense, it would have if we were testing a parser (i.e. pygettext)
14# - Tests should have only one assert.
Barry Warsaw64dab462000-08-30 03:32:07 +000015
Guido van Rossum39478e82007-08-27 17:23:59 +000016GNU_MO_DATA = b'''\
Martin v. Löwisd8996052002-11-21 21:45:32 +0000173hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
18AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
19AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
20eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
21aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
22CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
23Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
24ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
25MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
26YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN
27SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4
28NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0
29ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0
30d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
31eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
32IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
33ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
Barry Warsawff6d8132000-08-25 19:50:38 +000034'''
35
Guido van Rossum39478e82007-08-27 17:23:59 +000036UMO_DATA = b'''\
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000373hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABAAAAFEAAAAPAQAAVgAAAAQAAABm
38AQAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAYWLDngBQcm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1S
39ZXZpc2lvbi1EYXRlOiAyMDAzLTA0LTExIDEyOjQyLTA0MDAKTGFzdC1UcmFuc2xhdG9yOiBCYXJy
40eSBBLiBXQXJzYXcgPGJhcnJ5QHB5dGhvbi5vcmc+Ckxhbmd1YWdlLVRlYW06IFhYIDxweXRob24t
41ZGV2QHB5dGhvbi5vcmc+Ck1JTUUtVmVyc2lvbjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFp
42bjsgY2hhcnNldD11dGYtOApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0CkdlbmVyYXRl
43ZC1CeTogbWFudWFsbHkKAMKkeXoA
44'''
Fred Drake004d5e62000-10-23 17:22:08 +000045
Guido van Rossum39478e82007-08-27 17:23:59 +000046MMO_DATA = b'''\
Barry Warsawe960e222003-05-20 17:28:54 +0000473hIElQAAAAABAAAAHAAAACQAAAADAAAALAAAAAAAAAA4AAAAeAEAADkAAAABAAAAAAAAAAAAAAAA
48UHJvamVjdC1JZC1WZXJzaW9uOiBObyBQcm9qZWN0IDAuMApQT1QtQ3JlYXRpb24tRGF0ZTogV2Vk
49IERlYyAxMSAwNzo0NDoxNSAyMDAyClBPLVJldmlzaW9uLURhdGU6IDIwMDItMDgtMTQgMDE6MTg6
50NTgrMDA6MDAKTGFzdC1UcmFuc2xhdG9yOiBKb2huIERvZSA8amRvZUBleGFtcGxlLmNvbT4KSmFu
51ZSBGb29iYXIgPGpmb29iYXJAZXhhbXBsZS5jb20+Ckxhbmd1YWdlLVRlYW06IHh4IDx4eEBleGFt
52cGxlLmNvbT4KTUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFy
53c2V0PWlzby04ODU5LTE1CkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IHF1b3RlZC1wcmludGFi
54bGUKR2VuZXJhdGVkLUJ5OiBweWdldHRleHQucHkgMS4zCgA=
55'''
56
Barry Warsawff6d8132000-08-25 19:50:38 +000057LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
58MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
Barry Warsawa1ce93f2003-04-11 18:36:43 +000059UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo')
Barry Warsawe960e222003-05-20 17:28:54 +000060MMOFILE = os.path.join(LOCALEDIR, 'metadata.mo')
Barry Warsawff6d8132000-08-25 19:50:38 +000061
62
Barry Warsawa1ce93f2003-04-11 18:36:43 +000063class GettextBaseTest(unittest.TestCase):
Martin v. Löwisd8996052002-11-21 21:45:32 +000064 def setUp(self):
Neal Norwitza0e0cd32004-07-19 00:08:59 +000065 if not os.path.isdir(LOCALEDIR):
66 os.makedirs(LOCALEDIR)
Barry Warsawa1ce93f2003-04-11 18:36:43 +000067 fp = open(MOFILE, 'wb')
Georg Brandl706824f2009-06-04 09:42:55 +000068 fp.write(base64.decodebytes(GNU_MO_DATA))
Barry Warsawa1ce93f2003-04-11 18:36:43 +000069 fp.close()
70 fp = open(UMOFILE, 'wb')
Georg Brandl706824f2009-06-04 09:42:55 +000071 fp.write(base64.decodebytes(UMO_DATA))
Barry Warsawa1ce93f2003-04-11 18:36:43 +000072 fp.close()
Barry Warsawe960e222003-05-20 17:28:54 +000073 fp = open(MMOFILE, 'wb')
Georg Brandl706824f2009-06-04 09:42:55 +000074 fp.write(base64.decodebytes(MMO_DATA))
Barry Warsawe960e222003-05-20 17:28:54 +000075 fp.close()
Walter Dörwald155374d2009-05-01 19:58:58 +000076 self.env = support.EnvironmentVarGuard()
77 self.env['LANGUAGE'] = 'xx'
Barry Warsawa1ce93f2003-04-11 18:36:43 +000078
79 def tearDown(self):
Walter Dörwald155374d2009-05-01 19:58:58 +000080 self.env.__exit__()
81 del self.env
Brett Cannon4aebbb02003-04-27 19:42:41 +000082 shutil.rmtree(os.path.split(LOCALEDIR)[0])
Barry Warsawa1ce93f2003-04-11 18:36:43 +000083
84
85class GettextTestCase1(GettextBaseTest):
86 def setUp(self):
87 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +000088 self.localedir = os.curdir
89 self.mofile = MOFILE
Martin v. Löwisd8996052002-11-21 21:45:32 +000090 gettext.install('gettext', self.localedir)
91
Martin v. Löwisd8996052002-11-21 21:45:32 +000092 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +000093 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +000094 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +000095 eq(_('albatross'), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +000096 eq(_('mullusk'), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +000097 eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +000098 eq(_(r'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +000099
100 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000101 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000102 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000103 eq(_("albatross"), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000104 eq(_("mullusk"), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000105 eq(_(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000106 eq(_(r"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000107
108 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000109 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000110 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000111 eq(_('''albatross'''), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000112 eq(_('''mullusk'''), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000113 eq(_(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000114 eq(_(r'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000115
116 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000117 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000118 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000119 eq(_("""albatross"""), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000120 eq(_("""mullusk"""), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000121 eq(_(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000122 eq(_(r"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000123
124 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000125 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000126 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000127 eq(_('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000128support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000129gettext message catalog library.'''),
130 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000131fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000132trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000133
134 def test_the_alternative_interface(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000135 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000136 # test the alternative interface
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000137 fp = open(self.mofile, 'rb')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000138 t = gettext.GNUTranslations(fp)
139 fp.close()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000140 # Install the translation object
Martin v. Löwisd8996052002-11-21 21:45:32 +0000141 t.install()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000142 eq(_('nudge nudge'), 'wink wink')
143 # Try unicode return type
Benjamin Peterson801844d2008-07-14 14:32:15 +0000144 t.install()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000145 eq(_('mullusk'), 'bacon')
Georg Brandl602b9ba2006-02-19 13:26:36 +0000146 # Test installation of other methods
Georg Brandl1a3284e2007-12-02 09:40:06 +0000147 import builtins
Benjamin Peterson801844d2008-07-14 14:32:15 +0000148 t.install(names=["gettext", "lgettext"])
149 eq(_, t.gettext)
150 eq(builtins.gettext, t.gettext)
Georg Brandl602b9ba2006-02-19 13:26:36 +0000151 eq(lgettext, t.lgettext)
Georg Brandl1a3284e2007-12-02 09:40:06 +0000152 del builtins.gettext
153 del builtins.lgettext
Martin v. Löwisd8996052002-11-21 21:45:32 +0000154
155
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000156class GettextTestCase2(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000157 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000158 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000159 self.localedir = os.curdir
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000160 # Set up the bindings
Martin v. Löwisd8996052002-11-21 21:45:32 +0000161 gettext.bindtextdomain('gettext', self.localedir)
162 gettext.textdomain('gettext')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000163 # For convenience
Martin v. Löwisd8996052002-11-21 21:45:32 +0000164 self._ = gettext.gettext
165
Martin v. Löwisd8996052002-11-21 21:45:32 +0000166 def test_bindtextdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000167 self.assertEqual(gettext.bindtextdomain('gettext'), self.localedir)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000168
169 def test_textdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000170 self.assertEqual(gettext.textdomain(), 'gettext')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000171
172 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000173 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000174 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000175 eq(self._('albatross'), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000176 eq(self._('mullusk'), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000177 eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000178 eq(self._(r'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000179
180 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000181 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000182 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000183 eq(self._("albatross"), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000184 eq(self._("mullusk"), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000185 eq(self._(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000186 eq(self._(r"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000187
188 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000189 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000190 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000191 eq(self._('''albatross'''), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000192 eq(self._('''mullusk'''), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000193 eq(self._(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000194 eq(self._(r'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000195
196 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000197 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000198 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000199 eq(self._("""albatross"""), 'albatross')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000200 eq(self._("""mullusk"""), 'bacon')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000201 eq(self._(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000202 eq(self._(r"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000203
204 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000205 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000206 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000207 eq(self._('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000208support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000209gettext message catalog library.'''),
210 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000211fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000212trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000213
214
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000215class PluralFormsTestCase(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000216 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000217 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000218 self.mofile = MOFILE
219
220 def test_plural_forms1(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000221 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000222 x = gettext.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000223 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000224 x = gettext.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000225 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000226
227 def test_plural_forms2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000228 eq = self.assertEqual
229 fp = open(self.mofile, 'rb')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000230 t = gettext.GNUTranslations(fp)
231 fp.close()
Martin v. Löwisd8996052002-11-21 21:45:32 +0000232 x = t.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000233 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000234 x = t.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000235 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000236
237 def test_hu(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000238 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000239 f = gettext.c2py('0')
240 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000241 eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000242
243 def test_de(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000244 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000245 f = gettext.c2py('n != 1')
246 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000247 eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000248
249 def test_fr(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000250 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000251 f = gettext.c2py('n>1')
252 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000253 eq(s, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000254
255 def test_gd(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000256 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000257 f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2')
258 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000259 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000260
261 def test_gd2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000262 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000263 # Tests the combination of parentheses and "?:"
264 f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
265 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000266 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000267
268 def test_lt(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000269 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000270 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
271 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000272 eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000273
274 def test_ru(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000275 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000276 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
277 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000278 eq(s, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000279
280 def test_pl(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000281 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000282 f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
283 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000284 eq(s, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000285
286 def test_sl(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000287 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000288 f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
289 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000290 eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000291
292 def test_security(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000293 raises = self.assertRaises
Martin v. Löwisd8996052002-11-21 21:45:32 +0000294 # Test for a dangerous expression
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000295 raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000296
297
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000298class UnicodeTranslationsTest(GettextBaseTest):
299 def setUp(self):
300 GettextBaseTest.setUp(self)
301 fp = open(UMOFILE, 'rb')
302 try:
Barry Warsawedb155f2003-04-24 18:08:13 +0000303 self.t = gettext.GNUTranslations(fp)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000304 finally:
305 fp.close()
Benjamin Peterson801844d2008-07-14 14:32:15 +0000306 self._ = self.t.gettext
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000307
308 def test_unicode_msgid(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000309 unless = self.assertTrue
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000310 unless(isinstance(self._(''), str))
311 unless(isinstance(self._(''), str))
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000312
313 def test_unicode_msgstr(self):
314 eq = self.assertEqual
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000315 eq(self._('ab\xde'), '\xa4yz')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000316
317
Barry Warsawe960e222003-05-20 17:28:54 +0000318class WeirdMetadataTest(GettextBaseTest):
319 def setUp(self):
320 GettextBaseTest.setUp(self)
321 fp = open(MMOFILE, 'rb')
322 try:
323 try:
324 self.t = gettext.GNUTranslations(fp)
325 except:
326 self.tearDown()
327 raise
328 finally:
329 fp.close()
330
331 def test_weird_metadata(self):
332 info = self.t.info()
Christian Heimes6ae5d7f2007-10-31 18:53:44 +0000333 self.assertEqual(len(info), 9)
Barry Warsawe960e222003-05-20 17:28:54 +0000334 self.assertEqual(info['last-translator'],
335 'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
336
337
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000338def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000339 support.run_unittest(__name__)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000340
341if __name__ == '__main__':
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000342 test_main()
Martin v. Löwisd8996052002-11-21 21:45:32 +0000343
Barry Warsawff6d8132000-08-25 19:50:38 +0000344
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000345# For reference, here's the .po file used to created the GNU_MO_DATA above.
Martin v. Löwisd8996052002-11-21 21:45:32 +0000346#
347# The original version was automatically generated from the sources with
348# pygettext. Later it was manually modified to add plural forms support.
Barry Warsawff6d8132000-08-25 19:50:38 +0000349
Barry Warsaw64dab462000-08-30 03:32:07 +0000350'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000351# Dummy translation for the Python test_gettext.py module.
Barry Warsaw19148732001-08-24 19:11:57 +0000352# Copyright (C) 2001 Python Software Foundation
353# Barry Warsaw <barry@python.org>, 2000.
Barry Warsaw64dab462000-08-30 03:32:07 +0000354#
355msgid ""
356msgstr ""
357"Project-Id-Version: 2.0\n"
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000358"PO-Revision-Date: 2003-04-11 14:32-0400\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000359"Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000360"Language-Team: XX <python-dev@python.org>\n"
361"MIME-Version: 1.0\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000362"Content-Type: text/plain; charset=iso-8859-1\n"
363"Content-Transfer-Encoding: 8bit\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000364"Generated-By: pygettext.py 1.1\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000365"Plural-Forms: nplurals=2; plural=n!=1;\n"
Barry Warsawff6d8132000-08-25 19:50:38 +0000366
Barry Warsaw64dab462000-08-30 03:32:07 +0000367#: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
368#: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
369#: test_gettext.py:98
370msgid "nudge nudge"
371msgstr "wink wink"
Barry Warsawff6d8132000-08-25 19:50:38 +0000372
Barry Warsaw64dab462000-08-30 03:32:07 +0000373#: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
374#: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
375msgid "albatross"
376msgstr ""
Barry Warsawff6d8132000-08-25 19:50:38 +0000377
Barry Warsaw64dab462000-08-30 03:32:07 +0000378#: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
379#: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
380msgid "Raymond Luxury Yach-t"
381msgstr "Throatwobbler Mangrove"
Barry Warsawff6d8132000-08-25 19:50:38 +0000382
Barry Warsaw64dab462000-08-30 03:32:07 +0000383#: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
384#: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
385#: test_gettext.py:96
386msgid "mullusk"
387msgstr "bacon"
Barry Warsawff6d8132000-08-25 19:50:38 +0000388
Barry Warsaw64dab462000-08-30 03:32:07 +0000389#: test_gettext.py:40 test_gettext.py:101
390msgid ""
391"This module provides internationalization and localization\n"
392"support for your Python programs by providing an interface to the GNU\n"
393"gettext message catalog library."
394msgstr ""
395"Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
396"fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
397"trggrkg zrffntr pngnybt yvoenel."
Martin v. Löwisd8996052002-11-21 21:45:32 +0000398
399# Manually added, as neither pygettext nor xgettext support plural forms
400# in Python.
401msgid "There is %s file"
402msgid_plural "There are %s files"
403msgstr[0] "Hay %s fichero"
404msgstr[1] "Hay %s ficheros"
Barry Warsaw64dab462000-08-30 03:32:07 +0000405'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000406
407# Here's the second example po file example, used to generate the UMO_DATA
408# containing utf-8 encoded Unicode strings
409
410'''
411# Dummy translation for the Python test_gettext.py module.
412# Copyright (C) 2001 Python Software Foundation
413# Barry Warsaw <barry@python.org>, 2000.
414#
415msgid ""
416msgstr ""
417"Project-Id-Version: 2.0\n"
418"PO-Revision-Date: 2003-04-11 12:42-0400\n"
419"Last-Translator: Barry A. WArsaw <barry@python.org>\n"
420"Language-Team: XX <python-dev@python.org>\n"
421"MIME-Version: 1.0\n"
422"Content-Type: text/plain; charset=utf-8\n"
423"Content-Transfer-Encoding: 7bit\n"
424"Generated-By: manually\n"
425
426#: nofile:0
427msgid "ab\xc3\x9e"
428msgstr "\xc2\xa4yz"
429'''
Barry Warsawe960e222003-05-20 17:28:54 +0000430
431# Here's the third example po file, used to generate MMO_DATA
432
433'''
434msgid ""
435msgstr ""
436"Project-Id-Version: No Project 0.0\n"
437"POT-Creation-Date: Wed Dec 11 07:44:15 2002\n"
438"PO-Revision-Date: 2002-08-14 01:18:58+00:00\n"
439"Last-Translator: John Doe <jdoe@example.com>\n"
440"Jane Foobar <jfoobar@example.com>\n"
441"Language-Team: xx <xx@example.com>\n"
442"MIME-Version: 1.0\n"
443"Content-Type: text/plain; charset=iso-8859-15\n"
444"Content-Transfer-Encoding: quoted-printable\n"
445"Generated-By: pygettext.py 1.3\n"
446'''