-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpprint_test.py
More file actions
69 lines (56 loc) · 2.12 KB
/
pprint_test.py
File metadata and controls
69 lines (56 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Output formatting using `pprint`."""
from io import StringIO
import pprint
import json
from pathlib import Path
from _pytest.capture import CaptureFixture
def test_pp(capsys: CaptureFixture) -> None:
"""Using the shortcut `pp` function."""
colours = [["black", "cyan"], "white", ["green", "red", ["blue", "brown"]]]
pprint.pp(colours)
out, err = capsys.readouterr()
assert out == "[['black', 'cyan'], 'white', ['green', 'red', ['blue', 'brown']]]\n"
pprint.pp(colours, indent=4, width=30, depth=2)
out, err = capsys.readouterr()
assert (
out
== """\
[ ['black', 'cyan'],
'white',
['green', 'red', [...]]]\n"""
)
def test_pprint_pformat(capsys: CaptureFixture) -> None:
"""Using the shortcut pprint and pformat functions."""
colours = [["black", "cyan"], "white", ["green", "red", ["blue", "brown"]]]
pprint.pprint(colours)
out, err = capsys.readouterr()
assert out == "[['black', 'cyan'], 'white', ['green', 'red', ['blue', 'brown']]]\n"
output = StringIO()
pprint.pprint(colours, stream=output, indent=4, width=30, depth=2)
pprint_output = output.getvalue()
assert (
pprint_output
== """\
[ ['black', 'cyan'],
'white',
['green', 'red', [...]]]\n"""
)
assert pprint.pformat(colours, indent=4, width=30, depth=2) == pprint_output.strip()
def test_pretty_printer() -> None:
"""Using `PrettyPrinter` object and `pformat()` instance method."""
json_path = Path(__file__).parent.joinpath("pprint.json")
with open(json_path) as json_file:
project_info = json.load(json_file)
printer = pprint.PrettyPrinter(indent=2, width=60, depth=2)
assert (
printer.pformat(project_info)
== r"""{ 'info': { 'author': 'A. Random Developer',
'classifiers': [...],
'description': 'A sample Python project\n'
'=======================\n'
'\n'
'This is the description file '
'for the project.',
'name': 'sampleproject'},
'last_serial': 7562906}"""
)