Mercurial > babel > mirror
annotate babel/tests/support.py @ 559:8831b754f81e trunk
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
author | fschwarz |
---|---|
date | Wed, 31 Aug 2011 12:38:06 +0000 |
parents | ca203b2af83c |
children | f63a07d648b6 |
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 | 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 |
420 | 15 import os |
16 from StringIO import StringIO | |
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
|
17 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
|
18 |
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
|
19 from babel import support |
420 | 20 from babel.messages import Catalog |
21 from babel.messages.mofile import write_mo | |
22 | |
23 class TranslationsTestCase(unittest.TestCase): | |
24 | |
25 def setUp(self): | |
26 # Use a locale which won't fail to run the tests | |
27 os.environ['LANG'] = 'en_US.UTF-8' | |
28 messages1 = [ | |
29 ('foo', {'string': 'Voh'}), | |
30 ('foo', {'string': 'VohCTX', 'context': 'foo'}), | |
31 (('foo1', 'foos1'), {'string': ('Voh1', 'Vohs1')}), | |
32 (('foo1', 'foos1'), {'string': ('VohCTX1', 'VohsCTX1'), 'context': 'foo'}), | |
33 ] | |
34 messages2 = [ | |
35 ('foo', {'string': 'VohD'}), | |
36 ('foo', {'string': 'VohCTXD', 'context': 'foo'}), | |
37 (('foo1', 'foos1'), {'string': ('VohD1', 'VohsD1')}), | |
38 (('foo1', 'foos1'), {'string': ('VohCTXD1', 'VohsCTXD1'), 'context': 'foo'}), | |
39 ] | |
40 catalog1 = Catalog(locale='en_GB', domain='messages') | |
41 catalog2 = Catalog(locale='en_GB', domain='messages1') | |
42 for ids, kwargs in messages1: | |
43 catalog1.add(ids, **kwargs) | |
44 for ids, kwargs in messages2: | |
45 catalog2.add(ids, **kwargs) | |
46 catalog1_fp = StringIO() | |
47 catalog2_fp = StringIO() | |
48 write_mo(catalog1_fp, catalog1) | |
49 catalog1_fp.seek(0) | |
50 write_mo(catalog2_fp, catalog2) | |
51 catalog2_fp.seek(0) | |
52 translations1 = support.Translations(catalog1_fp) | |
53 translations2 = support.Translations(catalog2_fp, domain='messages1') | |
54 self.translations = translations1.add(translations2, merge=False) | |
55 | |
56 def assertEqualTypeToo(self, expected, result): | |
57 self.assertEqual(expected, result) | |
58 assert type(expected) == type(result), "instance type's do not " + \ | |
59 "match: %r!=%r" % (type(expected), type(result)) | |
60 | |
61 def test_pgettext(self): | |
62 self.assertEqualTypeToo('Voh', self.translations.gettext('foo')) | |
63 self.assertEqualTypeToo('VohCTX', self.translations.pgettext('foo', | |
64 'foo')) | |
65 | |
66 def test_upgettext(self): | |
67 self.assertEqualTypeToo(u'Voh', self.translations.ugettext('foo')) | |
68 self.assertEqualTypeToo(u'VohCTX', self.translations.upgettext('foo', | |
69 'foo')) | |
70 | |
71 def test_lpgettext(self): | |
72 self.assertEqualTypeToo('Voh', self.translations.lgettext('foo')) | |
73 self.assertEqualTypeToo('VohCTX', self.translations.lpgettext('foo', | |
74 'foo')) | |
75 | |
76 def test_npgettext(self): | |
77 self.assertEqualTypeToo('Voh1', | |
78 self.translations.ngettext('foo1', 'foos1', 1)) | |
79 self.assertEqualTypeToo('Vohs1', | |
80 self.translations.ngettext('foo1', 'foos1', 2)) | |
81 self.assertEqualTypeToo('VohCTX1', | |
82 self.translations.npgettext('foo', 'foo1', | |
83 'foos1', 1)) | |
84 self.assertEqualTypeToo('VohsCTX1', | |
85 self.translations.npgettext('foo', 'foo1', | |
86 'foos1', 2)) | |
87 | |
88 def test_unpgettext(self): | |
89 self.assertEqualTypeToo(u'Voh1', | |
90 self.translations.ungettext('foo1', 'foos1', 1)) | |
91 self.assertEqualTypeToo(u'Vohs1', | |
92 self.translations.ungettext('foo1', 'foos1', 2)) | |
93 self.assertEqualTypeToo(u'VohCTX1', | |
94 self.translations.unpgettext('foo', 'foo1', | |
95 'foos1', 1)) | |
96 self.assertEqualTypeToo(u'VohsCTX1', | |
97 self.translations.unpgettext('foo', 'foo1', | |
98 'foos1', 2)) | |
99 | |
100 def test_lnpgettext(self): | |
101 self.assertEqualTypeToo('Voh1', | |
102 self.translations.lngettext('foo1', 'foos1', 1)) | |
103 self.assertEqualTypeToo('Vohs1', | |
104 self.translations.lngettext('foo1', 'foos1', 2)) | |
105 self.assertEqualTypeToo('VohCTX1', | |
106 self.translations.lnpgettext('foo', 'foo1', | |
107 'foos1', 1)) | |
108 self.assertEqualTypeToo('VohsCTX1', | |
109 self.translations.lnpgettext('foo', 'foo1', | |
110 'foos1', 2)) | |
111 | |
112 def test_dpgettext(self): | |
113 self.assertEqualTypeToo( | |
114 'VohD', self.translations.dgettext('messages1', 'foo')) | |
115 self.assertEqualTypeToo( | |
116 'VohCTXD', self.translations.dpgettext('messages1', 'foo', 'foo')) | |
117 | |
118 def test_dupgettext(self): | |
119 self.assertEqualTypeToo( | |
120 u'VohD', self.translations.dugettext('messages1', 'foo')) | |
121 self.assertEqualTypeToo( | |
122 u'VohCTXD', self.translations.dupgettext('messages1', 'foo', 'foo')) | |
123 | |
124 def test_ldpgettext(self): | |
125 self.assertEqualTypeToo( | |
126 'VohD', self.translations.ldgettext('messages1', 'foo')) | |
127 self.assertEqualTypeToo( | |
128 'VohCTXD', self.translations.ldpgettext('messages1', 'foo', 'foo')) | |
129 | |
130 def test_dnpgettext(self): | |
131 self.assertEqualTypeToo( | |
132 'VohD1', self.translations.dngettext('messages1', 'foo1', 'foos1', 1)) | |
133 self.assertEqualTypeToo( | |
134 'VohsD1', self.translations.dngettext('messages1', 'foo1', 'foos1', 2)) | |
135 self.assertEqualTypeToo( | |
136 'VohCTXD1', self.translations.dnpgettext('messages1', 'foo', 'foo1', | |
137 'foos1', 1)) | |
138 self.assertEqualTypeToo( | |
139 'VohsCTXD1', self.translations.dnpgettext('messages1', 'foo', 'foo1', | |
140 'foos1', 2)) | |
141 | |
142 def test_dunpgettext(self): | |
143 self.assertEqualTypeToo( | |
144 u'VohD1', self.translations.dungettext('messages1', 'foo1', 'foos1', 1)) | |
145 self.assertEqualTypeToo( | |
146 u'VohsD1', self.translations.dungettext('messages1', 'foo1', 'foos1', 2)) | |
147 self.assertEqualTypeToo( | |
148 u'VohCTXD1', self.translations.dunpgettext('messages1', 'foo', 'foo1', | |
149 'foos1', 1)) | |
150 self.assertEqualTypeToo( | |
151 u'VohsCTXD1', self.translations.dunpgettext('messages1', 'foo', 'foo1', | |
152 'foos1', 2)) | |
153 | |
154 def test_ldnpgettext(self): | |
155 self.assertEqualTypeToo( | |
156 'VohD1', self.translations.ldngettext('messages1', 'foo1', 'foos1', 1)) | |
157 self.assertEqualTypeToo( | |
158 'VohsD1', self.translations.ldngettext('messages1', 'foo1', 'foos1', 2)) | |
159 self.assertEqualTypeToo( | |
160 'VohCTXD1', self.translations.ldnpgettext('messages1', 'foo', 'foo1', | |
161 'foos1', 1)) | |
162 self.assertEqualTypeToo( | |
163 'VohsCTXD1', self.translations.ldnpgettext('messages1', 'foo', 'foo1', | |
164 'foos1', 2)) | |
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
|
165 |
559
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
166 |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
167 class LazyProxyTestCase(unittest.TestCase): |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
168 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
|
169 self.counter = 0 |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
170 def add_one(): |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
171 self.counter += 1 |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
172 return self.counter |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
173 proxy = support.LazyProxy(add_one) |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
174 self.assertEqual(1, proxy.value) |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
175 self.assertEqual(1, proxy.value) |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
176 |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
177 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
|
178 self.counter = 0 |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
179 def add_one(): |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
180 self.counter += 1 |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
181 return self.counter |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
182 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
|
183 self.assertEqual(1, proxy.value) |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
184 self.assertEqual(2, proxy.value) |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
185 |
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
186 |
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
|
187 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
|
188 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
|
189 suite.addTest(doctest.DocTestSuite(support)) |
420 | 190 suite.addTest(unittest.makeSuite(TranslationsTestCase, 'test')) |
559
8831b754f81e
Allow disabling cache behaviour in LazyProxy (fixes #208, initial patch Pedro Algarvio)
fschwarz
parents:
530
diff
changeset
|
191 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
|
192 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
|
193 |
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
|
194 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
|
195 unittest.main(defaultTest='suite') |