-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from life4/ensure
@deal.ensure
- Loading branch information
Showing
6 changed files
with
88 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# ensure | ||
|
||
Ensure is a [postcondition](./post) that aceepts not only result, but also function arguments. Must be true after function executed. Raises `PostContractError` otherwise. | ||
|
||
```python | ||
@deal.ensure(lambda x, result: x != result) | ||
def double(x): | ||
return x * 2 | ||
|
||
double(2) | ||
# 4 | ||
|
||
double(0) | ||
# PostContractError: | ||
``` | ||
|
||
## Motivation | ||
|
||
Perfect for complex task that easy to check. For example: | ||
|
||
```python | ||
from typing import List | ||
|
||
# element at this position matches item | ||
@deal.ensure( | ||
lambda items, item, result: items[result] == item, | ||
message='invalid match', | ||
) | ||
# element at this position is the first match | ||
@deal.ensure( | ||
lambda items, item, result: not any(el == item for el in items[:result]), | ||
message='not the first match', | ||
) | ||
def index_of(items: List[int], item: int) -> int: | ||
for index, element in enumerate(items): | ||
if element == item: | ||
return index | ||
raise LookupError | ||
``` | ||
|
||
It allows you to simplify testing, easier check hypothesis, tell more about the function behavior. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
decorators/pre | ||
decorators/post | ||
decorators/ensure | ||
decorators/inv | ||
.. toctree:: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters