rich/tests/test_pretty.py

184 lines
5.1 KiB
Python
Raw Normal View History

2020-08-24 16:26:30 +00:00
from array import array
from collections import defaultdict
2021-03-06 14:14:06 +00:00
from dataclasses import dataclass, field
2020-08-08 10:47:10 +00:00
import io
import sys
2021-03-06 14:14:06 +00:00
from typing import List
2020-08-08 10:47:10 +00:00
from rich.console import Console
from rich.pretty import install, Pretty, pprint, pretty_repr, Node
2020-08-08 10:47:10 +00:00
def test_install():
console = Console(file=io.StringIO())
dh = sys.displayhook
install(console)
sys.displayhook("foo")
assert console.file.getvalue() == "'foo'\n"
assert sys.displayhook is not dh
def test_pretty():
test = {
"foo": [1, 2, 3, (4, 5, {6}, 7, 8, {9}), {}],
2020-08-24 16:26:30 +00:00
"bar": {"egg": "baz", "words": ["Hello World"] * 10},
2020-08-08 10:47:10 +00:00
False: "foo",
True: "",
"text": ("Hello World", "foo bar baz egg"),
}
2020-08-24 16:26:30 +00:00
result = pretty_repr(test, max_width=80)
print(result)
print(repr(result))
expected = "{\n 'foo': [1, 2, 3, (4, 5, {6}, 7, 8, {9}), {}],\n 'bar': {\n 'egg': 'baz',\n 'words': [\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World',\n 'Hello World'\n ]\n },\n False: 'foo',\n True: '',\n 'text': ('Hello World', 'foo bar baz egg')\n}"
2020-11-27 20:04:53 +00:00
print(expected)
2020-08-24 16:26:30 +00:00
assert result == expected
2020-08-08 10:47:10 +00:00
2021-03-06 14:14:06 +00:00
@dataclass
2021-03-06 14:25:23 +00:00
class ExampleDataclass:
2021-03-06 14:14:06 +00:00
foo: int
bar: str
ignore: int = field(repr=False)
baz: List[str] = field(default_factory=list)
def test_pretty_dataclass():
2021-03-06 14:25:23 +00:00
dc = ExampleDataclass(1000, "Hello, World", 999, ["foo", "bar", "baz"])
2021-03-06 14:14:06 +00:00
result = pretty_repr(dc, max_width=80)
print(repr(result))
assert (
result
2021-03-06 14:25:23 +00:00
== "ExampleDataclass(foo=1000, bar='Hello, World', baz=['foo', 'bar', 'baz'])"
2021-03-06 14:14:06 +00:00
)
result = pretty_repr(dc, max_width=16)
print(repr(result))
assert (
result
2021-03-06 14:25:23 +00:00
== "ExampleDataclass(\n foo=1000,\n bar='Hello, World',\n baz=[\n 'foo',\n 'bar',\n 'baz'\n ]\n)"
2021-03-06 14:14:06 +00:00
)
dc.bar = dc
result = pretty_repr(dc, max_width=80)
print(repr(result))
2021-03-06 14:25:23 +00:00
assert result == "ExampleDataclass(foo=1000, bar=..., baz=['foo', 'bar', 'baz'])"
2021-03-06 14:14:06 +00:00
2020-08-08 10:47:10 +00:00
def test_small_width():
test = ["Hello world! 12345"]
result = pretty_repr(test, max_width=10)
expected = "[\n 'Hello world! 12345'\n]"
2020-08-24 16:26:30 +00:00
assert result == expected
2020-08-08 10:47:10 +00:00
def test_broken_repr():
class BrokenRepr:
def __repr__(self):
1 / 0
test = [BrokenRepr()]
result = pretty_repr(test)
2020-08-24 16:26:30 +00:00
expected = "[<repr-error 'division by zero'>]"
assert result == expected
2020-08-08 14:57:01 +00:00
def test_recursive():
test = []
test.append(test)
result = pretty_repr(test)
expected = "[...]"
2020-08-24 16:26:30 +00:00
assert result == expected
def test_defaultdict():
test_dict = defaultdict(int, {"foo": 2})
result = pretty_repr(test_dict)
assert result == "defaultdict(<class 'int'>, {'foo': 2})"
def test_array():
test_array = array("I", [1, 2, 3])
result = pretty_repr(test_array)
assert result == "array('I', [1, 2, 3])"
def test_tuple_of_one():
assert pretty_repr((1,)) == "(1,)"
2020-10-05 14:30:29 +00:00
def test_node():
node = Node("abc")
assert pretty_repr(node) == "abc: "
2020-10-22 20:17:03 +00:00
def test_indent_lines():
console = Console(width=100, color_system=None)
console.begin_capture()
console.print(Pretty([100, 200], indent_guides=True), width=8)
expected = """\
[
100,
200
]
"""
result = console.end_capture()
print(repr(result))
print(result)
assert result == expected
def test_pprint():
console = Console(color_system=None)
console.begin_capture()
pprint(1, console=console)
assert console.end_capture() == "1\n"
def test_pprint_max_values():
console = Console(color_system=None)
console.begin_capture()
pprint([1, 2, 3, 4, 5, 6, 7, 8, 9, 0], console=console, max_length=2)
assert console.end_capture() == "[1, 2, ... +8]\n"
def test_pprint_max_items():
console = Console(color_system=None)
console.begin_capture()
pprint({"foo": 1, "bar": 2, "egg": 3}, console=console, max_length=2)
assert console.end_capture() == """{'foo': 1, 'bar': 2, ... +1}\n"""
2020-10-30 17:32:36 +00:00
def test_pprint_max_string():
console = Console(color_system=None)
console.begin_capture()
pprint(["Hello" * 20], console=console, max_string=8)
2020-11-03 09:53:18 +00:00
assert console.end_capture() == """['HelloHel'+92]\n"""
2020-11-27 20:04:53 +00:00
def test_tuples():
console = Console(color_system=None)
console.begin_capture()
pprint((1,), console=console)
pprint((1,), expand_all=True, console=console)
pprint(((1,),), expand_all=True, console=console)
result = console.end_capture()
print(repr(result))
expected = "(1,)\n(\n│ 1,\n)\n(\n│ (\n│ │ 1,\n│ ),\n)\n"
2020-11-27 20:06:59 +00:00
assert result == expected
2020-12-29 18:21:26 +00:00
def test_newline():
console = Console(color_system=None)
console.begin_capture()
console.print(Pretty((1,), insert_line=True, expand_all=True))
result = console.end_capture()
expected = "\n(\n 1,\n)\n"
assert result == expected
2021-02-20 11:05:21 +00:00
def test_empty_repr():
class Foo:
def __repr__(self):
return ""
2021-02-20 11:55:45 +00:00
assert pretty_repr(Foo()) == ""