How to use the hypothesis.strategies function in hypothesis

To help you get started, we’ve selected a few hypothesis examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fabiommendes / ox / tests / test_ast.py View on Github external
def exprs(depth=5, **kwargs):
    tt = st.one_of(st.just(Add), st.just(Sub), st.just(Mul), st.just(Div))
    if depth == 1:
        arg = numbers(**kwargs)
    else:
        arg = st.one_of(numbers(**kwargs), exprs(depth=depth - 1, **kwargs))
    return st.builds(lambda f, lhs, rhs: f(lhs, rhs), tt, arg, arg)
github HypothesisWorks / hypothesis / tests / nocover / test_deferred_strategies.py View on Github external
def build_strategy_for_indices(base, ixs, deferred):
        def f():
            return base(*[strategies[i] for i in ixs])
        f.__name__ = '%s([%s])' % (
            base.__name__, ', '.join(
                'strategies[%d]' % (i,) for i in ixs
            ))
        if deferred:
            return st.deferred(f)
        else:
            return f()
github ethereum / py-trie / tests / test_binaries_utils.py View on Github external
@given(value=st.lists(elements=st.integers(0, 1), min_size=0, max_size=1024))
def test_round_trip_bin_keypath_encoding(value):
    value_as_bin_keypath = encode_from_bin_keypath(bytes(value))
    result = decode_to_bin_keypath(value_as_bin_keypath)
    assert result == bytes(value)
github rsokl / MyGrad / tests / tensor_base / test_tensor.py View on Github external
@given(constant=st.booleans())
def test_math_methods(attr: str, constant: bool):
    x = Tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], constant=constant)

    assert hasattr(x, attr)
    method_out = getattr(x, attr).__call__()
    function_out = getattr(mg, attr).__call__(x)
    assert_equal(method_out.data, function_out.data)
    assert method_out.constant is constant
    assert type(method_out.creator) is type(function_out.creator)
github garyd203 / flying-circus / tests / core_test / common.py View on Github external
def aws_object_strategy(draw):
    """A strategy that produces an AWS CFN object."""
    attributes = draw(st.sets(aws_logical_name_strategy()))

    @attrs(these={name: attrib(default=None) for name in attributes}, **ATTRSCONFIG)
    class HypothesisedAWSObject(AWSObject):
        pass

    return draw(
        st.builds(
            HypothesisedAWSObject,
            **{name: aws_attribute_strategy() for name in attributes},
        )
github rocky / python-uncompyle6 / pytest / test_function_call.py View on Github external
:param draw: Callable which draws examples from other strategies.

        :return: The function call text.
        """
        st_positional_args = st.lists(
            alpha,
            min_size=min_positional_args,
            max_size=max_positional_args
        )
        st_keyword_args = st.lists(
            alpha,
            min_size=min_keyword_args,
            max_size=max_keyword_args
        )
        st_star_args = st.lists(
            alpha,
            min_size=min_star_args,
            max_size=max_star_args
        )
        st_double_star_args = st.lists(
            alpha,
            min_size=min_double_star_args,
            max_size=max_double_star_args
        )

        positional_args = draw(st_positional_args)
        keyword_args = draw(st_keyword_args)
        st_values = st.lists(
            expressions(),
            min_size=len(keyword_args),
            max_size=len(keyword_args)
github sarugaku / vistir / tests / strategies.py View on Github external
def urls():
    """
    Strategy for generating urls.
    """
    return st.builds(
        parsed_url,
        scheme=st.sampled_from(uri_schemes),
        netloc=dns_names(),
        path=st.lists(
            st.text(
                max_size=64,
                alphabet=st.characters(
                    blacklist_characters="/?#", blacklist_categories=("Cs",)
                ),
            ),
            min_size=1,
            max_size=10,
        )
        .map(to_text)
        .map("".join),
    )
github warner / python-ecdsa / src / ecdsa / test_numbertheory.py View on Github external
@st.composite
def st_primes(draw, *args, **kwargs):
    if "min_value" not in kwargs:  # pragma: no branch
        kwargs["min_value"] = 1
    prime = draw(
        st.sampled_from(smallprimes)
        | st.integers(*args, **kwargs).filter(is_prime)
    )
    return prime
github HypothesisWorks / hypothesis / src / hypothesis / __init__.py View on Github external
It verifies your code against a wide range of input and minimizes any
failing examples it finds.

"""

from __future__ import division, print_function, absolute_import, \
    unicode_literals

from hypothesis.searchstrategy import strategy
from hypothesis.settings import Settings, Verbosity
from hypothesis.version import __version_info__, __version__
from hypothesis.core import given, assume, find, example

# Force strategy extensions to be loaded here
import hypothesis.strategies as unused
[unused]

__all__ = [
    'Settings',
    'Verbosity',
    'assume',
    'given',
    'strategy',
    'find',
    'example',
    '__version__',
    '__version_info__',
]
github fabiommendes / sidekick / sidekick / hypothesis / base.py View on Github external
The category of atomic types to choose.

            'basic':
                Basic Python atomic literals (numbers, strings, bools, None, Ellipsis).
            'ordered':
                Exclude complex numbers, None, and Ellipsis since they do not have an
                ordering relation.
            'json':
                Only valid JSON data types (including valid Javascript numeric
                ranges)

        finite:
            Only yield finite numeric values (i.e., no NaN and infinities)
    """
    strategies = [
        st.booleans(),
        st.text(),
        st.floats(
            allow_nan=not finite and which != "json",
            allow_infinity=not finite and which != "json",
        ),
    ]
    add = strategies.append

    if which in ("basic", "ordered"):
        add(st.integers())
        add(st.just(None))
        add(st.binary())

    if which == "json":
        # See also:
        # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER