Skip to content

Commit cabf859

Browse files
committed
Add an utility to assert that a string matches a regex pattern
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 49fa46b commit cabf859

File tree

1 file changed

+20
-0
lines changed
  • tests/timeseries/_resampling/wall_clock_timer

1 file changed

+20
-0
lines changed

tests/timeseries/_resampling/wall_clock_timer/util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Utility functions for testing the wall clock timer."""
55

6+
import re
67
from datetime import datetime, timedelta, timezone
78
from typing import assert_never, overload
89

@@ -15,6 +16,7 @@
1516
# It also looks like we are not the only ones doing this, see:
1617
# https://github.com/pytest-dev/pytest/issues/8395
1718
from _pytest.python_api import ApproxBase
19+
from typing_extensions import override
1820

1921
from frequenz.sdk.timeseries._resampling._wall_clock_timer import ClocksInfo, TickInfo
2022

@@ -184,3 +186,21 @@ def approx_tick_info(
184186
object.__setattr__(approx_tick_info, "sleep_infos", approx_sleeps)
185187

186188
return approx_tick_info
189+
190+
191+
class matches_re: # pylint: disable=invalid-name
192+
"""Assert that a given string (or string representation) matches a regex pattern."""
193+
194+
def __init__(self, pattern: str, flags: int = 0) -> None:
195+
"""Initialize with a regex pattern and optional flags."""
196+
self._regex = re.compile(pattern, flags)
197+
198+
@override
199+
def __eq__(self, other: object) -> bool:
200+
"""Check if the string representation of `other` matches the regex pattern."""
201+
return bool(self._regex.match(str(other)))
202+
203+
@override
204+
def __repr__(self) -> str:
205+
"""Return a string representation of this instance."""
206+
return self._regex.pattern

0 commit comments

Comments
 (0)