Debian Codegraph @

hypothesis-python/tests/nocover/test_explore_arbitrary_languages.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 dataclasses import dataclass, field
12 from random import Random
13 from typing import Any
14
15 import pytest
16
17 from hypothesis import (
18 HealthCheck,
19 Phase,
20 Verbosity,
21 assume,
22 given,
23 note,
24 settings,
25 strategies as st,
26 )
27 from hypothesis.internal.conjecture.data import Status
28 from hypothesis.internal.conjecture.engine import ConjectureRunner
29
30 from tests.common.utils import Why, xfail_on_crosshair
31 from tests.conjecture.common import interesting_origin
32
33
34 @dataclass
35 class Write:
36 value: Any
37 child: Any
38
39
40 @dataclass
41 class Branch:
42 bits: Any
43 children: Any = field(default_factory=dict)
44
45
46 @dataclass
47 class Terminal:
48 status: Any
49 payload: Any = field(default=None)
50
51
52 nodes = st.deferred(lambda: terminals | writes | branches)
53 # Does not include Status.OVERRUN by design: That happens because of the size
54 # of the string, not the input language.
55 terminals = st.one_of(
56 st.just(Terminal(Status.VALID)),
57 st.just(Terminal(Status.INVALID)),
58 st.builds(Terminal, status=st.just(Status.INTERESTING), payload=st.integers(0, 10)),
59 )
60 branches = st.builds(Branch, bits=st.integers(1, 64))
61 writes = st.builds(Write, value=st.binary(min_size=1), child=nodes)
62
63
64 # Remember what the default phases are with no test running, so that we can
65 # run an outer test with non-default phases and then restore the defaults for
66 # the inner test.
67 _default_phases = settings.default.phases
68
69
70 def run_language_test_for(root, data, seed):
71 def test(local_data):
72 node = root
73 while not isinstance(node, Terminal):
74 if isinstance(node, Write):
75 local_data.draw_bytes(
76 len(node.value), len(node.value), forced=node.value
77 )
78 node = node.child
79 else:
80 assert isinstance(node, Branch)
81 c = local_data.draw_integer(0, 2**node.bits - 1)
82 try:
83 node = node.children[c]
84 except KeyError:
85 if data is None:
86 return
87 node = node.children.setdefault(c, data.draw(nodes))
88 assert isinstance(node, Terminal)
89 if node.status == Status.INTERESTING:
90 local_data.mark_interesting(interesting_origin(node.payload))
91 elif node.status == Status.INVALID:
92 local_data.mark_invalid()
93
94 runner = ConjectureRunner(
95 test,
96 settings=settings(
97 max_examples=1,
98 database=None,
99 suppress_health_check=list(HealthCheck),
100 verbosity=Verbosity.quiet,
101 # Restore the global default phases, so that we don't inherit the
102 # phases setting from the outer test.
103 phases=_default_phases,
104 ),
105 random=Random(seed),
106 )
107 try:
108 runner.run()
109 finally:
110 if data is not None:
111 note(root)
112 assume(runner.interesting_examples)
113
114
115 # technically nested-engine, but same problem
116 @xfail_on_crosshair(Why.nested_given, strict=False)
117 @settings(
118 suppress_health_check=list(HealthCheck),
119 deadline=None,
120 phases=set(settings.default.phases) - {Phase.shrink},
121 )
122 @given(st.data())
123 def test_explore_an_arbitrary_language(data):
124 root = data.draw(writes | branches)
125 seed = data.draw(st.integers(0, 2**64 - 1))
126 run_language_test_for(root, data, seed)
127
128
129 @pytest.mark.parametrize("seed, language", [])
130 def test_run_specific_example(seed, language):
131 """This test recreates individual languages generated with the main test.
132
133 These are typically manually pruned down a bit - e.g. it's
134 OK to remove VALID nodes because KeyError is treated as if it lead to one
135 in this test (but not in the @given test).
136
137 These tests are likely to be fairly fragile with respect to changes in the
138 underlying engine. Feel free to delete examples if they start failing after
139 a change.
140 """
141 run_language_test_for(language, None, seed)