"""Defines a stream based string matcher and a generic state object."""
class MatchStaticString:
"""Match a static string versus input."""
__test__ = False
def __init__(self, match_string):
"""Initialize using specified match string."""
self._string = match_string
self._input = ""
def match(self, input_char) -> bool:
"""
Check if `_input` matches the match `_string`.
Process one char at a time and build `_input` string.
Preserve built `_input` if partially matches `_string`.
Return True when `_input` is the same as `_string`.
"""
if input_char == '':
return False
self._input += str(input_char)
if self._input == self._string[:len(self._input)]:
if len(self._input) == len(self._string):
self._input = ""
return True
return False
self._input = self._input[1:]
return False
class TestState(MatchStaticString):
"""Generic test state object."""
__test__ = False
def __init__(self, match_string=''):
"""Initialize state fields."""
MatchStaticString.__init__(self, match_string)
print('\n*** Current test state: ', str(self), end='')
def handle_input(self, serial, input_char):
"""Handle input event and return next state."""
def __repr__(self):
"""Leverages the __str__ method to describe the TestState."""
return self.__str__()
def __str__(self):
"""Return state name."""
return self.__class__.__name__