blob: 2a1f24c479df1f50fed89790ed6851171748d3f1 [file] [log] [blame]
Barry Warsaw9e035922000-08-25 19:14:25 +00001import os
Barry Warsawff6d8132000-08-25 19:50:38 +00002import base64
Barry Warsaw9e035922000-08-25 19:14:25 +00003import gettext
4
Martin v. Löwisd8996052002-11-21 21:45:32 +00005import unittest
6from unittest import TestCase
Fred Drake004d5e62000-10-23 17:22:08 +00007
Martin v. Löwisd8996052002-11-21 21:45:32 +00008# TODO:
9# - Add new tests, for example for "dgettext"
10# - Remove dummy tests, for example testing for single and double quotes
11# has no sense, it would have if we were testing a parser (i.e. pygettext)
12# - Tests should have only one assert.
Barry Warsaw64dab462000-08-30 03:32:07 +000013
Fred Drake004d5e62000-10-23 17:22:08 +000014
Barry Warsawff6d8132000-08-25 19:50:38 +000015GNU_MO_DATA = '''\
Martin v. Löwisd8996052002-11-21 21:45:32 +0000163hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj
17AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD
18AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh
19eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU
20aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphdGlvbiBhbmQgbG9jYWxpemF0aW9u
21CnN1cHBvcnQgZm9yIHlvdXIgUHl0aG9uIHByb2dyYW1zIGJ5IHByb3ZpZGluZyBhbiBpbnRlcmZh
22Y2UgdG8gdGhlIEdOVQpnZXR0ZXh0IG1lc3NhZ2UgY2F0YWxvZyBsaWJyYXJ5LgBtdWxsdXNrAG51
23ZGdlIG51ZGdlAFByb2plY3QtSWQtVmVyc2lvbjogMi4wClBPLVJldmlzaW9uLURhdGU6IDIwMDAt
24MDgtMjkgMTI6MTktMDQ6MDAKTGFzdC1UcmFuc2xhdG9yOiBKLiBEYXZpZCBJYsOhw7FleiA8ai1k
25YXZpZEBub29zLmZyPgpMYW5ndWFnZS1UZWFtOiBYWCA8cHl0aG9uLWRldkBweXRob24ub3JnPgpN
26SU1FLVZlcnNpb246IDEuMApDb250ZW50LVR5cGU6IHRleHQvcGxhaW47IGNoYXJzZXQ9aXNvLTg4
27NTktMQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiBub25lCkdlbmVyYXRlZC1CeTogcHlnZXR0
28ZXh0LnB5IDEuMQpQbHVyYWwtRm9ybXM6IG5wbHVyYWxzPTI7IHBsdXJhbD1uIT0xOwoAVGhyb2F0
29d29iYmxlciBNYW5ncm92ZQBIYXkgJXMgZmljaGVybwBIYXkgJXMgZmljaGVyb3MAR3V2ZiB6YnFo
30eXIgY2ViaXZxcmYgdmFncmVhbmd2YmFueXZtbmd2YmEgbmFxIHlicG55dm1uZ3ZiYQpmaGNjYmVn
31IHNiZSBsYmhlIENsZ3ViYSBjZWJ0ZW56ZiBvbCBjZWJpdnF2YXQgbmEgdmFncmVzbnByIGdiIGd1
32ciBUQUgKdHJnZ3JrZyB6cmZmbnRyIHBuZ255YnQgeXZvZW5lbC4AYmFjb24Ad2luayB3aW5rAA==
Barry Warsawff6d8132000-08-25 19:50:38 +000033'''
34
Fred Drake004d5e62000-10-23 17:22:08 +000035
Barry Warsawff6d8132000-08-25 19:50:38 +000036LOCALEDIR = os.path.join('xx', 'LC_MESSAGES')
37MOFILE = os.path.join(LOCALEDIR, 'gettext.mo')
38
39def setup():
40 os.makedirs(LOCALEDIR)
Tim Petersc7951952000-08-26 21:01:27 +000041 fp = open(MOFILE, 'wb')
Barry Warsawff6d8132000-08-25 19:50:38 +000042 fp.write(base64.decodestring(GNU_MO_DATA))
43 fp.close()
Guido van Rossum65692572002-07-20 00:36:38 +000044 os.environ['LANGUAGE'] = 'xx'
Barry Warsawff6d8132000-08-25 19:50:38 +000045
46def teardown():
Guido van Rossum65692572002-07-20 00:36:38 +000047 os.environ['LANGUAGE'] = 'en'
Barry Warsawff6d8132000-08-25 19:50:38 +000048 os.unlink(MOFILE)
49 os.removedirs(LOCALEDIR)
50
51
Martin v. Löwisd8996052002-11-21 21:45:32 +000052class GettextTestCase1(TestCase):
53 def setUp(self):
54 self.localedir = os.curdir
55 self.mofile = MOFILE
56
57 gettext.install('gettext', self.localedir)
58
59
60 def test_some_translations(self):
61 # test some translations
62 assert _('albatross') == 'albatross'
63 assert _(u'mullusk') == 'bacon'
64 assert _(r'Raymond Luxury Yach-t') == 'Throatwobbler Mangrove'
65 assert _(ur'nudge nudge') == 'wink wink'
66
67
68 def test_double_quotes(self):
69 # double quotes
70 assert _("albatross") == 'albatross'
71 assert _(u"mullusk") == 'bacon'
72 assert _(r"Raymond Luxury Yach-t") == 'Throatwobbler Mangrove'
73 assert _(ur"nudge nudge") == 'wink wink'
74
75
76 def test_triple_single_quotes(self):
77 # triple single quotes
78 assert _('''albatross''') == 'albatross'
79 assert _(u'''mullusk''') == 'bacon'
80 assert _(r'''Raymond Luxury Yach-t''') == 'Throatwobbler Mangrove'
81 assert _(ur'''nudge nudge''') == 'wink wink'
82
83
84 def test_triple_double_quotes(self):
85 # triple double quotes
86 assert _("""albatross""") == 'albatross'
87 assert _(u"""mullusk""") == 'bacon'
88 assert _(r"""Raymond Luxury Yach-t""") == 'Throatwobbler Mangrove'
89 assert _(ur"""nudge nudge""") == 'wink wink'
90
91
92 def test_multiline_strings(self):
93 # multiline strings
94 assert _('''This module provides internationalization and localization
95support for your Python programs by providing an interface to the GNU
96gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
97fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
98trggrkg zrffntr pngnybt yvoenel.'''
99
100
101 def test_the_alternative_interface(self):
102 # test the alternative interface
103 fp = open(os.path.join(self.mofile), 'rb')
104 t = gettext.GNUTranslations(fp)
105 fp.close()
106
107 t.install()
108
109 assert _('nudge nudge') == 'wink wink'
110
111 # try unicode return type
112 t.install(unicode=1)
113
114 assert _('mullusk') == 'bacon'
115
116
117class GettextTestCase2(TestCase):
118 def setUp(self):
119 self.localedir = os.curdir
120
121 gettext.bindtextdomain('gettext', self.localedir)
122 gettext.textdomain('gettext')
123
124 self._ = gettext.gettext
125
126
127 def test_bindtextdomain(self):
128 assert gettext.bindtextdomain('gettext') == self.localedir
129
130
131 def test_textdomain(self):
132 assert gettext.textdomain() == 'gettext'
133
134
135 def test_some_translations(self):
136 # test some translations
137 assert self._('albatross') == 'albatross'
138 assert self._(u'mullusk') == 'bacon'
139 assert self._(r'Raymond Luxury Yach-t') == 'Throatwobbler Mangrove'
140 assert self._(ur'nudge nudge') == 'wink wink'
141
142
143 def test_double_quotes(self):
144 # double quotes
145 assert self._("albatross") == 'albatross'
146 assert self._(u"mullusk") == 'bacon'
147 assert self._(r"Raymond Luxury Yach-t") == 'Throatwobbler Mangrove'
148 assert self._(ur"nudge nudge") == 'wink wink'
149
150
151 def test_triple_single_quotes(self):
152 # triple single quotes
153 assert self._('''albatross''') == 'albatross'
154 assert self._(u'''mullusk''') == 'bacon'
155 assert self._(r'''Raymond Luxury Yach-t''') == 'Throatwobbler Mangrove'
156 assert self._(ur'''nudge nudge''') == 'wink wink'
157
158
159 def test_triple_double_quotes(self):
160 # triple double quotes
161 assert self._("""albatross""") == 'albatross'
162 assert self._(u"""mullusk""") == 'bacon'
163 assert self._(r"""Raymond Luxury Yach-t""") == 'Throatwobbler Mangrove'
164 assert self._(ur"""nudge nudge""") == 'wink wink'
165
166
167 def test_multiline_strings(self):
168 # multiline strings
169 assert self._('''This module provides internationalization and localization
170support for your Python programs by providing an interface to the GNU
171gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba
172fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
173trggrkg zrffntr pngnybt yvoenel.'''
174
175
176
177
178class PluralFormsTestCase(TestCase):
179 def setUp(self):
180 self.mofile = MOFILE
181
182 def test_plural_forms1(self):
183 x = gettext.ngettext('There is %s file', 'There are %s files', 1)
184 assert x == 'Hay %s fichero'
185
186 x = gettext.ngettext('There is %s file', 'There are %s files', 2)
187 assert x == 'Hay %s ficheros'
188
189
190 def test_plural_forms2(self):
191 fp = open(os.path.join(self.mofile), 'rb')
192 t = gettext.GNUTranslations(fp)
193 fp.close()
194
195 x = t.ngettext('There is %s file', 'There are %s files', 1)
196 assert x == 'Hay %s fichero'
197
198 x = t.ngettext('There is %s file', 'There are %s files', 2)
199 assert x == 'Hay %s ficheros'
200
201
202 def test_hu(self):
203 f = gettext.c2py('0')
204 s = ''.join([ str(f(x)) for x in range(200) ])
205 assert s == "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
206
207
208 def test_de(self):
209 f = gettext.c2py('n != 1')
210 s = ''.join([ str(f(x)) for x in range(200) ])
211 assert s == "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
212
213
214 def test_fr(self):
215 f = gettext.c2py('n>1')
216 s = ''.join([ str(f(x)) for x in range(200) ])
217 assert s == "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
218
219
220 def test_gd(self):
221 f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2')
222 s = ''.join([ str(f(x)) for x in range(200) ])
223 assert s == "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"
224
225
226 def test_gd2(self):
227 # Tests the combination of parentheses and "?:"
228 f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)')
229 s = ''.join([ str(f(x)) for x in range(200) ])
230 assert s == "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"
231
232
233 def test_lt(self):
234 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2')
235 s = ''.join([ str(f(x)) for x in range(200) ])
236 assert s == "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111"
237
238
239 def test_ru(self):
240 f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
241 s = ''.join([ str(f(x)) for x in range(200) ])
242 assert s == "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222"
243
244
245 def test_pl(self):
246 f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2')
247 s = ''.join([ str(f(x)) for x in range(200) ])
248 assert s == "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222"
249
250
251 def test_sl(self):
252 f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3')
253 s = ''.join([ str(f(x)) for x in range(200) ])
254 assert s == "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"
255
256
257 def test_security(self):
258 # Test for a dangerous expression
259 try:
260 gettext.c2py("os.chmod('/etc/passwd',0777)")
261 except ValueError:
262 pass
263 else:
264 raise AssertionError
265
266
267
268if __name__ == '__main__':
269 try:
270 setup()
271 unittest.main()
272 finally:
273 teardown()
274
Barry Warsawff6d8132000-08-25 19:50:38 +0000275
276
Fred Drake004d5e62000-10-23 17:22:08 +0000277
Barry Warsaw64dab462000-08-30 03:32:07 +0000278# For reference, here's the .po file used to created the .mo data above.
Martin v. Löwisd8996052002-11-21 21:45:32 +0000279#
280# The original version was automatically generated from the sources with
281# pygettext. Later it was manually modified to add plural forms support.
Barry Warsawff6d8132000-08-25 19:50:38 +0000282
Barry Warsaw64dab462000-08-30 03:32:07 +0000283'''
284# Dummy translation for Python's test_gettext.py module.
Barry Warsaw19148732001-08-24 19:11:57 +0000285# Copyright (C) 2001 Python Software Foundation
286# Barry Warsaw <barry@python.org>, 2000.
Barry Warsaw64dab462000-08-30 03:32:07 +0000287#
288msgid ""
289msgstr ""
290"Project-Id-Version: 2.0\n"
291"PO-Revision-Date: 2000-08-29 12:19-04:00\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000292"Last-Translator: J. David Ibanez <j-david@noos.fr>\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000293"Language-Team: XX <python-dev@python.org>\n"
294"MIME-Version: 1.0\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000295"Content-Type: text/plain; charset=iso-8859-1\n"
296"Content-Transfer-Encoding: 8bit\n"
Barry Warsaw64dab462000-08-30 03:32:07 +0000297"Generated-By: pygettext.py 1.1\n"
Martin v. Löwisd8996052002-11-21 21:45:32 +0000298"Plural-Forms: nplurals=2; plural=n!=1;\n"
Barry Warsawff6d8132000-08-25 19:50:38 +0000299
Barry Warsaw64dab462000-08-30 03:32:07 +0000300#: test_gettext.py:19 test_gettext.py:25 test_gettext.py:31 test_gettext.py:37
301#: test_gettext.py:51 test_gettext.py:80 test_gettext.py:86 test_gettext.py:92
302#: test_gettext.py:98
303msgid "nudge nudge"
304msgstr "wink wink"
Barry Warsawff6d8132000-08-25 19:50:38 +0000305
Barry Warsaw64dab462000-08-30 03:32:07 +0000306#: test_gettext.py:16 test_gettext.py:22 test_gettext.py:28 test_gettext.py:34
307#: test_gettext.py:77 test_gettext.py:83 test_gettext.py:89 test_gettext.py:95
308msgid "albatross"
309msgstr ""
Barry Warsawff6d8132000-08-25 19:50:38 +0000310
Barry Warsaw64dab462000-08-30 03:32:07 +0000311#: test_gettext.py:18 test_gettext.py:24 test_gettext.py:30 test_gettext.py:36
312#: test_gettext.py:79 test_gettext.py:85 test_gettext.py:91 test_gettext.py:97
313msgid "Raymond Luxury Yach-t"
314msgstr "Throatwobbler Mangrove"
Barry Warsawff6d8132000-08-25 19:50:38 +0000315
Barry Warsaw64dab462000-08-30 03:32:07 +0000316#: test_gettext.py:17 test_gettext.py:23 test_gettext.py:29 test_gettext.py:35
317#: test_gettext.py:56 test_gettext.py:78 test_gettext.py:84 test_gettext.py:90
318#: test_gettext.py:96
319msgid "mullusk"
320msgstr "bacon"
Barry Warsawff6d8132000-08-25 19:50:38 +0000321
Barry Warsaw64dab462000-08-30 03:32:07 +0000322#: test_gettext.py:40 test_gettext.py:101
323msgid ""
324"This module provides internationalization and localization\n"
325"support for your Python programs by providing an interface to the GNU\n"
326"gettext message catalog library."
327msgstr ""
328"Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba\n"
329"fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH\n"
330"trggrkg zrffntr pngnybt yvoenel."
Martin v. Löwisd8996052002-11-21 21:45:32 +0000331
332# Manually added, as neither pygettext nor xgettext support plural forms
333# in Python.
334msgid "There is %s file"
335msgid_plural "There are %s files"
336msgstr[0] "Hay %s fichero"
337msgstr[1] "Hay %s ficheros"
Barry Warsaw64dab462000-08-30 03:32:07 +0000338'''