diff --git a/test/integrationtests/voight_kampff/__init__.py b/test/integrationtests/voight_kampff/__init__.py index 26a04cb526ef..ec4e5594e4e2 100644 --- a/test/integrationtests/voight_kampff/__init__.py +++ b/test/integrationtests/voight_kampff/__init__.py @@ -25,5 +25,5 @@ wait_for_dialog_match, VoightKampffCriteriaMatcher, VoightKampffDialogMatcher, - VoightKampffEventMatcher + VoightKampffMessageMatcher ) diff --git a/test/integrationtests/voight_kampff/tools.py b/test/integrationtests/voight_kampff/tools.py index 185b1c30d86c..fad92f6312e5 100644 --- a/test/integrationtests/voight_kampff/tools.py +++ b/test/integrationtests/voight_kampff/tools.py @@ -24,9 +24,16 @@ DEFAULT_TIMEOUT = 10 -class VoightKampffEventMatcher: +class VoightKampffMessageMatcher: """Matches a specified message type to messages emitted on the bus. + Usage: + Intended for use in a single test condition. + + matcher = VoightKampffMessageMatcher(message_type, context) + match_found, error_message = matcher.match + assert match_found, error_message + Attributes: message_type: identifier of the message to search for on the bus context: the Behave context from the test utilizing this class @@ -38,9 +45,12 @@ def __init__(self, message_type: str, context: Any): self.message_type = message_type self.context = context self.match_event = Event() - self.match_found = False self.error_message = "" + @property + def match_found(self): + return self.match_event.is_set() + def match(self, timeout: int = None): """Attempts to match the requested message type to emitted bus events. @@ -58,15 +68,16 @@ def match(self, timeout: int = None): if not self.match_event.is_set(): self.match_event.wait(timeout=timeout) self.context.bus.remove(self.message_type, self.handle_message) - self.match_found = self.match_event.is_set() if not self.match_found: self._build_error_message() + return self.match_found, self.error_message + def _check_historical_messages(self): """Searches messages emitted before the event handler was defined.""" for message in self.context.bus.get_messages(self.message_type): self.handle_message(message) - if self.match_event.is_set(): + if self.match_found: break self.context.bus.clear_messages() @@ -86,9 +97,16 @@ def _build_error_message(self): ) -class VoightKampffDialogMatcher(VoightKampffEventMatcher): +class VoightKampffDialogMatcher(VoightKampffMessageMatcher): """Variation of VoightKampffEventMatcher for matching dialogs. + Usage: + Intended for use in a single test condition. + + matcher = VoightKampffMessageMatcher(message_type, context) + match_found, error_message = matcher.match + assert match_found, error_message + Attributes: dialogs: one or more dialog names that will constitute a match speak_messages: bus messages with message type of "speak" captured @@ -133,13 +151,20 @@ def _build_error_message(self): self.error_message += "\tMycroft didn't respond" -class VoightKampffCriteriaMatcher(VoightKampffEventMatcher): +class VoightKampffCriteriaMatcher(VoightKampffMessageMatcher): """Variation of VoightKampffEventMatcher for matching event data. In some cases, matching the message type is not enough. The test requires data in the message payload to match a specified criteria to pass. + Usage: + Intended for use in a single test condition. + + matcher = VoightKampffMessageMatcher(message_type, context) + match_found, error_message = matcher.match + assert match_found, error_message + Attributes: criteria_matcher: Function to determine if a message contains the data necessary for the test case to pass @@ -304,9 +329,9 @@ def then_wait(msg_type: str, criteria_func: Callable, context: Any, The success of the match attempt and an error message. """ matcher = VoightKampffCriteriaMatcher(msg_type, context, criteria_func) - matcher.match(timeout) + match_found, error_message = matcher.match(timeout) - return matcher.match_found, matcher.error_message + return match_found, error_message def then_wait_fail(msg_type: str, criteria_func: Callable, context: Any, @@ -323,8 +348,8 @@ def then_wait_fail(msg_type: str, criteria_func: Callable, context: Any, Returns: tuple (bool, str) test status and debug output """ - status, debug = then_wait(msg_type, criteria_func, context, timeout) - return not status, debug + match_found, error_message = then_wait(msg_type, criteria_func, context, timeout) + return not match_found, error_message # TODO: remove in 21.08 @@ -473,7 +498,7 @@ def wait_for_audio_service(context: Any, message_type: str): AssertionError if no match is found. """ msg_type = 'mycroft.audio.service.{}'.format(message_type) - event_matcher = VoightKampffEventMatcher(msg_type, context) - event_matcher.match() + event_matcher = VoightKampffMessageMatcher(msg_type, context) + match_found, error_message = event_matcher.match() - assert event_matcher.match_found, event_matcher.error_message + assert match_found, error_message