|
3 | 3 |
|
4 | 4 | """Utility functions for testing the wall clock timer."""
|
5 | 5 |
|
| 6 | +import re |
6 | 7 | from datetime import datetime, timedelta, timezone
|
7 | 8 | from typing import assert_never, overload
|
8 | 9 |
|
|
15 | 16 | # It also looks like we are not the only ones doing this, see:
|
16 | 17 | # https://github.com/pytest-dev/pytest/issues/8395
|
17 | 18 | from _pytest.python_api import ApproxBase
|
| 19 | +from typing_extensions import override |
18 | 20 |
|
19 | 21 | from frequenz.sdk.timeseries._resampling._wall_clock_timer import ClocksInfo, TickInfo
|
20 | 22 |
|
@@ -184,3 +186,21 @@ def approx_tick_info(
|
184 | 186 | object.__setattr__(approx_tick_info, "sleep_infos", approx_sleeps)
|
185 | 187 |
|
186 | 188 | 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