mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
* Add get_child_by_id and get_widget_by_id * Remove redundant code * Add unit tests for app-level get_child_by_id and get_widget_by_id * Remove redundant test fixture injection * Update CHANGELOG * Enforce uniqueness of ID amongst widget children * Enforce unique widget IDs amongst widgets mounted together * Update CHANGELOG.md * Ensuring unique IDs in a more logical place * Add docstring to NodeList._get_by_id * Dont use duplicate IDs in tests, dont mount 2000 widgets * Mounting less widgets in a unit test * Reword error message * Use lower-level depth first search in get_widget_by_id to break out early
108 lines
2.5 KiB
Python
108 lines
2.5 KiB
Python
import pytest
|
|
|
|
from textual.css.errors import StyleValueError
|
|
from textual.dom import DOMNode, BadIdentifier
|
|
|
|
|
|
def test_display_default():
|
|
node = DOMNode()
|
|
assert node.display is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"setter_value,style_value",
|
|
[[True, "block"], [False, "none"], ["block", "block"], ["none", "none"]],
|
|
)
|
|
def test_display_set_bool(setter_value, style_value):
|
|
node = DOMNode()
|
|
node.display = setter_value
|
|
assert node.styles.display == style_value
|
|
|
|
|
|
def test_display_set_invalid_value():
|
|
node = DOMNode()
|
|
with pytest.raises(StyleValueError):
|
|
node.display = "blah"
|
|
|
|
|
|
def test_validate():
|
|
with pytest.raises(BadIdentifier):
|
|
DOMNode(id="23")
|
|
with pytest.raises(BadIdentifier):
|
|
DOMNode(id=".3")
|
|
with pytest.raises(BadIdentifier):
|
|
DOMNode(classes="+2323")
|
|
with pytest.raises(BadIdentifier):
|
|
DOMNode(classes="foo 22")
|
|
|
|
node = DOMNode()
|
|
node.add_class("foo")
|
|
with pytest.raises(BadIdentifier):
|
|
node.add_class("1")
|
|
with pytest.raises(BadIdentifier):
|
|
node.remove_class("1")
|
|
with pytest.raises(BadIdentifier):
|
|
node.toggle_class("1")
|
|
|
|
|
|
@pytest.fixture
|
|
def search():
|
|
"""
|
|
a
|
|
/ \
|
|
b c
|
|
/ / \
|
|
d e f
|
|
"""
|
|
a = DOMNode(id="a")
|
|
b = DOMNode(id="b")
|
|
c = DOMNode(id="c")
|
|
d = DOMNode(id="d")
|
|
e = DOMNode(id="e")
|
|
f = DOMNode(id="f")
|
|
|
|
a._add_child(b)
|
|
a._add_child(c)
|
|
b._add_child(d)
|
|
c._add_child(e)
|
|
c._add_child(f)
|
|
|
|
yield a
|
|
|
|
|
|
def test_walk_children_depth(search):
|
|
children = [
|
|
node.id for node in search.walk_children(method="depth", with_self=False)
|
|
]
|
|
assert children == ["b", "d", "c", "e", "f"]
|
|
|
|
|
|
def test_walk_children_with_self_depth(search):
|
|
children = [
|
|
node.id for node in search.walk_children(method="depth", with_self=True)
|
|
]
|
|
assert children == ["a", "b", "d", "c", "e", "f"]
|
|
|
|
|
|
def test_walk_children_breadth(search):
|
|
children = [
|
|
node.id for node in search.walk_children(with_self=False, method="breadth")
|
|
]
|
|
print(children)
|
|
assert children == ["b", "c", "d", "e", "f"]
|
|
|
|
|
|
def test_walk_children_with_self_breadth(search):
|
|
children = [
|
|
node.id for node in search.walk_children(with_self=True, method="breadth")
|
|
]
|
|
print(children)
|
|
assert children == ["a", "b", "c", "d", "e", "f"]
|
|
|
|
children = [
|
|
node.id
|
|
for node in search.walk_children(with_self=True, method="breadth", reverse=True)
|
|
]
|
|
|
|
assert children == ["f", "e", "d", "c", "b", "a"]
|