Debian Codegraph @

tests/test_rich_print.py

raw
1 import io
2 import json
3
4 import rich
5 from rich.console import Console
6
7
8 def test_get_console():
9 console = rich.get_console()
10 assert isinstance(console, Console)
11
12
13 def test_reconfigure_console():
14 rich.reconfigure(width=100)
15 assert rich.get_console().width == 100
16
17
18 def test_rich_print():
19 console = rich.get_console()
20 output = io.StringIO()
21 backup_file = console.file
22 try:
23 console.file = output
24 rich.print("foo", "bar")
25 rich.print("foo\n")
26 rich.print("foo\n\n")
27 assert output.getvalue() == "foo bar\nfoo\n\nfoo\n\n\n"
28 finally:
29 console.file = backup_file
30
31
32 def test_rich_print_json():
33 console = rich.get_console()
34 with console.capture() as capture:
35 rich.print_json('[false, true, null, "foo"]', indent=4)
36 result = capture.get()
37 print(repr(result))
38 expected = '[\n false,\n true,\n null,\n "foo"\n]\n'
39 assert result == expected
40
41
42 def test_rich_print_json_round_trip():
43 data = ["x" * 100, 2e128]
44 console = rich.get_console()
45 with console.capture() as capture:
46 rich.print_json(data=data, indent=4)
47 result = capture.get()
48 print(repr(result))
49 result_data = json.loads(result)
50 assert result_data == data
51
52
53 def test_rich_print_json_no_truncation():
54 console = rich.get_console()
55 with console.capture() as capture:
56 rich.print_json(f'["{"x" * 100}", {int(2e128)}]', indent=4)
57 result = capture.get()
58 print(repr(result))
59 assert ("x" * 100) in result
60 assert str(int(2e128)) in result
61
62
63 def test_rich_print_X():
64 console = rich.get_console()
65 output = io.StringIO()
66 backup_file = console.file
67 try:
68 console.file = output
69 rich.print("foo")
70 rich.print("fooX")
71 rich.print("fooXX")
72 assert output.getvalue() == "foo\nfooX\nfooXX\n"
73 finally:
74 console.file = backup_file