[pypy-svn] r71990 - pypy/trunk/pypy/objspace/std/test

arigo at codespeak.net arigo at codespeak.net
Tue Mar 9 19:12:13 CET 2010


Author: arigo
Date: Tue Mar  9 19:12:11 2010
New Revision: 71990

Modified:
   pypy/trunk/pypy/objspace/std/test/test_stringformat.py
Log:
Fix test_stringformat.  It was mostly of the kind
assert "%d" % 23 == "23", which is actually constant-folded
by the CPython compiler :-(((


Modified: pypy/trunk/pypy/objspace/std/test/test_stringformat.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_stringformat.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_stringformat.py	Tue Mar  9 19:12:11 2010
@@ -15,7 +15,8 @@
         assert 'a23b23c' == 'a%(i)sb%(i)sc' % d
 
     def test_format_percent(self):
-        assert 'a%b' == 'a%%b' % {} 
+        d = {}
+        assert 'a%b' == 'a%%b' % d
 
     def test_format_empty_key(self):
         d = {'':42}
@@ -30,8 +31,10 @@
         raises(KeyError, 'a%(x)sb'.__mod__, d) 
 
     def test_format_error(self):
-        assert '' % {} == ''
-        raises(TypeError, "'' % 5")
+        d = {}
+        assert '' % d == ''
+        n = 5
+        raises(TypeError, "'' % n")
         class MyMapping(object):
             def __getitem__(self, key):
                 py.test.fail('should not be here')
@@ -45,16 +48,18 @@
 class AppTestStringObject:
 
     def test_format_item(self):
-        assert 'a23b' == 'a%sb' % 23
-        assert '23b' == '%sb' % 23
-        assert 'a23' == 'a%s' % 23
-        assert '23' == '%s' % 23
+        n = 23
+        assert 'a23b' == 'a%sb' % n
+        assert '23b' == '%sb' % n
+        assert 'a23' == 'a%s' % n
+        assert '23' == '%s' % n
 
     def test_format_percent(self):
-        assert 'a%b' == 'a%%b' % ()
-        assert '%b' == '%%b' % ()
-        assert 'a%' == 'a%%' % ()
-        assert '%' == '%%' % ()
+        t = ()
+        assert 'a%b' == 'a%%b' % t
+        assert '%b' == '%%b' % t
+        assert 'a%' == 'a%%' % t
+        assert '%' == '%%' % t
 
     def test_format_too_much(self):
         raises(TypeError, '%s%s'.__mod__, ())
@@ -65,47 +70,60 @@
         raises(TypeError, '%s%s'.__mod__, (23,)*4)
 
     def test_format_string(self):
-        assert '23' == '%s' % '23'
-        assert "'23'" == '%r' % '23'
-        raises(TypeError, '%d'.__mod__, "23")
+        s = '23'
+        assert '23' == '%s' % s
+        assert "'23'" == '%r' % s
+        raises(TypeError, '%d'.__mod__, s)
 
     def test_format_float(self):
-        assert '23' == '%d' % 23.456
-        assert '17' == '%x' % 23.456
-        assert '23.456' == '%s' % 23.456
+        f = 23.456
+        assert '23' == '%d' % f
+        assert '17' == '%x' % f
+        assert '23.456' == '%s' % f
         # for 'r' use a float that has an exact decimal rep:
-        assert '23.125' == '%r' % 23.125
-        assert '0.028' == '%.3f' % 0.0276    # should work on most platforms...
-        assert '   inf' == '%6g' % (1E200 * 1E200)
+        g = 23.125
+        assert '23.125' == '%r' % g
+        h = 0.0276
+        assert '0.028' == '%.3f' % h    # should work on most platforms...
+        big = 1E200
+        assert '   inf' == '%6g' % (big * big)
 
     def test_format_int(self):
         import sys
-        assert '23' == '%d' % 23
-        assert '17' == '%x' % 23
-        assert '0x17' == '%#x' % 23
-        assert '0x0' == '%#x' % 0
-        assert '23' == '%s' % 23
-        assert '23' == '%r' % 23
+        n = 23
+        z = 0
+        assert '23' == '%d' % n
+        assert '17' == '%x' % n
+        assert '0x17' == '%#x' % n
+        assert '0x0' == '%#x' % z
+        assert '23' == '%s' % n
+        assert '23' == '%r' % n
         assert ('%d' % (-sys.maxint-1,) == '-' + str(sys.maxint+1)
                                         == '-%d' % (sys.maxint+1,))
-        assert '1C' == '%X' % 28
-        assert '0X1C' == '%#X' % 28
-        assert '10' == '%o' % 8
-        assert '010' == '%#o' % 8
-        assert '-010' == '%#o' % -8
-        assert '0' == '%o' % 0
-        assert '0' == '%#o' % 0
-
-        assert '-0x017' == '%#06x' % -23
-        assert '0' == '%#.0o' % 0
+        n = 28
+        m = 8
+        assert '1C' == '%X' % n
+        assert '0X1C' == '%#X' % n
+        assert '10' == '%o' % m
+        assert '010' == '%#o' % m
+        assert '-010' == '%#o' % -m
+        assert '0' == '%o' % z
+        assert '0' == '%#o' % z
+
+        n = 23
+        f = 5
+        assert '-0x017' == '%#06x' % -n
+        assert '0' == '%#.0o' % z
 
     def test_format_list(self):
-        assert '<[1, 2]>' == '<%s>' % [1,2]
-        assert '<[1, 2]-[3, 4]>' == '<%s-%s>' % ([1,2], [3,4])
+        l = [1,2]
+        assert '<[1, 2]>' == '<%s>' % l
+        assert '<[1, 2]-[3, 4]>' == '<%s-%s>' % (l, [3,4])
 
     def test_format_tuple(self):
-        assert '<(1, 2)>' == '<%s>' % ((1,2),)
-        assert '<(1, 2)-(3, 4)>' == '<%s-%s>' % ((1,2), (3,4))
+        t = (1,2)
+        assert '<(1, 2)>' == '<%s>' % (t,)
+        assert '<(1, 2)-(3, 4)>' == '<%s-%s>' % (t, (3,4))
 
     def test_format_dict(self):
 
@@ -119,9 +137,10 @@
         #     \character{\%} character.
 
         # It is what CPython *does*, however.  All software sucks.
-        
-        assert '<{1: 2}>' == '<%s>' % {1:2}
-        assert '<{1: 2}-{3: 4}>' == '<%s-%s>' % ({1:2}, {3:4})
+
+        d = {1:2}
+        assert '<{1: 2}>' == '<%s>' % d
+        assert '<{1: 2}-{3: 4}>' == '<%s-%s>' % (d, {3:4})
 
     def test_format_wrong_char(self):
         raises(ValueError, 'a%Zb'.__mod__, ((23,),))
@@ -132,8 +151,10 @@
 
     def test_format_char(self):
         import sys
-        assert '%c' % 65 == 'A'
-        assert '%c' % 'e' == 'e'
+        A = 65
+        e = 'e'
+        assert '%c' % A == 'A'
+        assert '%c' % e == 'e'
         raises(OverflowError, '%c'.__mod__, (256,))
         raises(OverflowError, '%c'.__mod__, (-1,))
         raises(OverflowError, u'%c'.__mod__, (sys.maxunicode+1,))
@@ -143,52 +164,63 @@
 
 class AppTestWidthPrec:
     def test_width(self):
-        assert "%3s" %'a' == '  a'
-        assert "%-3s"%'a' == 'a  '
+        a = 'a'
+        assert "%3s" % a == '  a'
+        assert "%-3s"% a == 'a  '
 
     def test_prec_cornercase(self):
-        assert "%.0x" % 0 == ''
-        assert "%.x" % 0 == ''
-        assert "%.0d" % 0 == ''
-        assert "%.i" % 0 == ''
-        assert "%.0o" % 0 == ''
-        assert "%.o" % 0 == ''
+        z = 0
+        assert "%.0x" % z == ''
+        assert "%.x" % z == ''
+        assert "%.0d" % z == ''
+        assert "%.i" % z == ''
+        assert "%.0o" % z == ''
+        assert "%.o" % z == ''
 
     def test_prec_string(self):
-        assert "%.3s"%'a' ==     'a'
-        assert "%.3s"%'abcde' == 'abc'
+        a = 'a'
+        abcde = 'abcde'
+        assert "%.3s"% a ==     'a'
+        assert "%.3s"% abcde == 'abc'
 
     def test_prec_width_string(self):
-        assert "%5.3s" %'a' ==     '    a'
-        assert "%5.3s" %'abcde' == '  abc'
-        assert "%-5.3s"%'a' ==     'a    '
-        assert "%-5.3s"%'abcde' == 'abc  '
+        a = 'a'
+        abcde = 'abcde'
+        assert "%5.3s" % a ==     '    a'
+        assert "%5.3s" % abcde == '  abc'
+        assert "%-5.3s"% a ==     'a    '
+        assert "%-5.3s"% abcde == 'abc  '
 
     def test_zero_pad(self):
-        assert "%02d"%1 ==   "01"
-        assert "%05d"%1 ==   "00001"
-        assert "%-05d"%1 ==  "1    "
-        assert "%04f"%2.25 == "2.250000"
-        assert "%05g"%2.25 == "02.25"
-        assert "%-05g"%2.25 =="2.25 "
-        assert "%05s"%2.25 == " 2.25"
+        one = 1
+        ttf = 2.25
+        assert "%02d" % one ==   "01"
+        assert "%05d" % one ==   "00001"
+        assert "%-05d" % one ==  "1    "
+        assert "%04f" % ttf == "2.250000"
+        assert "%05g" % ttf == "02.25"
+        assert "%-05g" % ttf =="2.25 "
+        assert "%05s" % ttf == " 2.25"
 
         
     def test_star_width(self):
-        assert "%*s" %( 5, 'abc') ==  '  abc'
-        assert "%*s" %(-5, 'abc') ==  'abc  '
-        assert "%-*s"%( 5, 'abc') ==  'abc  '
-        assert "%-*s"%(-5, 'abc') ==  'abc  '
+        f = 5
+        assert "%*s" %( f, 'abc') ==  '  abc'
+        assert "%*s" %(-f, 'abc') ==  'abc  '
+        assert "%-*s"%( f, 'abc') ==  'abc  '
+        assert "%-*s"%(-f, 'abc') ==  'abc  '
 
     def test_star_prec(self):
-        assert "%.*s"%( 3, 'abc') ==  'abc'
-        assert "%.*s"%( 3, 'abcde') ==  'abc'
-        assert "%.*s"%(-3, 'abc') ==  ''
+        t = 3
+        assert "%.*s"%( t, 'abc') ==  'abc'
+        assert "%.*s"%( t, 'abcde') ==  'abc'
+        assert "%.*s"%(-t, 'abc') ==  ''
 
     def test_star_width_prec(self):
-        assert "%*.*s"%( 5, 3, 'abc') ==    '  abc'
-        assert "%*.*s"%( 5, 3, 'abcde') ==  '  abc'
-        assert "%*.*s"%(-5, 3, 'abcde') ==  'abc  '
+        f = 5
+        assert "%*.*s"%( f, 3, 'abc') ==    '  abc'
+        assert "%*.*s"%( f, 3, 'abcde') ==  '  abc'
+        assert "%*.*s"%(-f, 3, 'abcde') ==  'abc  '
 
     def test_too_long(self):
         def f(fmt, x):
@@ -206,7 +238,8 @@
 
 class AppTestUnicodeObject:
     def test_unicode_convert(self):
-        assert isinstance("%s" % (u"x"), unicode)
+        u = u"x"
+        assert isinstance("%s" % u, unicode)
 
     def test_unicode_nonascii(self):
         """
@@ -214,12 +247,14 @@
         a string format should decode the format string as ascii and return
         unicode.
         """
-        result = "%s" % u'\x80'
+        u = u'\x80'
+        result = "%s" % u
         assert isinstance(result, unicode)
-        assert result == u'\x80'
+        assert result == u
 
     def test_unicode_d(self):
-        assert u"%.1d" % 3 == '3'
+        t = 3
+        assert u"%.1d" % t == '3'
 
     def test_unicode_overflow(self):
         skip("nicely passes on top of CPython but requires > 2GB of RAM")
@@ -227,14 +262,18 @@
         raises((OverflowError, MemoryError), 'u"%.*d" % (sys.maxint, 1)')
 
     def test_unicode_format_a(self):
-        assert u'%x' % 10L == 'a'
+        ten = 10L
+        assert u'%x' % ten == 'a'
 
     def test_long_no_overflow(self):
-        assert "%x" % 100000000000L == "174876e800"
+        big = 100000000000L
+        assert "%x" % big == "174876e800"
 
     def test_missing_cases(self):
-        print '%032d' % -123456789012345678901234567890L
-        assert '%032d' % -123456789012345678901234567890L == '-0123456789012345678901234567890'
+        big = -123456789012345678901234567890L
+        print '%032d' % big
+        assert '%032d' % big == '-0123456789012345678901234567890'
 
     def test_invalid_char(self):
-        raises(ValueError, 'u"%\u1234" % (4,)')
+        f = 4
+        raises(ValueError, 'u"%\u1234" % (f,)')



More information about the Pypy-commit mailing list