declarative-mocks 0.2.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/PKG-INFO +15 -12
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/README.md +14 -11
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/pyproject.toml +5 -2
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/src/dmock/__init__.py +2 -1
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/src/dmock/_exceptions.py +9 -8
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/src/dmock/_expectation.py +55 -16
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/src/dmock/_matchers.py +52 -51
- declarative_mocks-0.4.0/src/dmock/_mock.py +437 -0
- declarative_mocks-0.4.0/src/dmock/_protocols.py +54 -0
- declarative_mocks-0.4.0/src/dmock/_pytest_plugin.py +47 -0
- declarative_mocks-0.4.0/src/dmock/_registry.py +66 -0
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/src/dmock/_types.py +26 -1
- declarative_mocks-0.2.0/src/dmock/_mock.py +0 -289
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/LICENSE +0 -0
- {declarative_mocks-0.2.0 → declarative_mocks-0.4.0}/src/dmock/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: declarative-mocks
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Declare mock behavior as ordered expectations. Simple, typed, gomock-inspired DSL over unittest.mock
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -76,7 +76,10 @@ Here's an example of how _dmock_ improves your tests:
|
|
|
76
76
|
from dmock import AnythingOfType, Contains, DeclarativeMock, StartsWith
|
|
77
77
|
|
|
78
78
|
service = DeclarativeMock(Directory)
|
|
79
|
-
|
|
79
|
+
# Using `Directory.lookup` helps a type checker to checks the call arguments and the values passed to `.returns` and `.runs`
|
|
80
|
+
service.expect(Directory.lookup, "ada").returns(User(id=1, name="Ada", email="ada@example.com"))
|
|
81
|
+
# or you could use the string form to produce a lenient expectation: any arguments, any outcome
|
|
82
|
+
# service.expect("lookup", "ada").returns(User(id=1, name="Ada", email="ada@example.com"))
|
|
80
83
|
|
|
81
84
|
assert service.lookup("ada") == User(
|
|
82
85
|
id=AnythingOfType(int),
|
|
@@ -143,16 +146,16 @@ calc.expect("price", 400).never()
|
|
|
143
146
|
calc.verify()
|
|
144
147
|
```
|
|
145
148
|
|
|
146
|
-
Async methods use the same DSL. `runs()` accepts a sync or an async
|
|
149
|
+
Async methods use the same DSL. `runs()` accepts a sync or an async function:
|
|
147
150
|
|
|
148
151
|
```python
|
|
149
152
|
from dmock import Anything, DeclarativeMock
|
|
150
153
|
|
|
151
154
|
mock = DeclarativeMock(MyService)
|
|
152
|
-
mock.expect(
|
|
155
|
+
mock.expect(MyService.aprocess_order, Anything()).returns("ok")
|
|
153
156
|
assert await mock.aprocess_order(1) == "ok"
|
|
154
157
|
|
|
155
|
-
mock.expect(
|
|
158
|
+
mock.expect(MyService.aprocess_order, Anything()).runs(lambda order_id: f"sync-{order_id}")
|
|
156
159
|
assert await mock.aprocess_order(7) == "sync-7"
|
|
157
160
|
|
|
158
161
|
|
|
@@ -160,7 +163,7 @@ async def async_fn(order_id: int) -> str:
|
|
|
160
163
|
return f"async-{order_id}"
|
|
161
164
|
|
|
162
165
|
|
|
163
|
-
mock.expect(
|
|
166
|
+
mock.expect(MyService.aprocess_order, Anything()).runs(async_fn)
|
|
164
167
|
assert await mock.aprocess_order(9) == "async-9"
|
|
165
168
|
```
|
|
166
169
|
|
|
@@ -170,17 +173,16 @@ Look up the dmock source code and tests yourself, it's full of examples!
|
|
|
170
173
|
|
|
171
174
|
## Limitations & non-goals
|
|
172
175
|
|
|
173
|
-
- **No signature binding.** `expect("method", …)` checks that `method` exists on the spec, not that the argument list matches the real signature.
|
|
174
|
-
- **No automatic** `verify()
|
|
176
|
+
- **No signature binding at runtime.** `expect("method", …)` checks that `method` exists on the spec, not that the argument list matches the real signature at runtime.
|
|
177
|
+
- **No automatic** `verify()` **outside pytest.** The pytest plugin checks every mock when the test finishes. Other runners still need an explicit `verify()`.
|
|
175
178
|
- **No spies and no patching.** The library does not wrap live objects, modules, or `sys.modules`. Just construct a `DeclarativeMock` and pass it in.
|
|
176
179
|
- **Not thread-safe.** Concurrent use of one mock from multiple threads is out of scope.
|
|
177
180
|
|
|
178
181
|
## Roadmap
|
|
179
182
|
|
|
180
|
-
|
|
183
|
+
Nothing is planned for 0.5.0, feel free to suggest!
|
|
181
184
|
|
|
182
|
-
- **
|
|
183
|
-
- **Per-outcome counts (maybe)** - repeat a `.returns()` / `.raises()` / `.runs()` without chaining it n times (today `.returns(1).returns(1).returns(1).returns(2)`).
|
|
185
|
+
- **Other test runners (low priority)** - unittest and similar runners do not verify mocks on teardown the way the pytest plugin does.
|
|
184
186
|
|
|
185
187
|
Other ideas belong in GitHub Issues.
|
|
186
188
|
|
|
@@ -192,7 +194,8 @@ ruff check .
|
|
|
192
194
|
ruff format --check .
|
|
193
195
|
mypy src tests
|
|
194
196
|
pytest
|
|
195
|
-
|
|
197
|
+
coverage run -m pytest
|
|
198
|
+
coverage report
|
|
196
199
|
```
|
|
197
200
|
|
|
198
201
|
See **[CONTRIBUTING.md](CONTRIBUTING.md)** for setup, commits, and tests. Agents follow **[AGENTS.md](AGENTS.md)**.
|
|
@@ -50,7 +50,10 @@ Here's an example of how _dmock_ improves your tests:
|
|
|
50
50
|
from dmock import AnythingOfType, Contains, DeclarativeMock, StartsWith
|
|
51
51
|
|
|
52
52
|
service = DeclarativeMock(Directory)
|
|
53
|
-
|
|
53
|
+
# Using `Directory.lookup` helps a type checker to checks the call arguments and the values passed to `.returns` and `.runs`
|
|
54
|
+
service.expect(Directory.lookup, "ada").returns(User(id=1, name="Ada", email="ada@example.com"))
|
|
55
|
+
# or you could use the string form to produce a lenient expectation: any arguments, any outcome
|
|
56
|
+
# service.expect("lookup", "ada").returns(User(id=1, name="Ada", email="ada@example.com"))
|
|
54
57
|
|
|
55
58
|
assert service.lookup("ada") == User(
|
|
56
59
|
id=AnythingOfType(int),
|
|
@@ -117,16 +120,16 @@ calc.expect("price", 400).never()
|
|
|
117
120
|
calc.verify()
|
|
118
121
|
```
|
|
119
122
|
|
|
120
|
-
Async methods use the same DSL. `runs()` accepts a sync or an async
|
|
123
|
+
Async methods use the same DSL. `runs()` accepts a sync or an async function:
|
|
121
124
|
|
|
122
125
|
```python
|
|
123
126
|
from dmock import Anything, DeclarativeMock
|
|
124
127
|
|
|
125
128
|
mock = DeclarativeMock(MyService)
|
|
126
|
-
mock.expect(
|
|
129
|
+
mock.expect(MyService.aprocess_order, Anything()).returns("ok")
|
|
127
130
|
assert await mock.aprocess_order(1) == "ok"
|
|
128
131
|
|
|
129
|
-
mock.expect(
|
|
132
|
+
mock.expect(MyService.aprocess_order, Anything()).runs(lambda order_id: f"sync-{order_id}")
|
|
130
133
|
assert await mock.aprocess_order(7) == "sync-7"
|
|
131
134
|
|
|
132
135
|
|
|
@@ -134,7 +137,7 @@ async def async_fn(order_id: int) -> str:
|
|
|
134
137
|
return f"async-{order_id}"
|
|
135
138
|
|
|
136
139
|
|
|
137
|
-
mock.expect(
|
|
140
|
+
mock.expect(MyService.aprocess_order, Anything()).runs(async_fn)
|
|
138
141
|
assert await mock.aprocess_order(9) == "async-9"
|
|
139
142
|
```
|
|
140
143
|
|
|
@@ -144,17 +147,16 @@ Look up the dmock source code and tests yourself, it's full of examples!
|
|
|
144
147
|
|
|
145
148
|
## Limitations & non-goals
|
|
146
149
|
|
|
147
|
-
- **No signature binding.** `expect("method", …)` checks that `method` exists on the spec, not that the argument list matches the real signature.
|
|
148
|
-
- **No automatic** `verify()
|
|
150
|
+
- **No signature binding at runtime.** `expect("method", …)` checks that `method` exists on the spec, not that the argument list matches the real signature at runtime.
|
|
151
|
+
- **No automatic** `verify()` **outside pytest.** The pytest plugin checks every mock when the test finishes. Other runners still need an explicit `verify()`.
|
|
149
152
|
- **No spies and no patching.** The library does not wrap live objects, modules, or `sys.modules`. Just construct a `DeclarativeMock` and pass it in.
|
|
150
153
|
- **Not thread-safe.** Concurrent use of one mock from multiple threads is out of scope.
|
|
151
154
|
|
|
152
155
|
## Roadmap
|
|
153
156
|
|
|
154
|
-
|
|
157
|
+
Nothing is planned for 0.5.0, feel free to suggest!
|
|
155
158
|
|
|
156
|
-
- **
|
|
157
|
-
- **Per-outcome counts (maybe)** - repeat a `.returns()` / `.raises()` / `.runs()` without chaining it n times (today `.returns(1).returns(1).returns(1).returns(2)`).
|
|
159
|
+
- **Other test runners (low priority)** - unittest and similar runners do not verify mocks on teardown the way the pytest plugin does.
|
|
158
160
|
|
|
159
161
|
Other ideas belong in GitHub Issues.
|
|
160
162
|
|
|
@@ -166,7 +168,8 @@ ruff check .
|
|
|
166
168
|
ruff format --check .
|
|
167
169
|
mypy src tests
|
|
168
170
|
pytest
|
|
169
|
-
|
|
171
|
+
coverage run -m pytest
|
|
172
|
+
coverage report
|
|
170
173
|
```
|
|
171
174
|
|
|
172
175
|
See **[CONTRIBUTING.md](CONTRIBUTING.md)** for setup, commits, and tests. Agents follow **[AGENTS.md](AGENTS.md)**.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "declarative-mocks"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.0"
|
|
4
4
|
description = "Declare mock behavior as ordered expectations. Simple, typed, gomock-inspired DSL over unittest.mock"
|
|
5
5
|
authors = [{ name = "Dimitry Churilov", email = "d.mdx3r@gmail.com" }]
|
|
6
6
|
license = "MIT"
|
|
@@ -28,6 +28,9 @@ Homepage = "https://github.com/MDx3R/declarative-mocks"
|
|
|
28
28
|
Repository = "https://github.com/MDx3R/declarative-mocks"
|
|
29
29
|
Issues = "https://github.com/MDx3R/declarative-mocks/issues"
|
|
30
30
|
|
|
31
|
+
[project.entry-points.pytest11]
|
|
32
|
+
dmock = "dmock._pytest_plugin"
|
|
33
|
+
|
|
31
34
|
[tool.poetry]
|
|
32
35
|
packages = [{ include = "dmock", from = "src" }]
|
|
33
36
|
|
|
@@ -131,4 +134,4 @@ branch = true
|
|
|
131
134
|
[tool.coverage.report]
|
|
132
135
|
show_missing = true
|
|
133
136
|
fail_under = 90
|
|
134
|
-
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:"]
|
|
137
|
+
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:", "@overload"]
|
|
@@ -50,7 +50,7 @@ from dmock._matchers import (
|
|
|
50
50
|
RegexSearch,
|
|
51
51
|
StartsWith,
|
|
52
52
|
)
|
|
53
|
-
from dmock._mock import DeclarativeMock
|
|
53
|
+
from dmock._mock import DeclarativeMock, dmock
|
|
54
54
|
|
|
55
55
|
|
|
56
56
|
try:
|
|
@@ -103,5 +103,6 @@ __all__ = [
|
|
|
103
103
|
"StartsWith",
|
|
104
104
|
"UnexpectedCallError",
|
|
105
105
|
"UnsatisfiedExpectationError",
|
|
106
|
+
"dmock",
|
|
106
107
|
"in_order",
|
|
107
108
|
]
|
|
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING
|
|
|
5
5
|
|
|
6
6
|
if TYPE_CHECKING:
|
|
7
7
|
from collections.abc import Sequence
|
|
8
|
+
from typing import Any
|
|
8
9
|
|
|
9
10
|
from dmock._expectation import Expectation
|
|
10
11
|
from dmock._types import RecordedCall
|
|
@@ -44,7 +45,7 @@ class NoMatchingCallError(UnexpectedCallError):
|
|
|
44
45
|
def __init__(
|
|
45
46
|
self,
|
|
46
47
|
call: RecordedCall,
|
|
47
|
-
rejected: Sequence[Expectation],
|
|
48
|
+
rejected: Sequence[Expectation[Any, Any]],
|
|
48
49
|
history: Sequence[RecordedCall],
|
|
49
50
|
) -> None:
|
|
50
51
|
self.call = call
|
|
@@ -66,7 +67,7 @@ class NoMatchingCallError(UnexpectedCallError):
|
|
|
66
67
|
self.history,
|
|
67
68
|
)
|
|
68
69
|
|
|
69
|
-
def _reason(self, exp: Expectation) -> str:
|
|
70
|
+
def _reason(self, exp: Expectation[Any, Any]) -> str:
|
|
70
71
|
if exp.is_exhausted():
|
|
71
72
|
maximum = exp.quantifier.max_calls
|
|
72
73
|
bound = "*" if maximum is None else str(maximum)
|
|
@@ -81,8 +82,8 @@ class BlockedCallError(UnexpectedCallError):
|
|
|
81
82
|
def __init__(
|
|
82
83
|
self,
|
|
83
84
|
call: RecordedCall,
|
|
84
|
-
expectation: Expectation,
|
|
85
|
-
blocked: Sequence[Expectation],
|
|
85
|
+
expectation: Expectation[Any, Any],
|
|
86
|
+
blocked: Sequence[Expectation[Any, Any]],
|
|
86
87
|
history: Sequence[RecordedCall],
|
|
87
88
|
) -> None:
|
|
88
89
|
self.call = call
|
|
@@ -101,7 +102,7 @@ class BlockedCallError(UnexpectedCallError):
|
|
|
101
102
|
)
|
|
102
103
|
|
|
103
104
|
@staticmethod
|
|
104
|
-
def _blocked_reason(req: Expectation) -> str:
|
|
105
|
+
def _blocked_reason(req: Expectation[Any, Any]) -> str:
|
|
105
106
|
return (
|
|
106
107
|
f"blocked by {req.method_name} "
|
|
107
108
|
f"(expected {req.quantifier.description}, got {req.call_count})"
|
|
@@ -111,7 +112,7 @@ class BlockedCallError(UnexpectedCallError):
|
|
|
111
112
|
class ExceededCallError(UnexpectedCallError):
|
|
112
113
|
"""A matching call exceeded the expectation's quantifier upper bound."""
|
|
113
114
|
|
|
114
|
-
def __init__(self, method_name: str, calls: int, max_calls: int) -> None:
|
|
115
|
+
def __init__(self, method_name: str, calls: int, max_calls: int | None) -> None:
|
|
115
116
|
self.method_name = method_name
|
|
116
117
|
self.calls = calls
|
|
117
118
|
self.max_calls = max_calls
|
|
@@ -121,7 +122,7 @@ class ExceededCallError(UnexpectedCallError):
|
|
|
121
122
|
return (
|
|
122
123
|
f"Unexpected call to {self.method_name!r}: "
|
|
123
124
|
f"called {self.calls} time(s), "
|
|
124
|
-
f"max allowed is {self.max_calls}."
|
|
125
|
+
f"max allowed is {self.max_calls or 0}."
|
|
125
126
|
)
|
|
126
127
|
|
|
127
128
|
|
|
@@ -130,7 +131,7 @@ class UnsatisfiedExpectationError(DeclarativeMockError):
|
|
|
130
131
|
|
|
131
132
|
def __init__(
|
|
132
133
|
self,
|
|
133
|
-
expectations: Sequence[Expectation],
|
|
134
|
+
expectations: Sequence[Expectation[Any, Any]],
|
|
134
135
|
history: Sequence[RecordedCall],
|
|
135
136
|
) -> None:
|
|
136
137
|
self.expectations = tuple(expectations)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
from collections import deque
|
|
4
|
-
from typing import TYPE_CHECKING, Self
|
|
4
|
+
from typing import TYPE_CHECKING, Generic, Self, final, overload
|
|
5
5
|
|
|
6
6
|
from dmock._exceptions import ConfigurationError, ExceededCallError
|
|
7
7
|
from dmock._matchers import (
|
|
@@ -9,6 +9,7 @@ from dmock._matchers import (
|
|
|
9
9
|
_AnyArgsSentinel, # pyright: ignore[reportPrivateUsage]
|
|
10
10
|
_AnyKwargsSentinel, # pyright: ignore[reportPrivateUsage]
|
|
11
11
|
)
|
|
12
|
+
from dmock._protocols import P, R
|
|
12
13
|
from dmock._types import (
|
|
13
14
|
AtLeast,
|
|
14
15
|
Between,
|
|
@@ -22,7 +23,8 @@ from dmock._types import (
|
|
|
22
23
|
|
|
23
24
|
|
|
24
25
|
if TYPE_CHECKING:
|
|
25
|
-
from collections.abc import Callable, Sequence
|
|
26
|
+
from collections.abc import Awaitable, Callable, Sequence
|
|
27
|
+
from typing import Any
|
|
26
28
|
|
|
27
29
|
from dmock._types import Outcome, Quantifier
|
|
28
30
|
|
|
@@ -34,7 +36,8 @@ def _value_matches(expected: object, actual: object) -> bool:
|
|
|
34
36
|
return expected == actual
|
|
35
37
|
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
@final
|
|
40
|
+
class Expectation(Generic[P, R]):
|
|
38
41
|
"""A single expected call: fluent builder for outcomes and quantifiers.
|
|
39
42
|
|
|
40
43
|
Constructed by :meth:`~dmock.DeclarativeMock.expect`. Chained ``returns`` /
|
|
@@ -71,19 +74,24 @@ class Expectation:
|
|
|
71
74
|
self._quantifier: Quantifier | None = None
|
|
72
75
|
self._optional: bool = False
|
|
73
76
|
self._calls = 0
|
|
74
|
-
self._requires: list[Expectation] = []
|
|
77
|
+
self._requires: list[Expectation[Any, Any]] = []
|
|
75
78
|
|
|
76
79
|
# -- Fluent outcome builders --
|
|
77
80
|
|
|
78
|
-
def returns(self, value:
|
|
81
|
+
def returns(self, value: R, *, repeat: int = 1) -> Self:
|
|
79
82
|
"""Declare the return value for the next matching call.
|
|
80
83
|
|
|
81
84
|
Args:
|
|
82
85
|
value: Object returned as-is (tuples are not unpacked).
|
|
86
|
+
repeat: How many successive calls receive this outcome. Must be
|
|
87
|
+
at least 1.
|
|
83
88
|
|
|
84
89
|
Returns:
|
|
85
90
|
This expectation, for chaining.
|
|
86
91
|
|
|
92
|
+
Raises:
|
|
93
|
+
ConfigurationError: If `repeat` is less than 1.
|
|
94
|
+
|
|
87
95
|
Examples:
|
|
88
96
|
>>> from dmock import DeclarativeMock
|
|
89
97
|
>>> class Service:
|
|
@@ -94,18 +102,27 @@ class Expectation:
|
|
|
94
102
|
>>> mock.fetch("a")
|
|
95
103
|
'A'
|
|
96
104
|
"""
|
|
97
|
-
self.
|
|
105
|
+
self._append_outcome(ReturnOutcome(value), repeat)
|
|
98
106
|
return self
|
|
99
107
|
|
|
100
|
-
def raises(
|
|
108
|
+
def raises(
|
|
109
|
+
self,
|
|
110
|
+
exc: BaseException | type[BaseException],
|
|
111
|
+
*,
|
|
112
|
+
repeat: int = 1,
|
|
113
|
+
) -> Self:
|
|
101
114
|
"""Declare that the next matching call raises `exc`.
|
|
102
115
|
|
|
103
116
|
Args:
|
|
104
117
|
exc: Exception instance, or a type instantiated with no arguments.
|
|
118
|
+
repeat: How many successive calls raise `exc`. Must be at least 1.
|
|
105
119
|
|
|
106
120
|
Returns:
|
|
107
121
|
This expectation, for chaining.
|
|
108
122
|
|
|
123
|
+
Raises:
|
|
124
|
+
ConfigurationError: If `repeat` is less than 1.
|
|
125
|
+
|
|
109
126
|
Examples:
|
|
110
127
|
>>> from dmock import DeclarativeMock
|
|
111
128
|
>>> class Service:
|
|
@@ -118,10 +135,16 @@ class Expectation:
|
|
|
118
135
|
...
|
|
119
136
|
KeyError
|
|
120
137
|
"""
|
|
121
|
-
self.
|
|
138
|
+
self._append_outcome(RaiseOutcome(exc), repeat)
|
|
122
139
|
return self
|
|
123
140
|
|
|
124
|
-
|
|
141
|
+
@overload
|
|
142
|
+
def runs(self, func: Callable[P, R], *, repeat: int = 1) -> Self: ...
|
|
143
|
+
|
|
144
|
+
@overload
|
|
145
|
+
def runs(self, func: Callable[P, Awaitable[R]], *, repeat: int = 1) -> Self: ...
|
|
146
|
+
|
|
147
|
+
def runs(self, func: Callable[P, R | Awaitable[R]], *, repeat: int = 1) -> Self:
|
|
125
148
|
"""Call `func` with the actual arguments for the next matching call.
|
|
126
149
|
|
|
127
150
|
The return value of `func` becomes the result of that call. Chaining
|
|
@@ -130,10 +153,14 @@ class Expectation:
|
|
|
130
153
|
|
|
131
154
|
Args:
|
|
132
155
|
func: Called as ``func(*args, **kwargs)`` of the matching invocation.
|
|
156
|
+
repeat: How many successive calls invoke `func`. Must be at least 1.
|
|
133
157
|
|
|
134
158
|
Returns:
|
|
135
159
|
This expectation, for chaining.
|
|
136
160
|
|
|
161
|
+
Raises:
|
|
162
|
+
ConfigurationError: If `repeat` is less than 1.
|
|
163
|
+
|
|
137
164
|
Examples:
|
|
138
165
|
>>> from dmock import DeclarativeMock
|
|
139
166
|
>>> class Service:
|
|
@@ -152,9 +179,21 @@ class Expectation:
|
|
|
152
179
|
>>> mock.fetch("a")
|
|
153
180
|
'from-returns'
|
|
154
181
|
"""
|
|
155
|
-
self.
|
|
182
|
+
self._append_outcome(RunOutcome(func), repeat)
|
|
156
183
|
return self
|
|
157
184
|
|
|
185
|
+
def _append_outcome(self, outcome: Outcome, repeat: int) -> None:
|
|
186
|
+
"""Append `outcome` once per `repeat`.
|
|
187
|
+
|
|
188
|
+
Raises:
|
|
189
|
+
ConfigurationError: If `repeat` is less than 1.
|
|
190
|
+
"""
|
|
191
|
+
if repeat < 1:
|
|
192
|
+
raise ConfigurationError(f"repeat requires repeat >= 1, got {repeat}.")
|
|
193
|
+
|
|
194
|
+
for _ in range(repeat):
|
|
195
|
+
self._outcomes.append(outcome)
|
|
196
|
+
|
|
158
197
|
# -- Fluent quantifiers --
|
|
159
198
|
|
|
160
199
|
def once(self) -> Self:
|
|
@@ -327,7 +366,7 @@ class Expectation:
|
|
|
327
366
|
"""
|
|
328
367
|
return self._set_quantifier(Never())
|
|
329
368
|
|
|
330
|
-
def not_before(self, *expectations: Expectation) -> Self:
|
|
369
|
+
def not_before(self, *expectations: Expectation[Any, Any]) -> Self:
|
|
331
370
|
"""Require that every `expectations` item is satisfied before this one.
|
|
332
371
|
|
|
333
372
|
Args:
|
|
@@ -364,10 +403,10 @@ class Expectation:
|
|
|
364
403
|
|
|
365
404
|
return self
|
|
366
405
|
|
|
367
|
-
def _is_reachable_from(self, source: Expectation) -> bool:
|
|
406
|
+
def _is_reachable_from(self, source: Expectation[Any, Any]) -> bool:
|
|
368
407
|
"""Return True if `self` is reachable by following `_requires` from `source`."""
|
|
369
408
|
visited: set[int] = set()
|
|
370
|
-
queue: deque[Expectation] = deque([source])
|
|
409
|
+
queue: deque[Expectation[Any, Any]] = deque([source])
|
|
371
410
|
while queue:
|
|
372
411
|
node = queue.popleft()
|
|
373
412
|
node_id = id(node)
|
|
@@ -383,7 +422,7 @@ class Expectation:
|
|
|
383
422
|
return False
|
|
384
423
|
|
|
385
424
|
@property
|
|
386
|
-
def requires(self) -> Sequence[Expectation]:
|
|
425
|
+
def requires(self) -> Sequence[Expectation[Any, Any]]:
|
|
387
426
|
"""Prerequisites that must be satisfied before this expectation can match."""
|
|
388
427
|
return self._requires
|
|
389
428
|
|
|
@@ -454,7 +493,7 @@ class Expectation:
|
|
|
454
493
|
"""
|
|
455
494
|
q = self.quantifier
|
|
456
495
|
self._calls += 1
|
|
457
|
-
if q.
|
|
496
|
+
if q.is_exceeded(self._calls):
|
|
458
497
|
raise ExceededCallError(self._method_name, self._calls, q.max_calls)
|
|
459
498
|
|
|
460
499
|
if not self._outcomes:
|
|
@@ -496,7 +535,7 @@ class Expectation:
|
|
|
496
535
|
return f"Expectation({self._method_name}({all_parts}))"
|
|
497
536
|
|
|
498
537
|
|
|
499
|
-
def in_order(*expectations: Expectation) -> None:
|
|
538
|
+
def in_order(*expectations: Expectation[Any, Any]) -> None:
|
|
500
539
|
"""Link `expectations` so each one requires the previous to be satisfied first.
|
|
501
540
|
|
|
502
541
|
Equivalent to calling ``expectations[i].not_before(expectations[i - 1])``
|