Skip to content

Commit 21734be

Browse files
committed
Improve the pretty print test protocol
1 parent 3073891 commit 21734be

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

Lib/test/test_pprint.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,6 @@ def __hash__(self):
135135
return self._hash
136136

137137

138-
class CustomPrintable:
139-
def __init__(self, name="my pprint", value=42):
140-
self.name = name
141-
self.value = value
142-
143-
def __str__(self):
144-
return "my str"
145-
146-
def __repr__(self):
147-
return "my str"
148-
149-
def __pprint__(self):
150-
yield self.name
151-
yield ("value", self.value)
152-
153-
154138
class QueryTestCase(unittest.TestCase):
155139

156140
def setUp(self):
@@ -1490,10 +1474,18 @@ def test_user_string(self):
14901474
'lazy dog'}""")
14911475

14921476
def test_custom_pprinter(self):
1477+
# Test __pprint__ with positional and keyword argument.
1478+
class CustomPrintable:
1479+
def __init__(self, name="my pprint", value=42, is_custom=True):
1480+
self.name = name
1481+
self.value = value
1482+
1483+
def __pprint__(self):
1484+
yield self.name
1485+
yield "value", self.value
1486+
14931487
stream = io.StringIO()
1494-
pp = pprint.PrettyPrinter(stream=stream)
1495-
custom_obj = CustomPrintable()
1496-
pp.pprint(custom_obj)
1488+
pprint.pprint(CustomPrintable(), stream=stream)
14971489
self.assertEqual(stream.getvalue(), "CustomPrintable('my pprint', value=42)\n")
14981490

14991491
def test_pprint_protocol_positional(self):
@@ -1505,7 +1497,10 @@ def __init__(self, x, y):
15051497
def __pprint__(self):
15061498
yield self.x
15071499
yield self.y
1508-
self.assertEqual(pprint.pformat(Point(1, 2)), "Point(1, 2)")
1500+
1501+
stream = io.StringIO()
1502+
pprint.pprint(Point(1, 2), stream=stream)
1503+
self.assertEqual(stream.getvalue(), "Point(1, 2)\n")
15091504

15101505
def test_pprint_protocol_keyword(self):
15111506
# Test __pprint__ with keyword arguments
@@ -1516,39 +1511,49 @@ def __init__(self, host, port):
15161511
def __pprint__(self):
15171512
yield ("host", self.host)
15181513
yield ("port", self.port)
1519-
self.assertEqual(pprint.pformat(Config("localhost", 8080)),
1520-
"Config(host='localhost', port=8080)")
1514+
1515+
stream = io.StringIO()
1516+
pprint.pprint(Config("localhost", 8080), stream=stream)
1517+
self.assertEqual(stream.getvalue(), "Config(host='localhost', port=8080)\n")
15211518

15221519
def test_pprint_protocol_default(self):
15231520
# Test __pprint__ with default values (3-tuple form)
1524-
class Bird:
1525-
def __init__(self, name, fly=True, extinct=False):
1526-
self.name = name
1527-
self.fly = fly
1528-
self.extinct = extinct
1521+
class Bass:
1522+
def __init__(self, strings: int, pickups: str, active: bool=False):
1523+
self._strings = strings
1524+
self._pickups = pickups
1525+
self._active = active
1526+
15291527
def __pprint__(self):
1530-
yield self.name
1531-
yield ("fly", self.fly, True) # hide if True
1532-
yield ("extinct", self.extinct, False) # hide if False
1528+
yield self._strings
1529+
yield 'pickups', self._pickups
1530+
yield 'active', self._active, False
15331531

1534-
# Defaults should be hidden
1535-
self.assertEqual(pprint.pformat(Bird("sparrow")),
1536-
"Bird('sparrow')")
1537-
# Non-defaults should be shown
1538-
self.assertEqual(pprint.pformat(Bird("dodo", fly=False, extinct=True)),
1539-
"Bird('dodo', fly=False, extinct=True)")
1532+
# Defaults should be hidden if the value is equal to the default.
1533+
stream = io.StringIO()
1534+
pprint.pprint(Bass(4, 'split coil P'), stream=stream)
1535+
self.assertEqual(stream.getvalue(), "Bass(4, pickups='split coil P')\n")
1536+
# Show the argument if the value is not equal to the default.
1537+
stream = io.StringIO()
1538+
pprint.pprint(Bass(5, 'humbucker', active=True), stream=stream)
1539+
self.assertEqual(stream.getvalue(), "Bass(5, pickups='humbucker', active=True)\n")
15401540

15411541
def test_pprint_protocol_nested(self):
1542-
# Test __pprint__ with nested objects
1542+
# Test __pprint__ with nested objects.
15431543
class Container:
15441544
def __init__(self, items):
15451545
self.items = items
15461546
def __pprint__(self):
1547-
yield ("items", self.items)
1547+
yield "items", self.items
1548+
1549+
stream = io.StringIO()
15481550
c = Container([1, 2, 3])
1549-
self.assertEqual(pprint.pformat(c), "Container(items=[1, 2, 3])")
1551+
pprint.pprint(c, stream=stream)
1552+
self.assertEqual(stream.getvalue(), "Container(items=[1, 2, 3])\n")
15501553
# Nested in a list
1551-
self.assertEqual(pprint.pformat([c]), "[Container(items=[1, 2, 3])]")
1554+
stream = io.StringIO()
1555+
pprint.pprint([c], stream=stream)
1556+
self.assertEqual(stream.getvalue(), "[Container(items=[1, 2, 3])]\n")
15521557

15531558
def test_pprint_protocol_isreadable(self):
15541559
# Test that isreadable works correctly with __pprint__

0 commit comments

Comments
 (0)