[pypy-svn] r5587 - in pypy/trunk/src/pypy: appspace objspace/std/test

mwh at codespeak.net mwh at codespeak.net
Fri Jul 16 15:38:24 CEST 2004


Author: mwh
Date: Fri Jul 16 15:38:23 2004
New Revision: 5587

Modified:
   pypy/trunk/src/pypy/appspace/_formatting.py
   pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py
Log:
Make the tests in test_stringformat object space agnostic (so, the
file should move...).
Fix the broken tests running them under the trivial object space
revealed (!!).
Fix the formatting code to work with the unbroken tests.


Modified: pypy/trunk/src/pypy/appspace/_formatting.py
==============================================================================
--- pypy/trunk/src/pypy/appspace/_formatting.py	(original)
+++ pypy/trunk/src/pypy/appspace/_formatting.py	Fri Jul 16 15:38:23 2004
@@ -145,11 +145,21 @@
         self.prec = None
         return self.std_wp(r)
 
+class HexFormatter(Formatter):
+    def format(self):
+        i = maybe_int(self.value)
+        r = hex(i)
+        if not self.flags.f_alt:
+            r = r[2:]
+        if self.char == 'X':
+            r = r.upper()
+        return self.std_wp(r)
+
 format_registry = {
     's':funcFormatter(str),
     'r':funcFormatter(repr),
-    'x':funcFormatter(maybe_int, hex),    
-    'X':funcFormatter(maybe_int, hex, lambda r:r.upper()),
+    'x':HexFormatter,
+    'X':HexFormatter,
     'd':funcFormatter(maybe_int, str),
     'f':floatFFormatter,
     'g':funcFormatter(str),

Modified: pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_stringformat.py	Fri Jul 16 15:38:23 2004
@@ -3,10 +3,6 @@
 
 class TestStringObjectWithDict(testit.AppTestCase):
 
-    def setUp(self):
-        # force testing to use standard objspace
-        self.space = testit.objspace('std')
-
     def test_format_item(self):
         d = {'i': 23}
         self.assertEquals('a23b', 'a%(i)sb' % d)
@@ -36,10 +32,6 @@
 
 class TestStringObject(testit.AppTestCase):
 
-    def setUp(self):
-        # force testing to use standard objspace
-        self.space = testit.objspace('std')
-
     def test_format_item(self):
         self.assertEquals('a23b', 'a%sb' % 23)
         self.assertEquals('23b', '%sb' % 23)
@@ -67,19 +59,15 @@
 
     def test_format_float(self):
         self.assertEquals('23', '%d' % 23.456)
-        self.assertEquals('0x17', '%x' % 23.456)
+        self.assertEquals('17', '%x' % 23.456)
         self.assertEquals('23.456', '%s' % 23.456)
-        # accept either exact or filled-with-9's for %r
-        r = '%r' % 23.45
-        if len(r)==5:
-            self.assertEquals('23.45', r)
-        else:
-            r9 = '23.44' + '9'*(len(r)-5)
-            self.assertEquals(r9, r)
+        # for 'r' use a float that has an exact decimal rep:
+        self.assertEquals('23.125', '%r' % 23.125)
 
     def test_format_int(self):
         self.assertEquals('23', '%d' % 23)
-        self.assertEquals('0x17', '%x' % 23)
+        self.assertEquals('17', '%x' % 23)
+        self.assertEquals('0x17', '%#x' % 23)
         self.assertEquals('23', '%s' % 23)
         self.assertEquals('23', '%r' % 23)
 
@@ -102,9 +90,6 @@
         self.assertRaises(ValueError, '%'.__mod__, ((23,),))
 
 class TestWidthPrec(testit.AppTestCase):
-    def setUp(self):
-        self.space = testit.objspace()
-
     def test_width(self):
         self.assertEquals("%3s" %'a', '  a')
         self.assertEquals("%-3s"%'a', 'a  ')



More information about the Pypy-commit mailing list