Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 1 | import os |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 2 | import base64 |
Barry Warsaw | 9e03592 | 2000-08-25 19:14:25 +0000 | [diff] [blame] | 3 | import gettext |
| 4 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | from unittest import TestCase |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 7 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 8 | # 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 Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 13 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 14 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 15 | GNU_MO_DATA = '''\ |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 16 | 3hIElQAAAAAGAAAAHAAAAEwAAAALAAAAfAAAAAAAAACoAAAAFQAAAKkAAAAjAAAAvwAAAKEAAADj |
| 17 | AAAABwAAAIUBAAALAAAAjQEAAEUBAACZAQAAFgAAAN8CAAAeAAAA9gIAAKEAAAAVAwAABQAAALcD |
| 18 | AAAJAAAAvQMAAAEAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAABQAAAAYAAAACAAAAAFJh |
| 19 | eW1vbmQgTHV4dXJ5IFlhY2gtdABUaGVyZSBpcyAlcyBmaWxlAFRoZXJlIGFyZSAlcyBmaWxlcwBU |
| 20 | aGlzIG1vZHVsZSBwcm92aWRlcyBpbnRlcm5hdGlvbmFsaXphd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50:38 +0000 | [diff] [blame] | 33 | ''' |
| 34 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 35 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 36 | LOCALEDIR = os.path.join('xx', 'LC_MESSAGES') |
| 37 | MOFILE = os.path.join(LOCALEDIR, 'gettext.mo') |
| 38 | |
| 39 | def setup(): |
| 40 | os.makedirs(LOCALEDIR) |
Tim Peters | c795195 | 2000-08-26 21:01:27 +0000 | [diff] [blame] | 41 | fp = open(MOFILE, 'wb') |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 42 | fp.write(base64.decodestring(GNU_MO_DATA)) |
| 43 | fp.close() |
Guido van Rossum | 6569257 | 2002-07-20 00:36:38 +0000 | [diff] [blame] | 44 | os.environ['LANGUAGE'] = 'xx' |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 45 | |
| 46 | def teardown(): |
Guido van Rossum | 6569257 | 2002-07-20 00:36:38 +0000 | [diff] [blame] | 47 | os.environ['LANGUAGE'] = 'en' |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 48 | os.unlink(MOFILE) |
| 49 | os.removedirs(LOCALEDIR) |
| 50 | |
| 51 | |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 52 | class 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 |
| 95 | support for your Python programs by providing an interface to the GNU |
| 96 | gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba |
| 97 | fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH |
| 98 | trggrkg 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 | |
| 117 | class 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 |
| 170 | support for your Python programs by providing an interface to the GNU |
| 171 | gettext message catalog library.''') == '''Guvf zbqhyr cebivqrf vagreangvbanyvmngvba naq ybpnyvmngvba |
| 172 | fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH |
| 173 | trggrkg zrffntr pngnybt yvoenel.''' |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | class 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 | |
| 268 | if __name__ == '__main__': |
| 269 | try: |
| 270 | setup() |
| 271 | unittest.main() |
| 272 | finally: |
| 273 | teardown() |
| 274 | |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 275 | |
| 276 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 277 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 278 | # For reference, here's the .po file used to created the .mo data above. |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 279 | # |
| 280 | # The original version was automatically generated from the sources with |
| 281 | # pygettext. Later it was manually modified to add plural forms support. |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 282 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 283 | ''' |
| 284 | # Dummy translation for Python's test_gettext.py module. |
Barry Warsaw | 1914873 | 2001-08-24 19:11:57 +0000 | [diff] [blame] | 285 | # Copyright (C) 2001 Python Software Foundation |
| 286 | # Barry Warsaw <barry@python.org>, 2000. |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 287 | # |
| 288 | msgid "" |
| 289 | msgstr "" |
| 290 | "Project-Id-Version: 2.0\n" |
| 291 | "PO-Revision-Date: 2000-08-29 12:19-04:00\n" |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 292 | "Last-Translator: J. David Ibanez <j-david@noos.fr>\n" |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 293 | "Language-Team: XX <python-dev@python.org>\n" |
| 294 | "MIME-Version: 1.0\n" |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 295 | "Content-Type: text/plain; charset=iso-8859-1\n" |
| 296 | "Content-Transfer-Encoding: 8bit\n" |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 297 | "Generated-By: pygettext.py 1.1\n" |
Martin v. Löwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 298 | "Plural-Forms: nplurals=2; plural=n!=1;\n" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 299 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 300 | #: 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 |
| 303 | msgid "nudge nudge" |
| 304 | msgstr "wink wink" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 305 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 306 | #: 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 |
| 308 | msgid "albatross" |
| 309 | msgstr "" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 310 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 311 | #: 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 |
| 313 | msgid "Raymond Luxury Yach-t" |
| 314 | msgstr "Throatwobbler Mangrove" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 315 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 316 | #: 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 |
| 319 | msgid "mullusk" |
| 320 | msgstr "bacon" |
Barry Warsaw | ff6d813 | 2000-08-25 19:50:38 +0000 | [diff] [blame] | 321 | |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 322 | #: test_gettext.py:40 test_gettext.py:101 |
| 323 | msgid "" |
| 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." |
| 327 | msgstr "" |
| 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öwis | d899605 | 2002-11-21 21:45:32 +0000 | [diff] [blame] | 331 | |
| 332 | # Manually added, as neither pygettext nor xgettext support plural forms |
| 333 | # in Python. |
| 334 | msgid "There is %s file" |
| 335 | msgid_plural "There are %s files" |
| 336 | msgstr[0] "Hay %s fichero" |
| 337 | msgstr[1] "Hay %s ficheros" |
Barry Warsaw | 64dab46 | 2000-08-30 03:32:07 +0000 | [diff] [blame] | 338 | ''' |