-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add read-only access to the children of a TreeNode
See #1398.
- Loading branch information
1 parent
2c827e1
commit 0029470
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
from textual.widgets import Tree, TreeNode | ||
|
||
def label_of(node: TreeNode[None]): | ||
"""Get the label of a node. | ||
TODO: This is just a helper function to reduce the number of type | ||
errors, which can and will be remove once this code is merged with a | ||
version of main that also has the TreeNode.label PR merged. | ||
""" | ||
return str(node._label) | ||
|
||
|
||
def test_tree_node_children() -> None: | ||
"""A node's children property should act like an immutable list.""" | ||
CHILDREN=23 | ||
tree = Tree[None]("Root") | ||
for child in range(CHILDREN): | ||
tree.root.add(str(child)) | ||
assert len(tree.root.children)==CHILDREN | ||
for child in range(CHILDREN): | ||
assert label_of(tree.root.children[child]) == str(child) | ||
assert label_of(tree.root.children[0]) == "0" | ||
assert label_of(tree.root.children[-1]) == str(CHILDREN-1) | ||
assert [label_of(node) for node in tree.root.children] == [str(n) for n in range(CHILDREN)] | ||
assert [label_of(node) for node in tree.root.children[:2]] == [str(n) for n in range(2)] | ||
with pytest.raises(TypeError): | ||
tree.root.children[0] = tree.root.children[1] | ||
with pytest.raises(TypeError): | ||
del tree.root.children[0] | ||
with pytest.raises(TypeError): | ||
del tree.root.children[0:2] |