|
| 1 | +"""Stubs for the subset of ``hypothesis.stateful`` used by the test suite. |
| 2 | +
|
| 3 | +The stateful API has no equivalent of ``@example``, so without Hypothesis |
| 4 | +there is nothing to replay: the ``TestCase`` generated for a state machine |
| 5 | +is skipped, and ``run_state_machine_as_test`` raises ``unittest.SkipTest``. |
| 6 | +The decorators and helpers are accepted so that state machine classes can |
| 7 | +be defined at import time. |
| 8 | +""" |
| 9 | +import unittest |
| 10 | + |
| 11 | +from ._helpers import stub_factory |
| 12 | +from .strategies import StubStrategy |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + "Bundle", |
| 16 | + "MultipleResults", |
| 17 | + "RuleBasedStateMachine", |
| 18 | + "consumes", |
| 19 | + "initialize", |
| 20 | + "invariant", |
| 21 | + "multiple", |
| 22 | + "precondition", |
| 23 | + "rule", |
| 24 | + "run_state_machine_as_test", |
| 25 | +] |
| 26 | + |
| 27 | +_SKIP_REASON = "Hypothesis required for stateful test" |
| 28 | + |
| 29 | +Bundle = stub_factory(StubStrategy, "hypothesis.stateful.Bundle") |
| 30 | + |
| 31 | + |
| 32 | +def consumes(bundle): |
| 33 | + return bundle |
| 34 | + |
| 35 | + |
| 36 | +class MultipleResults: |
| 37 | + def __init__(self, *values): |
| 38 | + self.values = values |
| 39 | + |
| 40 | + |
| 41 | +def multiple(*args): |
| 42 | + return MultipleResults(*args) |
| 43 | + |
| 44 | + |
| 45 | +def _identity_operator(f): |
| 46 | + return f |
| 47 | + |
| 48 | + |
| 49 | +def _identity_decorator(*_args, **_kwargs): |
| 50 | + return _identity_operator |
| 51 | + |
| 52 | + |
| 53 | +rule = _identity_decorator |
| 54 | +initialize = _identity_decorator |
| 55 | +invariant = _identity_decorator |
| 56 | +precondition = _identity_decorator |
| 57 | + |
| 58 | + |
| 59 | +class _TestCaseProperty: |
| 60 | + """Build (and cache) a skipped ``unittest.TestCase`` per state machine.""" |
| 61 | + |
| 62 | + def __init__(self): |
| 63 | + self._cases = {} |
| 64 | + |
| 65 | + def __get__(self, obj, owner): |
| 66 | + if owner not in self._cases: |
| 67 | + |
| 68 | + class StateMachineTestCase(unittest.TestCase): |
| 69 | + settings = None |
| 70 | + |
| 71 | + def runTest(self): |
| 72 | + raise unittest.SkipTest(_SKIP_REASON) |
| 73 | + |
| 74 | + StateMachineTestCase.__name__ = f"{owner.__name__}.TestCase" |
| 75 | + StateMachineTestCase.__qualname__ = f"{owner.__qualname__}.TestCase" |
| 76 | + self._cases[owner] = StateMachineTestCase |
| 77 | + return self._cases[owner] |
| 78 | + |
| 79 | + |
| 80 | +class RuleBasedStateMachine: |
| 81 | + TestCase = _TestCaseProperty() |
| 82 | + |
| 83 | + def bundle(self, name): |
| 84 | + return Bundle(name) |
| 85 | + |
| 86 | + def check_invariants(self, settings, output, runtimes): |
| 87 | + pass |
| 88 | + |
| 89 | + def setup_state(self): |
| 90 | + pass |
| 91 | + |
| 92 | + def teardown(self): |
| 93 | + pass |
| 94 | + |
| 95 | + |
| 96 | +def run_state_machine_as_test(state_machine_factory, *, settings=None): |
| 97 | + raise unittest.SkipTest(_SKIP_REASON) |
0 commit comments