blob: 517dfc0f318fae4753915fc5602e0332b0efa324 [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
7from test.test_support import run_suite
8
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
Barry Warsawff6d8132000-08-25 19:50:38 +000016GNU_MO_DATA = '''\
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
Barry Warsawa1ce93f2003-04-11 18:36:43 +000036UMO_DATA = '''\
373hIElQAAAAACAAAAHAAAACwAAAAFAAAAPAAAAAAAAABQAAAABAAAAFEAAAAPAQAAVgAAAAQAAABm
38AQAAAQAAAAIAAAAAAAAAAAAAAAAAAAAAYWLDngBQcm9qZWN0LUlkLVZlcnNpb246IDIuMApQTy1S
39ZXZpc2lvbi1EYXRlOiAyMDAzLTA0LTExIDEyOjQyLTA0MDAKTGFzdC1UcmFuc2xhdG9yOiBCYXJy
40eSBBLiBXQXJzYXcgPGJhcnJ5QHB5dGhvbi5vcmc+Ckxhbmd1YWdlLVRlYW06IFhYIDxweXRob24t
41ZGV2QHB5dGhvbi5vcmc+Ck1JTUUtVmVyc2lvbjogMS4wCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFp
42bjsgY2hhcnNldD11dGYtOApDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0CkdlbmVyYXRl
43ZC1CeTogbWFudWFsbHkKAMKkeXoA
44'''
Fred Drake004d5e62000-10-23 17:22:08 +000045
Barry Warsawe960e222003-05-20 17:28:54 +000046MMO_DATA = '''\
473hIElQAAAAABAAAAHAAAACQAAAADAAAALAAAAAAAAAA4AAAAeAEAADkAAAABAAAAAAAAAAAAAAAA
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')
Brett Cannon4aebbb02003-04-27 19:42:41 +000061try:
62 LANG = os.environ['LANGUAGE']
63except:
64 LANG = 'en'
Barry Warsawff6d8132000-08-25 19:50:38 +000065
66
Barry Warsawa1ce93f2003-04-11 18:36:43 +000067class GettextBaseTest(unittest.TestCase):
Martin v. Löwisd8996052002-11-21 21:45:32 +000068 def setUp(self):
Neal Norwitza0e0cd32004-07-19 00:08:59 +000069 if not os.path.isdir(LOCALEDIR):
70 os.makedirs(LOCALEDIR)
Barry Warsawa1ce93f2003-04-11 18:36:43 +000071 fp = open(MOFILE, 'wb')
72 fp.write(base64.decodestring(GNU_MO_DATA))
73 fp.close()
74 fp = open(UMOFILE, 'wb')
75 fp.write(base64.decodestring(UMO_DATA))
76 fp.close()
Barry Warsawe960e222003-05-20 17:28:54 +000077 fp = open(MMOFILE, 'wb')
78 fp.write(base64.decodestring(MMO_DATA))
79 fp.close()
Barry Warsawa1ce93f2003-04-11 18:36:43 +000080 os.environ['LANGUAGE'] = 'xx'
81
82 def tearDown(self):
Brett Cannon4aebbb02003-04-27 19:42:41 +000083 os.environ['LANGUAGE'] = LANG
84 shutil.rmtree(os.path.split(LOCALEDIR)[0])
Barry Warsawa1ce93f2003-04-11 18:36:43 +000085
86
87class GettextTestCase1(GettextBaseTest):
88 def setUp(self):
89 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +000090 self.localedir = os.curdir
91 self.mofile = MOFILE
Martin v. Löwisd8996052002-11-21 21:45:32 +000092 gettext.install('gettext', self.localedir)
93
Martin v. Löwisd8996052002-11-21 21:45:32 +000094 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +000095 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +000096 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +000097 eq(_('albatross'), 'albatross')
98 eq(_(u'mullusk'), 'bacon')
99 eq(_(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
100 eq(_(ur'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000101
102 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000103 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000104 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000105 eq(_("albatross"), 'albatross')
106 eq(_(u"mullusk"), 'bacon')
107 eq(_(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
108 eq(_(ur"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000109
110 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000111 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000112 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000113 eq(_('''albatross'''), 'albatross')
114 eq(_(u'''mullusk'''), 'bacon')
115 eq(_(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
116 eq(_(ur'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000117
118 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000119 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000120 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000121 eq(_("""albatross"""), 'albatross')
122 eq(_(u"""mullusk"""), 'bacon')
123 eq(_(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
124 eq(_(ur"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000125
126 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000127 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000128 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000129 eq(_('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000130support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000131gettext message catalog library.'''),
132 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000133fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000134trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000135
136 def test_the_alternative_interface(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000137 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000138 # test the alternative interface
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000139 fp = open(self.mofile, 'rb')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000140 t = gettext.GNUTranslations(fp)
141 fp.close()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000142 # Install the translation object
Martin v. Löwisd8996052002-11-21 21:45:32 +0000143 t.install()
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000144 eq(_('nudge nudge'), 'wink wink')
145 # Try unicode return type
146 t.install(unicode=True)
147 eq(_('mullusk'), 'bacon')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000148
149
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000150class GettextTestCase2(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000151 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000152 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000153 self.localedir = os.curdir
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000154 # Set up the bindings
Martin v. Löwisd8996052002-11-21 21:45:32 +0000155 gettext.bindtextdomain('gettext', self.localedir)
156 gettext.textdomain('gettext')
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000157 # For convenience
Martin v. Löwisd8996052002-11-21 21:45:32 +0000158 self._ = gettext.gettext
159
Martin v. Löwisd8996052002-11-21 21:45:32 +0000160 def test_bindtextdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000161 self.assertEqual(gettext.bindtextdomain('gettext'), self.localedir)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000162
163 def test_textdomain(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000164 self.assertEqual(gettext.textdomain(), 'gettext')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000165
166 def test_some_translations(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000167 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000168 # test some translations
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000169 eq(self._('albatross'), 'albatross')
170 eq(self._(u'mullusk'), 'bacon')
171 eq(self._(r'Raymond Luxury Yach-t'), 'Throatwobbler Mangrove')
172 eq(self._(ur'nudge nudge'), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000173
174 def test_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000175 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000176 # double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000177 eq(self._("albatross"), 'albatross')
178 eq(self._(u"mullusk"), 'bacon')
179 eq(self._(r"Raymond Luxury Yach-t"), 'Throatwobbler Mangrove')
180 eq(self._(ur"nudge nudge"), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000181
182 def test_triple_single_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000183 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000184 # triple single quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000185 eq(self._('''albatross'''), 'albatross')
186 eq(self._(u'''mullusk'''), 'bacon')
187 eq(self._(r'''Raymond Luxury Yach-t'''), 'Throatwobbler Mangrove')
188 eq(self._(ur'''nudge nudge'''), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000189
190 def test_triple_double_quotes(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000191 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000192 # triple double quotes
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000193 eq(self._("""albatross"""), 'albatross')
194 eq(self._(u"""mullusk"""), 'bacon')
195 eq(self._(r"""Raymond Luxury Yach-t"""), 'Throatwobbler Mangrove')
196 eq(self._(ur"""nudge nudge"""), 'wink wink')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000197
198 def test_multiline_strings(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000199 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000200 # multiline strings
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000201 eq(self._('''This module provides internationalization and localization
Martin v. Löwisd8996052002-11-21 21:45:32 +0000202support for your Python programs by providing an interface to the GNU
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000203gettext message catalog library.'''),
204 '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
Martin v. Löwisd8996052002-11-21 21:45:32 +0000205fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000206trggrkg zrffntr pngnybt yvoenel.''')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000207
208
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000209class PluralFormsTestCase(GettextBaseTest):
Martin v. Löwisd8996052002-11-21 21:45:32 +0000210 def setUp(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000211 GettextBaseTest.setUp(self)
Martin v. Löwisd8996052002-11-21 21:45:32 +0000212 self.mofile = MOFILE
213
214 def test_plural_forms1(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000215 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000216 x = gettext.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000217 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000218 x = gettext.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000219 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000220
221 def test_plural_forms2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000222 eq = self.assertEqual
223 fp = open(self.mofile, 'rb')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000224 t = gettext.GNUTranslations(fp)
225 fp.close()
Martin v. Löwisd8996052002-11-21 21:45:32 +0000226 x = t.ngettext('There is %s file', 'There are %s files', 1)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000227 eq(x, 'Hay %s fichero')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000228 x = t.ngettext('There is %s file', 'There are %s files', 2)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000229 eq(x, 'Hay %s ficheros')
Martin v. Löwisd8996052002-11-21 21:45:32 +0000230
231 def test_hu(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000232 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000233 f = gettext.c2py('0')
234 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000235 eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000236
237 def test_de(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('n != 1')
240 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000241 eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000242
243 def test_fr(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, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000248
249 def test_gd(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 ? 0 : n==2 ? 1 : 2')
252 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000253 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000254
255 def test_gd2(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000256 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000257 # Tests the combination of parentheses and "?:"
258 f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
259 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000260 eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000261
262 def test_lt(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000263 eq = self.assertEqual
Martin v. Löwisd8996052002-11-21 21:45:32 +0000264 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
265 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000266 eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000267
268 def test_ru(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%10<=4 && (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, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000273
274 def test_pl(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==1 ? 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, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000279
280 def test_sl(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%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
283 s = ''.join([ str(f(x)) for x in range(200) ])
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000284 eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000285
286 def test_security(self):
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000287 raises = self.assertRaises
Martin v. Löwisd8996052002-11-21 21:45:32 +0000288 # Test for a dangerous expression
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000289 raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)")
Martin v. Löwisd8996052002-11-21 21:45:32 +0000290
291
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000292class UnicodeTranslationsTest(GettextBaseTest):
293 def setUp(self):
294 GettextBaseTest.setUp(self)
295 fp = open(UMOFILE, 'rb')
296 try:
Barry Warsawedb155f2003-04-24 18:08:13 +0000297 self.t = gettext.GNUTranslations(fp)
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000298 finally:
299 fp.close()
300 self._ = self.t.ugettext
301
302 def test_unicode_msgid(self):
303 unless = self.failUnless
304 unless(isinstance(self._(''), unicode))
305 unless(isinstance(self._(u''), unicode))
306
307 def test_unicode_msgstr(self):
308 eq = self.assertEqual
309 eq(self._(u'ab\xde'), u'\xa4yz')
310
311
Barry Warsawe960e222003-05-20 17:28:54 +0000312class WeirdMetadataTest(GettextBaseTest):
313 def setUp(self):
314 GettextBaseTest.setUp(self)
315 fp = open(MMOFILE, 'rb')
316 try:
317 try:
318 self.t = gettext.GNUTranslations(fp)
319 except:
320 self.tearDown()
321 raise
322 finally:
323 fp.close()
324
325 def test_weird_metadata(self):
326 info = self.t.info()
327 self.assertEqual(info['last-translator'],
328 'John Doe <jdoe@example.com>\nJane Foobar <jfoobar@example.com>')
329
330
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000331def suite():
332 suite = unittest.TestSuite()
333 suite.addTest(unittest.makeSuite(GettextTestCase1))
334 suite.addTest(unittest.makeSuite(GettextTestCase2))
335 suite.addTest(unittest.makeSuite(PluralFormsTestCase))
336 suite.addTest(unittest.makeSuite(UnicodeTranslationsTest))
Barry Warsawe960e222003-05-20 17:28:54 +0000337 suite.addTest(unittest.makeSuite(WeirdMetadataTest))
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000338 return suite
Tim Peters0eadaac2003-04-24 16:02:54 +0000339
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000340
341def test_main():
342 run_suite(suite())
343
Martin v. Löwisd8996052002-11-21 21:45:32 +0000344
345if __name__ == '__main__':
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000346 test_main()
Martin v. Löwisd8996052002-11-21 21:45:32 +0000347
Barry Warsawff6d8132000-08-25 19:50:38 +0000348
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000349# For reference, here's the .po file used to created the GNU_MO_DATA above.
Martin v. Löwisd8996052002-11-21 21:45:32 +0000350#
351# The original version was automatically generated from the sources with
352# pygettext. Later it was manually modified to add plural forms support.
Barry Warsawff6d8132000-08-25 19:50:38 +0000353
Barry Warsaw64dab462000-08-30 03:32:07 +0000354'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000355# Dummy translation for the Python test_gettext.py module.
Barry Warsaw19148732001-08-24 19:11:57 +0000356# Copyright (C) 2001 Python Software Foundation
357# Barry Warsaw <barry@python.org>, 2000.
Barry Warsaw64dab462000-08-30 03:32:07 +0000358#
359msgid ""
360msgstr ""
361"Project-Id-Version: 2.0\n"
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000362"PO-Revision-Date: 2003-04-11 14:32-0400\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000363"Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000364"Language-Team: XX <python-dev@python.org>\n"
365"MIME-Version: 1.0\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000366"Content-Type: text/plain; charset=iso-8859-1\n"
367"Content-Transfer-Encoding: 8bit\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000368"Generated-By: pygettext.py 1.1\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000369"Plural-Forms: nplurals=2; plural=n!=1;\n"
Barry Warsawff6d8132000-08-25 19:50:38 +0000370
Barry Warsaw64dab462000-08-30 03:32:07 +0000371#: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
372#: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
373#: test_gettext.py:98
374msgid "nudge nudge"
375msgstr "wink wink"
Barry Warsawff6d8132000-08-25 19:50:38 +0000376
Barry Warsaw64dab462000-08-30 03:32:07 +0000377#: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
378#: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
379msgid "albatross"
380msgstr ""
Barry Warsawff6d8132000-08-25 19:50:38 +0000381
Barry Warsaw64dab462000-08-30 03:32:07 +0000382#: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
383#: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
384msgid "Raymond Luxury Yach-t"
385msgstr "Throatwobbler Mangrove"
Barry Warsawff6d8132000-08-25 19:50:38 +0000386
Barry Warsaw64dab462000-08-30 03:32:07 +0000387#: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
388#: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
389#: test_gettext.py:96
390msgid "mullusk"
391msgstr "bacon"
Barry Warsawff6d8132000-08-25 19:50:38 +0000392
Barry Warsaw64dab462000-08-30 03:32:07 +0000393#: test_gettext.py:40 test_gettext.py:101
394msgid ""
395"This module provides internationalization and localization\n"
396"support for your Python programs by providing an interface to the GNU\n"
397"gettext message catalog library."
398msgstr ""
399"Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
400"fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
401"trggrkg zrffntr pngnybt yvoenel."
Martin v. Löwisd8996052002-11-21 21:45:32 +0000402
403# Manually added, as neither pygettext nor xgettext support plural forms
404# in Python.
405msgid "There is %s file"
406msgid_plural "There are %s files"
407msgstr[0] "Hay %s fichero"
408msgstr[1] "Hay %s ficheros"
Barry Warsaw64dab462000-08-30 03:32:07 +0000409'''
Barry Warsawa1ce93f2003-04-11 18:36:43 +0000410
411# Here's the second example po file example, used to generate the UMO_DATA
412# containing utf-8 encoded Unicode strings
413
414'''
415# Dummy translation for the Python test_gettext.py module.
416# Copyright (C) 2001 Python Software Foundation
417# Barry Warsaw <barry@python.org>, 2000.
418#
419msgid ""
420msgstr ""
421"Project-Id-Version: 2.0\n"
422"PO-Revision-Date: 2003-04-11 12:42-0400\n"
423"Last-Translator: Barry A. WArsaw <barry@python.org>\n"
424"Language-Team: XX <python-dev@python.org>\n"
425"MIME-Version: 1.0\n"
426"Content-Type: text/plain; charset=utf-8\n"
427"Content-Transfer-Encoding: 7bit\n"
428"Generated-By: manually\n"
429
430#: nofile:0
431msgid "ab\xc3\x9e"
432msgstr "\xc2\xa4yz"
433'''
Barry Warsawe960e222003-05-20 17:28:54 +0000434
435# Here's the third example po file, used to generate MMO_DATA
436
437'''
438msgid ""
439msgstr ""
440"Project-Id-Version: No Project 0.0\n"
441"POT-Creation-Date: Wed Dec 11 07:44:15 2002\n"
442"PO-Revision-Date: 2002-08-14 01:18:58+00:00\n"
443"Last-Translator: John Doe <jdoe@example.com>\n"
444"Jane Foobar <jfoobar@example.com>\n"
445"Language-Team: xx <xx@example.com>\n"
446"MIME-Version: 1.0\n"
447"Content-Type: text/plain; charset=iso-8859-15\n"
448"Content-Transfer-Encoding: quoted-printable\n"
449"Generated-By: pygettext.py 1.3\n"
450'''