2020-04-19 13:59:10 +00:00
|
|
|
import io
|
2021-11-06 18:49:33 +00:00
|
|
|
import json
|
2020-04-19 13:59:10 +00:00
|
|
|
|
|
|
|
import rich
|
|
|
|
from rich.console import Console
|
|
|
|
|
|
|
|
|
2020-05-30 10:38:47 +00:00
|
|
|
def test_get_console():
|
|
|
|
console = rich.get_console()
|
|
|
|
assert isinstance(console, Console)
|
|
|
|
|
|
|
|
|
2020-12-13 12:23:28 +00:00
|
|
|
def test_reconfigure_console():
|
|
|
|
rich.reconfigure(width=100)
|
|
|
|
assert rich.get_console().width == 100
|
|
|
|
|
|
|
|
|
2020-04-19 13:59:10 +00:00
|
|
|
def test_rich_print():
|
2020-05-30 10:38:47 +00:00
|
|
|
console = rich.get_console()
|
2020-04-19 13:59:10 +00:00
|
|
|
output = io.StringIO()
|
2020-05-30 10:38:47 +00:00
|
|
|
backup_file = console.file
|
2020-04-19 13:59:10 +00:00
|
|
|
try:
|
2020-05-30 10:38:47 +00:00
|
|
|
console.file = output
|
2020-07-26 17:23:06 +00:00
|
|
|
rich.print("foo", "bar")
|
2020-07-26 17:20:45 +00:00
|
|
|
rich.print("foo\n")
|
|
|
|
rich.print("foo\n\n")
|
2020-07-26 17:23:06 +00:00
|
|
|
assert output.getvalue() == "foo bar\nfoo\n\nfoo\n\n\n"
|
2020-07-26 17:20:45 +00:00
|
|
|
finally:
|
|
|
|
console.file = backup_file
|
|
|
|
|
|
|
|
|
2021-08-28 14:04:06 +00:00
|
|
|
def test_rich_print_json():
|
|
|
|
console = rich.get_console()
|
|
|
|
with console.capture() as capture:
|
2021-08-29 14:34:13 +00:00
|
|
|
rich.print_json('[false, true, null, "foo"]', indent=4)
|
2021-08-28 14:04:06 +00:00
|
|
|
result = capture.get()
|
|
|
|
print(repr(result))
|
|
|
|
expected = '[\n false,\n true,\n null,\n "foo"\n]\n'
|
|
|
|
assert result == expected
|
|
|
|
|
|
|
|
|
2021-11-06 18:49:33 +00:00
|
|
|
def test_rich_print_json_round_trip():
|
|
|
|
data = ["x" * 100, 2e128]
|
|
|
|
console = rich.get_console()
|
|
|
|
with console.capture() as capture:
|
|
|
|
rich.print_json(data=data, indent=4)
|
|
|
|
result = capture.get()
|
|
|
|
print(repr(result))
|
|
|
|
result_data = json.loads(result)
|
|
|
|
assert result_data == data
|
|
|
|
|
|
|
|
|
|
|
|
def test_rich_print_json_no_truncation():
|
|
|
|
console = rich.get_console()
|
|
|
|
with console.capture() as capture:
|
|
|
|
rich.print_json(f'["{"x" * 100}", {int(2e128)}]', indent=4)
|
|
|
|
result = capture.get()
|
|
|
|
print(repr(result))
|
|
|
|
assert ("x" * 100) in result
|
|
|
|
assert str(int(2e128)) in result
|
|
|
|
|
|
|
|
|
2020-07-26 17:20:45 +00:00
|
|
|
def test_rich_print_X():
|
|
|
|
console = rich.get_console()
|
|
|
|
output = io.StringIO()
|
|
|
|
backup_file = console.file
|
|
|
|
try:
|
|
|
|
console.file = output
|
|
|
|
rich.print("foo")
|
|
|
|
rich.print("fooX")
|
|
|
|
rich.print("fooXX")
|
|
|
|
assert output.getvalue() == "foo\nfooX\nfooXX\n"
|
2020-04-19 13:59:10 +00:00
|
|
|
finally:
|
2020-05-30 10:38:47 +00:00
|
|
|
console.file = backup_file
|