-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2122347
commit 44f5741
Showing
5 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""mapping conjectures.""" | ||
|
||
import collections.abc | ||
|
||
import conjecture.base | ||
|
||
sentinel = object() | ||
|
||
|
||
def has_key(value: str, of: object = sentinel) -> conjecture.base.Conjecture: | ||
""" | ||
Has attribute. | ||
Propose that the value has the given key | ||
>>> assert value == conjecture.has_key("foo") | ||
>>> assert value == conjecture.has_key("foo", of=5) | ||
>>> assert value == conjecture.has_key("foo", of=conjecture.less_than(10)) | ||
:param value: the name of the key | ||
:param of: an optional value or conjecture to compare the key value against | ||
:return: a conjecture object | ||
""" | ||
# pylint: disable=invalid-name | ||
# of is a perfectly valid name. | ||
|
||
if of is sentinel: | ||
return conjecture.base.Conjecture( | ||
lambda x: isinstance(x, collections.abc.Mapping) and value in x | ||
) | ||
|
||
return conjecture.base.Conjecture( | ||
lambda x: isinstance(x, collections.abc.Mapping) and x.get(value) == of | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
""" | ||
Tests for :meth:`conjecture.has_key`. | ||
""" | ||
|
||
import string | ||
|
||
import hypothesis | ||
import hypothesis.strategies as st | ||
|
||
import conjecture | ||
|
||
|
||
@hypothesis.given( | ||
value=st.text(alphabet=string.ascii_letters, min_size=1), | ||
other=st.integers(), | ||
) | ||
def test_should_match_when_key_exists(value: str, other: int) -> None: | ||
""" | ||
has_key() should match when key exists. | ||
""" | ||
mapping = {value: other} | ||
|
||
assert conjecture.has_key(value).resolve(mapping) | ||
|
||
|
||
@hypothesis.given( | ||
value=st.text(alphabet=string.ascii_letters, min_size=1), | ||
key=st.text(alphabet=string.ascii_letters, min_size=1), | ||
other=st.integers(), | ||
) | ||
def test_should_not_match_when_key_doesnt_exists( | ||
value: str, | ||
key: str, | ||
other: int, | ||
) -> None: | ||
""" | ||
has_key() should not match when key doesn't exist. | ||
""" | ||
hypothesis.assume(value != key) | ||
|
||
mapping = {key: other} | ||
|
||
assert not conjecture.has_key(value).resolve(mapping) | ||
|
||
|
||
@hypothesis.given( | ||
value=st.text(alphabet=string.ascii_letters, min_size=1), | ||
other=st.integers(), | ||
) | ||
def test_should_match_when_key_value_matches(value: str, other: int) -> None: | ||
""" | ||
has_key() should match when key value matches. | ||
""" | ||
mapping = {value: other} | ||
|
||
assert conjecture.has_key(value, of=other).resolve(mapping) | ||
|
||
|
||
@hypothesis.given( | ||
value=st.text(alphabet=string.ascii_letters, min_size=1), | ||
wrong=st.integers(), | ||
other=st.integers(), | ||
) | ||
def test_should_not_match_when_key_value_doesnt_match( | ||
value: str, | ||
wrong: int, | ||
other: int, | ||
) -> None: | ||
""" | ||
has_key() should not match when key value doesn't match. | ||
""" | ||
hypothesis.assume(wrong != other) | ||
|
||
mapping = {value: wrong} | ||
|
||
assert not conjecture.has_key(value, of=other).resolve(mapping) | ||
|
||
|
||
@hypothesis.given( | ||
value=st.text(alphabet=string.ascii_letters, min_size=1), | ||
other=st.integers(), | ||
) | ||
def test_should_match_when_key_value_matches_conjecture(value: str, other: int) -> None: | ||
""" | ||
has_key() should match when key value matches conjecture. | ||
""" | ||
mapping = {value: other} | ||
|
||
always = conjecture.has(lambda x: True) | ||
|
||
assert conjecture.has_key(value, of=always).resolve(mapping) | ||
|
||
|
||
@hypothesis.given( | ||
value=st.text(alphabet=string.ascii_letters, min_size=1), | ||
other=st.integers(), | ||
) | ||
def test_should_not_match_when_key_value_doesnt_match_conjecture( | ||
value: str, other: int | ||
) -> None: | ||
""" | ||
has_key() should not match when key value doesn't match conjecture. | ||
""" | ||
mapping = {value: other} | ||
|
||
never = conjecture.has(lambda x: False) | ||
|
||
assert not conjecture.has_key(value, of=never).resolve(mapping) |