Debian Codegraph @

hypothesis-python/tests/cover/test_filtered_strategy.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 hypothesis.strategies as st
12 from hypothesis.internal.conjecture.data import ConjectureData
13 from hypothesis.strategies._internal.strategies import FilteredStrategy
14
15
16 def test_filter_iterations_are_marked_as_discarded():
17 variable_equal_to_zero = 0 # non-local references disables filter-rewriting
18 x = st.integers().filter(lambda x: x == variable_equal_to_zero)
19
20 data = ConjectureData.for_choices([1, 0])
21 assert data.draw(x) == 0
22 assert data.has_discards
23
24
25 def test_filtered_branches_are_all_filtered():
26 s = FilteredStrategy(st.integers() | st.text(), (bool,))
27 assert all(isinstance(x, FilteredStrategy) for x in s.branches)
28
29
30 def test_filter_conditions_may_be_empty():
31 s = FilteredStrategy(st.integers(), conditions=())
32 s.condition(0)
33
34
35 def test_nested_filteredstrategy_flattens_conditions():
36 s = FilteredStrategy(
37 FilteredStrategy(st.text(), conditions=(bool,)),
38 conditions=(len,),
39 )
40 assert s.filtered_strategy is st.text()
41 assert s.flat_conditions == (bool, len)