~vcs-imports/pybabel/trunk

« back to all changes in this revision

Viewing changes to babel/tests/support.py

  • Committer: fschwarz
  • Date: 2012-08-20 19:21:22 UTC
  • Revision ID: svn-v4:59ecc08e-a131-0410-9ea7-d4c0f28ac310:trunk:650
add babel.support.NullTranslations class similar to gettext.NullTranslations but with all of Babel's new *gettext methods (#277)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
# history and logs, available at http://babel.edgewall.org/log/.
13
13
 
14
14
import doctest
 
15
import inspect
15
16
import os
16
17
from StringIO import StringIO
17
18
import unittest
164
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
207
class LazyProxyTestCase(unittest.TestCase):
168
208
    def test_proxy_caches_result_of_function_call(self):
169
209
        self.counter = 0
188
228
    suite = unittest.TestSuite()
189
229
    suite.addTest(doctest.DocTestSuite(support))
190
230
    suite.addTest(unittest.makeSuite(TranslationsTestCase, 'test'))
 
231
    suite.addTest(unittest.makeSuite(NullTranslationsTestCase, 'test'))
191
232
    suite.addTest(unittest.makeSuite(LazyProxyTestCase, 'test'))
192
233
    return suite
193
234