annotate Lib/test/test_pprint.py @ 32527:0da660e05e69 legacy-trunk

"#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD isn't always set to an integer value when it's defined.
author Tim Peters <tim.peters@gmail.com>
date Wed, 07 Jul 2004 20:42:07 +0000
parents 71a294bd7950
children 2eb8be0f8731
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
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
2 import test.test_support
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
3 import unittest
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:
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
8 def uni(x):
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
9 return x
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
10
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
11 # list, tuple and dict subclasses that do or don't overwrite __repr__
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
12 class list2(list):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
13 pass
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
14 class list3(list):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
15 def __repr__(self):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
16 return list.__repr__(self)
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
17 class tuple2(tuple):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
18 pass
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
19 class tuple3(tuple):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
20 def __repr__(self):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
21 return tuple.__repr__(self)
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
22 class dict2(dict):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
23 pass
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
24 class dict3(dict):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
25 def __repr__(self):
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
26 return dict.__repr__(self)
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
27
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
28 class QueryTestCase(unittest.TestCase):
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
29
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
30 def setUp(self):
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
31 self.a = range(100)
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
32 self.b = range(200)
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
33 self.a[-12] = self.b
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
35 def test_basic(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
36 # 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
37 verify = self.assert_
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
38 pp = pprint.PrettyPrinter()
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
39 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
40 self.a, self.b):
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
41 # module-level convenience functions
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
42 verify(not pprint.isrecursive(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
43 "expected not isrecursive for %r" % (safe,))
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
44 verify(pprint.isreadable(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
45 "expected isreadable for %r" % (safe,))
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
46 # PrettyPrinter methods
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
47 verify(not pp.isrecursive(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
48 "expected not isrecursive for %r" % (safe,))
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
49 verify(pp.isreadable(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
50 "expected isreadable for %r" % (safe,))
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
51
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
52 def test_knotted(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
53 # Verify .isrecursive() and .isreadable() w/ recursion
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
54 # Tie a knot.
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
55 self.b[67] = self.a
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
56 # Messy dict.
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
57 self.d = {}
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
58 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
59
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
60 verify = self.assert_
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
61 pp = pprint.PrettyPrinter()
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
62
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
63 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
64 verify(pprint.isrecursive(icky), "expected isrecursive")
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
65 verify(not pprint.isreadable(icky), "expected not isreadable")
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
66 verify(pp.isrecursive(icky), "expected isrecursive")
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
67 verify(not pp.isreadable(icky), "expected not isreadable")
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
68
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
69 # Break the cycles.
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
70 self.d.clear()
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
71 del self.a[:]
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
72 del self.b[:]
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
73
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
74 for safe in self.a, self.b, self.d, (self.d, self.d):
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
75 # module-level convenience functions
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
76 verify(not pprint.isrecursive(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
77 "expected not isrecursive for %r" % (safe,))
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
78 verify(pprint.isreadable(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
79 "expected isreadable for %r" % (safe,))
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
80 # PrettyPrinter methods
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
81 verify(not pp.isrecursive(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
82 "expected not isrecursive for %r" % (safe,))
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
83 verify(pp.isreadable(safe),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
84 "expected isreadable for %r" % (safe,))
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
85
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
86 def test_unreadable(self):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
87 # Not recursive but not readable anyway
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
88 verify = self.assert_
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
89 pp = pprint.PrettyPrinter()
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
90 for unreadable in type(3), pprint, pprint.isrecursive:
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
91 # module-level convenience functions
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
92 verify(not pprint.isrecursive(unreadable),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
93 "expected not isrecursive for %r" % (unreadable,))
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
94 verify(not pprint.isreadable(unreadable),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
95 "expected not isreadable for %r" % (unreadable,))
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
96 # PrettyPrinter methods
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
97 verify(not pp.isrecursive(unreadable),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
98 "expected not isrecursive for %r" % (unreadable,))
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
99 verify(not pp.isreadable(unreadable),
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
100 "expected not isreadable for %r" % (unreadable,))
17820
4bcf88e4abff SF bug[ #423781: pprint.isrecursive() broken.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
101
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
102 def test_same_as_repr(self):
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
103 # Simple objects, small containers and classes that overwrite __repr__
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
104 # For those the result should be the 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
105 verify = self.assert_
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
106 for simple in (0, 0L, 0+0j, 0.0, "", uni(""),
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
107 (), tuple2(), tuple3(),
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
108 [], list2(), list3(),
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
109 {}, dict2(), dict3(),
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
110 verify, pprint,
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 17829
diff changeset
111 -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
112 (1,2), [3,4], {5: 6, 7: 8},
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
113 tuple2((1,2)), tuple3((1,2)), tuple3(range(100)),
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
114 [3,4], list2([3,4]), list3([3,4]), list3(range(100)),
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
115 {5: 6, 7: 8}, dict2({5: 6, 7: 8}), dict3({5: 6, 7: 8}),
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
116 dict3([(x,x) for x in range(100)]),
17828
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
117 {"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
118 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
119 ):
26745053f840 pprint's workhorse _safe_repr() function took time quadratic in the # of
Tim Peters <tim.peters@gmail.com>
parents: 17827
diff changeset
120 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
121 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
122 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
123 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
124 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
125 (native, got, function))
17827
0b3da99824c7 Convert the pprint test to use PyUnit.
Fred Drake <fdrake@acm.org>
parents: 17820
diff changeset
126
21061
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
127 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
128 # 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
129 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
130 'RPM_cal2': 48059,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
131 'Speed_cal': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
132 '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
133 '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
134 '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
135 '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
136 exp = """\
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
137 {'RPM_cal': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
138 'RPM_cal2': 48059,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
139 'Speed_cal': 0,
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
140 '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
141 '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
142 '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
143 'write_io_runtime_us': 43690}"""
31084
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
144 for type in [dict, dict2]:
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
145 self.assertEqual(pprint.pformat(type(o)), exp)
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
146
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
147 o = range(100)
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
148 exp = '[%s]' % ',\n '.join(map(str, o))
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
149 for type in [list, list2]:
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
150 self.assertEqual(pprint.pformat(type(o)), exp)
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
151
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
152 o = tuple(range(100))
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
153 exp = '(%s)' % ',\n '.join(map(str, o))
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
154 for type in [tuple, tuple2]:
e3025fe70d8b Patch #750542: pprint now will pretty print subclasses of list, tuple
Walter Dörwald <walter@livinglogic.de>
parents: 26930
diff changeset
155 self.assertEqual(pprint.pformat(type(o)), exp)
21061
1802a9edb4ee __format(): Applied SF patch #482003 by Skip to fix multiline dict
Barry Warsaw <barry@python.org>
parents: 20013
diff changeset
156
31085
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
157 # indent parameter
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
158 o = range(100)
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
159 exp = '[ %s]' % ',\n '.join(map(str, o))
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
160 for type in [list, list2]:
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
161 self.assertEqual(pprint.pformat(type(o), indent=4), exp)
a2f6c6a67f24 Add parameters indent, width and depth to pprint.pprint() and pprint.pformat()
Walter Dörwald <walter@livinglogic.de>
parents: 31084
diff changeset
162
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
163 def test_subclassing(self):
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
164 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
165 '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
166 exp = """\
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
167 {'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
168 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
169 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
170
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
171
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
172 class DottedPrettyPrinter(pprint.PrettyPrinter):
24957
dc66eb4cd17a Standardize behavior: no docstrings in test functions.
Guido van Rossum <guido@python.org>
parents: 24364
diff changeset
173
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
174 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
175 if isinstance(object, str):
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
176 if ' ' in object:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 31085
diff changeset
177 return repr(object), 1, 0
22766
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
178 else:
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
179 return object, 0, 0
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
180 else:
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
181 return pprint.PrettyPrinter.format(
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
182 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
183
d26d8575ac62 Add an experimental mechanism to support extending the pprint formatting.
Fred Drake <fdrake@acm.org>
parents: 21061
diff changeset
184
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
185 def test_main():
26930
32e4c7753416 Make sure PrettyPrinter methods that mirror the module-level
Fred Drake <fdrake@acm.org>
parents: 24957
diff changeset
186 test.test_support.run_unittest(QueryTestCase)
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
187
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
188
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 19343
diff changeset
189 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
190 test_main()