| 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 HealthCheck, assume, given, settings |
| 16 |
from hypothesis.database import InMemoryExampleDatabase |
| 17 |
from hypothesis.strategies import ( |
| 18 |
booleans, |
| 19 |
builds, |
| 20 |
floats, |
| 21 |
integers, |
| 22 |
just, |
| 23 |
lists, |
| 24 |
text, |
| 25 |
tuples, |
| 26 |
) |
| 27 |
|
| 28 |
from tests.common.debug import find_any, minimal |
| 29 |
from tests.common.utils import Why, xfail_on_crosshair |
| 30 |
|
| 31 |
ConstantLists = integers().flatmap(lambda i: lists(just(i))) |
| 32 |
|
| 33 |
OrderedPairs = integers(1, 200).flatmap(lambda e: tuples(integers(0, e - 1), just(e))) |
| 34 |
|
| 35 |
|
| 36 |
# This health check fails very very occasionally - rarely enough to not be worth |
| 37 |
# investigation |
| 38 |
@settings(max_examples=100, suppress_health_check=[HealthCheck.filter_too_much]) |
| 39 |
@given(ConstantLists) |
| 40 |
def test_constant_lists_are_constant(x): |
| 41 |
assume(len(x) >= 3) |
| 42 |
assert len(set(x)) == 1 |
| 43 |
|
| 44 |
|
| 45 |
@settings(max_examples=100) |
| 46 |
@given(OrderedPairs) |
| 47 |
def test_in_order(x): |
| 48 |
assert x[0] < x[1] |
| 49 |
|
| 50 |
|
| 51 |
@xfail_on_crosshair( |
| 52 |
Why.undiscovered |
| 53 |
) # (SampledFromStrategy.calc_label() hashes a symbolic float) |
| 54 |
def test_flatmap_retrieve_from_db(): |
| 55 |
track = [] |
| 56 |
|
| 57 |
@given(floats(0, 1).flatmap(lambda x: lists(just(x)))) |
| 58 |
@settings(database=InMemoryExampleDatabase()) |
| 59 |
def record_and_test_size(xs): |
| 60 |
if sum(xs) >= 1: |
| 61 |
track.append(xs) |
| 62 |
raise AssertionError |
| 63 |
|
| 64 |
with pytest.raises(AssertionError): |
| 65 |
record_and_test_size() |
| 66 |
|
| 67 |
assert track |
| 68 |
example = track[-1] |
| 69 |
track = [] |
| 70 |
|
| 71 |
with pytest.raises(AssertionError): |
| 72 |
record_and_test_size() |
| 73 |
|
| 74 |
assert track[0] == example |
| 75 |
|
| 76 |
|
| 77 |
def test_flatmap_does_not_reuse_strategies(): |
| 78 |
s = builds(list).flatmap(just) |
| 79 |
assert find_any(s) is not find_any(s) |
| 80 |
|
| 81 |
|
| 82 |
def test_flatmap_has_original_strategy_repr(): |
| 83 |
ints = integers() |
| 84 |
ints_up = ints.flatmap(lambda n: integers(min_value=n)) |
| 85 |
assert repr(ints) in repr(ints_up) |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.skipif( |
| 89 |
settings.get_current_profile_name() == "crosshair", |
| 90 |
reason="takes ~6 mins in CI, but ~7 sec in isolation. Unsure why", |
| 91 |
) |
| 92 |
def test_mixed_list_flatmap(): |
| 93 |
s = lists(booleans().flatmap(lambda b: booleans() if b else text())) |
| 94 |
|
| 95 |
def criterion(ls): |
| 96 |
c = Counter(type(l) for l in ls) |
| 97 |
return len(c) >= 2 and min(c.values()) >= 3 |
| 98 |
|
| 99 |
result = minimal(s, criterion) |
| 100 |
assert len(result) == 6 |
| 101 |
assert set(result) == {False, ""} |
| 102 |
|
| 103 |
|
| 104 |
@xfail_on_crosshair(Why.undiscovered) # for n >= 8 at least |
| 105 |
@pytest.mark.parametrize("n", range(1, 10)) |
| 106 |
def test_can_shrink_through_a_binding(n): |
| 107 |
bool_lists = integers(0, 100).flatmap( |
| 108 |
lambda k: lists(booleans(), min_size=k, max_size=k) |
| 109 |
) |
| 110 |
assert minimal(bool_lists, lambda x: x.count(True) >= n) == [True] * n |
| 111 |
|
| 112 |
|
| 113 |
@xfail_on_crosshair(Why.undiscovered) # for n >= 8 at least |
| 114 |
@pytest.mark.parametrize("n", range(1, 10)) |
| 115 |
def test_can_delete_in_middle_of_a_binding(n): |
| 116 |
bool_lists = integers(1, 100).flatmap( |
| 117 |
lambda k: lists(booleans(), min_size=k, max_size=k) |
| 118 |
) |
| 119 |
result = minimal(bool_lists, lambda x: x[0] and x[-1] and x.count(False) >= n) |
| 120 |
assert result == [True] + [False] * n + [True] |