|
| 1 | +""" |
| 2 | +Tests for :meth:`conjecture.has_key`. |
| 3 | +""" |
| 4 | + |
| 5 | +import string |
| 6 | + |
| 7 | +import hypothesis |
| 8 | +import hypothesis.strategies as st |
| 9 | + |
| 10 | +import conjecture |
| 11 | + |
| 12 | + |
| 13 | +@hypothesis.given( |
| 14 | + value=st.text(alphabet=string.ascii_letters, min_size=1), |
| 15 | + other=st.integers(), |
| 16 | +) |
| 17 | +def test_should_match_when_key_exists(value: str, other: int) -> None: |
| 18 | + """ |
| 19 | + has_key() should match when key exists. |
| 20 | + """ |
| 21 | + mapping = {value: other} |
| 22 | + |
| 23 | + assert conjecture.has_key(value).resolve(mapping) |
| 24 | + |
| 25 | + |
| 26 | +@hypothesis.given( |
| 27 | + value=st.text(alphabet=string.ascii_letters, min_size=1), |
| 28 | + key=st.text(alphabet=string.ascii_letters, min_size=1), |
| 29 | + other=st.integers(), |
| 30 | +) |
| 31 | +def test_should_not_match_when_key_doesnt_exists( |
| 32 | + value: str, |
| 33 | + key: str, |
| 34 | + other: int, |
| 35 | +) -> None: |
| 36 | + """ |
| 37 | + has_key() should not match when key doesn't exist. |
| 38 | + """ |
| 39 | + hypothesis.assume(value != key) |
| 40 | + |
| 41 | + mapping = {key: other} |
| 42 | + |
| 43 | + assert not conjecture.has_key(value).resolve(mapping) |
| 44 | + |
| 45 | + |
| 46 | +@hypothesis.given( |
| 47 | + value=st.text(alphabet=string.ascii_letters, min_size=1), |
| 48 | + other=st.integers(), |
| 49 | +) |
| 50 | +def test_should_match_when_key_value_matches(value: str, other: int) -> None: |
| 51 | + """ |
| 52 | + has_key() should match when key value matches. |
| 53 | + """ |
| 54 | + mapping = {value: other} |
| 55 | + |
| 56 | + assert conjecture.has_key(value, of=other).resolve(mapping) |
| 57 | + |
| 58 | + |
| 59 | +@hypothesis.given( |
| 60 | + value=st.text(alphabet=string.ascii_letters, min_size=1), |
| 61 | + wrong=st.integers(), |
| 62 | + other=st.integers(), |
| 63 | +) |
| 64 | +def test_should_not_match_when_key_value_doesnt_match( |
| 65 | + value: str, |
| 66 | + wrong: int, |
| 67 | + other: int, |
| 68 | +) -> None: |
| 69 | + """ |
| 70 | + has_key() should not match when key value doesn't match. |
| 71 | + """ |
| 72 | + hypothesis.assume(wrong != other) |
| 73 | + |
| 74 | + mapping = {value: wrong} |
| 75 | + |
| 76 | + assert not conjecture.has_key(value, of=other).resolve(mapping) |
| 77 | + |
| 78 | + |
| 79 | +@hypothesis.given( |
| 80 | + value=st.text(alphabet=string.ascii_letters, min_size=1), |
| 81 | + other=st.integers(), |
| 82 | +) |
| 83 | +def test_should_match_when_key_value_matches_conjecture(value: str, other: int) -> None: |
| 84 | + """ |
| 85 | + has_key() should match when key value matches conjecture. |
| 86 | + """ |
| 87 | + mapping = {value: other} |
| 88 | + |
| 89 | + always = conjecture.has(lambda x: True) |
| 90 | + |
| 91 | + assert conjecture.has_key(value, of=always).resolve(mapping) |
| 92 | + |
| 93 | + |
| 94 | +@hypothesis.given( |
| 95 | + value=st.text(alphabet=string.ascii_letters, min_size=1), |
| 96 | + other=st.integers(), |
| 97 | +) |
| 98 | +def test_should_not_match_when_key_value_doesnt_match_conjecture( |
| 99 | + value: str, other: int |
| 100 | +) -> None: |
| 101 | + """ |
| 102 | + has_key() should not match when key value doesn't match conjecture. |
| 103 | + """ |
| 104 | + mapping = {value: other} |
| 105 | + |
| 106 | + never = conjecture.has(lambda x: False) |
| 107 | + |
| 108 | + assert not conjecture.has_key(value, of=never).resolve(mapping) |
0 commit comments