Maintain backwards compatibility with python < 2.3 by dynamically
[python/dscho.git] / Lib / test / test_pprint.py
blob0e6559d6b5fef5a6353dc26e63bd863ee6dae211
1 import pprint
2 import test.test_support
3 import unittest
5 try:
6 uni = unicode
7 except NameError:
8 def uni(x):
9 return x
12 class QueryTestCase(unittest.TestCase):
14 def setUp(self):
15 self.a = range(100)
16 self.b = range(200)
17 self.a[-12] = self.b
19 def test_basic(self):
20 # Verify .isrecursive() and .isreadable() w/o recursion
21 verify = self.assert_
22 pp = pprint.PrettyPrinter()
23 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
24 self.a, self.b):
25 # module-level convenience functions
26 verify(not pprint.isrecursive(safe),
27 "expected not isrecursive for " + `safe`)
28 verify(pprint.isreadable(safe),
29 "expected isreadable for " + `safe`)
30 # PrettyPrinter methods
31 verify(not pp.isrecursive(safe),
32 "expected not isrecursive for " + `safe`)
33 verify(pp.isreadable(safe),
34 "expected isreadable for " + `safe`)
36 def test_knotted(self):
37 # Verify .isrecursive() and .isreadable() w/ recursion
38 # Tie a knot.
39 self.b[67] = self.a
40 # Messy dict.
41 self.d = {}
42 self.d[0] = self.d[1] = self.d[2] = self.d
44 verify = self.assert_
45 pp = pprint.PrettyPrinter()
47 for icky in self.a, self.b, self.d, (self.d, self.d):
48 verify(pprint.isrecursive(icky), "expected isrecursive")
49 verify(not pprint.isreadable(icky), "expected not isreadable")
50 verify(pp.isrecursive(icky), "expected isrecursive")
51 verify(not pp.isreadable(icky), "expected not isreadable")
53 # Break the cycles.
54 self.d.clear()
55 del self.a[:]
56 del self.b[:]
58 for safe in self.a, self.b, self.d, (self.d, self.d):
59 # module-level convenience functions
60 verify(not pprint.isrecursive(safe),
61 "expected not isrecursive for " + `safe`)
62 verify(pprint.isreadable(safe),
63 "expected isreadable for " + `safe`)
64 # PrettyPrinter methods
65 verify(not pp.isrecursive(safe),
66 "expected not isrecursive for " + `safe`)
67 verify(pp.isreadable(safe),
68 "expected isreadable for " + `safe`)
70 def test_unreadable(self):
71 # Not recursive but not readable anyway
72 verify = self.assert_
73 pp = pprint.PrettyPrinter()
74 for unreadable in type(3), pprint, pprint.isrecursive:
75 # module-level convenience functions
76 verify(not pprint.isrecursive(unreadable),
77 "expected not isrecursive for " + `unreadable`)
78 verify(not pprint.isreadable(unreadable),
79 "expected not isreadable for " + `unreadable`)
80 # PrettyPrinter methods
81 verify(not pp.isrecursive(unreadable),
82 "expected not isrecursive for " + `unreadable`)
83 verify(not pp.isreadable(unreadable),
84 "expected not isreadable for " + `unreadable`)
86 def test_same_as_repr(self):
87 # Simple objects and small containers that should be same as repr()
88 verify = self.assert_
89 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), (), [], {}, verify, pprint,
90 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
91 (1,2), [3,4], {5: 6, 7: 8},
92 {"xy\tab\n": (3,), 5: [[]], (): {}},
93 range(10, -11, -1)
95 native = repr(simple)
96 for function in "pformat", "saferepr":
97 f = getattr(pprint, function)
98 got = f(simple)
99 verify(native == got, "expected %s got %s from pprint.%s" %
100 (native, got, function))
103 def test_basic_line_wrap(self):
104 # verify basic line-wrapping operation
105 o = {'RPM_cal': 0,
106 'RPM_cal2': 48059,
107 'Speed_cal': 0,
108 'controldesk_runtime_us': 0,
109 'main_code_runtime_us': 0,
110 'read_io_runtime_us': 0,
111 'write_io_runtime_us': 43690}
112 exp = """\
113 {'RPM_cal': 0,
114 'RPM_cal2': 48059,
115 'Speed_cal': 0,
116 'controldesk_runtime_us': 0,
117 'main_code_runtime_us': 0,
118 'read_io_runtime_us': 0,
119 'write_io_runtime_us': 43690}"""
120 self.assertEqual(pprint.pformat(o), exp)
122 def test_subclassing(self):
123 o = {'names with spaces': 'should be presented using repr()',
124 'others.should.not.be': 'like.this'}
125 exp = """\
126 {'names with spaces': 'should be presented using repr()',
127 others.should.not.be: like.this}"""
128 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
131 class DottedPrettyPrinter(pprint.PrettyPrinter):
133 def format(self, object, context, maxlevels, level):
134 if isinstance(object, str):
135 if ' ' in object:
136 return `object`, 1, 0
137 else:
138 return object, 0, 0
139 else:
140 return pprint.PrettyPrinter.format(
141 self, object, context, maxlevels, level)
144 def test_main():
145 test.test_support.run_unittest(QueryTestCase)
148 if __name__ == "__main__":
149 test_main()