Skip to content

Commit

Permalink
tests: introducing 'find_next' function
Browse files Browse the repository at this point in the history
can be used to limit find for consecutive message only.
  • Loading branch information
lomnido authored and Anāth Šoka committed Aug 28, 2024
1 parent 4c80af0 commit 89b202b
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions cli_ui/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MessageRecorder:

def __init__(self) -> None:
cli_ui._MESSAGES = []
self.idx_find_next: int = 0

def start(self) -> None:
"""Start recording messages"""
Expand All @@ -32,9 +33,29 @@ def find(self, pattern: str) -> Optional[str]:
when looking for recorded message
"""
regexp = re.compile(pattern)
for message in cli_ui._MESSAGES:
for idx, message in enumerate(cli_ui._MESSAGES):
if re.search(regexp, message):
return message
if isinstance(message, str):
self.idx_find_next = idx + 1
return message
return None

def find_next(self, pattern: str) -> Optional[str]:
"""Same as 'find', however finds in next message ONLY.
:param pattern: regular expression pattern to use
when looking for recorded message
This is particulary usefull when we want to match only consecutive message.
Calling this function can be repeated for further consecutive message match.
"""
if len(cli_ui._MESSAGES) > self.idx_find_next:
regexp = re.compile(pattern)
message = cli_ui._MESSAGES[self.idx_find_next]
if re.search(regexp, message):
if isinstance(message, str):
self.idx_find_next += 1
return message
return None


Expand Down

0 comments on commit 89b202b

Please sign in to comment.