Commits

strontium  committed abda06d

Add unittests to replace doctests for babel.numbers
All tests pass under py2.6,2.7 and 3.2

  • Participants
  • Parent commits 1ba8ff2

Comments (0)

Files changed (1)

File babel/tests/numbers.py

         fmt = numbers.format_scientific(0, '#E0', locale='en_US')
         self.assertEqual(fmt, '0E0')
 
+class numbersDocTest(unittest.TestCase):
+    
+    def test_numbers_bankersround(self):
+        self.assertEqual(numbers.bankersround(5.5, 0), 6.0)
+        self.assertEqual(numbers.bankersround(6.5, 0), 6.0)
+        self.assertEqual(numbers.bankersround(-6.5, 0), -6.0)
+        self.assertEqual(numbers.bankersround(1234.0, -2), 1200.0)
+        
+    def test_numbers_format_currency(self):
+        self.assertEqual(numbers.format_currency(1099.98, 'USD', locale='en_US'), u'$1,099.98')
+        self.assertEqual(numbers.format_currency(1099.98, 'USD', locale='es_CO'), u'US$\xa01.099,98')
+        self.assertEqual(numbers.format_currency(1099.98, 'EUR', locale='de_DE'), u'1.099,98\xa0\u20ac')
+        self.assertEqual(numbers.format_currency(1099.98, 'EUR', u'\xa4\xa4 #,##0.00', locale='en_US'), u'EUR 1,099.98')
+        
+    def test_numbers_format_decimal(self):
+        self.assertEqual(numbers.format_decimal(1.2345, locale='en_US'), u'1.234')
+        self.assertEqual(numbers.format_decimal(1.2346, locale='en_US'), u'1.235')
+        self.assertEqual(numbers.format_decimal(-1.2346, locale='en_US'), u'-1.235')
+        self.assertEqual(numbers.format_decimal(1.2345, locale='sv_SE'), u'1,234')
+        self.assertEqual(numbers.format_decimal(1.2345, locale='de'), u'1,234')
+        self.assertEqual(numbers.format_decimal(12345.5, locale='en_US'), u'12,345.5')
+    
+    def test_numbers_format_number(self):
+        self.assertEqual(numbers.format_number(1099, locale='en_US'), u'1,099')
+        self.assertEqual(numbers.format_number(1099, locale='de_DE'), u'1.099')
+        
+    def test_numbers_format_percent(self):
+        self.assertEqual(numbers.format_percent(0.34, locale='en_US'), u'34%')
+        self.assertEqual(numbers.format_percent(25.1234, locale='en_US'), u'2,512%')
+        self.assertEqual(numbers.format_percent(25.1234, locale='sv_SE'), u'2\xa0512\xa0%')
+        self.assertEqual(numbers.format_percent(25.1234, u'#,##0\u2030', locale='en_US'), u'25,123\u2030')
+    
+    def test_numbers_format_scientific(self):
+        self.assertEqual(numbers.format_scientific(10000, locale='en_US'), u'1E4')
+        self.assertEqual(numbers.format_scientific(1234567, u'##0E00', locale='en_US'), u'1.23E06')
+        
+    def test_numbers_get_currency_name(self):
+        self.assertEqual(numbers.get_currency_name('USD', 'en_US'), u'US Dollar')
+        
+    def test_numbers_get_currency_symbol(self):
+        self.assertEqual(numbers.get_currency_symbol('USD', 'en_US'), u'$')
+        
+    def test_numbers_get_decimal_symbol(self):
+        self.assertEqual(numbers.get_decimal_symbol('en_US'), u'.')
+        
+    def test_numbers_get_exponential_symbol(self):
+        self.assertEqual(numbers.get_exponential_symbol('en_US'), u'E')
+        
+    def test_numbers_get_group_symbol(self):
+        self.assertEqual(numbers.get_group_symbol('en_US'), u',')
+        
+    def test_numbers_get_minus_sign_symbol(self):
+        self.assertEqual(numbers.get_minus_sign_symbol('en_US'), u'-')
+        
+    def test_numbers_get_plus_sign_symbol(self):
+        self.assertEqual(numbers.get_plus_sign_symbol('en_US'), u'+')
+        
+    def test_numbers_parse_decimal(self):
+        self.assertEqual(numbers.parse_decimal('1,099.98', locale='en_US'), 1099.98)
+        self.assertEqual(numbers.parse_decimal('1.099,98', locale='de'), 1099.98)
+        self.assertRaises(numbers.NumberFormatError, numbers.parse_decimal, '2,109,998', locale='de')
+    
+    def test_numbers_parse_number(self):
+        self.assertEqual(numbers.parse_number('1,099', locale='en_US'), 1099L)
+        self.assertEqual(numbers.parse_number('1.099', locale='de_DE'), 1099L)
+        self.assertRaises(numbers.NumberFormatError, numbers.parse_number, '1.099,98', locale='de')
 
 def suite():
     suite = unittest.TestSuite()
     if sys.version_info < (3, 0):
         #Doctests are not converted properly by 2to3, skip them for Python3.
         suite.addTest(doctest.DocTestSuite(numbers))
+    suite.addTest(unittest.makeSuite(numbersDocTest))
     suite.addTest(unittest.makeSuite(FormatDecimalTestCase))
     return suite