| 1 |
import functools |
| 2 |
|
| 3 |
from ._helpers import StubClass, stub_factory |
| 4 |
|
| 5 |
|
| 6 |
class StubStrategy(StubClass): |
| 7 |
def __make_trailing_repr(self, transformation_name, func): |
| 8 |
func_name = func.__name__ or repr(func) |
| 9 |
return f"{self!r}.{transformation_name}({func_name})" |
| 10 |
|
| 11 |
def map(self, pack): |
| 12 |
return self._with_repr(self.__make_trailing_repr("map", pack)) |
| 13 |
|
| 14 |
def flatmap(self, expand): |
| 15 |
return self._with_repr(self.__make_trailing_repr("flatmap", expand)) |
| 16 |
|
| 17 |
def filter(self, condition): |
| 18 |
return self._with_repr(self.__make_trailing_repr("filter", condition)) |
| 19 |
|
| 20 |
def __or__(self, other): |
| 21 |
new_repr = f"one_of({self!r}, {other!r})" |
| 22 |
return self._with_repr(new_repr) |
| 23 |
|
| 24 |
|
| 25 |
_STRATEGIES = { |
| 26 |
"binary", |
| 27 |
"booleans", |
| 28 |
"builds", |
| 29 |
"characters", |
| 30 |
"complex_numbers", |
| 31 |
"composite", |
| 32 |
"data", |
| 33 |
"dates", |
| 34 |
"datetimes", |
| 35 |
"decimals", |
| 36 |
"deferred", |
| 37 |
"dictionaries", |
| 38 |
"emails", |
| 39 |
"fixed_dictionaries", |
| 40 |
"floats", |
| 41 |
"fractions", |
| 42 |
"from_regex", |
| 43 |
"from_type", |
| 44 |
"frozensets", |
| 45 |
"functions", |
| 46 |
"integers", |
| 47 |
"iterables", |
| 48 |
"just", |
| 49 |
"lists", |
| 50 |
"none", |
| 51 |
"nothing", |
| 52 |
"one_of", |
| 53 |
"permutations", |
| 54 |
"random_module", |
| 55 |
"randoms", |
| 56 |
"recursive", |
| 57 |
"register_type_strategy", |
| 58 |
"runner", |
| 59 |
"sampled_from", |
| 60 |
"sets", |
| 61 |
"shared", |
| 62 |
"slices", |
| 63 |
"timedeltas", |
| 64 |
"times", |
| 65 |
"text", |
| 66 |
"tuples", |
| 67 |
"uuids", |
| 68 |
} |
| 69 |
|
| 70 |
__all__ = sorted(_STRATEGIES) |
| 71 |
|
| 72 |
|
| 73 |
def composite(f): |
| 74 |
strategy = stub_factory(StubStrategy, f.__name__) |
| 75 |
|
| 76 |
@functools.wraps(f) |
| 77 |
def inner(*args, **kwargs): |
| 78 |
return strategy(*args, **kwargs) |
| 79 |
|
| 80 |
return inner |
| 81 |
|
| 82 |
|
| 83 |
def __getattr__(name): |
| 84 |
if name not in _STRATEGIES: |
| 85 |
raise AttributeError(f"Unknown attribute {name}") |
| 86 |
|
| 87 |
return stub_factory(StubStrategy, f"hypothesis.strategies.{name}") |
| 88 |
|
| 89 |
|
| 90 |
def __dir__(): |
| 91 |
return __all__ |