Debian Codegraph @

hypothesis-python/tests/conjecture/test_local_constants.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 math
12 import sys
13 from types import SimpleNamespace
14
15 import pytest
16
17 from hypothesis import given, settings, strategies as st
18 from hypothesis.configuration import StorageDirectory
19 from hypothesis.internal.conjecture import providers
20 from hypothesis.internal.conjecture.choice import choice_equal
21 from hypothesis.internal.conjecture.providers import CONSTANTS_CACHE
22 from hypothesis.internal.constants_ast import Constants
23
24 from tests.common.debug import find_any
25 from tests.common.utils import Why, xfail_on_crosshair
26
27
28 # I tried using @given(st.integers()) here, but I think there is a bad interaction
29 # with CONSTANTS_CACHE when testing it inside of a hypothesis test.
30 @pytest.mark.parametrize("value", [2**20 - 50, 2**10 - 10, 129387123, -19827321, 0])
31 def test_can_draw_local_constants_integers(monkeypatch, value):
32 # _get_local_constants normally invalidates this cache for us, but we're
33 # monkeypatching it.
34 CONSTANTS_CACHE.cache.clear()
35 monkeypatch.setattr(
36 providers, "_get_local_constants", lambda: Constants(integers={value})
37 )
38 find_any(st.integers(), lambda v: choice_equal(v, value))
39
40
41 @xfail_on_crosshair(Why.undiscovered) # I think float_to_int is difficult for crosshair
42 @pytest.mark.parametrize("value", [1.2938, -1823.0239, 1e999, math.nan])
43 def test_can_draw_local_constants_floats(monkeypatch, value):
44 CONSTANTS_CACHE.cache.clear()
45 monkeypatch.setattr(
46 providers, "_get_local_constants", lambda: Constants(floats={value})
47 )
48 find_any(st.floats(), lambda v: choice_equal(v, value))
49
50
51 @pytest.mark.parametrize("value", [b"abdefgh", b"a" * 50])
52 def test_can_draw_local_constants_bytes(monkeypatch, value):
53 CONSTANTS_CACHE.cache.clear()
54 monkeypatch.setattr(
55 providers, "_get_local_constants", lambda: Constants(bytes={value})
56 )
57 find_any(st.binary(), lambda v: choice_equal(v, value))
58
59
60 @pytest.mark.parametrize("value", ["abdefgh", "a" * 50])
61 def test_can_draw_local_constants_string(monkeypatch, value):
62 CONSTANTS_CACHE.cache.clear()
63 monkeypatch.setattr(
64 providers, "_get_local_constants", lambda: Constants(strings={value})
65 )
66 # we have a bunch of strings in GLOBAL_CONSTANTS, so it might take a while
67 # to generate our local constant.
68 find_any(
69 st.text(),
70 lambda v: choice_equal(v, value),
71 settings=settings(max_examples=5_000),
72 )
73
74
75 def test_actual_collection(monkeypatch, tmp_path):
76 # covering test for doing some real work collecting constants. We'll fake
77 # hypothesis as being the "local" module, just to get some real constant
78 # collection going.
79
80 # reset cache checks
81 monkeypatch.setattr(providers, "_sys_modules_len", None)
82 monkeypatch.setattr(providers, "_seen_modules", set())
83 monkeypatch.setattr(providers, "is_local_module_file", lambda f: "hypothesis" in f)
84 # Without monkeypatching this, this test only covers all the right lines the
85 # first time it's executed, since it falls back to the cache the second time.
86 # Reset to a guaranteed-empty storage directory to ensure consistent coverage.
87 monkeypatch.setattr(
88 "hypothesis.internal.constants_ast.storage_directory",
89 lambda *names, **kwargs: StorageDirectory(
90 tmp_path.joinpath(*names), home_directory=tmp_path
91 ),
92 )
93
94 @given(st.integers())
95 @settings(max_examples=100)
96 def f(n):
97 pass
98
99 f()
100
101
102 def test_unhashable_sys_modules_entry(monkeypatch):
103 # Regression test for https://github.com/HypothesisWorks/hypothesis/issues/4660
104 # Some packages (e.g. cog) place unhashable objects like SimpleNamespace
105 # in sys.modules. This should not crash _get_local_constants.
106 monkeypatch.setattr(providers, "_sys_modules_len", None)
107 monkeypatch.setattr(providers, "_seen_modules", set())
108 monkeypatch.setitem(sys.modules, "_unhashable_test_mod", SimpleNamespace())
109
110 providers._get_local_constants()