Debian Codegraph @

hypothesis-python/tests/conjecture/test_provider_contract.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 import pytest
12
13 from hypothesis import example, given, strategies as st
14 from hypothesis.errors import StopTest
15 from hypothesis.internal.compat import WINDOWS
16 from hypothesis.internal.conjecture.choice import (
17 choice_equal,
18 choice_from_index,
19 choice_permitted,
20 )
21 from hypothesis.internal.conjecture.data import ConjectureData
22 from hypothesis.internal.conjecture.providers import (
23 BytestringProvider,
24 HypothesisProvider,
25 URandomProvider,
26 )
27 from hypothesis.internal.intervalsets import IntervalSet
28
29 from tests.conjecture.common import (
30 choice_types_constraints,
31 float_constr,
32 integer_constr,
33 nodes,
34 string_constr,
35 )
36
37
38 @example(b"\x00" * 100, [("integer", integer_constr())])
39 @example(b"\x00" * 100, [("integer", integer_constr(0, 2))])
40 @example(b"\x00" * 100, [("integer", integer_constr(0, 0))])
41 @example(b"\x00" * 100, [("integer", integer_constr(min_value=0))])
42 @example(b"\x00" * 100, [("integer", integer_constr(max_value=2))])
43 @example(b"\x00" * 100, [("integer", integer_constr(0, 2, weights={0: 0.1}))])
44 @example(b"\x00" * 100, [("boolean", {"p": 1.0})])
45 @example(b"\x00" * 100, [("boolean", {"p": 0.0})])
46 @example(b"\x00" * 100, [("boolean", {"p": 1e-99})])
47 @example(b"\x00" * 100, [("string", string_constr(IntervalSet.from_string("a")))])
48 @example(b"\x00" * 100, [("float", float_constr())])
49 @example(b"\x00" * 100, [("bytes", {"min_size": 0, "max_size": 10})])
50 @example(b"\x00", [("integer", integer_constr())])
51 @given(st.binary(min_size=200), st.lists(choice_types_constraints()))
52 def test_provider_contract_bytestring(bytestring, choice_type_and_constraints):
53 data = ConjectureData(
54 random=None,
55 observer=None,
56 provider=BytestringProvider,
57 provider_kw={"bytestring": bytestring},
58 )
59
60 for choice_type, constraints in choice_type_and_constraints:
61 # for the threading ci tests
62 constraints = constraints.copy()
63 try:
64 value = getattr(data, f"draw_{choice_type}")(**constraints)
65 except StopTest:
66 return
67
68 assert choice_permitted(value, constraints)
69 constraints["forced"] = choice_from_index(0, choice_type, constraints)
70 assert choice_equal(
71 constraints["forced"], getattr(data, f"draw_{choice_type}")(**constraints)
72 )
73
74
75 @pytest.mark.parametrize(
76 "provider",
77 [
78 pytest.param(
79 URandomProvider,
80 marks=pytest.mark.skipif(
81 WINDOWS, reason="/dev/urandom not available on windows"
82 ),
83 ),
84 HypothesisProvider,
85 ],
86 )
87 @given(st.lists(nodes()), st.randoms())
88 def test_provider_contract(provider, nodes, random):
89 data = ConjectureData(random=random, provider=provider)
90 for node in nodes:
91 value = getattr(data, f"draw_{node.type}")(**node.constraints)
92 assert choice_permitted(value, node.constraints)