From 89b202bee87d7a63be4f2a90dddb39c8817e0d74 Mon Sep 17 00:00:00 2001 From: lomnido Date: Wed, 28 Aug 2024 12:13:39 +0200 Subject: [PATCH 1/2] tests: introducing 'find_next' function can be used to limit find for consecutive message only. --- cli_ui/tests/conftest.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/cli_ui/tests/conftest.py b/cli_ui/tests/conftest.py index b9d407a..9ed7ed8 100644 --- a/cli_ui/tests/conftest.py +++ b/cli_ui/tests/conftest.py @@ -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""" @@ -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 From fdb0ac6283f5686fe200a0d96faa21ab133d89d4 Mon Sep 17 00:00:00 2001 From: lomnido <135008914+lomnido@users.noreply.github.com> Date: Fri, 20 Sep 2024 09:32:41 +0200 Subject: [PATCH 2/2] refactoring: using better name: 'find_right_after' for 'conftest.py' --- cli_ui/tests/conftest.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cli_ui/tests/conftest.py b/cli_ui/tests/conftest.py index 9ed7ed8..76bd15d 100644 --- a/cli_ui/tests/conftest.py +++ b/cli_ui/tests/conftest.py @@ -40,8 +40,10 @@ def find(self, pattern: str) -> Optional[str]: return message return None - def find_next(self, pattern: str) -> Optional[str]: - """Same as 'find', however finds in next message ONLY. + 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