annotate Lib/test/test_pprint.py @ 25135:0f0016632be4 legacy-trunk

_formatparam(), set_param(): RFC 2231 encoding support by Oleg Broytmann in SF patch #600096. Specifically, the former function now encodes the triplets, while the latter adds optional charset and language arguments.
author Barry Warsaw <barry@python.org>
date Fri, 06 Sep 2002 03:55:04 +0000
parents dc66eb4cd17a
children 32e4c7753416
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1 import pprint
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
2 import unittest
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 22766
diff changeset
3 from test import test_support
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
4
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
5 try:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
6 uni = unicode
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
7 except NameError:
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
8 def uni(x):return x
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
9
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
10
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
11 class QueryTestCase(unittest.TestCase):
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
13 def setUp(self):
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
14 self.a = range(100)
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
15 self.b = range(200)
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
16 self.a[-12] = self.b
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
18 def test_basic(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
19 # Verify .isrecursive() and .isreadable() w/o recursion
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
20 verify = self.assert_
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
21 for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
22 self.a, self.b):
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
23 verify(not pprint.isrecursive(safe),
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
24 "expected not isrecursive for " + `safe`)
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
25 verify(pprint.isreadable(safe),
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
26 "expected isreadable for " + `safe`)
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
28 def test_knotted(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
29 # Verify .isrecursive() and .isreadable() w/ recursion
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
30 # Tie a knot.
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
31 self.b[67] = self.a
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
32 # Messy dict.
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
33 self.d = {}
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
34 self.d[0] = self.d[1] = self.d[2] = self.d
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
35
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
36 verify = self.assert_
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
37
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
38 for icky in self.a, self.b, self.d, (self.d, self.d):
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
39 verify(pprint.isrecursive(icky), "expected isrecursive")
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
40 verify(not pprint.isreadable(icky), "expected not isreadable")
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
41
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
42 # Break the cycles.
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
43 self.d.clear()
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
44 del self.a[:]
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
45 del self.b[:]
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
46
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
47 for safe in self.a, self.b, self.d, (self.d, self.d):
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
48 verify(not pprint.isrecursive(safe),
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
49 "expected not isrecursive for " + `safe`)
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
50 verify(pprint.isreadable(safe),
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
51 "expected isreadable for " + `safe`)
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
52
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
53 def test_unreadable(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
54 # Not recursive but not readable anyway
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
55 verify = self.assert_
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
56 for unreadable in type(3), pprint, pprint.isrecursive:
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
57 verify(not pprint.isrecursive(unreadable),
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
58 "expected not isrecursive for " + `unreadable`)
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
59 verify(not pprint.isreadable(unreadable),
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
60 "expected not isreadable for " + `unreadable`)
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
61
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
62 def test_same_as_repr(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
63 # Simple objects and small containers that should be same as repr()
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
64 verify = self.assert_
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
65 for simple in (0, 0L, 0+0j, 0.0, "", uni(""), (), [], {}, verify, pprint,
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
66 -6, -6L, -6-6j, -1.5, "x", uni("x"), (3,), [3], {3: 6},
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
67 (1,2), [3,4], {5: 6, 7: 8},
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
68 {"xy\tab\n": (3,), 5: [[]], (): {}},
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
69 range(10, -11, -1)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
70 ):
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
71 native = repr(simple)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
72 for function in "pformat", "saferepr":
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
73 f = getattr(pprint, function)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
74 got = f(simple)
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
75 verify(native == got, "expected %s got %s from pprint.%s" %
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
76 (native, got, function))
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
77
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
78
21061
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
79 def test_basic_line_wrap(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
80 # verify basic line-wrapping operation
21061
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
81 o = {'RPM_cal': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
82 'RPM_cal2': 48059,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
83 'Speed_cal': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
84 'controldesk_runtime_us': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
85 'main_code_runtime_us': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
86 'read_io_runtime_us': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
87 'write_io_runtime_us': 43690}
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
88 exp = """\
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
89 {'RPM_cal': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
90 'RPM_cal2': 48059,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
91 'Speed_cal': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
92 'controldesk_runtime_us': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
93 'main_code_runtime_us': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
94 'read_io_runtime_us': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
95 'write_io_runtime_us': 43690}"""
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
96 self.assertEqual(pprint.pformat(o), exp)
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
97
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
98 def test_subclassing(self):
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
99 o = {'names with spaces': 'should be presented using repr()',
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
100 'others.should.not.be': 'like.this'}
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
101 exp = """\
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
102 {'names with spaces': 'should be presented using repr()',
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
103 others.should.not.be: like.this}"""
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
104 self.assertEqual(DottedPrettyPrinter().pformat(o), exp)
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
105
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
106
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
107 class DottedPrettyPrinter(pprint.PrettyPrinter):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
108
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
109 def format(self, object, context, maxlevels, level):
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
110 if isinstance(object, str):
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
111 if ' ' in object:
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
112 return `object`, 1, 0
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
113 else:
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
114 return object, 0, 0
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
115 else:
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
116 return pprint.PrettyPrinter.format(
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
117 self, object, context, maxlevels, level)
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
118
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
119
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
120 def test_main():
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
121 test_support.run_unittest(QueryTestCase)
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
122
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
123
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
124 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
125 test_main()