[Python-checkins] r54205 - sandbox/trunk/pep3101/test_simpleformat.py

eric.smith python-checkins at python.org
Wed Mar 7 12:23:05 CET 2007


Author: eric.smith
Date: Wed Mar  7 12:23:01 2007
New Revision: 54205

Modified:
   sandbox/trunk/pep3101/test_simpleformat.py
Log:
Added test cases for invalid types passed to formatters.  Added test case for deriving from long and making sure correct __format__ is called.  This is a future-proofness test in case __format__ gets implemented by long.

Modified: sandbox/trunk/pep3101/test_simpleformat.py
==============================================================================
--- sandbox/trunk/pep3101/test_simpleformat.py	(original)
+++ sandbox/trunk/pep3101/test_simpleformat.py	Wed Mar  7 12:23:01 2007
@@ -140,7 +140,7 @@
         self.formatEqualsWithUnicode("'abcdefg", "{0:8r}", "abcdefg")
 
     def test_decimal_specifiers(self):
-        self.assertRaises(TypeError, "{0:d}", "non-number")
+        self.formatRaises(TypeError, "{0:d}", "non-number")
 
         self.formatEqualsWithUnicode("0", "{0:d}", 0)
         self.formatEqualsWithUnicode("0", "{0:d}", long(0))
@@ -182,7 +182,7 @@
     def test_octal_specifiers(self):
         n = int("31415", 8)
 
-        self.assertRaises(TypeError, "{0:o", "non-number")
+        self.formatRaises(TypeError, "{0:o}", "non-number")
 
         self.formatEqualsWithUnicode("0", "{0:o}", 0)
         self.formatEqualsWithUnicode("31415", "{0:o}", n)
@@ -225,16 +225,28 @@
         #self.formatRaises(TypeError, "{0:c}", 3.14)
 
     def test_exponent_specifiers(self):
+        self.formatRaises(TypeError, "{0:e}", "a string")
+        self.formatRaises(TypeError, "{0:E}", "a string")
+        self.formatRaises(TypeError, "{0:e}", 1j)
+        self.formatRaises(TypeError, "{0:E}", 1j)
         self.formatEqualsWithUnicodeUC("3.141500e+00", "{0:e}", 3.1415)
         self.formatEqualsWithUnicodeUC("3.1415000000e+00", "{0:.10e}", 3.1415)
 
     def test_fixed_specifiers(self):
+        self.formatRaises(TypeError, "{0:f}", "a string")
+        self.formatRaises(TypeError, "{0:F}", "a string")
+        self.formatRaises(TypeError, "{0:f}", 1j)
+        self.formatRaises(TypeError, "{0:F}", 1j)
         self.formatEqualsWithUnicode("3.141500", "{0:f}", 3.1415)
         self.formatEqualsWithUnicode("3.1415000000", "{0:.10f}", 3.1415)
         self.formatEqualsWithUnicode("3.1415e+200", "{0:f}", 3.1415e200)
         self.formatEqualsWithUnicode("3.1415e+200", "{0:F}", 3.1415e200)
 
     def test_general_specifiers(self):
+        self.formatRaises(TypeError, "{0:g}", "a string")
+        self.formatRaises(TypeError, "{0:G}", "a string")
+        self.formatRaises(TypeError, "{0:g}", 1j)
+        self.formatRaises(TypeError, "{0:G}", 1j)
         self.formatEqualsWithUnicodeUC("3.1415", "{0:g}", 3.1415)
         self.formatEqualsWithUnicodeUC("3.1415", "{0:.10g}", 3.1415)
         self.formatEqualsWithUnicodeUC("3.1415e+200", "{0:g}", 3.1415e200)
@@ -311,6 +323,24 @@
         self.formatRaises(ValueError, "{0:k}", 0)
         self.formatRaises(ValueError, "{0:4<10.20K}", 0)
 
+    def test_derive_from_long(self):
+        # make sure classes that inherit from long and don't specify
+        # __format__ get long's default format
+        class SimpleLong(long):
+            pass
+
+        l = SimpleLong(2**100)
+        self.formatEquals("1" + "0" * 100, "{0:b}", l)
+
+        # make sure that classes that inherit from long and do specify
+        # __format__ get it called
+        class FormatLong(long):
+            def __format__(self, specifiers):
+                return "how now brown cow"
+
+        l = FormatLong(2**100)
+        self.formatEquals("how now brown cow", "{0:b}", l)
+
     def test_name_mapper(self):
         mydict = dict(foo=1, bar=2)
         dict2 = mydict, dict(foobar=3)


More information about the Python-checkins mailing list