remotivelabs.topology.testing.hamcrest

PyHamcrest-based helpers for polling assertions.

Optional PyHamcrest-based helpers for polling assertions in async tests. Requires the hamcrest extras:

pip install remotivelabs-topology[hamcrest]

remotivelabs.topology.testing.hamcrest.await_at_most provides a fluent retry API that repeatedly calls a function until its return value satisfies a matcher or the timeout is reached:

from remotivelabs.broker import BrokerClient, FrameSubscription, SignalValue

from remotivelabs.topology.namespaces.can import CanNamespace
from remotivelabs.topology.testing.hamcrest import await_at_most, contains_signal_value_sequence


async def test_signal_sequence(broker_client: BrokerClient) -> None:
    async with CanNamespace("HazardLightControlUnit-DriverCan0", broker_client) as ns:
        stream = await ns.subscribe_frames(FrameSubscription(name="HazardLightButton"))
        values: list[SignalValue] = []

        async def consume_next() -> list[SignalValue]:
            frame = await anext(stream)
            values.append(frame.signals["HazardLightButton.HazardLightButton"])
            return values

        # Assert the button transitions 0 → 1 → 0 within 5 seconds
        await await_at_most(5).until(consume_next, contains_signal_value_sequence(0, 1, 0))

Signal value matchers:

  • remotivelabs.topology.testing.hamcrest.contains_signal_value_sequence — passes if a list contains the given values as a contiguous subsequence (consecutive duplicates are collapsed).
  • remotivelabs.topology.testing.hamcrest.ends_with_signal_value_sequence — passes if a list ends with the given sequence.
  • remotivelabs.topology.testing.hamcrest.contains_signal_value_sequences — passes if each key in a dict maps to a list containing its expected subsequence.
  • remotivelabs.topology.testing.hamcrest.has_last_element — passes if the last element of a list satisfies a nested matcher.
 1"""
 2# PyHamcrest-based helpers for polling assertions.
 3
 4<!-- start-include -->
 5Optional [PyHamcrest](https://pypi.org/project/PyHamcrest/)-based helpers for polling assertions
 6in async tests. Requires the `hamcrest` extras:
 7
 8```bash
 9pip install remotivelabs-topology[hamcrest]
10```
11
12`remotivelabs.topology.testing.hamcrest.await_at_most` provides a fluent retry API that repeatedly
13calls a function until its return value satisfies a matcher or the timeout is reached:
14
15```python
16.. include:: ../../_docs/snippets/testing_hamcrest.py
17```
18
19Signal value matchers:
20
21- `remotivelabs.topology.testing.hamcrest.contains_signal_value_sequence` — passes if a list
22  contains the given values as a contiguous subsequence (consecutive duplicates are collapsed).
23- `remotivelabs.topology.testing.hamcrest.ends_with_signal_value_sequence` — passes if a list
24  ends with the given sequence.
25- `remotivelabs.topology.testing.hamcrest.contains_signal_value_sequences` — passes if each key
26  in a dict maps to a list containing its expected subsequence.
27- `remotivelabs.topology.testing.hamcrest.has_last_element` — passes if the last element of a
28  list satisfies a nested matcher.
29<!-- end-include -->
30"""
31
32try:
33    import hamcrest  # noqa: F401
34except ImportError as e:
35    raise ImportError(
36        "The 'remotivelabs.topology.testing.hamcrest' subpackage requires hamcrest extras.\n"
37        "Install it with: pip install remotivelabs-topology[hamcrest]"
38    ) from e
39
40from remotivelabs.topology.testing.hamcrest.await_at_most import await_at_most  # noqa: F401
41from remotivelabs.topology.testing.hamcrest.matchers import (
42    contains_signal_value_sequence,  # noqa: F401
43    contains_signal_value_sequences,  # noqa: F401
44    ends_with_signal_value_sequence,  # noqa: F401
45    has_last_element,  # noqa: F401
46)
47
48all = [
49    "await_at_most",
50    "contains_signal_value_sequence",
51    "contains_signal_value_sequences",
52    "ends_with_signal_value_sequence",
53    "has_last_element",
54]
all = ['await_at_most', 'contains_signal_value_sequence', 'contains_signal_value_sequences', 'ends_with_signal_value_sequence', 'has_last_element']