code
stringlengths 26
870k
| docstring
stringlengths 1
65.6k
| func_name
stringlengths 1
194
| language
stringclasses 1
value | repo
stringlengths 8
68
| path
stringlengths 5
194
| url
stringlengths 46
254
| license
stringclasses 4
values |
---|---|---|---|---|---|---|---|
def test_functions() -> None:
"""Test count parameters of functions."""
def foo(): ...
def bar(a): ...
def baz(a, b): ...
# repeat to allow for caching
for _ in range(3):
assert count_parameters(foo) == 0
assert count_parameters(bar) == 1
assert count_parameters(baz) == 2 | Test count parameters of functions. | test_functions | python | Textualize/textual | tests/test_count_parameters.py | https://github.com/Textualize/textual/blob/master/tests/test_count_parameters.py | MIT |
async def test_footer_bindings() -> None:
app_binding_count = 0
class TestWidget(Widget, can_focus=True):
BINDINGS = [
Binding("b", "widget_binding", "Overridden Binding"),
]
DEFAULT_CSS = """
TestWidget {
border: tall $background;
width: 50%;
height: 50%;
content-align: center middle;
&:focus {
border: tall $secondary;
}
}
"""
def action_widget_binding(self) -> None:
assert False, "should never be called since there is a priority binding"
class PriorityBindingApp(App):
BINDINGS = [
Binding("b", "app_binding", "Priority Binding", priority=True),
]
CSS = """
Screen {
align: center middle;
}
"""
def compose(self) -> ComposeResult:
yield TestWidget()
yield Button("Move Focus")
yield Footer()
def action_app_binding(self) -> None:
nonlocal app_binding_count
app_binding_count += 1
app = PriorityBindingApp()
async with app.run_test() as pilot:
await pilot.pause()
assert app_binding_count == 0
await pilot.click("Footer", offset=(1, 0))
assert app_binding_count == 1
await pilot.click("Footer")
assert app_binding_count == 2 | def action_widget_binding(self) -> None:
assert False, "should never be called since there is a priority binding"
class PriorityBindingApp(App):
BINDINGS = [
Binding("b", "app_binding", "Priority Binding", priority=True),
]
CSS = | test_footer_bindings | python | Textualize/textual | tests/footer/test_footer.py | https://github.com/Textualize/textual/blob/master/tests/footer/test_footer.py | MIT |
def compose(self) -> ComposeResult:
"""Compose the child widgets."""
yield MyTree("Root", id="test-tree") | Compose the child widgets. | compose | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def test_tree_node_selected_message() -> None:
"""Selecting a node should result in a selected message being emitted."""
async with TreeApp().run_test() as pilot:
await pilot.press("enter")
await pilot.pause()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeSelected", "test-tree"),
("NodeExpanded", "test-tree"),
] | Selecting a node should result in a selected message being emitted. | test_tree_node_selected_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def test_tree_node_selected_message_no_auto() -> None:
"""Selecting a node should result in only a selected message being emitted."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(MyTree).auto_expand = False
await pilot.press("enter")
await pilot.pause()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeSelected", "test-tree"),
] | Selecting a node should result in only a selected message being emitted. | test_tree_node_selected_message_no_auto | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def test_tree_node_expanded_message() -> None:
"""Expanding a node should result in an expanded message being emitted."""
async with TreeApp().run_test() as pilot:
await pilot.press("space")
await pilot.pause()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
] | Expanding a node should result in an expanded message being emitted. | test_tree_node_expanded_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def tree_node_expanded_by_code_message() -> None:
"""Expanding a node via the API should result in an expanded message being posted."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.children[0].expand()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
] | Expanding a node via the API should result in an expanded message being posted. | tree_node_expanded_by_code_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def tree_node_all_expanded_by_code_message() -> None:
"""Expanding all nodes via the API should result in expanded messages being posted."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.children[0].expand_all()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
] | Expanding all nodes via the API should result in expanded messages being posted. | tree_node_all_expanded_by_code_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def test_tree_node_collapsed_message() -> None:
"""Collapsing a node should result in a collapsed message being emitted."""
async with TreeApp().run_test() as pilot:
await pilot.press("space", "space")
await pilot.pause()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
("NodeCollapsed", "test-tree"),
] | Collapsing a node should result in a collapsed message being emitted. | test_tree_node_collapsed_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def tree_node_collapsed_by_code_message() -> None:
"""Collapsing a node via the API should result in a collapsed message being posted."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.children[0].expand().collapse()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
("NodeCollapsed", "test-tree"),
] | Collapsing a node via the API should result in a collapsed message being posted. | tree_node_collapsed_by_code_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def tree_node_all_collapsed_by_code_message() -> None:
"""Collapsing all nodes via the API should result in collapsed messages being posted."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.children[0].expand_all().collapse_all()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
("NodeCollapsed", "test-tree"),
] | Collapsing all nodes via the API should result in collapsed messages being posted. | tree_node_all_collapsed_by_code_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def tree_node_toggled_by_code_message() -> None:
"""Toggling a node twice via the API should result in expanded and collapsed messages."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.children[0].toggle().toggle()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
("NodeCollapsed", "test-tree"),
] | Toggling a node twice via the API should result in expanded and collapsed messages. | tree_node_toggled_by_code_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def tree_node_all_toggled_by_code_message() -> None:
"""Toggling all nodes twice via the API should result in expanded and collapsed messages."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.children[0].toggle_all().toggle_all()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
("NodeCollapsed", "test-tree"),
] | Toggling all nodes twice via the API should result in expanded and collapsed messages. | tree_node_all_toggled_by_code_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def test_tree_node_highlighted_message() -> None:
"""Highlighting a node should result in a highlighted message being emitted."""
async with TreeApp().run_test() as pilot:
await pilot.press("enter", "down")
await pilot.pause()
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeSelected", "test-tree"),
("NodeExpanded", "test-tree"),
("NodeHighlighted", "test-tree"),
] | Highlighting a node should result in a highlighted message being emitted. | test_tree_node_highlighted_message | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
def compose(self) -> ComposeResult:
"""Compose the child widgets."""
yield Button(id="expander")
yield Button(id="collapser")
yield MyTree("Root", id="test-tree") | Compose the child widgets. | compose | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
def compose(self) -> ComposeResult:
"""Compose the child widgets."""
yield TreeWrapper(self._auto_expand) | Compose the child widgets. | compose | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def test_expand_node_from_code() -> None:
"""Expanding a node from code should result in the appropriate message."""
async with TreeViaCodeApp(False).run_test() as pilot:
await pilot.click("#expander")
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
] | Expanding a node from code should result in the appropriate message. | test_expand_node_from_code | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
async def test_collapse_node_from_code() -> None:
"""Collapsing a node from code should result in the appropriate message."""
async with TreeViaCodeApp(True).run_test() as pilot:
await pilot.click("#collapser")
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeCollapsed", "test-tree"),
] | Collapsing a node from code should result in the appropriate message. | test_collapse_node_from_code | python | Textualize/textual | tests/tree/test_tree_messages.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_messages.py | MIT |
def label_of(node: TreeNode[None]):
"""Get the label of a node as a string"""
return str(node.label) | Get the label of a node as a string | label_of | python | Textualize/textual | tests/tree/test_tree_node_children.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_node_children.py | MIT |
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] | A node's children property should act like an immutable list. | test_tree_node_children | python | Textualize/textual | tests/tree/test_tree_node_children.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_node_children.py | MIT |
def test_get_tree_node_by_id() -> None:
"""It should be possible to get a TreeNode by its ID."""
tree = Tree[None]("Anakin")
child = tree.root.add("Leia")
grandchild = child.add("Ben")
assert tree.get_node_by_id(tree.root.id).id == tree.root.id
assert tree.get_node_by_id(child.id).id == child.id
assert tree.get_node_by_id(grandchild.id).id == grandchild.id
with pytest.raises(UnknownNodeID):
tree.get_node_by_id(cast(NodeID, grandchild.id + 1000)) | It should be possible to get a TreeNode by its ID. | test_get_tree_node_by_id | python | Textualize/textual | tests/tree/test_tree_get_node_by_id.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_get_node_by_id.py | MIT |
def test_tree_node_label() -> None:
"""It should be possible to modify a TreeNode's label."""
node = TreeNode(Tree[None]("Xenomorph Lifecycle"), None, 0, "Facehugger")
assert node.label == Text("Facehugger")
node.label = "Chestbuster"
assert node.label == Text("Chestbuster") | It should be possible to modify a TreeNode's label. | test_tree_node_label | python | Textualize/textual | tests/tree/test_tree_node_label.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_node_label.py | MIT |
def test_tree_node_label_via_tree() -> None:
"""It should be possible to modify a TreeNode's label when created via a Tree."""
tree = Tree[None]("Xenomorph Lifecycle")
node = tree.root.add("Facehugger")
assert node.label == Text("Facehugger")
node.label = "Chestbuster"
assert node.label == Text("Chestbuster") | It should be possible to modify a TreeNode's label when created via a Tree. | test_tree_node_label_via_tree | python | Textualize/textual | tests/tree/test_tree_node_label.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_node_label.py | MIT |
async def test_initial_state() -> None:
"""Initially all the visible nodes should have had a render call."""
app = RefreshApp()
async with app.run_test():
assert app.query_one(HistoryTree).render_hits == {(0,0), (1,0), (2,0)} | Initially all the visible nodes should have had a render call. | test_initial_state | python | Textualize/textual | tests/tree/test_node_refresh.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_node_refresh.py | MIT |
async def test_root_refresh() -> None:
"""A refresh of the root node should cause a subsequent render call."""
async with RefreshApp().run_test() as pilot:
assert (0, 1) not in pilot.app.query_one(HistoryTree).render_hits
pilot.app.query_one(HistoryTree).counter += 1
pilot.app.query_one(HistoryTree).root.refresh()
await pilot.pause()
assert (0, 1) in pilot.app.query_one(HistoryTree).render_hits | A refresh of the root node should cause a subsequent render call. | test_root_refresh | python | Textualize/textual | tests/tree/test_node_refresh.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_node_refresh.py | MIT |
async def test_child_refresh() -> None:
"""A refresh of the child node should cause a subsequent render call."""
async with RefreshApp().run_test() as pilot:
assert (1, 1) not in pilot.app.query_one(HistoryTree).render_hits
pilot.app.query_one(HistoryTree).counter += 1
pilot.app.query_one(HistoryTree).root.children[0].refresh()
await pilot.pause()
assert (1, 1) in pilot.app.query_one(HistoryTree).render_hits | A refresh of the child node should cause a subsequent render call. | test_child_refresh | python | Textualize/textual | tests/tree/test_node_refresh.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_node_refresh.py | MIT |
async def test_grandchild_refresh() -> None:
"""A refresh of the grandchild node should cause a subsequent render call."""
async with RefreshApp().run_test() as pilot:
assert (2, 1) not in pilot.app.query_one(HistoryTree).render_hits
pilot.app.query_one(HistoryTree).counter += 1
pilot.app.query_one(HistoryTree).root.children[0].children[0].refresh()
await pilot.pause()
assert (2, 1) in pilot.app.query_one(HistoryTree).render_hits | A refresh of the grandchild node should cause a subsequent render call. | test_grandchild_refresh | python | Textualize/textual | tests/tree/test_node_refresh.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_node_refresh.py | MIT |
def test_tree_node_parent() -> None:
"""It should be possible to access a TreeNode's parent."""
tree = Tree[None]("Anakin")
child = tree.root.add("Leia")
grandchild = child.add("Ben")
assert tree.root.parent is None
assert grandchild.parent == child
assert child.parent == tree.root | It should be possible to access a TreeNode's parent. | test_tree_node_parent | python | Textualize/textual | tests/tree/test_tree_node_parent.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_node_parent.py | MIT |
async def test_tree_node_expand() -> None:
"""Expanding one node should not expand all nodes."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.expand()
assert pilot.app.query_one(Tree).root.is_expanded is True
check_node = pilot.app.query_one(Tree).root.children[0]
while check_node.children:
assert any(child.is_expanded for child in check_node.children) is False
check_node = check_node.children[0] | Expanding one node should not expand all nodes. | test_tree_node_expand | python | Textualize/textual | tests/tree/test_tree_expand_etc.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_expand_etc.py | MIT |
async def test_tree_node_expand_all() -> None:
"""Expanding all on a node should expand all child nodes too."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.expand_all()
assert pilot.app.query_one(Tree).root.is_expanded is True
check_node = pilot.app.query_one(Tree).root.children[0]
while check_node.children:
assert check_node.children[0].is_expanded is True
assert any(child.is_expanded for child in check_node.children[1:]) is False
check_node = check_node.children[0] | Expanding all on a node should expand all child nodes too. | test_tree_node_expand_all | python | Textualize/textual | tests/tree/test_tree_expand_etc.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_expand_etc.py | MIT |
async def test_tree_node_collapse() -> None:
"""Collapsing one node should not collapse all nodes."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.expand_all()
pilot.app.query_one(Tree).root.children[0].collapse()
assert pilot.app.query_one(Tree).root.children[0].is_expanded is False
check_node = pilot.app.query_one(Tree).root.children[0].children[0]
while check_node.children:
assert all(child.is_expanded for child in check_node.children) is True
check_node = check_node.children[0] | Collapsing one node should not collapse all nodes. | test_tree_node_collapse | python | Textualize/textual | tests/tree/test_tree_expand_etc.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_expand_etc.py | MIT |
async def test_tree_node_collapse_all() -> None:
"""Collapsing all on a node should collapse all child noes too."""
async with TreeApp().run_test() as pilot:
pilot.app.query_one(Tree).root.expand_all()
pilot.app.query_one(Tree).root.children[0].collapse_all()
assert pilot.app.query_one(Tree).root.children[0].is_expanded is False
check_node = pilot.app.query_one(Tree).root.children[0].children[0]
while check_node.children:
assert check_node.children[0].is_expanded is False
assert all(child.is_expanded for child in check_node.children[1:]) is True
check_node = check_node.children[0] | Collapsing all on a node should collapse all child noes too. | test_tree_node_collapse_all | python | Textualize/textual | tests/tree/test_tree_expand_etc.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_expand_etc.py | MIT |
async def test_tree_node_toggle() -> None:
"""Toggling one node should not toggle all nodes."""
async with TreeApp().run_test() as pilot:
assert pilot.app.query_one(Tree).root.is_expanded is False
check_node = pilot.app.query_one(Tree).root.children[0]
while check_node.children:
assert any(child.is_expanded for child in check_node.children) is False
check_node = check_node.children[0]
pilot.app.query_one(Tree).root.toggle()
assert pilot.app.query_one(Tree).root.is_expanded is True
check_node = pilot.app.query_one(Tree).root.children[0]
while check_node.children:
assert any(child.is_expanded for child in check_node.children) is False
check_node = check_node.children[0] | Toggling one node should not toggle all nodes. | test_tree_node_toggle | python | Textualize/textual | tests/tree/test_tree_expand_etc.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_expand_etc.py | MIT |
async def test_tree_node_toggle_all() -> None:
"""Toggling all on a node should toggle all child nodes too."""
async with TreeApp().run_test() as pilot:
assert pilot.app.query_one(Tree).root.is_expanded is False
check_node = pilot.app.query_one(Tree).root.children[0]
while check_node.children:
assert any(child.is_expanded for child in check_node.children) is False
check_node = check_node.children[0]
pilot.app.query_one(Tree).root.toggle_all()
assert pilot.app.query_one(Tree).root.is_expanded is True
check_node = pilot.app.query_one(Tree).root.children[0]
while check_node.children:
assert check_node.children[0].is_expanded is True
assert any(child.is_expanded for child in check_node.children[1:]) is False
check_node = check_node.children[0]
pilot.app.query_one(Tree).root.toggle_all()
assert pilot.app.query_one(Tree).root.is_expanded is False
check_node = pilot.app.query_one(Tree).root.children[0]
while check_node.children:
assert any(child.is_expanded for child in check_node.children) is False
check_node = check_node.children[0] | Toggling all on a node should toggle all child nodes too. | test_tree_node_toggle_all | python | Textualize/textual | tests/tree/test_tree_expand_etc.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_expand_etc.py | MIT |
async def test_move_cursor() -> None:
"""Test moving the cursor to a node (updating the highlighted node)."""
async with TreeApp().run_test() as pilot:
app = pilot.app
tree: Tree[str] = app.query_one(Tree)
node_to_move_to = app.node
tree.move_cursor(node_to_move_to)
await pilot.pause()
# Note there are no Selected messages. We only move the cursor.
assert app.messages == [
("NodeExpanded", 0), # From the call to `tree.root.expand()` in compose
("NodeHighlighted", 0), # From the initial highlight of the root node
("NodeHighlighted", 1), # From the call to `tree.move_cursor`
] | Test moving the cursor to a node (updating the highlighted node). | test_move_cursor | python | Textualize/textual | tests/tree/test_tree_cursor.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_cursor.py | MIT |
async def test_directory_tree_file_selected_message(tmp_path: Path) -> None:
"""Selecting a file should result in a file selected message being emitted."""
FILE_NAME = "hello.txt"
# Creating one file under root
file = tmp_path / FILE_NAME
file.touch()
async with DirectoryTreeApp(tmp_path).run_test() as pilot:
tree = pilot.app.query_one(DirectoryTree)
await pilot.pause()
# Sanity check - file is the only child of root
assert len(tree.root.children) == 1
node = tree.root.children[0]
assert node.label == Text(FILE_NAME)
# Navigate to the file and select it
await pilot.press("down", "enter")
await pilot.pause()
assert pilot.app.messages == ["FileSelected"] | Selecting a file should result in a file selected message being emitted. | test_directory_tree_file_selected_message | python | Textualize/textual | tests/tree/test_directory_tree.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_directory_tree.py | MIT |
async def test_directory_tree_directory_selected_message(tmp_path: Path) -> None:
"""Selecting a directory should result in a directory selected message being emitted."""
SUBDIR = "subdir"
FILE_NAME = "hello.txt"
# Creating node with one file as its child
subdir = tmp_path / SUBDIR
subdir.mkdir()
file = subdir / FILE_NAME
file.touch()
async with DirectoryTreeApp(tmp_path).run_test() as pilot:
tree = pilot.app.query_one(DirectoryTree)
await pilot.pause()
# Sanity check - subdirectory is the only child of root
assert len(tree.root.children) == 1
node = tree.root.children[0]
assert node.label == Text(SUBDIR)
# Navigate to the subdirectory and select it
await pilot.press("down", "enter")
await pilot.pause()
assert pilot.app.messages == ["DirectorySelected"]
# Select the subdirectory again
await pilot.press("enter")
await pilot.pause()
assert pilot.app.messages == ["DirectorySelected", "DirectorySelected"] | Selecting a directory should result in a directory selected message being emitted. | test_directory_tree_directory_selected_message | python | Textualize/textual | tests/tree/test_directory_tree.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_directory_tree.py | MIT |
async def test_directory_tree_reload_node(tmp_path: Path) -> None:
"""Reloading a node of a directory tree should display newly created file inside the directory."""
RELOADED_DIRECTORY = "parentdir"
FILE1_NAME = "log.txt"
FILE2_NAME = "hello.txt"
# Creating node with one file as its child
reloaded_dir = tmp_path / RELOADED_DIRECTORY
reloaded_dir.mkdir()
file1 = reloaded_dir / FILE1_NAME
file1.touch()
async with DirectoryTreeApp(tmp_path).run_test() as pilot:
tree = pilot.app.query_one(DirectoryTree)
await pilot.pause()
# Sanity check - node is the only child of root
assert len(tree.root.children) == 1
node = tree.root.children[0]
assert node.label == Text(RELOADED_DIRECTORY)
node.expand()
await pilot.pause()
# Creating new file under the node
file2 = reloaded_dir / FILE2_NAME
file2.touch()
# Without reloading the node, the newly created file does not show up as its child
assert len(node.children) == 1
assert node.children[0].label == Text(FILE1_NAME)
tree.reload_node(node)
node.collapse()
node.expand()
await pilot.pause()
# After reloading the node, both files show up as children
assert len(node.children) == 2
assert [child.label for child in node.children] == [
Text(filename) for filename in sorted({FILE1_NAME, FILE2_NAME})
] | Reloading a node of a directory tree should display newly created file inside the directory. | test_directory_tree_reload_node | python | Textualize/textual | tests/tree/test_directory_tree.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_directory_tree.py | MIT |
async def test_directory_tree_reload_other_node(tmp_path: Path) -> None:
"""Reloading a node of a directory tree should not reload content of other directory."""
RELOADED_DIRECTORY = "parentdir"
NOT_RELOADED_DIRECTORY = "otherdir"
FILE1_NAME = "log.txt"
NOT_RELOADED_FILE3_NAME = "demo.txt"
NOT_RELOADED_FILE4_NAME = "unseen.txt"
# Creating two nodes, each having one file as child
reloaded_dir = tmp_path / RELOADED_DIRECTORY
reloaded_dir.mkdir()
file1 = reloaded_dir / FILE1_NAME
file1.touch()
non_reloaded_dir = tmp_path / NOT_RELOADED_DIRECTORY
non_reloaded_dir.mkdir()
file3 = non_reloaded_dir / NOT_RELOADED_FILE3_NAME
file3.touch()
async with DirectoryTreeApp(tmp_path).run_test() as pilot:
tree = pilot.app.query_one(DirectoryTree)
await pilot.pause()
# Sanity check - the root has the two nodes as its children (in alphabetical order)
assert len(tree.root.children) == 2
unaffected_node = tree.root.children[0]
node = tree.root.children[1]
assert unaffected_node.label == Text(NOT_RELOADED_DIRECTORY)
assert node.label == Text(RELOADED_DIRECTORY)
unaffected_node.expand()
node.expand()
await pilot.pause()
assert len(unaffected_node.children) == 1
assert unaffected_node.children[0].label == Text(NOT_RELOADED_FILE3_NAME)
# Creating new file under the node that won't be reloaded
file4 = non_reloaded_dir / NOT_RELOADED_FILE4_NAME
file4.touch()
tree.reload_node(node)
node.collapse()
node.expand()
unaffected_node.collapse()
unaffected_node.expand()
await pilot.pause()
# After reloading one node, the new file under the other one does not show up
assert len(unaffected_node.children) == 1
assert unaffected_node.children[0].label == Text(NOT_RELOADED_FILE3_NAME) | Reloading a node of a directory tree should not reload content of other directory. | test_directory_tree_reload_other_node | python | Textualize/textual | tests/tree/test_directory_tree.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_directory_tree.py | MIT |
async def test_directory_tree_reloading_preserves_state(tmp_path: Path) -> None:
"""Regression test for https://github.com/Textualize/textual/issues/4122.
Ensures `clear_node` does clear the node specified.
"""
ROOT = "root"
structure = [
ROOT,
"root/file1.txt",
"root/file2.txt",
]
for path in structure:
if path.endswith(".txt"):
(tmp_path / path).touch()
else:
(tmp_path / path).mkdir()
app = DirectoryTreeApp(tmp_path / ROOT)
async with app.run_test() as pilot:
directory_tree = app.query_one(DirectoryTree)
directory_tree.clear_node(directory_tree.root)
await pilot.pause()
assert not directory_tree.root.children | Regression test for https://github.com/Textualize/textual/issues/4122.
Ensures `clear_node` does clear the node specified. | test_directory_tree_reloading_preserves_state | python | Textualize/textual | tests/tree/test_directory_tree.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_directory_tree.py | MIT |
async def test_tree_simple_clear() -> None:
"""Clearing a tree should keep the old root label and data."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
pilot.app.query_one(VerseTree).clear()
assert len(tree.root.children) == 0
assert str(tree.root.label) == "White Sun"
assert isinstance(tree.root.data, VerseStar) | Clearing a tree should keep the old root label and data. | test_tree_simple_clear | python | Textualize/textual | tests/tree/test_tree_clearing.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_clearing.py | MIT |
async def test_tree_reset_with_label() -> None:
"""Resetting a tree with a new label should use the new label and set the data to None."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
pilot.app.query_one(VerseTree).reset(label="Jiangyin")
assert len(tree.root.children) == 0
assert str(tree.root.label) == "Jiangyin"
assert tree.root.data is None | Resetting a tree with a new label should use the new label and set the data to None. | test_tree_reset_with_label | python | Textualize/textual | tests/tree/test_tree_clearing.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_clearing.py | MIT |
async def test_tree_reset_with_label_and_data() -> None:
"""Resetting a tree with a label and data have that label and data used."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
pilot.app.query_one(VerseTree).reset(label="Jiangyin", data=VersePlanet())
assert len(tree.root.children) == 0
assert str(tree.root.label) == "Jiangyin"
assert isinstance(tree.root.data, VersePlanet) | Resetting a tree with a label and data have that label and data used. | test_tree_reset_with_label_and_data | python | Textualize/textual | tests/tree/test_tree_clearing.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_clearing.py | MIT |
async def test_tree_remove_children_of_root():
"""Test removing the children of the root."""
async with TreeClearApp().run_test() as pilot:
tree = pilot.app.query_one(VerseTree)
assert len(tree.root.children) > 1
tree.root.remove_children()
assert len(tree.root.children) == 0 | Test removing the children of the root. | test_tree_remove_children_of_root | python | Textualize/textual | tests/tree/test_tree_clearing.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_clearing.py | MIT |
async def test_attempt_to_remove_root():
"""Attempting to remove the root should be an error."""
async with TreeClearApp().run_test() as pilot:
with pytest.raises(RemoveRootError):
pilot.app.query_one(VerseTree).root.remove() | Attempting to remove the root should be an error. | test_attempt_to_remove_root | python | Textualize/textual | tests/tree/test_tree_clearing.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_clearing.py | MIT |
def compose(self) -> ComposeResult:
"""Compose the child widgets."""
yield Tree("Root", disabled=self.disabled, id="test-tree") | Compose the child widgets. | compose | python | Textualize/textual | tests/tree/test_tree_availability.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_availability.py | MIT |
async def test_creating_disabled_tree():
"""Mounting a disabled `Tree` should result in the base `Widget`
having a `disabled` property equal to `True`"""
app = TreeApp(disabled=True)
async with app.run_test() as pilot:
tree = app.query_one(Tree)
assert not tree.focusable
assert tree.disabled
assert tree.cursor_line == 0
await pilot.click("#test-tree")
await pilot.pause()
await pilot.press("down")
await pilot.pause()
assert tree.cursor_line == 0 | Mounting a disabled `Tree` should result in the base `Widget`
having a `disabled` property equal to `True` | test_creating_disabled_tree | python | Textualize/textual | tests/tree/test_tree_availability.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_availability.py | MIT |
async def test_creating_enabled_tree():
"""Mounting an enabled `Tree` should result in the base `Widget`
having a `disabled` property equal to `False`"""
app = TreeApp(disabled=False)
async with app.run_test() as pilot:
tree = app.query_one(Tree)
assert tree.focusable
assert not tree.disabled
assert tree.cursor_line == 0
await pilot.click("#test-tree")
await pilot.pause()
await pilot.press("down")
await pilot.pause()
assert tree.cursor_line == 1 | Mounting an enabled `Tree` should result in the base `Widget`
having a `disabled` property equal to `False` | test_creating_enabled_tree | python | Textualize/textual | tests/tree/test_tree_availability.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_availability.py | MIT |
async def test_disabled_tree_node_selected_message() -> None:
"""Clicking the root node disclosure triangle on a disabled tree
should result in no messages being emitted."""
app = TreeApp(disabled=True)
async with app.run_test() as pilot:
tree = app.query_one(Tree)
# try clicking on a disabled tree
await pilot.click("#test-tree")
await pilot.pause()
assert pilot.app.messages == [("NodeHighlighted", "test-tree")]
# make sure messages DO flow after enabling a disabled tree
tree.disabled = False
await pilot.click("#test-tree")
await pilot.pause()
print(pilot.app.messages)
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
] | Clicking the root node disclosure triangle on a disabled tree
should result in no messages being emitted. | test_disabled_tree_node_selected_message | python | Textualize/textual | tests/tree/test_tree_availability.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_availability.py | MIT |
async def test_enabled_tree_node_selected_message() -> None:
"""Clicking the root node disclosure triangle on an enabled tree
should result in an `NodeExpanded` message being emitted."""
app = TreeApp(disabled=False)
async with app.run_test() as pilot:
tree = app.query_one(Tree)
# try clicking on an enabled tree
await pilot.click("#test-tree")
await pilot.pause()
print(pilot.app.messages)
assert pilot.app.messages == [
("NodeHighlighted", "test-tree"),
("NodeExpanded", "test-tree"),
]
tree.disabled = True
# make sure messages DO NOT flow after disabling an enabled tree
app.messages = []
await pilot.click("#test-tree")
await pilot.pause()
assert not pilot.app.messages | Clicking the root node disclosure triangle on an enabled tree
should result in an `NodeExpanded` message being emitted. | test_enabled_tree_node_selected_message | python | Textualize/textual | tests/tree/test_tree_availability.py | https://github.com/Textualize/textual/blob/master/tests/tree/test_tree_availability.py | MIT |
async def work_with(launcher: Callable[[WorkApp], WorkType]) -> None:
"""Core code for testing a work decorator."""
app = WorkApp()
async with app.run_test() as pilot:
app.launch(launcher(app))
await app.workers.wait_for_complete()
result = await app.worker.wait()
assert result == "foo"
await pilot.pause()
assert app.states == [
WorkerState.PENDING,
WorkerState.RUNNING,
WorkerState.SUCCESS,
] | Core code for testing a work decorator. | work_with | python | Textualize/textual | tests/workers/test_work_decorator.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_work_decorator.py | MIT |
async def test_async_work() -> None:
"""It should be possible to decorate an async method as an async worker."""
await work_with(lambda app: app.async_work) | It should be possible to decorate an async method as an async worker. | test_async_work | python | Textualize/textual | tests/workers/test_work_decorator.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_work_decorator.py | MIT |
async def test_async_thread_work() -> None:
"""It should be possible to decorate an async method as a thread worker."""
await work_with(lambda app: app.async_thread_work) | It should be possible to decorate an async method as a thread worker. | test_async_thread_work | python | Textualize/textual | tests/workers/test_work_decorator.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_work_decorator.py | MIT |
async def test_thread_work() -> None:
"""It should be possible to decorate a non-async method as a thread worker."""
await work_with(lambda app: app.thread_work) | It should be possible to decorate a non-async method as a thread worker. | test_thread_work | python | Textualize/textual | tests/workers/test_work_decorator.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_work_decorator.py | MIT |
def test_decorate_non_async_no_thread_argument() -> None:
"""Decorating a non-async method without saying explicitly that it's a thread is an error."""
with pytest.raises(WorkerDeclarationError):
class _(App[None]):
@work
def foo(self) -> None:
pass | Decorating a non-async method without saying explicitly that it's a thread is an error. | test_decorate_non_async_no_thread_argument | python | Textualize/textual | tests/workers/test_work_decorator.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_work_decorator.py | MIT |
def test_decorate_non_async_no_thread_is_false() -> None:
"""Decorating a non-async method and saying it isn't a thread is an error."""
with pytest.raises(WorkerDeclarationError):
class _(App[None]):
@work(thread=False)
def foo(self) -> None:
pass | Decorating a non-async method and saying it isn't a thread is an error. | test_decorate_non_async_no_thread_is_false | python | Textualize/textual | tests/workers/test_work_decorator.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_work_decorator.py | MIT |
async def test_calling_workers_from_within_workers(call_stack: Tuple[str]):
"""Regression test for https://github.com/Textualize/textual/issues/3472.
This makes sure we can nest worker calls without a problem.
"""
app = NestedWorkersApp(list(call_stack))
async with app.run_test():
app.call_from_stack()
# We need multiple awaits because we're creating a chain of workers that may
# have multiple async workers, each of which may need the await to have enough
# time to call the next one in the chain.
for _ in range(len(call_stack)):
await app.workers.wait_for_complete()
assert app.call_stack == [] | Regression test for https://github.com/Textualize/textual/issues/3472.
This makes sure we can nest worker calls without a problem. | test_calling_workers_from_within_workers | python | Textualize/textual | tests/workers/test_work_decorator.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_work_decorator.py | MIT |
async def test_run_worker_async() -> None:
"""Check self.run_worker"""
worker_events: list[Worker.StateChanged] = []
work_result: str = ""
new_worker: Worker
class WorkerWidget(Widget):
async def work(self) -> str:
nonlocal work_result
await asyncio.sleep(0.02)
work_result = "foo"
return "foo"
def on_mount(self):
nonlocal new_worker
new_worker = self.run_worker(self.work, start=False)
def on_worker_state_changed(self, event) -> None:
worker_events.append(event)
class WorkerApp(App):
def compose(self) -> ComposeResult:
yield WorkerWidget()
app = WorkerApp()
async with app.run_test():
assert new_worker in app.workers
assert len(app.workers) == 1
app.workers.start_all()
await app.workers.wait_for_complete()
assert len(app.workers) == 0
assert work_result == "foo"
assert isinstance(worker_events[0].worker.node, WorkerWidget)
states = [event.state for event in worker_events]
assert states == [
WorkerState.PENDING,
WorkerState.RUNNING,
WorkerState.SUCCESS,
] | Check self.run_worker | test_run_worker_async | python | Textualize/textual | tests/workers/test_worker_manager.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker_manager.py | MIT |
async def test_run_worker_thread_non_async() -> None:
"""Check self.run_worker"""
worker_events: list[Worker.StateChanged] = []
work_result: str = ""
class WorkerWidget(Widget):
def work(self) -> str:
nonlocal work_result
time.sleep(0.02)
work_result = "foo"
return "foo"
def on_mount(self):
self.run_worker(self.work, thread=True)
def on_worker_state_changed(self, event) -> None:
worker_events.append(event)
class WorkerApp(App):
def compose(self) -> ComposeResult:
yield WorkerWidget()
app = WorkerApp()
async with app.run_test():
await app.workers.wait_for_complete()
assert work_result == "foo"
assert isinstance(worker_events[0].worker.node, WorkerWidget)
states = [event.state for event in worker_events]
assert states == [
WorkerState.PENDING,
WorkerState.RUNNING,
WorkerState.SUCCESS,
] | Check self.run_worker | test_run_worker_thread_non_async | python | Textualize/textual | tests/workers/test_worker_manager.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker_manager.py | MIT |
async def test_run_worker_thread_async() -> None:
"""Check self.run_worker"""
worker_events: list[Worker.StateChanged] = []
work_result: str = ""
class WorkerWidget(Widget):
async def work(self) -> str:
nonlocal work_result
time.sleep(0.02)
work_result = "foo"
return "foo"
def on_mount(self):
self.run_worker(self.work, thread=True)
def on_worker_state_changed(self, event) -> None:
worker_events.append(event)
class WorkerApp(App):
def compose(self) -> ComposeResult:
yield WorkerWidget()
app = WorkerApp()
async with app.run_test():
await app.workers.wait_for_complete()
assert work_result == "foo"
assert isinstance(worker_events[0].worker.node, WorkerWidget)
states = [event.state for event in worker_events]
assert states == [
WorkerState.PENDING,
WorkerState.RUNNING,
WorkerState.SUCCESS,
] | Check self.run_worker | test_run_worker_thread_async | python | Textualize/textual | tests/workers/test_worker_manager.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker_manager.py | MIT |
async def test_initialize():
"""Test initial values."""
def foo() -> str:
return "foo"
app = App()
async with app.run_test():
worker = Worker(app, foo, name="foo", group="foo-group", description="Foo test")
repr(worker)
assert worker.state == WorkerState.PENDING
assert not worker.is_cancelled
assert not worker.is_running
assert not worker.is_finished
assert worker.completed_steps == 0
assert worker.total_steps is None
assert worker.progress == 0.0
assert worker.result is None | Test initial values. | test_initialize | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
def foo() -> str:
"""Regular function."""
return "foo" | Regular function. | test_run_success.foo | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
async def bar() -> str:
"""Coroutine."""
return "bar" | Coroutine. | test_run_success.bar | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
async def baz() -> str:
"""Coroutine."""
return "baz" | Coroutine. | test_run_success.baz | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
async def test_run_success() -> None:
"""Test successful runs."""
def foo() -> str:
"""Regular function."""
return "foo"
async def bar() -> str:
"""Coroutine."""
return "bar"
async def baz() -> str:
"""Coroutine."""
return "baz"
class RunApp(App):
pass
app = RunApp()
async with app.run_test():
# Call regular function
foo_worker: Worker[str] = Worker(
app, foo, name="foo", group="foo-group", description="Foo test", thread=True
)
# Call coroutine function
bar_worker: Worker[str] = Worker(
app, bar, name="bar", group="bar-group", description="Bar test"
)
# Call coroutine
baz_worker: Worker[str] = Worker(
app, baz(), name="baz", group="baz-group", description="Baz test"
)
# Call coroutine function in a thread
bar_thread_worker: Worker[str] = Worker(
app, bar, name="bar", group="bar-group", description="Bar test", thread=True
)
# Call coroutine in a thread.
baz_thread_worker: Worker[str] = Worker(
app,
baz(),
name="baz",
group="baz-group",
description="Baz test",
thread=True,
)
assert foo_worker.result is None
assert bar_worker.result is None
assert baz_worker.result is None
assert bar_thread_worker.result is None
assert baz_thread_worker.result is None
foo_worker._start(app)
bar_worker._start(app)
baz_worker._start(app)
bar_thread_worker._start(app)
baz_thread_worker._start(app)
assert await foo_worker.wait() == "foo"
assert await bar_worker.wait() == "bar"
assert await baz_worker.wait() == "baz"
assert await bar_thread_worker.wait() == "bar"
assert await baz_thread_worker.wait() == "baz"
assert foo_worker.result == "foo"
assert bar_worker.result == "bar"
assert baz_worker.result == "baz"
assert bar_thread_worker.result == "bar"
assert baz_thread_worker.result == "baz" | Test successful runs. | test_run_success | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
async def test_run_cancel() -> None:
"""Test run may be cancelled."""
async def run_error() -> str:
await asyncio.sleep(0.1)
return "Never"
class ErrorApp(App):
pass
app = ErrorApp()
async with app.run_test():
worker: Worker[str] = Worker(app, run_error)
worker._start(app)
await asyncio.sleep(0)
worker.cancel()
assert worker.is_cancelled
with pytest.raises(WorkerCancelled):
await worker.wait() | Test run may be cancelled. | test_run_cancel | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
async def test_run_cancel_immediately() -> None:
"""Edge case for cancelling immediately."""
async def run_error() -> str:
await asyncio.sleep(0.1)
return "Never"
class ErrorApp(App):
pass
app = ErrorApp()
async with app.run_test():
worker: Worker[str] = Worker(app, run_error)
worker._start(app)
worker.cancel()
assert worker.is_cancelled
with pytest.raises(WorkerCancelled):
await worker.wait() | Edge case for cancelling immediately. | test_run_cancel_immediately | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
async def test_get_worker() -> None:
"""Check get current worker."""
async def run_worker() -> Worker:
worker = get_current_worker()
return worker
class WorkerApp(App):
pass
app = WorkerApp()
async with app.run_test():
worker: Worker[Worker] = Worker(app, run_worker)
worker._start(app)
assert await worker.wait() is worker | Check get current worker. | test_get_worker | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
def test_no_active_worker() -> None:
"""No active worker raises a specific exception."""
with pytest.raises(NoActiveWorker):
get_current_worker() | No active worker raises a specific exception. | test_no_active_worker | python | Textualize/textual | tests/workers/test_worker.py | https://github.com/Textualize/textual/blob/master/tests/workers/test_worker.py | MIT |
async def test_no_startup_messages():
"""An input with no initial value should have no initial messages."""
async with InputApp().run_test() as pilot:
assert pilot.app.messages == [] | An input with no initial value should have no initial messages. | test_no_startup_messages | python | Textualize/textual | tests/input/test_input_messages.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_messages.py | MIT |
async def test_startup_messages_with_initial_value():
"""An input with an initial value should send a changed event."""
async with InputApp("Hello, World!").run_test() as pilot:
assert pilot.app.messages == ["Changed"] | An input with an initial value should send a changed event. | test_startup_messages_with_initial_value | python | Textualize/textual | tests/input/test_input_messages.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_messages.py | MIT |
async def test_typing_from_empty_causes_changed():
"""An input with no initial value should send messages when entering text."""
input_text = "Hello, World!"
async with InputApp().run_test() as pilot:
await pilot.press(*input_text)
assert pilot.app.messages == ["Changed"] * len(input_text) | An input with no initial value should send messages when entering text. | test_typing_from_empty_causes_changed | python | Textualize/textual | tests/input/test_input_messages.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_messages.py | MIT |
async def test_typing_from_pre_populated_causes_changed():
"""An input with initial value should send messages when entering text after an initial message."""
input_text = "Hello, World!"
async with InputApp(input_text).run_test() as pilot:
await pilot.press(*input_text)
assert pilot.app.messages == ["Changed"] + (["Changed"] * len(input_text)) | An input with initial value should send messages when entering text after an initial message. | test_typing_from_pre_populated_causes_changed | python | Textualize/textual | tests/input/test_input_messages.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_messages.py | MIT |
async def test_submit_empty_input():
"""Pressing enter on an empty input should send a submitted event."""
async with InputApp().run_test() as pilot:
await pilot.press("enter")
assert pilot.app.messages == ["Submitted"] | Pressing enter on an empty input should send a submitted event. | test_submit_empty_input | python | Textualize/textual | tests/input/test_input_messages.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_messages.py | MIT |
async def test_submit_pre_populated_input():
"""Pressing enter on a pre-populated input should send a changed then submitted event."""
async with InputApp("The owls are not what they seem").run_test() as pilot:
await pilot.press("enter")
assert pilot.app.messages == ["Changed", "Submitted"] | Pressing enter on a pre-populated input should send a changed then submitted event. | test_submit_pre_populated_input | python | Textualize/textual | tests/input/test_input_messages.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_messages.py | MIT |
async def test_paste_event_impact():
"""A paste event should result in a changed event."""
async with InputApp().run_test() as pilot:
await pilot.app._post_message(Paste("Hello, World"))
await pilot.pause()
assert pilot.app.messages == ["Changed"] | A paste event should result in a changed event. | test_paste_event_impact | python | Textualize/textual | tests/input/test_input_messages.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_messages.py | MIT |
async def test_input_home() -> None:
"""Going home should always land at position zero."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.action_home()
assert input.cursor_position == 0 | Going home should always land at position zero. | test_input_home | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_end() -> None:
"""Going end should always land at the last position."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.action_end()
assert input.cursor_position == len(input.value) | Going end should always land at the last position. | test_input_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_right_from_home() -> None:
"""Going right should always land at the next position, if there is one."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.cursor_position = 0
input.action_cursor_right()
assert input.cursor_position == (1 if input.value else 0) | Going right should always land at the next position, if there is one. | test_input_right_from_home | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_right_from_end() -> None:
"""Going right should always stay put if doing so from the end."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.action_end()
input.action_cursor_right()
assert input.cursor_position == len(input.value) | Going right should always stay put if doing so from the end. | test_input_right_from_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_left_from_home() -> None:
"""Going left from home should stay put."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.cursor_position = 0
input.action_cursor_left()
assert input.cursor_position == 0 | Going left from home should stay put. | test_input_left_from_home | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_left_from_end() -> None:
"""Going left from the end should go back one place, where possible."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.action_end()
input.action_cursor_left()
assert input.cursor_position == (len(input.value) - 1 if input.value else 0) | Going left from the end should go back one place, where possible. | test_input_left_from_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_left_word_from_home() -> None:
"""Going left one word from the start should do nothing."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.cursor_position = 0
input.action_cursor_left_word()
assert input.cursor_position == 0 | Going left one word from the start should do nothing. | test_input_left_word_from_home | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_left_word_from_end() -> None:
"""Going left one word from the end should land correctly.."""
async with InputTester().run_test() as pilot:
expected_at: dict[str | None, int] = {
"empty": 0,
"single-word": 0,
"multi-no-punctuation": 33,
"multi-punctuation": 47,
"multi-and-hyphenated": 26,
}
for input in pilot.app.query(Input):
input.action_end()
input.action_cursor_left_word()
assert input.cursor_position == expected_at[input.id] | Going left one word from the end should land correctly.. | test_input_left_word_from_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_password_input_left_word_from_end() -> None:
"""Going left one word from the end in a password field should land at home."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.action_end()
input.password = True
input.action_cursor_left_word()
assert input.cursor_position == 0 | Going left one word from the end in a password field should land at home. | test_password_input_left_word_from_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_right_word_from_home() -> None:
"""Going right one word from the start should land correctly.."""
async with InputTester().run_test() as pilot:
expected_at: dict[str | None, int] = {
"empty": 0,
"single-word": 5,
"multi-no-punctuation": 6,
"multi-punctuation": 3,
"multi-and-hyphenated": 5,
}
for input in pilot.app.query(Input):
input.cursor_position = 0
input.action_cursor_right_word()
assert input.cursor_position == expected_at[input.id] | Going right one word from the start should land correctly.. | test_input_right_word_from_home | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_password_input_right_word_from_home() -> None:
"""Going right one word from the start of a password input should go to the end."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.password = True
input.action_cursor_right_word()
assert input.cursor_position == len(input.value) | Going right one word from the start of a password input should go to the end. | test_password_input_right_word_from_home | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_right_word_from_end() -> None:
"""Going right one word from the end should do nothing."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.action_end()
input.action_cursor_right_word()
assert input.cursor_position == len(input.value) | Going right one word from the end should do nothing. | test_input_right_word_from_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_right_word_to_the_end() -> None:
"""Using right-word to get to the end should hop the correct number of times."""
async with InputTester().run_test() as pilot:
expected_hops: dict[str | None, int] = {
"empty": 0,
"single-word": 1,
"multi-no-punctuation": 6,
"multi-punctuation": 10,
"multi-and-hyphenated": 7,
}
for input in pilot.app.query(Input):
input.cursor_position = 0
hops = 0
while input.cursor_position < len(input.value):
input.action_cursor_right_word()
hops += 1
assert hops == expected_hops[input.id] | Using right-word to get to the end should hop the correct number of times. | test_input_right_word_to_the_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
async def test_input_left_word_from_the_end() -> None:
"""Using left-word to get home from the end should hop the correct number of times."""
async with InputTester().run_test() as pilot:
expected_hops: dict[str | None, int] = {
"empty": 0,
"single-word": 1,
"multi-no-punctuation": 6,
"multi-punctuation": 10,
"multi-and-hyphenated": 7,
}
for input in pilot.app.query(Input):
input.action_end()
hops = 0
while input.cursor_position:
input.action_cursor_left_word()
hops += 1
assert hops == expected_hops[input.id] | Using left-word to get home from the end should hop the correct number of times. | test_input_left_word_from_the_end | python | Textualize/textual | tests/input/test_input_key_movement_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_movement_actions.py | MIT |
def test_input_number_type():
"""Test number type regex, value should be number or the prefix of a valid number"""
number = _RESTRICT_TYPES["number"]
assert re.fullmatch(number, "0")
assert re.fullmatch(number, "0.")
assert re.fullmatch(number, ".")
assert re.fullmatch(number, "-")
assert re.fullmatch(number, "+")
assert re.fullmatch(number, ".0")
assert re.fullmatch(number, "1.1")
assert re.fullmatch(number, "1_")
assert re.fullmatch(number, "1_2")
assert re.fullmatch(number, "-000_123_456.78e01_234")
assert re.fullmatch(number, "1e1")
assert re.fullmatch(number, "1")
assert re.fullmatch(number, "1.")
assert re.fullmatch(number, "1.2")
assert re.fullmatch(number, "1.2e")
assert re.fullmatch(number, "1.2e-")
assert re.fullmatch(number, "1.2e-1")
assert re.fullmatch(number, "1.2e-10")
assert re.fullmatch(number, "1.2E10")
assert not re.fullmatch(number, "1.2e10e")
assert not re.fullmatch(number, "-000_123_456.78e01_234.")
assert not re.fullmatch(number, "e") # float("e23") is not valid
assert not re.fullmatch(number, "1f2")
assert not re.fullmatch(number, "inf")
assert not re.fullmatch(number, "nan")
assert not re.fullmatch(number, "-inf") | Test number type regex, value should be number or the prefix of a valid number | test_input_number_type | python | Textualize/textual | tests/input/test_input_restrict.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_restrict.py | MIT |
def test_input_integer_type():
"""Test input type regex"""
integer = _RESTRICT_TYPES["integer"]
assert re.fullmatch(integer, "0")
assert re.fullmatch(integer, "1")
assert re.fullmatch(integer, "10")
assert re.fullmatch(integer, "123456789")
assert re.fullmatch(integer, "-")
assert re.fullmatch(integer, "+")
assert re.fullmatch(integer, "-1")
assert re.fullmatch(integer, "+2")
assert re.fullmatch(integer, "+0")
assert re.fullmatch(integer, "+0_")
assert re.fullmatch(integer, "+0_1")
assert re.fullmatch(integer, "+0_12")
assert re.fullmatch(integer, "+0_123")
assert not re.fullmatch(integer, "+_123")
assert not re.fullmatch(integer, "123.")
assert not re.fullmatch(integer, "+2e")
assert not re.fullmatch(integer, "foo") | Test input type regex | test_input_integer_type | python | Textualize/textual | tests/input/test_input_restrict.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_restrict.py | MIT |
async def test_bad_type():
"""Check an invalid type raises a ValueError."""
class InputApp(App):
def compose(self) -> ComposeResult:
yield Input(type="foo") # Bad type
app = InputApp()
with pytest.raises(ValueError):
async with app.run_test():
pass | Check an invalid type raises a ValueError. | test_bad_type | python | Textualize/textual | tests/input/test_input_restrict.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_restrict.py | MIT |
async def test_max_length():
"""Check max_length limits characters."""
class InputApp(App):
AUTO_FOCUS = "Input"
def compose(self) -> ComposeResult:
yield Input(max_length=5)
async with InputApp().run_test() as pilot:
input_widget = pilot.app.query_one(Input)
await pilot.press("1")
assert input_widget.value == "1"
await pilot.press("2", "3", "4", "5")
assert input_widget.value == "12345"
# Value is max length, no more characters are permitted
await pilot.press("6")
assert input_widget.value == "12345"
await pilot.press("7")
assert input_widget.value == "12345"
# Backspace is ok
await pilot.press("backspace")
assert input_widget.value == "1234"
await pilot.press("0")
assert input_widget.value == "12340"
# Back to maximum
await pilot.press("1")
assert input_widget.value == "12340" | Check max_length limits characters. | test_max_length | python | Textualize/textual | tests/input/test_input_restrict.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_restrict.py | MIT |
async def test_restrict():
"""Test restriction by regex."""
class InputApp(App):
AUTO_FOCUS = "Input"
def compose(self) -> ComposeResult:
yield Input(restrict="[abc]*")
async with InputApp().run_test() as pilot:
input_widget = pilot.app.query_one(Input)
await pilot.press("a")
assert input_widget.value == "a"
await pilot.press("b")
assert input_widget.value == "ab"
await pilot.press("c")
assert input_widget.value == "abc"
# "d" is restricted
await pilot.press("d")
assert input_widget.value == "abc"
# "a" is not
await pilot.press("a")
assert input_widget.value == "abca" | Test restriction by regex. | test_restrict | python | Textualize/textual | tests/input/test_input_restrict.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_restrict.py | MIT |
async def test_delete_left_from_home() -> None:
"""Deleting left from home should do nothing."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.cursor_position = 0
input.action_delete_left()
assert input.cursor_position == 0
assert input.value == TEST_INPUTS[input.id] | Deleting left from home should do nothing. | test_delete_left_from_home | python | Textualize/textual | tests/input/test_input_key_modification_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_modification_actions.py | MIT |
async def test_delete_left_from_end() -> None:
"""Deleting left from end should remove the last character (if there is one)."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.action_end()
input.action_delete_left()
assert input.cursor_position == len(input.value)
assert input.value == TEST_INPUTS[input.id][:-1] | Deleting left from end should remove the last character (if there is one). | test_delete_left_from_end | python | Textualize/textual | tests/input/test_input_key_modification_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_modification_actions.py | MIT |
async def test_delete_left_word_from_home() -> None:
"""Deleting word left from home should do nothing."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.cursor_position = 0
input.action_delete_left_word()
assert input.cursor_position == 0
assert input.value == TEST_INPUTS[input.id] | Deleting word left from home should do nothing. | test_delete_left_word_from_home | python | Textualize/textual | tests/input/test_input_key_modification_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_modification_actions.py | MIT |
async def test_delete_left_word_from_end() -> None:
"""Deleting word left from end should remove the expected text."""
async with InputTester().run_test() as pilot:
expected: dict[str | None, str] = {
"empty": "",
"multi-no-punctuation": "Curse your sudden but inevitable ",
"multi-punctuation": "We have done the impossible, and that makes us ",
"multi-and-hyphenated": "Long as she does it quiet-",
}
for input in pilot.app.query(Input):
input.action_delete_left_word()
assert input.cursor_position == len(input.value)
assert input.value == expected[input.id] | Deleting word left from end should remove the expected text. | test_delete_left_word_from_end | python | Textualize/textual | tests/input/test_input_key_modification_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_modification_actions.py | MIT |
async def test_password_delete_left_word_from_end() -> None:
"""Deleting word left from end of a password input should delete everything."""
async with InputTester().run_test() as pilot:
for input in pilot.app.query(Input):
input.password = True
input.action_delete_left_word()
assert input.cursor_position == 0
assert input.value == "" | Deleting word left from end of a password input should delete everything. | test_password_delete_left_word_from_end | python | Textualize/textual | tests/input/test_input_key_modification_actions.py | https://github.com/Textualize/textual/blob/master/tests/input/test_input_key_modification_actions.py | MIT |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.