Skip to content

Commit

Permalink
raise ValueError if hmenu==-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jborbely committed Mar 18, 2024
1 parent 54ed113 commit 3e1209b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions msl/loadlib/activex.py
Expand Up @@ -372,6 +372,10 @@ def checked(self) -> bool:
@checked.setter
def checked(self, value: bool) -> None:
"""Set the checked state of the menu item."""
if self._hmenu == -1:
raise ValueError('A MenuItem must first be added to a Menu '
'before it can be checked')

# MF_CHECKED=8, MF_UNCHECKED=0
state = 8 if value else 0
previous = user32.CheckMenuItem(self._hmenu, self._id, state)
Expand Down Expand Up @@ -451,6 +455,9 @@ def checked(self) -> MenuItem | None:
def checked(self, item: MenuItem | None) -> None:
"""Sets the menu item that is currently checked in the group."""
for i in self:
if i.hmenu == -1:
raise ValueError('A MenuGroup must first be added to a Menu '
'before a MenuItem can be checked')
i.checked = i == item

@property
Expand Down
12 changes: 11 additions & 1 deletion tests/test_application.py
Expand Up @@ -22,6 +22,11 @@ def test_menu_item():
mi2 = activex.MenuItem(hmenu=123, id=8, text='hello world', callback=None, flags=1, data=[-1, 0, 1])
assert mi != mi2 # IDs do not match

mi3 = activex.MenuItem(hmenu=-1, id=1, text='a', callback=None, flags=0, data=None)

with pytest.raises(ValueError, match='A MenuItem must first be added to a Menu'):
mi3.checked = True


def test_menu_group():
mg = activex.MenuGroup()
Expand All @@ -33,15 +38,20 @@ def test_menu_group():
a = mg.append('a')
assert isinstance(a, activex.MenuItem)
mg.append('b', data=8, flags=activex.MenuFlag.POPUP)
mg.append_separator()
mg.append('c', callback=None)
assert mg.name == 'click'
assert mg.checked is None
assert str(mg) == "<MenuGroup name='click' size=3>"
assert str(mg) == "<MenuGroup name='click' size=4>"

for item in mg:
assert isinstance(item, activex.MenuItem)
assert item.checked is False

for item in [a, None]:
with pytest.raises(ValueError, match='A MenuGroup must first be added to a Menu'):
mg.checked = item


@skipif_not_windows
def test_menu():
Expand Down

0 comments on commit 3e1209b

Please sign in to comment.