Skip to content

Commit fecf915

Browse files
author
Anāth Šoka
committed
extend 'find' capabilities greatly
introduce new functions like: * 'find_after' * 'find_right_after' * 'find_eob' - returns True if it is end of the buffer to allow more precise match of the buffer
1 parent 797c1de commit fecf915

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

cli_ui/tests/conftest.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class MessageRecorder:
1111

1212
def __init__(self) -> None:
1313
cli_ui._MESSAGES = []
14+
self.idx_find_next: int = 0
1415

1516
def start(self) -> None:
1617
"""Start recording messages"""
@@ -24,6 +25,7 @@ def stop(self) -> None:
2425
def reset(self) -> None:
2526
"""Reset the list"""
2627
cli_ui._MESSAGES = []
28+
self.idx_find_next = 0
2729

2830
def find(self, pattern: str) -> Optional[str]:
2931
"""Find a message in the list of recorded message
@@ -32,11 +34,57 @@ def find(self, pattern: str) -> Optional[str]:
3234
when looking for recorded message
3335
"""
3436
regexp = re.compile(pattern)
35-
for message in cli_ui._MESSAGES:
37+
for idx, message in enumerate(cli_ui._MESSAGES):
3638
if re.search(regexp, message):
37-
return message
39+
if isinstance(message, str):
40+
self.idx_find_next = idx + 1
41+
return message
3842
return None
3943

44+
def find_after(self, pattern: str) -> Optional[str]:
45+
"""Same as 'find', but it check any message that comes
46+
after the last 'find'|'find_after'|'find_right_after'
47+
48+
:param pattern: regular expression pattern to use
49+
when looking for recorded message
50+
"""
51+
regexp = re.compile(pattern)
52+
for idx, message in enumerate(cli_ui._MESSAGES):
53+
if idx < self.idx_find_next:
54+
continue
55+
if re.search(regexp, message):
56+
if isinstance(message, str):
57+
self.idx_find_next = idx + 1
58+
return message
59+
return None
60+
61+
def find_right_after(self, pattern: str) -> Optional[str]:
62+
"""Same as 'find', but only check the message that is right after
63+
the one found last time. if no message was found before, the 1st
64+
message in buffer is checked
65+
66+
This is particulary usefull if we want to match only consecutive message.
67+
Calling this function can be repeated for further consecutive message match.
68+
"""
69+
if len(cli_ui._MESSAGES) > self.idx_find_next:
70+
regexp = re.compile(pattern)
71+
message = cli_ui._MESSAGES[self.idx_find_next]
72+
if re.search(regexp, message):
73+
if isinstance(message, str):
74+
self.idx_find_next += 1
75+
return message
76+
return None
77+
78+
def find_eob(self) -> bool: # find end of (the) buffer
79+
"""If we want to be sure we are at the end of the message buffer
80+
81+
That means, that our last 'find' or 'find_ritht_after'
82+
matched the very last line of the message buffer"""
83+
if len(cli_ui._MESSAGES) == self.idx_find_next:
84+
return True
85+
else:
86+
return False
87+
4088

4189
@pytest.fixture
4290
def message_recorder(request: Any) -> Iterator[MessageRecorder]:

0 commit comments

Comments
 (0)