Skip to content

Commit

Permalink
Add test that walk() alters objects in place
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed Oct 5, 2021
1 parent bac7fff commit 348fa17
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,30 @@ def test_html_save(snapshot):
desc = tags.meta(name="description", content="test")
doc = html_document(div("foo", dep), desc, lang="en")
snapshot.assert_match(saved_html(doc), "html_save_doc")


def test_tag_walk():
# walk() alters the tree in place, and also returns the altered object.
x = div("hello ", tags.i("world"))
y = div("The value of x is: ", x)

def alter(x: TagChild) -> TagChild:
if isinstance(x, str):
return x.upper()
elif isinstance(x, tag):
x._attrs["a"] = "foo"
if x.name == "i":
x.name = "b"

return x

res = x.walk(alter)

assert y.children[1] is x
assert x is res

assert x.get_attr("a") == "foo"
assert x.children[0] == "HELLO "
assert x.children[1].name == "b"
assert x.children[1].get_attr("a") == "foo"
assert x.children[1].children[0] == "WORLD"

0 comments on commit 348fa17

Please sign in to comment.