annotate babel/tests/support.py @ 603:60f716236946 trunk

update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
author fschwarz
date Wed, 22 Aug 2012 20:28:32 +0000
parents 33c8c68b96c7
children
rev   line source
61
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
2 #
530
ca203b2af83c Update the copyright line.
jruigrok
parents: 420
diff changeset
3 # Copyright (C) 2007-2011 Edgewall Software
61
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
4 # All rights reserved.
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
5 #
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
8 # are also available at http://babel.edgewall.org/wiki/License.
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
9 #
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://babel.edgewall.org/log/.
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
13
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
14 import doctest
596
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
15 import inspect
420
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
16 import os
603
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
17 import shutil
420
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
18 from StringIO import StringIO
603
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
19 import tempfile
61
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
20 import unittest
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
21
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
22 from babel import support
420
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
23 from babel.messages import Catalog
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
24 from babel.messages.mofile import write_mo
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
25
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
26 class TranslationsTestCase(unittest.TestCase):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
27
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
28 def setUp(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
29 # Use a locale which won't fail to run the tests
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
30 os.environ['LANG'] = 'en_US.UTF-8'
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
31 messages1 = [
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
32 ('foo', {'string': 'Voh'}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
33 ('foo', {'string': 'VohCTX', 'context': 'foo'}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
34 (('foo1', 'foos1'), {'string': ('Voh1', 'Vohs1')}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
35 (('foo1', 'foos1'), {'string': ('VohCTX1', 'VohsCTX1'), 'context': 'foo'}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
36 ]
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
37 messages2 = [
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
38 ('foo', {'string': 'VohD'}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
39 ('foo', {'string': 'VohCTXD', 'context': 'foo'}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
40 (('foo1', 'foos1'), {'string': ('VohD1', 'VohsD1')}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
41 (('foo1', 'foos1'), {'string': ('VohCTXD1', 'VohsCTXD1'), 'context': 'foo'}),
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
42 ]
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
43 catalog1 = Catalog(locale='en_GB', domain='messages')
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
44 catalog2 = Catalog(locale='en_GB', domain='messages1')
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
45 for ids, kwargs in messages1:
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
46 catalog1.add(ids, **kwargs)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
47 for ids, kwargs in messages2:
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
48 catalog2.add(ids, **kwargs)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
49 catalog1_fp = StringIO()
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
50 catalog2_fp = StringIO()
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
51 write_mo(catalog1_fp, catalog1)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
52 catalog1_fp.seek(0)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
53 write_mo(catalog2_fp, catalog2)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
54 catalog2_fp.seek(0)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
55 translations1 = support.Translations(catalog1_fp)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
56 translations2 = support.Translations(catalog2_fp, domain='messages1')
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
57 self.translations = translations1.add(translations2, merge=False)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
58
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
59 def assertEqualTypeToo(self, expected, result):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
60 self.assertEqual(expected, result)
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
61 assert type(expected) == type(result), "instance type's do not " + \
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
62 "match: %r!=%r" % (type(expected), type(result))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
63
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
64 def test_pgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
65 self.assertEqualTypeToo('Voh', self.translations.gettext('foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
66 self.assertEqualTypeToo('VohCTX', self.translations.pgettext('foo',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
67 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
68
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
69 def test_upgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
70 self.assertEqualTypeToo(u'Voh', self.translations.ugettext('foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
71 self.assertEqualTypeToo(u'VohCTX', self.translations.upgettext('foo',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
72 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
73
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
74 def test_lpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
75 self.assertEqualTypeToo('Voh', self.translations.lgettext('foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
76 self.assertEqualTypeToo('VohCTX', self.translations.lpgettext('foo',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
77 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
78
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
79 def test_npgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
80 self.assertEqualTypeToo('Voh1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
81 self.translations.ngettext('foo1', 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
82 self.assertEqualTypeToo('Vohs1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
83 self.translations.ngettext('foo1', 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
84 self.assertEqualTypeToo('VohCTX1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
85 self.translations.npgettext('foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
86 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
87 self.assertEqualTypeToo('VohsCTX1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
88 self.translations.npgettext('foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
89 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
90
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
91 def test_unpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
92 self.assertEqualTypeToo(u'Voh1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
93 self.translations.ungettext('foo1', 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
94 self.assertEqualTypeToo(u'Vohs1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
95 self.translations.ungettext('foo1', 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
96 self.assertEqualTypeToo(u'VohCTX1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
97 self.translations.unpgettext('foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
98 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
99 self.assertEqualTypeToo(u'VohsCTX1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
100 self.translations.unpgettext('foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
101 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
102
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
103 def test_lnpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
104 self.assertEqualTypeToo('Voh1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
105 self.translations.lngettext('foo1', 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
106 self.assertEqualTypeToo('Vohs1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
107 self.translations.lngettext('foo1', 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
108 self.assertEqualTypeToo('VohCTX1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
109 self.translations.lnpgettext('foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
110 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
111 self.assertEqualTypeToo('VohsCTX1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
112 self.translations.lnpgettext('foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
113 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
114
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
115 def test_dpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
116 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
117 'VohD', self.translations.dgettext('messages1', 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
118 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
119 'VohCTXD', self.translations.dpgettext('messages1', 'foo', 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
120
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
121 def test_dupgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
122 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
123 u'VohD', self.translations.dugettext('messages1', 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
124 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
125 u'VohCTXD', self.translations.dupgettext('messages1', 'foo', 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
126
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
127 def test_ldpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
128 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
129 'VohD', self.translations.ldgettext('messages1', 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
130 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
131 'VohCTXD', self.translations.ldpgettext('messages1', 'foo', 'foo'))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
132
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
133 def test_dnpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
134 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
135 'VohD1', self.translations.dngettext('messages1', 'foo1', 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
136 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
137 'VohsD1', self.translations.dngettext('messages1', 'foo1', 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
138 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
139 'VohCTXD1', self.translations.dnpgettext('messages1', 'foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
140 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
141 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
142 'VohsCTXD1', self.translations.dnpgettext('messages1', 'foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
143 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
144
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
145 def test_dunpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
146 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
147 u'VohD1', self.translations.dungettext('messages1', 'foo1', 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
148 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
149 u'VohsD1', self.translations.dungettext('messages1', 'foo1', 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
150 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
151 u'VohCTXD1', self.translations.dunpgettext('messages1', 'foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
152 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
153 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
154 u'VohsCTXD1', self.translations.dunpgettext('messages1', 'foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
155 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
156
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
157 def test_ldnpgettext(self):
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
158 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
159 'VohD1', self.translations.ldngettext('messages1', 'foo1', 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
160 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
161 'VohsD1', self.translations.ldngettext('messages1', 'foo1', 'foos1', 2))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
162 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
163 'VohCTXD1', self.translations.ldnpgettext('messages1', 'foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
164 'foos1', 1))
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
165 self.assertEqualTypeToo(
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
166 'VohsCTXD1', self.translations.ldnpgettext('messages1', 'foo', 'foo1',
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
167 'foos1', 2))
603
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
168
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
169 def test_load(self):
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
170 tempdir = tempfile.mkdtemp()
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
171 try:
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
172 messages_dir = os.path.join(tempdir, 'fr', 'LC_MESSAGES')
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
173 os.makedirs(messages_dir)
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
174 catalog = Catalog(locale='fr', domain='messages')
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
175 catalog.add('foo', 'bar')
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
176 write_mo(file(os.path.join(messages_dir, 'messages.mo'), 'wb'), catalog)
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
177
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
178 translations = support.Translations.load(tempdir, locales=('fr',), domain='messages')
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
179 self.assertEqual('bar', translations.gettext('foo'))
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
180 finally:
60f716236946 update Translations.load to use new parameter name ('fp' instead of 'fileobj'), regression introduced in r653
fschwarz
parents: 599
diff changeset
181 shutil.rmtree(tempdir)
61
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
182
559
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
183
596
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
184 class NullTranslationsTestCase(unittest.TestCase):
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
185 def setUp(self):
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
186 fp = StringIO()
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
187 write_mo(fp, Catalog(locale='de'))
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
188 fp.seek(0)
599
33c8c68b96c7 change constructor for babel.support.Translations to __init__(fp=None, domain=None) as its super class gettext.GNUTranslations uses "fp" as well (even if "fileobj" is a better name)
fschwarz
parents: 598
diff changeset
189 self.translations = support.Translations(fp=fp)
596
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
190 self.null_translations = support.NullTranslations(fp=fp)
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
191
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
192 def method_names(self):
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
193 return [name for name in dir(self.translations) if 'gettext' in name]
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
194
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
195 def test_same_methods(self):
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
196 for name in self.method_names():
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
197 if not hasattr(self.null_translations, name):
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
198 self.fail('NullTranslations does not provide method %r' % name)
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
199
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
200 def test_method_signature_compatibility(self):
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
201 for name in self.method_names():
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
202 translations_method = getattr(self.translations, name)
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
203 null_method = getattr(self.null_translations, name)
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
204 signature = inspect.getargspec
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
205 self.assertEqual(signature(translations_method),
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
206 signature(null_method))
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
207
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
208 def test_same_return_values(self):
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
209 data = {
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
210 'message': u'foo', 'domain': u'domain', 'context': 'tests',
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
211 'singular': u'bar', 'plural': u'baz', 'num': 1,
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
212 'msgid1': u'bar', 'msgid2': u'baz', 'n': 1,
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
213 }
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
214 for name in self.method_names():
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
215 method = getattr(self.translations, name)
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
216 null_method = getattr(self.null_translations, name)
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
217 signature = inspect.getargspec(method)
598
838ba3796ad6 fix tests on Python 2.4, named tuples are not available there
fschwarz
parents: 596
diff changeset
218 parameter_names = [name for name in signature[0] if name != 'self']
596
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
219 values = [data[name] for name in parameter_names]
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
220 self.assertEqual(method(*values), null_method(*values))
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
221
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
222
559
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
223 class LazyProxyTestCase(unittest.TestCase):
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
224 def test_proxy_caches_result_of_function_call(self):
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
225 self.counter = 0
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
226 def add_one():
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
227 self.counter += 1
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
228 return self.counter
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
229 proxy = support.LazyProxy(add_one)
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
230 self.assertEqual(1, proxy.value)
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
231 self.assertEqual(1, proxy.value)
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
232
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
233 def test_can_disable_proxy_cache(self):
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
234 self.counter = 0
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
235 def add_one():
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
236 self.counter += 1
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
237 return self.counter
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
238 proxy = support.LazyProxy(add_one, enable_cache=False)
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
239 self.assertEqual(1, proxy.value)
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
240 self.assertEqual(2, proxy.value)
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
241
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
242
61
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
243 def suite():
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
244 suite = unittest.TestSuite()
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
245 suite.addTest(doctest.DocTestSuite(support))
420
0f040e15ef66 Add support for `msgctxt`. See #54.
palgarvio
parents: 61
diff changeset
246 suite.addTest(unittest.makeSuite(TranslationsTestCase, 'test'))
596
f63a07d648b6 add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
fschwarz
parents: 559
diff changeset
247 suite.addTest(unittest.makeSuite(NullTranslationsTestCase, 'test'))
559
8831b754f81e Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents: 530
diff changeset
248 suite.addTest(unittest.makeSuite(LazyProxyTestCase, 'test'))
61
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
249 return suite
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
250
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
251 if __name__ == '__main__':
9d13b9a5d727 Move `Translations` and `LazyProxy` to new `babel.support` module, which should contain any convenience code that is useful for applications using Babel/I18n, but not used by Babel itself.
cmlenz
parents:
diff changeset
252 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software