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