Skip to content

Commit

Permalink
Add __h__() for simple representations
Browse files Browse the repository at this point in the history
  • Loading branch information
breuleux committed Jun 13, 2024
1 parent 8292237 commit 00baf4a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hrepr/hgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ def process(self, node: Union[str, TextFormatter]):

@ovld
def process(self, node: object):
return str(node)
if hasattr(node, "__h__"):
return self.process(node.__h__())
else:
return str(node)

@ovld
def process(self, node: Tag):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ def test_style():
)


class Hobbit:
def __h__(self):
return H.b("preciouss")


def test_dunder_h():
assert matches(H.div(Hobbit()), "<div><b>preciouss</b></div>")


@one_test_per_assert
def test_voids():
assert matches(H.area(), "<area />")
Expand Down

0 comments on commit 00baf4a

Please sign in to comment.