Skip to content

Commit 795bb25

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

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

@@ -176,3 +178,21 @@ def approx_tick_info(
176178
object.__setattr__(approx_tick_info, "sleep_infos", approx_sleeps)
177179

178180
return approx_tick_info
181+
182+
183+
class matches_re: # pylint: disable=invalid-name
184+
"""Assert that a given string (or string representation) matches a regex pattern."""
185+
186+
def __init__(self, pattern: str, flags: int = 0) -> None:
187+
"""Initialize with a regex pattern and optional flags."""
188+
self._regex = re.compile(pattern, flags)
189+
190+
@override
191+
def __eq__(self, other: object) -> bool:
192+
"""Check if the string representation of `other` matches the regex pattern."""
193+
return bool(self._regex.match(str(other)))
194+
195+
@override
196+
def __repr__(self) -> str:
197+
"""Return a string representation of this instance."""
198+
return self._regex.pattern

0 commit comments

Comments
 (0)