Skip to content

Commit

Permalink
test: add tests for PyTreeSpec.__new__()
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Oct 25, 2024
1 parent a1df59b commit 1a6a2dc
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/test_treespec.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,70 @@
)


def test_treespec_new():
with pytest.raises(TypeError):
optree.PyTreeSpec()

message_regex = r'The tree node traversal is empty\. \((function `.*` )?at file .*:\d+\)'

treespec = optree.PyTreeSpec.__new__(optree.PyTreeSpec)
with pytest.raises(SystemError, match=message_regex):
_ = treespec.num_nodes
with pytest.raises(SystemError, match=message_regex):
_ = treespec.num_children
with pytest.raises(SystemError, match=message_regex):
_ = treespec.num_leaves
assert treespec.none_is_leaf is False
assert treespec.namespace == ''
with pytest.raises(SystemError, match=message_regex):
_ = treespec.type
with pytest.raises(SystemError, match=message_regex):
_ = treespec.kind

with pytest.raises(SystemError, match=message_regex):
_ = treespec.paths()
with pytest.raises(SystemError, match=message_regex):
_ = treespec.accessors()
with pytest.raises(SystemError, match=message_regex):
_ = treespec.entries()
with pytest.raises(SystemError, match=message_regex):
_ = treespec.entry(0)
with pytest.raises(SystemError, match=message_regex):
_ = treespec.children()
with pytest.raises(SystemError, match=message_regex):
_ = treespec.child(0)

with pytest.raises(SystemError, match=message_regex):
_ = treespec.is_leaf()
with pytest.raises(SystemError, match=message_regex):
_ = treespec.is_prefix(optree.treespec_leaf(), strict=True)
with pytest.raises(SystemError, match=message_regex):
_ = treespec.is_prefix(optree.treespec_leaf(), strict=False)
with pytest.raises(SystemError, match=message_regex):
_ = treespec.is_suffix(optree.treespec_leaf(), strict=True)
with pytest.raises(SystemError, match=message_regex):
_ = treespec.is_suffix(optree.treespec_leaf(), strict=False)
with pytest.raises(SystemError, match=message_regex):
_ = treespec == optree.treespec_leaf()
with pytest.raises(SystemError, match=message_regex):
_ = treespec != optree.treespec_leaf()
with pytest.raises(SystemError, match=message_regex):
_ = treespec < optree.treespec_leaf()
with pytest.raises(SystemError, match=message_regex):
_ = treespec <= optree.treespec_leaf()
with pytest.raises(SystemError, match=message_regex):
_ = treespec > optree.treespec_leaf()
with pytest.raises(SystemError, match=message_regex):
_ = treespec >= optree.treespec_leaf()

with pytest.raises(SystemError, match=message_regex):
_ = repr(treespec)
with pytest.raises(SystemError, match=message_regex):
_ = hash(treespec)
with pytest.raises(SystemError, match=message_regex):
_ = len(treespec)


def test_treespec_equal_hash():
for i, tree1 in enumerate(TREES):
treespec1 = optree.tree_structure(tree1)
Expand Down

0 comments on commit 1a6a2dc

Please sign in to comment.