| 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 |
from pytest import raises |
| 13 |
|
| 14 |
from hypothesis import find, given, settings, strategies as st |
| 15 |
from hypothesis.errors import InvalidArgument |
| 16 |
|
| 17 |
|
| 18 |
@given(st.integers(), st.data()) |
| 19 |
def test_conditional_draw(x, data): |
| 20 |
y = data.draw(st.integers(min_value=x)) |
| 21 |
assert y >= x |
| 22 |
|
| 23 |
|
| 24 |
def test_prints_on_failure(): |
| 25 |
@given(st.data()) |
| 26 |
def test(data): |
| 27 |
x = data.draw(st.lists(st.integers(0, 10), min_size=2)) |
| 28 |
y = data.draw(st.sampled_from(x)) |
| 29 |
x.remove(y) |
| 30 |
if y in x: |
| 31 |
raise ValueError |
| 32 |
|
| 33 |
with raises(ValueError) as err: |
| 34 |
test() |
| 35 |
assert "Draw 1: [0, 0]" in err.value.__notes__ |
| 36 |
assert "Draw 2: 0" in err.value.__notes__ |
| 37 |
|
| 38 |
|
| 39 |
def test_prints_labels_if_given_on_failure(): |
| 40 |
@given(st.data()) |
| 41 |
def test(data): |
| 42 |
x = data.draw(st.lists(st.integers(0, 10), min_size=2), label="Some numbers") |
| 43 |
y = data.draw(st.sampled_from(x), label="A number") |
| 44 |
assert y in x |
| 45 |
x.remove(y) |
| 46 |
assert y not in x |
| 47 |
|
| 48 |
with raises(AssertionError) as err: |
| 49 |
test() |
| 50 |
assert "Draw 1 (Some numbers): [0, 0]" in err.value.__notes__ |
| 51 |
assert "Draw 2 (A number): 0" in err.value.__notes__ |
| 52 |
|
| 53 |
|
| 54 |
def test_given_twice_is_same(): |
| 55 |
@given(st.data(), st.data()) |
| 56 |
def test(data1, data2): |
| 57 |
data1.draw(st.integers()) |
| 58 |
data2.draw(st.integers()) |
| 59 |
raise ValueError |
| 60 |
|
| 61 |
with raises(ValueError) as err: |
| 62 |
test() |
| 63 |
assert "Draw 1: 0" in err.value.__notes__ |
| 64 |
assert "Draw 2: 0" in err.value.__notes__ |
| 65 |
|
| 66 |
|
| 67 |
# `find` doesn't seem to be thread-safe, though I don't actually see why not |
| 68 |
@pytest.mark.xfail( |
| 69 |
settings._current_profile == "threading", strict=False, reason="not thread-safe?" |
| 70 |
) |
| 71 |
def test_data_supports_find(): |
| 72 |
data = find(st.data(), lambda data: data.draw(st.integers()) >= 10) |
| 73 |
assert data.conjecture_data.choices == (10,) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize("f", ["filter", "map", "flatmap"]) |
| 77 |
def test_errors_when_normal_strategy_functions_are_used(f): |
| 78 |
with raises(InvalidArgument): |
| 79 |
getattr(st.data(), f)(lambda x: 1) |
| 80 |
|
| 81 |
|
| 82 |
def test_nice_repr(): |
| 83 |
assert repr(st.data()) == "data()" |