Skip to content

Commit c92fbe1

Browse files
committed
Add unit tests on contructor, add/radd, and extend
1 parent 988acc5 commit c92fbe1

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

tests/test_tags.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,97 @@ def alter(x: TagNode) -> TagNode:
698698
assert cast_tag(x.children[1]).children[0] == "WORLD"
699699

700700

701+
def test_taglist_constructor():
702+
703+
# From docs.python.org/3/library/collections.html#collections.UserList:
704+
# > Subclasses of UserList are expected to offer a constructor which can be called
705+
# > with either no arguments or one argument. List operations which return a new
706+
# > sequence attempt to create an instance of the actual implementation class. To do
707+
# > so, it assumes that the constructor can be called with a single parameter, which
708+
# > is a sequence object used as a data source.
709+
710+
x = TagList()
711+
assert isinstance(x, TagList)
712+
assert len(x) == 0
713+
assert x.get_html_string() == ""
714+
715+
x = TagList("foo")
716+
assert isinstance(x, TagList)
717+
assert len(x) == 1
718+
assert x.get_html_string() == "foo"
719+
720+
x = TagList(["foo", "bar"])
721+
assert isinstance(x, TagList)
722+
assert len(x) == 2
723+
assert x.get_html_string() == "foobar"
724+
725+
# Also support multiple inputs
726+
x = TagList("foo", "bar")
727+
assert isinstance(x, TagList)
728+
assert len(x) == 2
729+
assert x.get_html_string() == "foobar"
730+
731+
732+
def test_taglist_add():
733+
734+
# Similar to `HTML(UserString)`, a `TagList(UserList)` should be the result when
735+
# adding to anything else.
736+
737+
empty_arr = []
738+
int_arr = [1]
739+
tl_foo = TagList("foo")
740+
tl_bar = TagList("bar")
741+
742+
def assert_tag_list(x: TagList, contents: list[str]) -> None:
743+
assert isinstance(x, TagList)
744+
assert len(x) == len(contents)
745+
for i, content_item in enumerate(contents):
746+
assert x[i] == content_item
747+
748+
# Make sure the TagLists are not altered over time
749+
assert len(empty_arr) == 0
750+
assert len(int_arr) == 1
751+
assert len(tl_foo) == 1
752+
assert len(tl_bar) == 1
753+
assert int_arr[0] == 1
754+
assert tl_foo[0] == "foo"
755+
assert tl_bar[0] == "bar"
756+
757+
assert_tag_list(empty_arr + tl_foo, ["foo"])
758+
assert_tag_list(tl_foo + empty_arr, ["foo"])
759+
assert_tag_list(int_arr + tl_foo, ["1", "foo"])
760+
assert_tag_list(tl_foo + int_arr, ["foo", "1"])
761+
assert_tag_list(tl_foo + tl_bar, ["foo", "bar"])
762+
assert_tag_list(tl_foo + "bar", ["foo", "bar"])
763+
assert_tag_list("foo" + tl_bar, ["foo", "bar"])
764+
765+
766+
def test_taglist_extend():
767+
x = TagList("foo")
768+
y = ["bar", "baz"]
769+
x.extend(y)
770+
assert isinstance(x, TagList)
771+
assert list(x) == ["foo", "bar", "baz"]
772+
assert y == ["bar", "baz"]
773+
774+
x = TagList("foo")
775+
y = TagList("bar", "baz")
776+
x.extend(y)
777+
assert isinstance(x, TagList)
778+
assert list(x) == ["foo", "bar", "baz"]
779+
assert list(y) == ["bar", "baz"]
780+
781+
x = TagList("foo")
782+
y = "bar"
783+
x.extend(y)
784+
assert list(x) == ["foo", "bar"]
785+
assert y == "bar"
786+
787+
x = TagList("foo")
788+
x.extend(TagList("bar"))
789+
assert list(x) == ["foo", "bar"]
790+
791+
701792
def test_taglist_flatten():
702793
x = div(1, TagList(2, TagList(span(3), 4)))
703794
assert list(x.children) == ["1", "2", span("3"), "4"]

0 commit comments

Comments
 (0)