| 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, strategies as st |
| 14 |
from hypothesis.errors import InvalidArgument |
| 15 |
|
| 16 |
from tests.common.debug import assert_no_examples, minimal |
| 17 |
|
| 18 |
|
| 19 |
def test_resampling(): |
| 20 |
x = minimal( |
| 21 |
st.lists(st.integers(), min_size=1).flatmap( |
| 22 |
lambda x: st.lists(st.sampled_from(x)) |
| 23 |
), |
| 24 |
lambda x: len(x) >= 10 and len(set(x)) == 1, |
| 25 |
) |
| 26 |
assert x == [0] * 10 |
| 27 |
|
| 28 |
|
| 29 |
@given(st.lists(st.nothing())) |
| 30 |
def test_list_of_nothing(xs): |
| 31 |
assert xs == [] |
| 32 |
|
| 33 |
|
| 34 |
@given(st.sets(st.nothing())) |
| 35 |
def test_set_of_nothing(xs): |
| 36 |
assert xs == set() |
| 37 |
|
| 38 |
|
| 39 |
def test_validates_min_size(): |
| 40 |
with pytest.raises(InvalidArgument): |
| 41 |
st.lists(st.nothing(), min_size=1).validate() |
| 42 |
|
| 43 |
|
| 44 |
def test_function_composition(): |
| 45 |
assert st.nothing().map(lambda x: "hi").is_empty |
| 46 |
assert st.nothing().filter(lambda x: True).is_empty |
| 47 |
assert st.nothing().flatmap(lambda x: st.integers()).is_empty |
| 48 |
|
| 49 |
|
| 50 |
def test_tuples_detect_empty_elements(): |
| 51 |
assert st.tuples(st.nothing()).is_empty |
| 52 |
|
| 53 |
|
| 54 |
def test_fixed_dictionaries_detect_empty_values(): |
| 55 |
assert st.fixed_dictionaries({"a": st.nothing()}).is_empty |
| 56 |
|
| 57 |
|
| 58 |
def test_no_examples(): |
| 59 |
assert_no_examples(st.nothing()) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.parametrize( |
| 63 |
"s", |
| 64 |
[ |
| 65 |
st.nothing(), |
| 66 |
st.nothing().map(lambda x: x), |
| 67 |
st.nothing().filter(lambda x: True), |
| 68 |
st.nothing().flatmap(lambda x: st.integers()), |
| 69 |
], |
| 70 |
) |
| 71 |
def test_empty(s): |
| 72 |
assert s.is_empty |