| 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 pytest |
| 12 |
|
| 13 |
from hypothesis import strategies as st |
| 14 |
from hypothesis.internal.conjecture.data import ConjectureData |
| 15 |
from hypothesis.strategies._internal.lazy import LazyStrategy |
| 16 |
|
| 17 |
|
| 18 |
def test_labels_are_cached(): |
| 19 |
x = st.integers() |
| 20 |
assert x.label is x.label |
| 21 |
|
| 22 |
|
| 23 |
def test_labels_are_distinct(): |
| 24 |
assert st.integers().label != st.text().label |
| 25 |
|
| 26 |
|
| 27 |
@st.composite |
| 28 |
def foo(draw): |
| 29 |
return draw(st.none()) |
| 30 |
|
| 31 |
|
| 32 |
@st.composite |
| 33 |
def bar(draw): |
| 34 |
return draw(st.none()) |
| 35 |
|
| 36 |
|
| 37 |
@st.composite |
| 38 |
def baz(draw): |
| 39 |
return draw(st.booleans()) |
| 40 |
|
| 41 |
|
| 42 |
def test_equivalent_composites_have_same_label(): |
| 43 |
assert foo().label == bar().label |
| 44 |
|
| 45 |
|
| 46 |
def test_different_composites_have_different_labels(): |
| 47 |
assert foo().label != baz().label |
| 48 |
|
| 49 |
|
| 50 |
def test_one_of_label_is_distinct(): |
| 51 |
a = st.integers() |
| 52 |
b = st.booleans() |
| 53 |
assert st.one_of(a, b).label != st.one_of(b, a).label |
| 54 |
|
| 55 |
|
| 56 |
def test_lists_label_by_element(): |
| 57 |
assert st.lists(st.integers()).label != st.lists(st.booleans()).label |
| 58 |
|
| 59 |
|
| 60 |
def test_label_of_deferred_strategy_is_well_defined(): |
| 61 |
recursive = st.deferred(lambda: st.lists(recursive)) |
| 62 |
recursive.label |
| 63 |
|
| 64 |
|
| 65 |
@pytest.mark.parametrize( |
| 66 |
"strategy", |
| 67 |
[ |
| 68 |
lambda: [st.none()], |
| 69 |
lambda: [st.integers()], |
| 70 |
lambda: [st.lists(st.floats())], |
| 71 |
lambda: [st.none(), st.integers(), st.lists(st.floats())], |
| 72 |
], |
| 73 |
) |
| 74 |
def test_sampled_from_label_with_strategies_does_not_change(strategy): |
| 75 |
s1 = st.sampled_from(strategy()) |
| 76 |
s2 = st.sampled_from(strategy()) |
| 77 |
assert s1.label == s2.label |
| 78 |
|
| 79 |
|
| 80 |
def test_label_of_enormous_sampled_range(): |
| 81 |
# this should not take forever. |
| 82 |
st.sampled_from(range(2**30)).label |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.parametrize( |
| 86 |
"strategy", |
| 87 |
[ |
| 88 |
lambda: st.deferred(lambda: st.integers()), |
| 89 |
lambda: LazyStrategy(st.integers, (), {}), |
| 90 |
], |
| 91 |
) |
| 92 |
def test_draw_uses_wrapped_label(strategy): |
| 93 |
cd = ConjectureData.for_choices([0]) |
| 94 |
strategy = strategy() |
| 95 |
cd.draw(strategy) |
| 96 |
cd.freeze() |
| 97 |
|
| 98 |
assert len(cd.spans) == 2 |
| 99 |
assert cd.spans[1].label == st.integers().label |
| 100 |
|
| 101 |
|
| 102 |
def test_deferred_label(): |
| 103 |
strategy = st.deferred(lambda: st.integers()) |
| 104 |
assert strategy.label != st.integers().label |