Skip to content

Commit

Permalink
More tests, some failing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed May 26, 2017
1 parent 37bb46c commit abe9890
Showing 1 changed file with 72 additions and 7 deletions.
79 changes: 72 additions & 7 deletions tests/py3/test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
from __future__ import division, print_function, absolute_import

import io
import collections

import pytest

from hypothesis import given
import hypothesis.strategies as st
from hypothesis import find, given
from hypothesis.errors import InvalidArgument
from hypothesis.strategies import from_type
from hypothesis.searchstrategy import types

typing = pytest.importorskip('typing')


@pytest.mark.parametrize('typ', types.get_all_typing_classes())
def test_resolve_typing_module(typ):
typing = pytest.importorskip('typing')
# This is covered in special cases; the real purpose is to make sure we
# notice if something (else) odd is added to the typing module.

# TODO: new test which includes parameters, ie List[...] or Dict[..., ...]
try:
strategy = from_type(typ)
strategy = st.from_type(typ)
except InvalidArgument:
pytest.skip(msg='TODO: implement strategy for this type')
pytest.skip(msg='TODO: implement strategy for type %r' % typ)

@given(strategy)
def inner(ex):
Expand All @@ -61,3 +61,68 @@ def inner(ex):
assert isinstance(ex, typ)

inner()


def test_mock_Any():
assert st.from_type(typing.Any).example().is_magic_mock


def test_generator_return_not_handled():
# Not handled due to SyntaxError under Python2
# A fix would be better, but just test the actual behaviour for now
# Note: lazy evaluation means assertions are only raised when we iterate
with pytest.raises(AssertionError):
for _ in find(st.from_type(typing.Generator[int, int, int]),
lambda x: True):
pass
# But an explicit None is OK
for _ in find(st.from_type(typing.Generator[int, int, None]),
lambda x: True):
pass


@pytest.mark.parametrize('typ,instance_of', [
(typing.Union[int, str], (int, str)),
(typing.Optional[int], (int, type(None))),
(typing.Text, str),
])
def test_specialised_scalar_types(typ, instance_of):
@given(st.from_type(typ))
def inner(ex):
assert isinstance(ex, instance_of)

inner()


@pytest.mark.parametrize('typ,coll_type,instance_of', [
(typing.Set[int], set, int),
(typing.Dict[int, int], dict, int),
(typing.ItemsView[int, int], type({}.items()), tuple),
(typing.KeysView[int], type({}.keys()), int),
(typing.ValuesView[int], type({}.values()), int),
(typing.List[int], list, int),
(typing.Tuple[int], tuple, int),
(typing.Tuple[int, ...], tuple, int),
(typing.NamedTuple('A_NamedTuple', (('elem', int),)), tuple, int),
])
def test_specialised_collection_types(typ, coll_type, instance_of):
@given(st.from_type(typ))
def inner(ex):
assert isinstance(ex, coll_type)
assert all(isinstance(elem, instance_of) for elem in ex)

inner()


@pytest.mark.parametrize('un', [
typing.Union[int, str],
typing.Union[list, dict, bool],
typing.Optional[int],
])
def test_union_with_None_minimises_to_None(un):
least = find(st.from_type(un), lambda ex: True)
if issubclass(type(None), un):
assert least is None
else:
assert least is not None
assert not isinstance(least, collections.abc.Container)

0 comments on commit abe9890

Please sign in to comment.