8
8
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
9
9
# obtain one at https://mozilla.org/MPL/2.0/.
10
10
11
+ from unittest import SkipTest
12
+
11
13
from hypothesis import HealthCheck , Phase , Verbosity , given , settings as Settings
14
+ from hypothesis .control import _current_build_context
12
15
from hypothesis .errors import Found , NoSuchExample , Unsatisfiable
13
16
from hypothesis .internal .reflection import get_pretty_function_description
14
17
@@ -26,6 +29,11 @@ def minimal(definition, condition=lambda x: True, settings=None, timeout_after=1
26
29
runtime = None
27
30
result = None
28
31
32
+ if (
33
+ context := _current_build_context .value
34
+ ) and context .data .provider .avoid_realization :
35
+ raise SkipTest ("`minimal()` helper not supported under symbolic execution" )
36
+
29
37
def wrapped_condition (x ):
30
38
nonlocal runtime
31
39
if timeout_after is not None :
@@ -71,6 +79,13 @@ def inner(x):
71
79
72
80
73
81
def find_any (definition , condition = lambda _ : True , settings = None ):
82
+ # If nested within an existing @given
83
+ if context := _current_build_context .value :
84
+ while True :
85
+ if condition (s := context .data .draw (definition )):
86
+ return s
87
+
88
+ # If top-level
74
89
settings = settings or Settings .default
75
90
return minimal (
76
91
definition ,
@@ -83,8 +98,7 @@ def find_any(definition, condition=lambda _: True, settings=None):
83
98
84
99
def assert_no_examples (strategy , condition = lambda _ : True ):
85
100
try :
86
- result = find_any (strategy , condition )
87
- raise AssertionError (f"Expected no results but found { result !r} " )
101
+ assert_all_examples (strategy , lambda val : not condition (val ))
88
102
except (Unsatisfiable , NoSuchExample ):
89
103
pass
90
104
@@ -95,14 +109,21 @@ def assert_all_examples(strategy, predicate, settings=None):
95
109
:param strategy: Hypothesis strategy to check
96
110
:param predicate: (callable) Predicate that takes example and returns bool
97
111
"""
112
+ if context := _current_build_context .value :
113
+ for _ in range (20 ):
114
+ s = context .data .draw (strategy )
115
+ msg = f"Found { s !r} using strategy { strategy } which does not match"
116
+ assert predicate (s ), msg
117
+
118
+ else :
98
119
99
- @given (strategy )
100
- @Settings (parent = settings , database = None )
101
- def assert_examples (s ):
102
- msg = f"Found { s !r} using strategy { strategy } which does not match"
103
- assert predicate (s ), msg
120
+ @given (strategy )
121
+ @Settings (parent = settings , database = None )
122
+ def assert_examples (s ):
123
+ msg = f"Found { s !r} using strategy { strategy } which does not match"
124
+ assert predicate (s ), msg
104
125
105
- assert_examples ()
126
+ assert_examples ()
106
127
107
128
108
129
def assert_simple_property (strategy , predicate , settings = None ):
0 commit comments