comparison babel/tests/support.py @ 596:f63a07d648b6 trunk

add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)
author fschwarz
date Mon, 20 Aug 2012 19:21:22 +0000
parents 8831b754f81e
children 838ba3796ad6
comparison
equal deleted inserted replaced
595:57a08cc52623 596:f63a07d648b6
10 # This software consists of voluntary contributions made by many 10 # This software consists of voluntary contributions made by many
11 # individuals. For the exact contribution history, see the revision 11 # individuals. For the exact contribution history, see the revision
12 # history and logs, available at http://babel.edgewall.org/log/. 12 # history and logs, available at http://babel.edgewall.org/log/.
13 13
14 import doctest 14 import doctest
15 import inspect
15 import os 16 import os
16 from StringIO import StringIO 17 from StringIO import StringIO
17 import unittest 18 import unittest
18 19
19 from babel import support 20 from babel import support
162 self.assertEqualTypeToo( 163 self.assertEqualTypeToo(
163 'VohsCTXD1', self.translations.ldnpgettext('messages1', 'foo', 'foo1', 164 'VohsCTXD1', self.translations.ldnpgettext('messages1', 'foo', 'foo1',
164 'foos1', 2)) 165 'foos1', 2))
165 166
166 167
168 class NullTranslationsTestCase(unittest.TestCase):
169 def setUp(self):
170 fp = StringIO()
171 write_mo(fp, Catalog(locale='de'))
172 fp.seek(0)
173 self.translations = support.Translations(fileobj=fp)
174 self.null_translations = support.NullTranslations(fp=fp)
175
176 def method_names(self):
177 return [name for name in dir(self.translations) if 'gettext' in name]
178
179 def test_same_methods(self):
180 for name in self.method_names():
181 if not hasattr(self.null_translations, name):
182 self.fail('NullTranslations does not provide method %r' % name)
183
184 def test_method_signature_compatibility(self):
185 for name in self.method_names():
186 translations_method = getattr(self.translations, name)
187 null_method = getattr(self.null_translations, name)
188 signature = inspect.getargspec
189 self.assertEqual(signature(translations_method),
190 signature(null_method))
191
192 def test_same_return_values(self):
193 data = {
194 'message': u'foo', 'domain': u'domain', 'context': 'tests',
195 'singular': u'bar', 'plural': u'baz', 'num': 1,
196 'msgid1': u'bar', 'msgid2': u'baz', 'n': 1,
197 }
198 for name in self.method_names():
199 method = getattr(self.translations, name)
200 null_method = getattr(self.null_translations, name)
201 signature = inspect.getargspec(method)
202 parameter_names = [name for name in signature.args if name != 'self']
203 values = [data[name] for name in parameter_names]
204 self.assertEqual(method(*values), null_method(*values))
205
206
167 class LazyProxyTestCase(unittest.TestCase): 207 class LazyProxyTestCase(unittest.TestCase):
168 def test_proxy_caches_result_of_function_call(self): 208 def test_proxy_caches_result_of_function_call(self):
169 self.counter = 0 209 self.counter = 0
170 def add_one(): 210 def add_one():
171 self.counter += 1 211 self.counter += 1
186 226
187 def suite(): 227 def suite():
188 suite = unittest.TestSuite() 228 suite = unittest.TestSuite()
189 suite.addTest(doctest.DocTestSuite(support)) 229 suite.addTest(doctest.DocTestSuite(support))
190 suite.addTest(unittest.makeSuite(TranslationsTestCase, 'test')) 230 suite.addTest(unittest.makeSuite(TranslationsTestCase, 'test'))
231 suite.addTest(unittest.makeSuite(NullTranslationsTestCase, 'test'))
191 suite.addTest(unittest.makeSuite(LazyProxyTestCase, 'test')) 232 suite.addTest(unittest.makeSuite(LazyProxyTestCase, 'test'))
192 return suite 233 return suite
193 234
194 if __name__ == '__main__': 235 if __name__ == '__main__':
195 unittest.main(defaultTest='suite') 236 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software