Debian Codegraph @

Lib/test/support/_hypothesis_stubs/__init__.py

raw
1 from enum import Enum
2 import functools
3 import unittest
4
5 __all__ = [
6 "given",
7 "example",
8 "assume",
9 "reject",
10 "register_random",
11 "strategies",
12 "HealthCheck",
13 "settings",
14 "Verbosity",
15 ]
16
17 from . import strategies
18
19
20 def given(*_args, **_kwargs):
21 def decorator(f):
22 if examples := getattr(f, "_examples", []):
23
24 @functools.wraps(f)
25 def test_function(self):
26 for example_args, example_kwargs in examples:
27 with self.subTest(*example_args, **example_kwargs):
28 f(self, *example_args, **example_kwargs)
29
30 else:
31 # If we have found no examples, we must skip the test. If @example
32 # is applied after @given, it will re-wrap the test to remove the
33 # skip decorator.
34 test_function = unittest.skip(
35 "Hypothesis required for property test with no " +
36 "specified examples"
37 )(f)
38
39 test_function._given = True
40 return test_function
41
42 return decorator
43
44
45 def example(*args, **kwargs):
46 if bool(args) == bool(kwargs):
47 raise ValueError("Must specify exactly one of *args or **kwargs")
48
49 def decorator(f):
50 base_func = getattr(f, "__wrapped__", f)
51 if not hasattr(base_func, "_examples"):
52 base_func._examples = []
53
54 base_func._examples.append((args, kwargs))
55
56 if getattr(f, "_given", False):
57 # If the given decorator is below all the example decorators,
58 # it would be erroneously skipped, so we need to re-wrap the new
59 # base function.
60 f = given()(base_func)
61
62 return f
63
64 return decorator
65
66
67 def assume(condition):
68 if not condition:
69 raise unittest.SkipTest("Unsatisfied assumption")
70 return True
71
72
73 def reject():
74 assume(False)
75
76
77 def register_random(*args, **kwargs):
78 pass # pragma: no cover
79
80
81 def settings(*args, **kwargs):
82 return lambda f: f # pragma: nocover
83
84
85 class HealthCheck(Enum):
86 data_too_large = 1
87 filter_too_much = 2
88 too_slow = 3
89 return_value = 5
90 large_base_example = 7
91 not_a_test_method = 8
92
93 @classmethod
94 def all(cls):
95 return list(cls)
96
97
98 class Verbosity(Enum):
99 quiet = 0
100 normal = 1
101 verbose = 2
102 debug = 3
103
104
105 class Phase(Enum):
106 explicit = 0
107 reuse = 1
108 generate = 2
109 target = 3
110 shrink = 4
111 explain = 5