| 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 find, given, strategies as st |
| 14 |
from hypothesis.errors import InvalidArgument |
| 15 |
from hypothesis.strategies._internal.core import defines_strategy |
| 16 |
|
| 17 |
from tests.common.debug import check_can_generate_examples |
| 18 |
|
| 19 |
|
| 20 |
def test_does_not_error_on_initial_calculation(): |
| 21 |
st.floats(max_value=float("nan")) |
| 22 |
st.sampled_from([]) |
| 23 |
st.lists(st.integers(), min_size=5, max_size=2) |
| 24 |
st.floats(min_value=2.0, max_value=1.0) |
| 25 |
|
| 26 |
|
| 27 |
def test_errors_each_time(): |
| 28 |
s = st.integers(max_value=1, min_value=3) |
| 29 |
with pytest.raises(InvalidArgument): |
| 30 |
check_can_generate_examples(s) |
| 31 |
with pytest.raises(InvalidArgument): |
| 32 |
check_can_generate_examples(s) |
| 33 |
|
| 34 |
|
| 35 |
def test_errors_on_test_invocation(): |
| 36 |
@given(st.integers(max_value=1, min_value=3)) |
| 37 |
def test(x): |
| 38 |
pass |
| 39 |
|
| 40 |
with pytest.raises(InvalidArgument): |
| 41 |
test() |
| 42 |
|
| 43 |
|
| 44 |
def test_errors_on_find(): |
| 45 |
s = st.lists(st.integers(), min_size=5, max_size=2) |
| 46 |
with pytest.raises(InvalidArgument): |
| 47 |
find(s, lambda x: True) |
| 48 |
|
| 49 |
|
| 50 |
def test_errors_on_example(): |
| 51 |
s = st.floats(min_value=2.0, max_value=1.0) |
| 52 |
with pytest.raises(InvalidArgument): |
| 53 |
check_can_generate_examples(s) |
| 54 |
|
| 55 |
|
| 56 |
def test_does_not_recalculate_the_strategy(): |
| 57 |
calls = 0 |
| 58 |
|
| 59 |
@defines_strategy() |
| 60 |
def foo(): |
| 61 |
nonlocal calls |
| 62 |
calls += 1 |
| 63 |
return st.just(1) |
| 64 |
|
| 65 |
f = foo() |
| 66 |
assert calls == 0 |
| 67 |
check_can_generate_examples(f) |
| 68 |
assert calls == 1 |
| 69 |
check_can_generate_examples(f) |
| 70 |
assert calls == 1 |