Commits

strontium  committed ceb747e

Add doctest replacements for plural.py, support.py and util.py
All doctests now have an equivalent unittest
All unittests pass on Py2.6, 2.7 and 3.2

  • Participants
  • Parent commits abda06d

Comments (0)

Files changed (3)

File babel/tests/plural.py

 
 from babel import plural
 
+class pluralDocTest(unittest.TestCase):
+    def test_plural_PluralRule(self):
+        rule = plural.PluralRule({'one': 'n is 1'})
+        self.assertEqual(rule(1), 'one')
+        self.assertEqual(rule(2), 'other')
+        
+    def test_plural_rules(self):
+        rule = plural.PluralRule({'one': 'n is 1'})
+        self.assertEqual(rule.rules, {'one': 'n is 1'})
+        
+    def test_plural_cldr_modulo(self):
+        self.assertEqual(plural.cldr_modulo(-3, 5), -3)
+        self.assertEqual(plural.cldr_modulo(-3, -5), -3)
+        self.assertEqual(plural.cldr_modulo(3, 5), 3)
+    
+    def test_plural_in_range(self):
+        self.assertTrue(plural.in_range(1, 1, 3))
+        self.assertTrue(plural.in_range(3, 1, 3))
+        self.assertFalse(plural.in_range(1.2, 1, 4))
+        self.assertFalse(plural.in_range(10, 1, 4))
 
+    def test_plural_to_gettext(self):
+        self.assertEqual(plural.to_gettext({'one': 'n is 1', 'two': 'n is 2'}), 
+            'nplurals=3; plural=((n == 2) ? 1 : (n == 1) ? 0 : 2)')
+            
+    def test_plural_to_javascript(self):
+        self.assertEqual(plural.to_javascript({'one': 'n is 1'}), 
+            "(function(n) { return (n == 1) ? 'one' : 'other'; })")
+            
+    def test_plural_to_python(self):
+        func = plural.to_python({'one': 'n is 1', 'few': 'n in 2..4'})
+        self.assertEqual(func(1), 'one')
+        self.assertEqual(func(3), 'few')
+        
+    def test_plural_within_range(self):
+        self.assertTrue(plural.within_range(1, 1, 3))
+        self.assertTrue(plural.within_range(1.0, 1, 3))
+        self.assertTrue(plural.within_range(1.2, 1, 4))
+        self.assertFalse(plural.within_range(10, 1, 4))
+        
 def suite():
     suite = unittest.TestSuite()
     import sys
     if sys.version_info < (3, 0):
         #Doctests are not converted properly by 2to3, skip them for Python3.
         suite.addTest(doctest.DocTestSuite(plural))
+    suite.addTest(unittest.makeSuite(pluralDocTest))
     return suite
 
 

File babel/tests/support.py

 else:
     from io import BytesIO
 
-from babel.py2compat import safeToStr, safeToByte
+from babel.py2compat import safeToByte
     
 import unittest
 
 from babel import support
 from babel.messages import Catalog
 from babel.messages.mofile import write_mo
+from datetime import date, datetime, timedelta
+from pytz import timezone
+from babel.util import UTC
 
 class TranslationsTestCase(unittest.TestCase):
     
             safeToByte('VohsCTXD1'), self.translations.ldnpgettext('messages1', 'foo', 'foo1',
                                                        'foos1', 2))
 
+class supportDocTest(unittest.TestCase):
+    
+    def test_support_Format(self):
+        fmt = support.Format('en_US', UTC)
+        self.assertEqual(fmt.date(date(2007, 4, 1)), u'Apr 1, 2007')
+        self.assertEqual(fmt.decimal(1.2345), u'1.234')
+        
+    def test_support_date(self):
+        fmt = support.Format('en_US')
+        self.assertEqual(fmt.date(date(2007, 4, 1)), u'Apr 1, 2007')
+        
+    def test_support_datetime(self):
+        fmt = support.Format('en_US', tzinfo=timezone('US/Eastern'))
+        self.assertEqual(fmt.datetime(datetime(2007, 4, 1, 15, 30)), u'Apr 1, 2007 11:30:00 AM')
+        
+    def test_support_decimal(self): 
+        fmt = support.Format('en_US')
+        self.assertEqual(fmt.decimal(1.2345), u'1.234')
+
+    def test_support_number(self):
+        fmt = support.Format('en_US')
+        self.assertEqual(fmt.number(1099), u'1,099')
+        
+    def test_support_percent(self):
+        fmt = support.Format('en_US')
+        self.assertEqual(fmt.percent(0.34), u'34%')
+        
+    def test_support_time(self): 
+        fmt = support.Format('en_US', tzinfo=timezone('US/Eastern'))
+        self.assertEqual(fmt.time(datetime(2007, 4, 1, 15, 30)), u'11:30:00 AM')
+        
+    def test_support_timedelta(self): 
+        fmt = support.Format('en_US')
+        self.assertEqual(fmt.timedelta(timedelta(weeks=11)), u'3 mths')
+        
+    def test_support_LazyProxy(self):
+        
+        def greeting(name='world'):
+            return 'Hello, %s!' % name
+            
+        lazy_greeting = support.LazyProxy(greeting, name='Joe')
+        self.assertEqual(lazy_greeting, 'Hello, Joe!')
+        self.assertEqual(u'  ' + lazy_greeting, u'  Hello, Joe!')
+        self.assertEqual(u'(%s)' % lazy_greeting, u'(Hello, Joe!)')
+        greetings = [
+            support.LazyProxy(greeting, 'world'),
+            support.LazyProxy(greeting, 'Joe'),
+            support.LazyProxy(greeting, 'universe'),
+        ]
+        greetings.sort()
+        self.assertEqual(greetings[0], 'Hello, Joe!')
+        self.assertEqual(greetings[1], 'Hello, universe!')
+        self.assertEqual(greetings[2], 'Hello, world!')
+        self.assertEqual(len(greetings), 3)
+
 def suite():
     suite = unittest.TestSuite()
     import sys
     if sys.version_info < (3, 0):
         #Doctests are not converted properly by 2to3, skip them for Python3.
         suite.addTest(doctest.DocTestSuite(support))
+    suite.addTest(unittest.makeSuite(supportDocTest))
     suite.addTest(unittest.makeSuite(TranslationsTestCase, 'test'))
     return suite
 

File babel/tests/util.py

 
 from babel import util
 
+class utilDocTest(unittest.TestCase):
+    
+    def test_util_distinct(self):
+        self.assertEqual(list(util.distinct([1, 2, 1, 3, 4, 4])), [1, 2, 3, 4])
+        self.assertEqual(list(util.distinct('foobar')), ['f', 'o', 'b', 'a', 'r'])
+        
+    def test_util_pathmatch(self):
+        self.assertTrue(util.pathmatch('**.py', 'bar.py'))
+        self.assertTrue(util.pathmatch('**.py', 'foo/bar/baz.py'))
+        self.assertFalse(util.pathmatch('**.py', 'templates/index.html'))
+        self.assertTrue(util.pathmatch('**/templates/*.html', 'templates/index.html'))
+        self.assertFalse(util.pathmatch('**/templates/*.html', 'templates/foo/bar.html'))
+    
 def suite():
     suite = unittest.TestSuite()
     import sys
     if sys.version_info < (3, 0):
         #Doctests are not converted properly by 2to3, skip them for Python3.
         suite.addTest(doctest.DocTestSuite(util))
+        
+    suite.addTest(unittest.makeSuite(utilDocTest))
     return suite
 
 if __name__ == '__main__':