Skip to content

Commit

Permalink
Updated dummy assertions to be simple functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Feb 25, 2023
1 parent 2ea497e commit c8e41b1
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 66 deletions.
3 changes: 1 addition & 2 deletions android/tests_backend/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ def assert_container(self, container):
else:
raise AssertionError(f"cannot find {self.native} in {container_native}")

def alignment_equivalent(self, actual, expected):
def assert_alignment_equivalent(self, actual, expected):
assert actual == expected
return True

async def redraw(self):
"""Request a redraw of the app, waiting until that redraw has completed."""
Expand Down
3 changes: 1 addition & 2 deletions cocoa/tests_backend/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ def assert_container(self, container):
else:
raise ValueError(f"cannot find {self.native} in {container_native}")

def alignment_equivalent(self, actual, expected):
def assert_alignment_equivalent(self, actual, expected):
assert actual == expected
return True

async def redraw(self):
"""Request a redraw of the app, waiting until that redraw has completed."""
Expand Down
10 changes: 5 additions & 5 deletions core/tests/widgets/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import toga
from toga_dummy.utils import (
EventLog,
action_performed,
action_performed_with,
assert_action_performed,
assert_action_performed_with,
attribute_value,
)

Expand All @@ -18,7 +18,7 @@ def test_widget_created(button):
"""A button can be created."""
# Round trip the impl/interface
assert button._impl.interface == button
assert action_performed(button, "create Button")
assert_action_performed(button, "create Button")


@pytest.mark.parametrize(
Expand All @@ -43,7 +43,7 @@ def test_button_text(button, value, expected):
assert attribute_value(button, "text") == expected

# A rehint was performed
assert action_performed(button, "rehint")
assert_action_performed(button, "rehint")


def test_button_on_press(button):
Expand All @@ -66,6 +66,6 @@ def callback(widget, **extra):
button.on_press(button, a=1)

# Callback was invoked
assert action_performed_with(
assert_action_performed_with(
button, "callback invoked", widget=button, extra={"a": 1}
)
84 changes: 34 additions & 50 deletions dummy/src/toga_dummy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,11 @@ def layout(self, root, viewport):

###########################################################################
# Pytest widget assertion helpers
#
# These helpers are written so that they can be used as:
# assert action_not_performed(...)
#
# The functions all return True on success, but raise assertions on failure.
# This allows the methods to have helpful context-sensitive failure messages,
# while preserving the pytest "assert" syntax.
###############################################################################


def module_action_not_performed(_module, _action):
"""Determine that the module-level action was *not* performed.
def assert_module_action_not_performed(_module, _action):
"""Assert that the module-level action was *not* performed.
:param _module: The module with the action that should not have been performed.
:param _action: The name of the action to check
Expand All @@ -213,11 +206,10 @@ def module_action_not_performed(_module, _action):
), f"Action {_action!r} unexpectedly performed by {_module}."
except AttributeError:
pytest.fail(f"Module {_module} is not a logged object")
return True


def module_action_performed(_module, _action):
"""Determine that a module-level action was performed.
def assert_module_action_performed(_module, _action):
"""Assert that a module-level action was performed.
:param _module: The module with the action that should have been performed.
:param _action: The name of the action to check
Expand All @@ -230,22 +222,21 @@ def module_action_performed(_module, _action):
)
except AttributeError:
pytest.fail(f"Module {_module} is not a logged object")
return True


def module_action_performed_with(_module, _action, **test_data):
"""Determine if the module-level action was performed with specific test data.
def assert_module_action_performed_with(_module, _action, **test_data):
"""Assert if the module-level action was performed with specific test data.
:param _module: The module with the action that should have been performed.
:param _action: The name of the action to check
:param test_data: The arguments that should have been passed to the action.
:returns: True if a matching action was performed.
"""
try:
found = True
# Iterate over every action that was performed on
# this object.
for _, data in _MODULES[_module]._actions[_action].items():
found = True
# Iterate over every key and value in the test
# data. If the value in the recorded action
# doesn't match the requested value, then this isn't
Expand All @@ -259,12 +250,10 @@ def module_action_performed_with(_module, _action, **test_data):

# Default behavior is to be found; so if we're
# still in a "found" state, this action is a match
# for the test data. Otherwise, reset, and try again
# for the test data. Otherwise, try again
# with the next recorded action.
if found:
return True
else:
found = True
return

# None of the recorded actions match the test data.
actual_actions = sorted(_MODULES[_module]._actions.keys())
Expand Down Expand Up @@ -323,8 +312,8 @@ def attribute_values(_widget, _attr):
pytest.fail(f"Widget {_widget} is not a logged object")


def attribute_retrieved(_widget, _attr):
"""Determine that the widget implementation attempted to retrieve an attribute.
def assert_attribute_retrieved(_widget, _attr):
"""Assert that the widget implementation attempted to retrieve an attribute.
:param _widget: The interface of the widget to check
:param _attr: The attribute to check.
Expand All @@ -338,11 +327,10 @@ def attribute_retrieved(_widget, _attr):
)
except AttributeError:
pytest.fail(f"Widget {_widget} is not a logged object")
return True


def attribute_not_retrieved(_widget, _attr):
"""Determine that the widget implementation did not attempt to retrieve an attribute.
def assert_attribute_not_retrieved(_widget, _attr):
"""Assert that the widget implementation did not attempt to retrieve an attribute.
:param _widget: The interface of the widget to check
:param _attr: The attribute to check.
Expand All @@ -354,11 +342,10 @@ def attribute_not_retrieved(_widget, _attr):
), f"Widget {_widget} unexpectedly retrieved the attribute {_attr!r}."
except AttributeError:
pytest.fail(f"Widget {_widget} is not a logged object")
return True


def attribute_not_set(_widget, _attr):
"""Determine that the widget implementation did not attempt to set an attribute.
def assert_attribute_not_set(_widget, _attr):
"""Assert that the widget implementation did not attempt to set an attribute.
:param _widget: The interface of the widget to check
:param _attr: The attribute to check.
Expand All @@ -370,11 +357,10 @@ def attribute_not_set(_widget, _attr):
), f"Widget {_widget} unexpectedly set the attribute {_attr!r}."
except AttributeError:
pytest.fail(f"Widget {_widget} is not a logged object")
return True


def action_not_performed(_widget, _action):
"""Determine that the named action was *not* performed by a widget.
def assert_action_not_performed(_widget, _action):
"""Assert that the named action was *not* performed by a widget.
:param _widget: The interface of the widget to check
:param _action: The action to check.
Expand All @@ -386,11 +372,10 @@ def action_not_performed(_widget, _action):
), f"Action {_action!r} unexpectedly performed by {_widget}."
except AttributeError:
pytest.fail(f"Widget {_widget} is not a logged object")
return True


def action_performed(_widget, _action):
"""Determine that the named action was performed by a widget.
def assert_action_performed(_widget, _action):
"""Assert that the named action was performed by a widget.
:param _widget: The interface of the widget to check
:param _action: The action to check.
Expand All @@ -403,22 +388,21 @@ def action_performed(_widget, _action):
)
except AttributeError:
pytest.fail(f"Widget {_widget} is not a logged object")
return True


def action_performed_with(_widget, _action, **test_data):
"""Determine if an action was performed with specific test data.
def assert_action_performed_with(_widget, _action, **test_data):
"""Assert if an action was performed with specific test data.
:param _widget: The interface of the widget to check
:param _action: The action to check.
:param test_data: The arguments that should have been passed to the action.
:returns: True if the action was performed
"""
try:
found = True
# Iterate over every action that was performed on
# this object.
for _, data in _widget._impl._actions[_action].items():
found = True
# Iterate over every key and value in the test
# data. If the value in the recorded action
# doesn't match the requested value, then this isn't
Expand Down Expand Up @@ -447,9 +431,7 @@ def action_performed_with(_widget, _action, **test_data):
# for the test data. Otherwise, reset, and try again
# with the next recorded action.
if found:
return True
else:
found = True
return

# None of the recorded actions match the test data.
pytest.fail(
Expand Down Expand Up @@ -499,7 +481,7 @@ def assertFunctionNotPerformed(self, _module, _action):
_module: The module with the action that should not have been performed.
_action: The name of the action to check
"""
self.pytest_assert(module_action_not_performed, _module, _action)
self.pytest_assert(assert_module_action_not_performed, _module, _action)

def assertFunctionPerformed(self, _module, _action):
"""Assert that the action function from module was performed.
Expand All @@ -508,7 +490,7 @@ def assertFunctionPerformed(self, _module, _action):
_module: The module with the action that should have been performed.
_action: The name of the action to check
"""
self.pytest_assert(module_action_performed, _module, _action)
self.pytest_assert(assert_module_action_performed, _module, _action)

def assertFunctionPerformedWith(self, _module, _action, **test_data):
"""Confirm that the action function form module was performed with
Expand All @@ -523,7 +505,9 @@ def assertFunctionPerformedWith(self, _module, _action, **test_data):
If a matching action was performed, the full data of
the performed action if. False otherwise.
"""
self.pytest_assert(module_action_performed_with, _module, _action, **test_data)
self.pytest_assert(
assert_module_action_performed_with, _module, _action, **test_data
)

#####

Expand Down Expand Up @@ -557,13 +541,13 @@ def assertValueGet(self, _widget, _attr):
_widget: The interface of the widget to check
_attr: The attribute that should have been retrieved
"""
self.pytest_assert(attribute_retrieved, _widget, _attr)
self.pytest_assert(assert_attribute_retrieved, _widget, _attr)

def assertValueNotGet(self, _widget, _attr):
self.pytest_assert(attribute_not_retrieved, _widget, _attr)
self.pytest_assert(assert_attribute_not_retrieved, _widget, _attr)

def assertValueNotSet(self, _widget, _attr):
self.pytest_assert(attribute_not_set, _widget, _attr)
self.pytest_assert(assert_attribute_not_set, _widget, _attr)

def assertActionNotPerformed(self, _widget, _action):
"""Assert that the named action was *not* performed by a widget.
Expand All @@ -572,15 +556,15 @@ def assertActionNotPerformed(self, _widget, _action):
_widget: The interface of the widget that should not have performed the action.
_action: The name of the action to check
"""
self.pytest_assert(action_not_performed, _widget, _action)
self.pytest_assert(assert_action_not_performed, _widget, _action)

def assertActionPerformed(self, _widget, _action):
"""Assert that the named action performed by a widget.
Args:
_widget: The interface of the widget that should have performed the action.
_action: The name of the action to check
"""
self.pytest_assert(action_performed, _widget, _action)
self.pytest_assert(assert_action_performed, _widget, _action)

def assertActionPerformedWith(self, _widget, _action, **test_data):
"""Was the action performed with specific test data.
Expand All @@ -594,4 +578,4 @@ def assertActionPerformedWith(self, _widget, _action, **test_data):
If a matching action was performed, the full data of
the performed action if. False otherwise.
"""
self.pytest_assert(action_performed_with, _widget, _action, **test_data)
self.pytest_assert(assert_action_performed_with, _widget, _action, **test_data)
3 changes: 1 addition & 2 deletions gtk/tests_backend/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def assert_container(self, container):
else:
raise ValueError(f"cannot find {self.native} in {container_native}")

def alignment_equivalent(self, actual, expected):
def assert_alignment_equivalent(self, actual, expected):
assert actual == expected
return True

async def redraw(self):
"""Request a redraw of the app, waiting until that redraw has completed."""
Expand Down
3 changes: 1 addition & 2 deletions iOS/tests_backend/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def assert_container(self, container):
else:
raise ValueError(f"cannot find {self.native} in {container_native}")

def alignment_equivalent(self, actual, expected):
def assert_alignment_equivalent(self, actual, expected):
assert actual == expected
return True

async def redraw(self):
"""Request a redraw of the app, waiting until that redraw has completed."""
Expand Down
2 changes: 1 addition & 1 deletion testbed/tests/widgets/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def test_alignment(widget, probe):
for alignment in [RIGHT, CENTER, JUSTIFY]:
widget.style.text_align = alignment
await probe.redraw()
assert probe.alignment_equivalent(probe.alignment, alignment)
probe.assert_alignment_equivalent(probe.alignment, alignment)

# Clearing the alignment reverts to default alignment of LEFT
widget.style.text_align = None
Expand Down
3 changes: 1 addition & 2 deletions winforms/tests_backend/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ def assert_container(self, container):
else:
raise ValueError(f"cannot find {self.native} in {container_native}")

def alignment_equivalent(self, actual, expected):
def assert_alignment_equivalent(self, actual, expected):
# Winforms doesn't have a "Justified" alignment; it falls back to LEFT
if expected == JUSTIFY:
assert actual == LEFT
else:
assert actual == expected
return True

async def redraw(self):
"""Request a redraw of the app, waiting until that redraw has completed."""
Expand Down

0 comments on commit c8e41b1

Please sign in to comment.