Debian Codegraph @

hypothesis-python/tests/nocover/test_duplication.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 Counter
12
13 import pytest
14
15 from hypothesis import given, settings
16 from hypothesis.strategies._internal import SearchStrategy
17
18 from tests.common.utils import Why, xfail_on_crosshair
19
20
21 class Blocks(SearchStrategy):
22 def __init__(self, n):
23 super().__init__()
24 self.n = n
25
26 def do_draw(self, data):
27 return data.draw_bytes(self.n, self.n)
28
29
30 @xfail_on_crosshair(Why.symbolic_outside_context)
31 @pytest.mark.parametrize("n", range(1, 5))
32 def test_does_not_duplicate_blocks(n):
33 counts = Counter()
34
35 @given(Blocks(n))
36 @settings(database=None)
37 def test(b):
38 counts[b] += 1
39
40 test()
41 assert set(counts.values()) == {1}
42
43
44 @xfail_on_crosshair(Why.other, strict=False) # CrosshairInternal for n>0
45 @pytest.mark.parametrize("n", range(1, 5))
46 def test_mostly_does_not_duplicate_blocks_even_when_failing(n):
47 counts = Counter()
48
49 @settings(database=None)
50 @given(Blocks(n))
51 def test(b):
52 counts[b] += 1
53 if len(counts) > 3:
54 raise ValueError
55
56 try:
57 test()
58 except ValueError:
59 pass
60 # There are two circumstances in which a duplicate is allowed: We replay
61 # the failing test once to check for flakiness, and then we replay the
62 # fully minimized failing test at the end to display the error. The
63 # complication comes from the fact that these may or may not be the same
64 # test case, so we can see either two test cases each run twice or one
65 # test case which has been run three times.
66 assert set(counts.values()) in ({1, 2}, {1, 3})
67 assert len([k for k, v in counts.items() if v > 1]) <= 2