Debian Codegraph @

hypothesis-python/tests/nocover/test_pretty_repr.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 from collections import OrderedDict
12
13 import pytest
14
15 from hypothesis import given, strategies as st
16 from hypothesis.control import reject
17 from hypothesis.errors import HypothesisDeprecationWarning, InvalidArgument
18
19
20 def foo(x):
21 pass
22
23
24 def bar(x):
25 pass
26
27
28 def baz(x):
29 pass
30
31
32 fns = [foo, bar, baz]
33
34
35 def builds_ignoring_invalid(target, *args, **kwargs):
36 def splat(value):
37 try:
38 result = target(*value[0], **value[1])
39 result.validate()
40 return result
41 except (HypothesisDeprecationWarning, InvalidArgument):
42 reject()
43
44 return st.tuples(st.tuples(*args), st.fixed_dictionaries(kwargs)).map(splat)
45
46
47 size_strategies = {
48 "min_size": st.integers(min_value=0, max_value=100),
49 "max_size": st.integers(min_value=0, max_value=100) | st.none(),
50 }
51
52 values = st.integers() | st.text()
53
54
55 Strategies = st.recursive(
56 st.one_of(
57 st.sampled_from(
58 [
59 st.none(),
60 st.booleans(),
61 st.randoms(use_true_random=True),
62 st.complex_numbers(),
63 st.randoms(use_true_random=True),
64 st.fractions(),
65 st.decimals(),
66 ]
67 ),
68 st.builds(st.just, values),
69 st.builds(st.sampled_from, st.lists(values, min_size=1)),
70 builds_ignoring_invalid(st.floats, st.floats(), st.floats()),
71 ),
72 lambda x: st.one_of(
73 builds_ignoring_invalid(st.lists, x, **size_strategies),
74 builds_ignoring_invalid(st.sets, x, **size_strategies),
75 builds_ignoring_invalid(lambda v: st.tuples(*v), st.lists(x)),
76 builds_ignoring_invalid(lambda v: st.one_of(*v), st.lists(x, min_size=1)),
77 builds_ignoring_invalid(
78 st.dictionaries,
79 x,
80 x,
81 dict_class=st.sampled_from([dict, OrderedDict]),
82 **size_strategies,
83 ),
84 st.builds(lambda s, f: s.map(f), x, st.sampled_from(fns)),
85 ),
86 )
87
88
89 strategy_globals = {k: getattr(st, k) for k in dir(st)}
90
91 strategy_globals["OrderedDict"] = OrderedDict
92 strategy_globals["inf"] = float("inf")
93 strategy_globals["nan"] = float("nan")
94 strategy_globals["foo"] = foo
95 strategy_globals["bar"] = bar
96 strategy_globals["baz"] = baz
97
98
99 @given(Strategies)
100 def test_repr_evals_to_thing_with_same_repr(strategy):
101 r = repr(strategy)
102 via_eval = eval(r, strategy_globals)
103 r2 = repr(via_eval)
104 assert r == r2
105
106
107 @pytest.mark.parametrize(
108 "r",
109 [
110 "none().filter(foo).map(bar)",
111 "just(1).filter(foo).map(bar)",
112 "sampled_from([1, 2, 3]).filter(foo).map(bar)",
113 ],
114 )
115 def test_sampled_transform_reprs(r):
116 assert repr(eval(r, strategy_globals)) == r