-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathtest_repr.py
More file actions
155 lines (115 loc) · 3.67 KB
/
test_repr.py
File metadata and controls
155 lines (115 loc) · 3.67 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from typing import Optional
import pytest
import rich.repr
from rich.console import Console
from inspect import Parameter
@rich.repr.auto
class Foo:
def __init__(self, foo: str, bar: Optional[int] = None, egg: int = 1):
self.foo = foo
self.bar = bar
self.egg = egg
def __rich_repr__(self):
yield self.foo
yield None, self.foo,
yield "bar", self.bar, None
yield "egg", self.egg
@rich.repr.auto
class Egg:
def __init__(self, foo: str, bar: Optional[int] = None, egg: int = 1):
self.foo = foo
self.bar = bar
self.egg = egg
@rich.repr.auto
class BrokenEgg:
def __init__(self, foo: str, *, bar: Optional[int] = None, egg: int = 1):
self.foo = foo
self.fubar = bar
self.egg = egg
@rich.repr.auto(angular=True)
class AngularEgg:
def __init__(self, foo: str, *, bar: Optional[int] = None, egg: int = 1):
self.foo = foo
self.bar = bar
self.egg = egg
@rich.repr.auto
class Bar(Foo):
def __rich_repr__(self):
yield (self.foo,)
yield None, self.foo,
yield "bar", self.bar, None
yield "egg", self.egg
__rich_repr__.angular = True
class StupidClass:
def __init__(self, a):
self.a = a
def __eq__(self, other) -> bool:
if other is Parameter.empty:
return True
try:
return self.a == other.a
except Exception:
return False
def __ne__(self, other: object) -> bool:
return not self.__eq__(other)
class NotStupid:
pass
@rich.repr.auto
class Bird:
def __init__(
self, name, eats, fly=True, another=StupidClass(2), extinct=NotStupid()
):
self.name = name
self.eats = eats
self.fly = fly
self.another = another
self.extinct = extinct
def test_rich_repr() -> None:
assert (repr(Foo("hello"))) == "Foo('hello', 'hello', egg=1)"
assert (repr(Foo("hello", bar=3))) == "Foo('hello', 'hello', bar=3, egg=1)"
def test_rich_repr_positional_only() -> None:
_locals = locals().copy()
exec(
"""\
@rich.repr.auto
class PosOnly:
def __init__(self, foo, /):
self.foo = 1
""",
globals(),
_locals,
)
p = _locals["PosOnly"](1)
assert repr(p) == "PosOnly(1)"
def test_rich_angular() -> None:
assert (repr(Bar("hello"))) == "<Bar 'hello' 'hello' egg=1>"
assert (repr(Bar("hello", bar=3))) == "<Bar 'hello' 'hello' bar=3 egg=1>"
def test_rich_repr_auto() -> None:
assert repr(Egg("hello", egg=2)) == "Egg('hello', egg=2)"
stupid_class = StupidClass(9)
not_stupid = NotStupid()
assert (
repr(Bird("penguin", ["fish"], another=stupid_class, extinct=not_stupid))
== f"Bird('penguin', ['fish'], another={repr(stupid_class)}, extinct={repr(not_stupid)})"
)
def test_rich_repr_auto_with_tuples() -> None:
assert repr(Egg(('hello', 'world'), egg=2)) == "Egg(('hello', 'world'), egg=2)"
def test_rich_repr_auto_angular() -> None:
assert repr(AngularEgg("hello", egg=2)) == "<AngularEgg 'hello' egg=2>"
def test_broken_egg() -> None:
with pytest.raises(rich.repr.ReprError):
repr(BrokenEgg("foo"))
def test_rich_pretty() -> None:
console = Console()
with console.capture() as capture:
console.print(Foo("hello", bar=3))
result = capture.get()
expected = "Foo('hello', 'hello', bar=3, egg=1)\n"
assert result == expected
def test_rich_pretty_angular() -> None:
console = Console()
with console.capture() as capture:
console.print(Bar("hello", bar=3))
result = capture.get()
expected = "<Bar 'hello' 'hello' bar=3 egg=1>\n"
assert result == expected