17
17
import attr
18
18
import pytest
19
19
20
- from hypothesis import given
20
+ from hypothesis import given , strategies as st
21
21
from hypothesis .errors import InvalidArgument , Unsatisfiable
22
22
from hypothesis .internal .conjecture .data import ConjectureData
23
23
from hypothesis .internal .reflection import get_pretty_function_description
24
- from hypothesis .strategies import booleans , data , integers , just , lists , none , tuples
25
24
from hypothesis .strategies ._internal .utils import to_jsonable
26
25
27
26
from tests .common .debug import assert_simple_property , check_can_generate_examples
28
27
from tests .common .utils import checks_deprecated_behaviour
29
28
30
29
31
30
def test_or_errors_when_given_non_strategy ():
32
- bools = tuples (booleans ())
31
+ bools = st . tuples (st . booleans ())
33
32
with pytest .raises (ValueError ):
34
33
bools | "foo"
35
34
@@ -49,29 +48,29 @@ class WeirdRepr:
49
48
def __repr__ (self ):
50
49
return "ABCDEFG"
51
50
52
- assert repr (just (WeirdRepr ())) == f"just({ WeirdRepr ()!r} )"
51
+ assert repr (st . just (WeirdRepr ())) == f"just({ WeirdRepr ()!r} )"
53
52
54
53
55
54
def test_just_strategy_does_not_draw ():
56
55
data = ConjectureData .for_choices ([])
57
- s = just ("hello" )
56
+ s = st . just ("hello" )
58
57
assert s .do_draw (data ) == "hello"
59
58
60
59
61
60
def test_none_strategy_does_not_draw ():
62
61
data = ConjectureData .for_choices ([])
63
- s = none ()
62
+ s = st . none ()
64
63
assert s .do_draw (data ) is None
65
64
66
65
67
66
def test_can_map ():
68
- s = integers ().map (pack = lambda t : "foo" )
67
+ s = st . integers ().map (pack = lambda t : "foo" )
69
68
assert_simple_property (s , lambda v : v == "foo" )
70
69
71
70
72
71
def test_example_raises_unsatisfiable_when_too_filtered ():
73
72
with pytest .raises (Unsatisfiable ):
74
- check_can_generate_examples (integers ().filter (lambda x : False ))
73
+ check_can_generate_examples (st . integers ().filter (lambda x : False ))
75
74
76
75
77
76
def nameless_const (x ):
@@ -83,20 +82,20 @@ def f(u, v):
83
82
84
83
def test_can_map_nameless ():
85
84
f = nameless_const (2 )
86
- assert get_pretty_function_description (f ) in repr (integers ().map (f ))
85
+ assert get_pretty_function_description (f ) in repr (st . integers ().map (f ))
87
86
88
87
89
88
def test_can_flatmap_nameless ():
90
- f = nameless_const (just (3 ))
91
- assert get_pretty_function_description (f ) in repr (integers ().flatmap (f ))
89
+ f = nameless_const (st . just (3 ))
90
+ assert get_pretty_function_description (f ) in repr (st . integers ().flatmap (f ))
92
91
93
92
94
93
def test_flatmap_with_invalid_expand ():
95
94
with pytest .raises (InvalidArgument ):
96
- check_can_generate_examples (just (100 ).flatmap (lambda n : "a" ))
95
+ check_can_generate_examples (st . just (100 ).flatmap (lambda n : "a" ))
97
96
98
97
99
- _bad_random_strategy = lists (integers (), min_size = 1 ).map (random .choice )
98
+ _bad_random_strategy = st . lists (st . integers (), min_size = 1 ).map (random .choice )
100
99
101
100
102
101
@checks_deprecated_behaviour
@@ -106,7 +105,7 @@ def test_use_of_global_random_is_deprecated_in_given():
106
105
107
106
@checks_deprecated_behaviour
108
107
def test_use_of_global_random_is_deprecated_in_interactive_draws ():
109
- @given (data ())
108
+ @given (st . data ())
110
109
def inner (d ):
111
110
d .draw (_bad_random_strategy )
112
111
@@ -178,3 +177,8 @@ def test_jsonable_override():
178
177
obj = HasCustomJsonFormat ("expected" )
179
178
assert to_jsonable (obj , avoid_realization = False ) == "surprise!"
180
179
assert to_jsonable (obj , avoid_realization = True ) == "<symbolic>"
180
+
181
+
182
+ def test_deferred_strategy_draw ():
183
+ strategy = st .deferred (lambda : st .integers ())
184
+ assert strategy .do_draw (ConjectureData .for_choices ([0 ])) == 0
0 commit comments