Debian Codegraph @

tests/test_repr.py

raw
1 from typing import Optional
2
3 import pytest
4
5 import rich.repr
6 from rich.console import Console
7
8 from inspect import Parameter
9
10
11 @rich.repr.auto
12 class Foo:
13 def __init__(self, foo: str, bar: Optional[int] = None, egg: int = 1):
14 self.foo = foo
15 self.bar = bar
16 self.egg = egg
17
18 def __rich_repr__(self):
19 yield self.foo
20 yield None, self.foo,
21 yield "bar", self.bar, None
22 yield "egg", self.egg
23
24
25 @rich.repr.auto
26 class Egg:
27 def __init__(self, foo: str, bar: Optional[int] = None, egg: int = 1):
28 self.foo = foo
29 self.bar = bar
30 self.egg = egg
31
32
33 @rich.repr.auto
34 class BrokenEgg:
35 def __init__(self, foo: str, *, bar: Optional[int] = None, egg: int = 1):
36 self.foo = foo
37 self.fubar = bar
38 self.egg = egg
39
40
41 @rich.repr.auto(angular=True)
42 class AngularEgg:
43 def __init__(self, foo: str, *, bar: Optional[int] = None, egg: int = 1):
44 self.foo = foo
45 self.bar = bar
46 self.egg = egg
47
48
49 @rich.repr.auto
50 class Bar(Foo):
51 def __rich_repr__(self):
52 yield (self.foo,)
53 yield None, self.foo,
54 yield "bar", self.bar, None
55 yield "egg", self.egg
56
57 __rich_repr__.angular = True
58
59
60 class StupidClass:
61 def __init__(self, a):
62 self.a = a
63
64 def __eq__(self, other) -> bool:
65 if other is Parameter.empty:
66 return True
67 try:
68 return self.a == other.a
69 except Exception:
70 return False
71
72 def __ne__(self, other: object) -> bool:
73 return not self.__eq__(other)
74
75
76 class NotStupid:
77 pass
78
79
80 @rich.repr.auto
81 class Bird:
82 def __init__(
83 self, name, eats, fly=True, another=StupidClass(2), extinct=NotStupid()
84 ):
85 self.name = name
86 self.eats = eats
87 self.fly = fly
88 self.another = another
89 self.extinct = extinct
90
91
92 def test_rich_repr() -> None:
93 assert (repr(Foo("hello"))) == "Foo('hello', 'hello', egg=1)"
94 assert (repr(Foo("hello", bar=3))) == "Foo('hello', 'hello', bar=3, egg=1)"
95
96
97 def test_rich_repr_positional_only() -> None:
98 _locals = locals().copy()
99 exec(
100 """\
101 @rich.repr.auto
102 class PosOnly:
103 def __init__(self, foo, /):
104 self.foo = 1
105 """,
106 globals(),
107 _locals,
108 )
109 p = _locals["PosOnly"](1)
110 assert repr(p) == "PosOnly(1)"
111
112
113 def test_rich_angular() -> None:
114 assert (repr(Bar("hello"))) == "<Bar 'hello' 'hello' egg=1>"
115 assert (repr(Bar("hello", bar=3))) == "<Bar 'hello' 'hello' bar=3 egg=1>"
116
117
118 def test_rich_repr_auto() -> None:
119 assert repr(Egg("hello", egg=2)) == "Egg('hello', egg=2)"
120 stupid_class = StupidClass(9)
121 not_stupid = NotStupid()
122 assert (
123 repr(Bird("penguin", ["fish"], another=stupid_class, extinct=not_stupid))
124 == f"Bird('penguin', ['fish'], another={repr(stupid_class)}, extinct={repr(not_stupid)})"
125 )
126
127
128 def test_rich_repr_auto_angular() -> None:
129 assert repr(AngularEgg("hello", egg=2)) == "<AngularEgg 'hello' egg=2>"
130
131
132 def test_broken_egg() -> None:
133 with pytest.raises(rich.repr.ReprError):
134 repr(BrokenEgg("foo"))
135
136
137 def test_rich_pretty() -> None:
138 console = Console()
139 with console.capture() as capture:
140 console.print(Foo("hello", bar=3))
141 result = capture.get()
142 expected = "Foo('hello', 'hello', bar=3, egg=1)\n"
143 assert result == expected
144
145
146 def test_rich_pretty_angular() -> None:
147 console = Console()
148 with console.capture() as capture:
149 console.print(Bar("hello", bar=3))
150 result = capture.get()
151 expected = "<Bar 'hello' 'hello' bar=3 egg=1>\n"
152 assert result == expected