Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: introducing 'find_right_after' function #115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 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,31 @@ 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_right_after(self, pattern: str) -> Optional[str]:
"""Same as 'find', but only check the message that is right after
the one found last time. if no message was found before, the 1st
message in buffer is checked

: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
Loading