PyXR

c:\python24\lib \ test \ test_pprint.py



0001 import pprint
0002 import test.test_support
0003 import unittest
0004 
0005 try:
0006     uni = unicode
0007 except NameError:
0008     def uni(x):
0009         return x
0010 
0011 # list, tuple and dict subclasses that do or don't overwrite __repr__
0012 class list2(list):
0013     pass
0014 class list3(list):
0015     def __repr__(self):
0016         return list.__repr__(self)
0017 class tuple2(tuple):
0018     pass
0019 class tuple3(tuple):
0020     def __repr__(self):
0021         return tuple.__repr__(self)
0022 class dict2(dict):
0023     pass
0024 class dict3(dict):
0025     def __repr__(self):
0026         return dict.__repr__(self)
0027 
0028 class QueryTestCase(unittest.TestCase):
0029 
0030     def setUp(self):
0031         self.a = range(100)
0032         self.b = range(200)
0033         self.a[-12] = self.b
0034 
0035     def test_basic(self):
0036         # Verify .isrecursive() and .isreadable() w/o recursion
0037         verify = self.assert_
0038         pp = pprint.PrettyPrinter()
0039         for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
0040                      self.a, self.b):
0041             # module-level convenience functions
0042             verify(not pprint.isrecursive(safe),
0043                    "expected not isrecursive for %r" % (safe,))
0044             verify(pprint.isreadable(safe),
0045                    "expected isreadable for %r" % (safe,))
0046             # PrettyPrinter methods
0047             verify(not pp.isrecursive(safe),
0048                    "expected not isrecursive for %r" % (safe,))
0049             verify(pp.isreadable(safe),
0050                    "expected isreadable for %r" % (safe,))
0051 
0052     def test_knotted(self):
0053         # Verify .isrecursive() and .isreadable() w/ recursion
0054         # Tie a knot.
0055         self.b[67] = self.a
0056         # Messy dict.
0057         self.d = {}
0058         self.d[0] = self.d[1] = self.d[2] = self.d
0059 
0060         verify = self.assert_
0061         pp = pprint.PrettyPrinter()
0062 
0063         for icky in self.a, self.b, self.d, (self.d, self.d):
0064             verify(pprint.isrecursive(icky), "expected isrecursive")
0065             verify(not pprint.isreadable(icky),  "expected not isreadable")
0066             verify(pp.isrecursive(icky), "expected isrecursive")
0067             verify(not pp.isreadable(icky),  "expected not isreadable")
0068 
0069         # Break the cycles.
0070         self.d.clear()
0071         del self.a[:]
0072         del self.b[:]
0073 
0074         for safe in self.a, self.b, self.d, (self.d, self.d):
0075             # module-level convenience functions
0076             verify(not pprint.isrecursive(safe),
0077                    "expected not isrecursive for %r" % (safe,))
0078             verify(pprint.isreadable(safe),
0079                    "expected isreadable for %r" % (safe,))
0080             # PrettyPrinter methods
0081             verify(not pp.isrecursive(safe),
0082                    "expected not isrecursive for %r" % (safe,))
0083             verify(pp.isreadable(safe),
0084                    "expected isreadable for %r" % (safe,))
0085 
0086     def test_unreadable(self):
0087         # Not recursive but not readable anyway
0088         verify = self.assert_
0089         pp = pprint.PrettyPrinter()
0090         for unreadable in type(3), pprint, pprint.isrecursive:
0091             # module-level convenience functions
0092             verify(not pprint.isrecursive(unreadable),
0093                    "expected not isrecursive for %r" % (unreadable,))
0094             verify(not pprint.isreadable(unreadable),
0095                    "expected not isreadable for %r" % (unreadable,))
0096             # PrettyPrinter methods
0097             verify(not pp.isrecursive(unreadable),
0098                    "expected not isrecursive for %r" % (unreadable,))
0099             verify(not pp.isreadable(unreadable),
0100                    "expected not isreadable for %r" % (unreadable,))
0101 
0102     def test_same_as_repr(self):
0103         # Simple objects, small containers and classes that overwrite __repr__
0104         # For those the result should be the same as repr()
0105         verify = self.assert_
0106         for simple in (0, 0L, 0+0j, 0.0, "", uni(""),
0107                        (), tuple2(), tuple3(),
0108                        [], list2(), list3(),
0109                        {}, dict2(), dict3(),
0110                        verify, pprint,
0111                        -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
0112                        (1,2), [3,4], {5: 6, 7: 8},
0113                        tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
0114                        [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
0115                        {5: 6, 7: 8}, dict2({5: 6, 7: 8}), dict3({5: 6, 7: 8}),
0116                        dict3([(x,x) for x in range(100)]),
0117                        {"xy\tab\n": (3,), 5: [[]], (): {}},
0118                        range(10, -11, -1)
0119                       ):
0120             native = repr(simple)
0121             for function in "pformat", "saferepr":
0122                 f = getattr(pprint, function)
0123                 got = f(simple)
0124                 verify(native == got, "expected %s got %s from pprint.%s" %
0125                                       (native, got, function))
0126 
0127     def test_basic_line_wrap(self):
0128         # verify basic line-wrapping operation
0129         o = {'RPM_cal': 0,
0130              'RPM_cal2': 48059,
0131              'Speed_cal': 0,
0132              'controldesk_runtime_us': 0,
0133              'main_code_runtime_us': 0,
0134              'read_io_runtime_us': 0,
0135              'write_io_runtime_us': 43690}
0136         exp = """\
0137 {'RPM_cal': 0,
0138  'RPM_cal2': 48059,
0139  'Speed_cal': 0,
0140  'controldesk_runtime_us': 0,
0141  'main_code_runtime_us': 0,
0142  'read_io_runtime_us': 0,
0143  'write_io_runtime_us': 43690}"""
0144         for type in [dict, dict2]:
0145             self.assertEqual(pprint.pformat(type(o)), exp)
0146 
0147         o = range(100)
0148         exp = '[%s]' % ',\n '.join(map(str, o))
0149         for type in [list, list2]:
0150             self.assertEqual(pprint.pformat(type(o)), exp)
0151 
0152         o = tuple(range(100))
0153         exp = '(%s)' % ',\n '.join(map(str, o))
0154         for type in [tuple, tuple2]:
0155             self.assertEqual(pprint.pformat(type(o)), exp)
0156 
0157         # indent parameter
0158         o = range(100)
0159         exp = '[   %s]' % ',\n    '.join(map(str, o))
0160         for type in [list, list2]:
0161             self.assertEqual(pprint.pformat(type(o), indent=4), exp)
0162 
0163     def test_subclassing(self):
0164         o = {'names with spaces': 'should be presented using repr()',
0165              'others.should.not.be': 'like.this'}
0166         exp = """\
0167 {'names with spaces': 'should be presented using repr()',
0168  others.should.not.be: like.this}"""
0169         self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
0170 
0171 
0172 class DottedPrettyPrinter(pprint.PrettyPrinter):
0173 
0174     def format(self, object, context, maxlevels, level):
0175         if isinstance(object, str):
0176             if ' ' in object:
0177                 return repr(object), 1, 0
0178             else:
0179                 return object, 0, 0
0180         else:
0181             return pprint.PrettyPrinter.format(
0182                 self, object, context, maxlevels, level)
0183 
0184 
0185 def test_main():
0186     test.test_support.run_unittest(QueryTestCase)
0187 
0188 
0189 if __name__ == "__main__":
0190     test_main()
0191 

Generated by PyXR 0.9.4
SourceForge.net Logo