| 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 math |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import find, settings as Settings |
| 16 |
from hypothesis.errors import NoSuchExample |
| 17 |
from hypothesis.strategies import booleans, dictionaries, floats, integers, lists |
| 18 |
|
| 19 |
from tests.common.debug import minimal |
| 20 |
|
| 21 |
|
| 22 |
def test_can_find_an_int(): |
| 23 |
assert minimal(integers()) == 0 |
| 24 |
assert minimal(integers(), lambda x: x >= 13) == 13 |
| 25 |
|
| 26 |
|
| 27 |
def test_can_find_list(): |
| 28 |
x = minimal(lists(integers()), lambda x: sum(x) >= 10) |
| 29 |
assert sum(x) == 10 |
| 30 |
|
| 31 |
|
| 32 |
def test_can_find_nan(): |
| 33 |
minimal(floats(), math.isnan) |
| 34 |
|
| 35 |
|
| 36 |
def test_can_find_nans(): |
| 37 |
x = minimal(lists(floats()), lambda x: math.isnan(sum(x))) |
| 38 |
if len(x) == 1: |
| 39 |
assert math.isnan(x[0]) |
| 40 |
else: |
| 41 |
assert 2 <= len(x) <= 3 |
| 42 |
|
| 43 |
|
| 44 |
def test_condition_is_name(): |
| 45 |
settings = Settings(max_examples=20) |
| 46 |
with pytest.raises(NoSuchExample) as e: |
| 47 |
find(booleans(), lambda x: False, settings=settings) |
| 48 |
assert "lambda x:" in e.value.args[0] |
| 49 |
|
| 50 |
with pytest.raises(NoSuchExample) as e: |
| 51 |
find(integers(), lambda x: "☃" in str(x), settings=settings) |
| 52 |
assert "lambda x:" in e.value.args[0] |
| 53 |
|
| 54 |
def bad(x): |
| 55 |
return False |
| 56 |
|
| 57 |
with pytest.raises(NoSuchExample) as e: |
| 58 |
find(integers(), bad, settings=settings) |
| 59 |
assert "bad" in e.value.args[0] |
| 60 |
|
| 61 |
|
| 62 |
def test_find_dictionary(): |
| 63 |
smallest = minimal( |
| 64 |
dictionaries(keys=integers(), values=integers()), |
| 65 |
lambda xs: any(kv[0] > kv[1] for kv in xs.items()), |
| 66 |
) |
| 67 |
assert len(smallest) == 1 |