Debian Codegraph @

testing/io/test_pprint.py

raw
1 from __future__ import annotations
2
3 from collections import ChainMap
4 from collections import Counter
5 from collections import defaultdict
6 from collections import deque
7 from collections import OrderedDict
8 from dataclasses import dataclass
9 import textwrap
10 from types import MappingProxyType
11 from types import SimpleNamespace
12 from typing import Any
13
14 from _pytest._io.pprint import PrettyPrinter
15 import pytest
16
17
18 @dataclass
19 class EmptyDataclass:
20 pass
21
22
23 @dataclass
24 class DataclassWithOneItem:
25 foo: str
26
27
28 @dataclass
29 class DataclassWithTwoItems:
30 foo: str
31 bar: str
32
33
34 @pytest.mark.parametrize(
35 ("data", "expected"),
36 (
37 pytest.param(
38 EmptyDataclass(),
39 "EmptyDataclass()",
40 id="dataclass-empty",
41 ),
42 pytest.param(
43 DataclassWithOneItem(foo="bar"),
44 """
45 DataclassWithOneItem(
46 foo='bar',
47 )
48 """,
49 id="dataclass-one-item",
50 ),
51 pytest.param(
52 DataclassWithTwoItems(foo="foo", bar="bar"),
53 """
54 DataclassWithTwoItems(
55 foo='foo',
56 bar='bar',
57 )
58 """,
59 id="dataclass-two-items",
60 ),
61 pytest.param(
62 {},
63 "{}",
64 id="dict-empty",
65 ),
66 pytest.param(
67 {"one": 1},
68 """
69 {
70 'one': 1,
71 }
72 """,
73 id="dict-one-item",
74 ),
75 pytest.param(
76 {"one": 1, "two": 2},
77 """
78 {
79 'one': 1,
80 'two': 2,
81 }
82 """,
83 id="dict-two-items",
84 ),
85 pytest.param(OrderedDict(), "OrderedDict()", id="ordereddict-empty"),
86 pytest.param(
87 OrderedDict({"one": 1}),
88 """
89 OrderedDict({
90 'one': 1,
91 })
92 """,
93 id="ordereddict-one-item",
94 ),
95 pytest.param(
96 OrderedDict({"one": 1, "two": 2}),
97 """
98 OrderedDict({
99 'one': 1,
100 'two': 2,
101 })
102 """,
103 id="ordereddict-two-items",
104 ),
105 pytest.param(
106 [],
107 "[]",
108 id="list-empty",
109 ),
110 pytest.param(
111 [1],
112 """
113 [
114 1,
115 ]
116 """,
117 id="list-one-item",
118 ),
119 pytest.param(
120 [1, 2],
121 """
122 [
123 1,
124 2,
125 ]
126 """,
127 id="list-two-items",
128 ),
129 pytest.param(
130 tuple(),
131 "()",
132 id="tuple-empty",
133 ),
134 pytest.param(
135 (1,),
136 """
137 (
138 1,
139 )
140 """,
141 id="tuple-one-item",
142 ),
143 pytest.param(
144 (1, 2),
145 """
146 (
147 1,
148 2,
149 )
150 """,
151 id="tuple-two-items",
152 ),
153 pytest.param(
154 set(),
155 "set()",
156 id="set-empty",
157 ),
158 pytest.param(
159 {1},
160 """
161 {
162 1,
163 }
164 """,
165 id="set-one-item",
166 ),
167 pytest.param(
168 {1, 2},
169 """
170 {
171 1,
172 2,
173 }
174 """,
175 id="set-two-items",
176 ),
177 pytest.param(
178 MappingProxyType({}),
179 "mappingproxy({})",
180 id="mappingproxy-empty",
181 ),
182 pytest.param(
183 MappingProxyType({"one": 1}),
184 """
185 mappingproxy({
186 'one': 1,
187 })
188 """,
189 id="mappingproxy-one-item",
190 ),
191 pytest.param(
192 MappingProxyType({"one": 1, "two": 2}),
193 """
194 mappingproxy({
195 'one': 1,
196 'two': 2,
197 })
198 """,
199 id="mappingproxy-two-items",
200 ),
201 pytest.param(
202 SimpleNamespace(),
203 "namespace()",
204 id="simplenamespace-empty",
205 ),
206 pytest.param(
207 SimpleNamespace(one=1),
208 """
209 namespace(
210 one=1,
211 )
212 """,
213 id="simplenamespace-one-item",
214 ),
215 pytest.param(
216 SimpleNamespace(one=1, two=2),
217 """
218 namespace(
219 one=1,
220 two=2,
221 )
222 """,
223 id="simplenamespace-two-items",
224 ),
225 pytest.param(
226 defaultdict(str), "defaultdict(<class 'str'>, {})", id="defaultdict-empty"
227 ),
228 pytest.param(
229 defaultdict(str, {"one": "1"}),
230 """
231 defaultdict(<class 'str'>, {
232 'one': '1',
233 })
234 """,
235 id="defaultdict-one-item",
236 ),
237 pytest.param(
238 defaultdict(str, {"one": "1", "two": "2"}),
239 """
240 defaultdict(<class 'str'>, {
241 'one': '1',
242 'two': '2',
243 })
244 """,
245 id="defaultdict-two-items",
246 ),
247 pytest.param(
248 Counter(),
249 "Counter()",
250 id="counter-empty",
251 ),
252 pytest.param(
253 Counter("1"),
254 """
255 Counter({
256 '1': 1,
257 })
258 """,
259 id="counter-one-item",
260 ),
261 pytest.param(
262 Counter("121"),
263 """
264 Counter({
265 '1': 2,
266 '2': 1,
267 })
268 """,
269 id="counter-two-items",
270 ),
271 pytest.param(ChainMap(), "ChainMap({})", id="chainmap-empty"),
272 pytest.param(
273 ChainMap({"one": 1, "two": 2}),
274 """
275 ChainMap(
276 {
277 'one': 1,
278 'two': 2,
279 },
280 )
281 """,
282 id="chainmap-one-item",
283 ),
284 pytest.param(
285 ChainMap({"one": 1}, {"two": 2}),
286 """
287 ChainMap(
288 {
289 'one': 1,
290 },
291 {
292 'two': 2,
293 },
294 )
295 """,
296 id="chainmap-two-items",
297 ),
298 pytest.param(
299 deque(),
300 "deque([])",
301 id="deque-empty",
302 ),
303 pytest.param(
304 deque([1]),
305 """
306 deque([
307 1,
308 ])
309 """,
310 id="deque-one-item",
311 ),
312 pytest.param(
313 deque([1, 2]),
314 """
315 deque([
316 1,
317 2,
318 ])
319 """,
320 id="deque-two-items",
321 ),
322 pytest.param(
323 deque([1, 2], maxlen=3),
324 """
325 deque(maxlen=3, [
326 1,
327 2,
328 ])
329 """,
330 id="deque-maxlen",
331 ),
332 pytest.param(
333 {
334 "chainmap": ChainMap({"one": 1}, {"two": 2}),
335 "counter": Counter("122"),
336 "dataclass": DataclassWithTwoItems(foo="foo", bar="bar"),
337 "defaultdict": defaultdict(str, {"one": "1", "two": "2"}),
338 "deque": deque([1, 2], maxlen=3),
339 "dict": {"one": 1, "two": 2},
340 "list": [1, 2],
341 "mappingproxy": MappingProxyType({"one": 1, "two": 2}),
342 "ordereddict": OrderedDict({"one": 1, "two": 2}),
343 "set": {1, 2},
344 "simplenamespace": SimpleNamespace(one=1, two=2),
345 "tuple": (1, 2),
346 },
347 """
348 {
349 'chainmap': ChainMap(
350 {
351 'one': 1,
352 },
353 {
354 'two': 2,
355 },
356 ),
357 'counter': Counter({
358 '2': 2,
359 '1': 1,
360 }),
361 'dataclass': DataclassWithTwoItems(
362 foo='foo',
363 bar='bar',
364 ),
365 'defaultdict': defaultdict(<class 'str'>, {
366 'one': '1',
367 'two': '2',
368 }),
369 'deque': deque(maxlen=3, [
370 1,
371 2,
372 ]),
373 'dict': {
374 'one': 1,
375 'two': 2,
376 },
377 'list': [
378 1,
379 2,
380 ],
381 'mappingproxy': mappingproxy({
382 'one': 1,
383 'two': 2,
384 }),
385 'ordereddict': OrderedDict({
386 'one': 1,
387 'two': 2,
388 }),
389 'set': {
390 1,
391 2,
392 },
393 'simplenamespace': namespace(
394 one=1,
395 two=2,
396 ),
397 'tuple': (
398 1,
399 2,
400 ),
401 }
402 """,
403 id="deep-example",
404 ),
405 ),
406 )
407 def test_consistent_pretty_printer(data: Any, expected: str) -> None:
408 assert PrettyPrinter().pformat(data) == textwrap.dedent(expected).strip()