| 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 given, settings |
| 14 |
from hypothesis.errors import InvalidArgument |
| 15 |
|
| 16 |
|
| 17 |
def e(a, *args, **kwargs): |
| 18 |
return (a, args, kwargs) |
| 19 |
|
| 20 |
|
| 21 |
def e_to_str(elt): |
| 22 |
f, args, kwargs = getattr(elt, "values", elt) |
| 23 |
bits = list(map(repr, args)) |
| 24 |
bits.extend(sorted(f"{k}={v!r}" for k, v in kwargs.items())) |
| 25 |
return "{}({})".format(f.__name__, ", ".join(bits)) |
| 26 |
|
| 27 |
|
| 28 |
def argument_validation_test(bad_args): |
| 29 |
@pytest.mark.parametrize( |
| 30 |
("function", "args", "kwargs"), bad_args, ids=list(map(e_to_str, bad_args)) |
| 31 |
) |
| 32 |
def test_raise_invalid_argument(function, args, kwargs): |
| 33 |
# some invalid argument tests may find multiple distinct invalid inputs, |
| 34 |
# which hypothesis raises as an exception group (and is not caught by |
| 35 |
# pytest.raises). |
| 36 |
@given(function(*args, **kwargs)) |
| 37 |
@settings(report_multiple_bugs=False) |
| 38 |
def test(x): |
| 39 |
pass |
| 40 |
|
| 41 |
with pytest.raises(InvalidArgument): |
| 42 |
test() |
| 43 |
|
| 44 |
return test_raise_invalid_argument |