Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property-based tests for keys(), values(), items() #217

Merged
merged 1 commit into from Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/properties/_strategies.py
Expand Up @@ -19,6 +19,7 @@

MAX = int(getenv('HYPOTHESIS_GEN_MAX_SIZE', '0')) or None

DATA = st.data()
BIDICT_TYPES = st.sampled_from(t.BIDICT_TYPES)
MUTABLE_BIDICT_TYPES = st.sampled_from(t.MUTABLE_BIDICT_TYPES)
FROZEN_BIDICT_TYPES = st.sampled_from(t.FROZEN_BIDICT_TYPES)
Expand All @@ -38,6 +39,8 @@
# provides enough coverage; including more just slows down example generation.
ATOMS = st.none() | BOOLEANS | st.integers()
PAIRS = st.tuples(ATOMS, ATOMS)
FROSETS = st.frozensets(ATOMS)
FROSETS_PAIRS = st.frozensets(PAIRS)
NON_MAPPINGS = ATOMS | st.iterables(ATOMS)
ODICTS_KW_PAIRS = st.dictionaries(TEXT, ATOMS, dict_class=OrderedDict, max_size=MAX)
L_PAIRS = st.lists(PAIRS, max_size=MAX)
Expand Down
34 changes: 33 additions & 1 deletion tests/properties/test_properties.py
Expand Up @@ -12,7 +12,8 @@

from copy import deepcopy
from collections import OrderedDict
from collections.abc import Iterable
from collections.abc import Iterable, KeysView, ValuesView, ItemsView

from itertools import tee
from platform import python_implementation
from weakref import ref
Expand Down Expand Up @@ -425,3 +426,34 @@ def test_inverted_bidict(bi_and_mapping):
mapping_inv = {v: k for (k, v) in mapping.items()}
assert all(i == j for (i, j) in zip(inverted(bi), mapping_inv.items()))
assert all(i == j for (i, j) in zip(inverted(inverted(bi)), mapping.items()))


_SET_METHOD_NAMES = (
'__le__', '__lt__', '__gt__', '__ge__', '__eq__', '__and__', '__rand__',
'__or__', '__ror__', '__sub__', '__rsub__', '__xor__', '__rxor__', 'isdisjoint',
)


@given(st.BIDICTS, st.DATA)
def test_views(bi, data):
"""Optimized view APIs should be equivalent to using the corresponding MappingViews from :mod:`collections.abc`."""
for check, expect in (bi.keys(), KeysView(bi)), (bi.values(), ValuesView(bi)), (bi.items(), ItemsView(bi)):
# 0-arity methods: __len__, __iter__
assert check.__len__() == expect.__len__()
assert list(check.__iter__()) == list(expect.__iter__())
# 1-arity methods: __contains__
draw_from = st.PAIRS if isinstance(expect, ItemsView) else st.ATOMS
arg = data.draw(draw_from)
assert check.__contains__(arg) == expect.__contains__(arg)
# Methods of set-like views
if isinstance(expect, ItemsView):
draw_from = st.FROSETS_PAIRS
elif isinstance(expect, KeysView):
draw_from = st.FROSETS
else:
continue
arg = data.draw(draw_from)
for methname in _SET_METHOD_NAMES:
check_ = getattr(check, methname)(arg)
expect_ = getattr(expect, methname)(arg)
assert check_ == expect_