| 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 re |
| 12 |
from collections.abc import Sequence |
| 13 |
|
| 14 |
import pytest |
| 15 |
|
| 16 |
from hypothesis import given, strategies as st |
| 17 |
from hypothesis.errors import InvalidArgument |
| 18 |
|
| 19 |
from tests.common.debug import assert_no_examples |
| 20 |
|
| 21 |
|
| 22 |
def test_one_of_empty(): |
| 23 |
e = st.one_of() |
| 24 |
assert e.is_empty |
| 25 |
assert_no_examples(e) |
| 26 |
|
| 27 |
|
| 28 |
@given(st.one_of(st.integers().filter(bool))) |
| 29 |
def test_one_of_filtered(i): |
| 30 |
assert bool(i) |
| 31 |
|
| 32 |
|
| 33 |
@given(st.one_of(st.just(100).flatmap(st.integers))) |
| 34 |
def test_one_of_flatmapped(i): |
| 35 |
assert i >= 100 |
| 36 |
|
| 37 |
|
| 38 |
def test_one_of_single_strategy_is_noop(): |
| 39 |
s = st.integers() |
| 40 |
assert st.one_of(s) is s |
| 41 |
assert st.one_of([s]) is s |
| 42 |
|
| 43 |
|
| 44 |
def test_one_of_without_strategies_suggests_sampled_from(): |
| 45 |
with pytest.raises( |
| 46 |
InvalidArgument, |
| 47 |
match=re.escape("Did you mean st.sampled_from([1, 2, 3])?"), |
| 48 |
): |
| 49 |
st.one_of(1, 2, 3) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
"strategy, count", |
| 54 |
[ |
| 55 |
(st.one_of(st.integers(), st.integers(), st.integers()), 1), |
| 56 |
(st.one_of(st.integers(), st.one_of(st.integers(), st.integers())), 2), |
| 57 |
((st.integers() | st.integers()) | st.integers(), 1), |
| 58 |
(st.integers() | (st.integers() | st.integers()), 1), |
| 59 |
(st.integers() | st.text() | st.booleans(), 1), |
| 60 |
(st.from_type(int | Sequence[int]), 2), |
| 61 |
], |
| 62 |
) |
| 63 |
def test_one_of_unwrapping(strategy, count): |
| 64 |
assert repr(strategy).count("one_of(") == count |