Skip to content

Commit

Permalink
Add test coverage for core command changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Nov 20, 2023
1 parent 4af03f1 commit 39316be
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/toga/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ def __eq__(self, other: Any) -> bool:
return self.key == other.key

def __repr__(self) -> str:
title_string = "Status group" if self.is_status_item else "Group"
parent_string = (
f" parent={self.parent} section={self.section}"
if self.parent is not None
else ""
)
return f"<Group text={self.text!r} order={self.order}{parent_string}>"
return f"<{title_string} text={self.text!r} order={self.order}{parent_string}>"

@property
def key(self) -> tuple[(int, int, str)]:
Expand Down
36 changes: 36 additions & 0 deletions core/tests/command/test_commandset.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,42 @@ def test_add_clear_with_app(app, change_handler):
assert list(app.commands) == [cmd_a, cmd1b, cmd2, cmd1a, cmd_b]


@pytest.mark.parametrize("change_handler", [(None), (Mock())])
def test_suspend_updates(change_handler):
"""Updates can be suspended."""
# Put some commands into the app
cmd_a = toga.Command(None, text="App command a")
cmd_b = toga.Command(None, text="App command b")
cs = CommandSet(on_change=change_handler)
cs.add(cmd_a, cmd_b)
assert list(cs) == [cmd_a, cmd_b]

# Clear the change handler
if change_handler:
change_handler.reset_mock()

# Suspend updates and add some more commands
with cs.suspend_updates():
cmd_c = toga.Command(None, text="App command c")
cmd_d = toga.Command(None, text="App command d")
cs.add(cmd_c, cmd_d)

# New commands exist
assert list(cs) == [cmd_a, cmd_b, cmd_c, cmd_d]

# change handler hasn't been invoked
if change_handler:
change_handler.assert_not_called()

# Change handler was called once.
if change_handler:
change_handler.assert_called_once()
change_handler.reset_mock()

# New commands still exist
assert list(cs) == [cmd_a, cmd_b, cmd_c, cmd_d]


def test_ordering(
parent_group_1,
parent_group_2,
Expand Down
60 changes: 60 additions & 0 deletions core/tests/command/test_group.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import pytest

import toga
Expand All @@ -10,6 +12,8 @@ def test_create():
grp = toga.Group("Group name")
assert grp.text == "Group name"
assert grp.order == 0
assert grp.section == 0
assert grp.parent is None

assert repr(grp) == "<Group text='Group name' order=0>"

Expand Down Expand Up @@ -39,6 +43,62 @@ def test_create_section_without_parent():
toga.Group("Group name", order=2, section=3)


def test_create_status_group():
"""A status group cannot be created."""
grp = toga.Group("Group name", order=2, status_item=True)

assert grp.text == "Group name"
assert grp.order == 2
assert grp.section == 0
assert grp.parent is None

assert repr(grp) == "<Status group text='Group name' order=2>"


def test_create_status_subgroup():
"""A status group cannot be created with parent."""
parent = toga.Group("Parent name")
with pytest.raises(
ValueError,
match=r"Sub-groups cannot be status items",
):
toga.Group("Group name", parent=parent, status_item=True)


@pytest.mark.parametrize("construct", [True, False])
def test_icon_construction(app, construct):
"""The command icon can be set during construction"""
if construct:
icon = toga.Icon("path/to/icon")
else:
icon = "path/to/icon"

grp = toga.Group("Test group", icon=icon)
assert isinstance(grp.icon, toga.Icon)
assert grp.icon.path == Path("path/to/icon")


@pytest.mark.parametrize("construct", [True, False])
def test_icon(app, construct):
"""The command icon can be changed"""
if construct:
icon = toga.Icon("path/to/icon")
else:
icon = "path/to/icon"

grp = toga.Group("Test command")

# No icon by default
assert grp.icon is None

# Change icon
grp.icon = icon

# Icon path matches
assert isinstance(grp.icon, toga.Icon)
assert grp.icon.path == Path("path/to/icon")


def test_hashable():
"""Groups are hashable."""
grp1 = toga.Group("text 1")
Expand Down

0 comments on commit 39316be

Please sign in to comment.