Updated for hfsplus module, new gusi libs.
[python/dscho.git] / Lib / test / test_pprint.py
blob14626fb13fff5e53af70fcaf46b0ee0a6681eeae
1 import pprint
2 import unittest
4 import test_support
6 try:
7 uni = unicode
8 except NameError:
9 def uni(x):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 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
23 self.a, self.b):
24 verify(not pprint.isrecursive(safe),
25 "expected not isrecursive for " + `safe`)
26 verify(pprint.isreadable(safe),
27 "expected isreadable for " + `safe`)
29 def test_knotted(self):
30 """Verify .isrecursive() and .isreadable() w/ recursion."""
31 # Tie a knot.
32 self.b[67] = self.a
33 # Messy dict.
34 self.d = {}
35 self.d[0] = self.d[1] = self.d[2] = self.d
37 verify = self.assert_
39 for icky in self.a, self.b, self.d, (self.d, self.d):
40 verify(pprint.isrecursive(icky), "expected isrecursive")
41 verify(not pprint.isreadable(icky), "expected not isreadable")
43 # Break the cycles.
44 self.d.clear()
45 del self.a[:]
46 del self.b[:]
48 for safe in self.a, self.b, self.d, (self.d, self.d):
49 verify(not pprint.isrecursive(safe),
50 "expected not isrecursive for " + `safe`)
51 verify(pprint.isreadable(safe),
52 "expected isreadable for " + `safe`)
54 def test_unreadable(self):
55 """Not recursive but not readable anyway."""
56 verify = self.assert_
57 for unreadable in type(3), pprint, pprint.isrecursive:
58 verify(not pprint.isrecursive(unreadable),
59 "expected not isrecursive for " + `unreadable`)
60 verify(not pprint.isreadable(unreadable),
61 "expected not isreadable for " + `unreadable`)
63 def test_same_as_repr(self):
64 "Simple objects and small containers that should be same as repr()."
65 verify = self.assert_
66 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), (), [], {}, verify, pprint,
67 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
68 (1,2), [3,4], {5: 6, 7: 8},
69 {"xy\tab\n": (3,), 5: [[]], (): {}},
70 range(10, -11, -1)
72 native = repr(simple)
73 for function in "pformat", "saferepr":
74 f = getattr(pprint, function)
75 got = f(simple)
76 verify(native == got, "expected %s got %s from pprint.%s" %
77 (native, got, function))
80 def test_basic_line_wrap(self):
81 """verify basic line-wrapping operation"""
82 o = {'RPM_cal': 0,
83 'RPM_cal2': 48059,
84 'Speed_cal': 0,
85 'controldesk_runtime_us': 0,
86 'main_code_runtime_us': 0,
87 'read_io_runtime_us': 0,
88 'write_io_runtime_us': 43690}
89 exp = """\
90 {'RPM_cal': 0,
91 'RPM_cal2': 48059,
92 'Speed_cal': 0,
93 'controldesk_runtime_us': 0,
94 'main_code_runtime_us': 0,
95 'read_io_runtime_us': 0,
96 'write_io_runtime_us': 43690}"""
97 self.assertEqual(pprint.pformat(o), exp)
99 def test_main():
100 test_support.run_unittest(QueryTestCase)
103 if __name__ == "__main__":
104 test_main()