Skip to content

Commit 53d7b5e

Browse files
Add some tests for the pprint module
1 parent 19934b2 commit 53d7b5e

File tree

1 file changed

+327
-5
lines changed

1 file changed

+327
-5
lines changed

testing/io/test_pprint.py

Lines changed: 327 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,330 @@
1+
import textwrap
2+
from collections import ChainMap
3+
from collections import Counter
4+
from collections import defaultdict
5+
from collections import deque
6+
from collections import OrderedDict
7+
from dataclasses import dataclass
8+
from types import MappingProxyType
9+
from types import SimpleNamespace
10+
from typing import Any
11+
12+
import pytest
113
from _pytest._io.pprint import PrettyPrinter
214

315

4-
def test_pformat_dispatch():
5-
printer = PrettyPrinter(width=5)
6-
assert printer.pformat("a") == "'a'"
7-
assert printer.pformat("a" * 10) == "'aaaaaaaaaa'"
8-
assert printer.pformat("foo bar") == "('foo '\n 'bar')"
16+
@dataclass
17+
class EmptyDataclass:
18+
pass
19+
20+
21+
@dataclass
22+
class DataclassWithOneItem:
23+
foo: str
24+
25+
26+
@dataclass
27+
class DataclassWithTwoItems:
28+
foo: str
29+
bar: str
30+
31+
32+
@pytest.mark.parametrize(
33+
("data", "expected"),
34+
(
35+
pytest.param(
36+
EmptyDataclass(),
37+
"EmptyDataclass()",
38+
id="dataclass-empty",
39+
),
40+
pytest.param(
41+
DataclassWithOneItem(foo="bar"),
42+
"""
43+
DataclassWithOneItem(foo='bar')
44+
""",
45+
id="dataclass-one-item",
46+
),
47+
pytest.param(
48+
DataclassWithTwoItems(foo="foo", bar="bar"),
49+
"""
50+
DataclassWithTwoItems(foo='foo',
51+
bar='bar')
52+
""",
53+
id="dataclass-two-items",
54+
),
55+
pytest.param(
56+
{},
57+
"{}",
58+
id="dict-empty",
59+
),
60+
pytest.param(
61+
{"one": 1},
62+
"""
63+
{'one': 1}
64+
""",
65+
id="dict-one-item",
66+
),
67+
pytest.param(
68+
{"one": 1, "two": 2},
69+
"""
70+
{'one': 1,
71+
'two': 2}
72+
""",
73+
id="dict-two-items",
74+
),
75+
pytest.param(OrderedDict(), "OrderedDict()", id="ordereddict-empty"),
76+
pytest.param(
77+
OrderedDict({"one": 1}),
78+
"""
79+
OrderedDict([('one',
80+
1)])
81+
""",
82+
id="ordereddict-one-item",
83+
),
84+
pytest.param(
85+
OrderedDict({"one": 1, "two": 2}),
86+
"""
87+
OrderedDict([('one',
88+
1),
89+
('two',
90+
2)])
91+
""",
92+
id="ordereddict-two-items",
93+
),
94+
pytest.param(
95+
[],
96+
"[]",
97+
id="list-empty",
98+
),
99+
pytest.param(
100+
[1],
101+
"""
102+
[1]
103+
""",
104+
id="list-one-item",
105+
),
106+
pytest.param(
107+
[1, 2],
108+
"""
109+
[1,
110+
2]
111+
""",
112+
id="list-two-items",
113+
),
114+
pytest.param(
115+
tuple(),
116+
"()",
117+
id="tuple-empty",
118+
),
119+
pytest.param(
120+
(1,),
121+
"""
122+
(1,)
123+
""",
124+
id="tuple-one-item",
125+
),
126+
pytest.param(
127+
(1, 2),
128+
"""
129+
(1,
130+
2)
131+
""",
132+
id="tuple-two-items",
133+
),
134+
pytest.param(
135+
set(),
136+
"set()",
137+
id="set-empty",
138+
),
139+
pytest.param(
140+
{1},
141+
"""
142+
{1}
143+
""",
144+
id="set-one-item",
145+
),
146+
pytest.param(
147+
{1, 2},
148+
"""
149+
{1,
150+
2}
151+
""",
152+
id="set-two-items",
153+
),
154+
pytest.param(
155+
MappingProxyType({}),
156+
"mappingproxy({})",
157+
id="mappingproxy-empty",
158+
),
159+
pytest.param(
160+
MappingProxyType({"one": 1}),
161+
"""
162+
mappingproxy({'one': 1})
163+
""",
164+
id="mappingproxy-one-item",
165+
),
166+
pytest.param(
167+
MappingProxyType({"one": 1, "two": 2}),
168+
"""
169+
mappingproxy({'one': 1,
170+
'two': 2})
171+
""",
172+
id="mappingproxy-two-items",
173+
),
174+
pytest.param(
175+
SimpleNamespace(),
176+
"namespace()",
177+
id="simplenamespace-empty",
178+
),
179+
pytest.param(
180+
SimpleNamespace(one=1),
181+
"""
182+
namespace(one=1)
183+
""",
184+
id="simplenamespace-one-item",
185+
),
186+
pytest.param(
187+
SimpleNamespace(one=1, two=2),
188+
"""
189+
namespace(one=1,
190+
two=2)
191+
""",
192+
id="simplenamespace-two-items",
193+
),
194+
pytest.param(
195+
defaultdict(str), "defaultdict(<class 'str'>, {})", id="defaultdict-empty"
196+
),
197+
pytest.param(
198+
defaultdict(str, {"one": "1"}),
199+
"""
200+
defaultdict(<class 'str'>,
201+
{'one': '1'})
202+
""",
203+
id="defaultdict-one-item",
204+
),
205+
pytest.param(
206+
defaultdict(str, {"one": "1", "two": "2"}),
207+
"""
208+
defaultdict(<class 'str'>,
209+
{'one': '1',
210+
'two': '2'})
211+
""",
212+
id="defaultdict-two-items",
213+
),
214+
pytest.param(
215+
Counter(),
216+
"Counter()",
217+
id="counter-empty",
218+
),
219+
pytest.param(
220+
Counter("1"),
221+
"""
222+
Counter({'1': 1})
223+
""",
224+
id="counter-one-item",
225+
),
226+
pytest.param(
227+
Counter("121"),
228+
"""
229+
Counter({'1': 2,
230+
'2': 1})
231+
""",
232+
id="counter-two-items",
233+
),
234+
pytest.param(ChainMap(), "ChainMap({})", id="chainmap-empty"),
235+
pytest.param(
236+
ChainMap({"one": 1, "two": 2}),
237+
"""
238+
ChainMap({'one': 1,
239+
'two': 2})
240+
""",
241+
id="chainmap-one-item",
242+
),
243+
pytest.param(
244+
ChainMap({"one": 1}, {"two": 2}),
245+
"""
246+
ChainMap({'one': 1},
247+
{'two': 2})
248+
""",
249+
id="chainmap-two-items",
250+
),
251+
pytest.param(
252+
deque(),
253+
"deque([])",
254+
id="deque-empty",
255+
),
256+
pytest.param(
257+
deque([1]),
258+
"""
259+
deque([1])
260+
""",
261+
id="deque-one-item",
262+
),
263+
pytest.param(
264+
deque([1, 2]),
265+
"""
266+
deque([1,
267+
2])
268+
""",
269+
id="deque-two-items",
270+
),
271+
pytest.param(
272+
deque([1, 2], maxlen=3),
273+
"""
274+
deque([1,
275+
2],
276+
maxlen=3)
277+
""",
278+
id="deque-maxlen",
279+
),
280+
pytest.param(
281+
{
282+
"chainmap": ChainMap({"one": 1}, {"two": 2}),
283+
"counter": Counter("122"),
284+
"dataclass": DataclassWithTwoItems(foo="foo", bar="bar"),
285+
"defaultdict": defaultdict(str, {"one": "1", "two": "2"}),
286+
"deque": deque([1, 2], maxlen=3),
287+
"dict": {"one": 1, "two": 2},
288+
"list": [1, 2],
289+
"mappingproxy": MappingProxyType({"one": 1, "two": 2}),
290+
"ordereddict": OrderedDict({"one": 1, "two": 2}),
291+
"set": {1, 2},
292+
"simplenamespace": SimpleNamespace(one=1, two=2),
293+
"tuple": (1, 2),
294+
},
295+
"""
296+
{'chainmap': ChainMap({'one': 1},
297+
{'two': 2}),
298+
'counter': Counter({'2': 2,
299+
'1': 1}),
300+
'dataclass': DataclassWithTwoItems(foo='foo',
301+
bar='bar'),
302+
'defaultdict': defaultdict(<class 'str'>,
303+
{'one': '1',
304+
'two': '2'}),
305+
'deque': deque([1,
306+
2],
307+
maxlen=3),
308+
'dict': {'one': 1,
309+
'two': 2},
310+
'list': [1,
311+
2],
312+
'mappingproxy': mappingproxy({'one': 1,
313+
'two': 2}),
314+
'ordereddict': OrderedDict([('one',
315+
1),
316+
('two',
317+
2)]),
318+
'set': {1,
319+
2},
320+
'simplenamespace': namespace(one=1,
321+
two=2),
322+
'tuple': (1,
323+
2)}
324+
""",
325+
id="deep-example",
326+
),
327+
),
328+
)
329+
def test_consistent_pretty_printer(data: Any, expected: str) -> None:
330+
assert PrettyPrinter().pformat(data) == textwrap.dedent(expected).strip()

0 commit comments

Comments
 (0)