| 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 dpcontracts import require |
| 13 |
|
| 14 |
from hypothesis import given |
| 15 |
from hypothesis.errors import InvalidArgument |
| 16 |
from hypothesis.extra.dpcontracts import fulfill |
| 17 |
from hypothesis.internal.conjecture.utils import identity |
| 18 |
from hypothesis.strategies import builds, integers |
| 19 |
|
| 20 |
|
| 21 |
@require("division is undefined for zero", lambda args: args.n != 0) |
| 22 |
def invert(n): |
| 23 |
return 1 / n |
| 24 |
|
| 25 |
|
| 26 |
@given(builds(fulfill(invert), integers())) |
| 27 |
def test_contract_filter_builds(x): |
| 28 |
assert -1 <= x <= 1 |
| 29 |
|
| 30 |
|
| 31 |
@given(integers()) |
| 32 |
def test_contract_filter_inline(n): |
| 33 |
assert -1 <= fulfill(invert)(n) <= 1 |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize("f", [int, identity, lambda x: None]) |
| 37 |
def test_no_vacuous_fulfill(f): |
| 38 |
with pytest.raises(InvalidArgument): |
| 39 |
fulfill(f) |