예제 #1
0
 def __init__(
     self, strategy, expand
 ):
     super(FlatMapStrategy, self).__init__(
         strategy=TupleStrategy((
             strategy, MorpherStrategy()), tuple))
     self.flatmapped_strategy = strategy
     self.expand = expand
     self.settings = Settings.default
예제 #2
0
def composite(f):
    """Defines a strategy that is built out of potentially arbitrarily many
    other strategies.

    This is intended to be used as a decorator. See the full
    documentation for more details about how to use this function.

    """

    from hypothesis.searchstrategy.morphers import MorpherStrategy
    import inspect
    from hypothesis.internal.reflection import copy_argspec
    argspec = inspect.getargspec(f)

    if (
        argspec.defaults is not None and
        len(argspec.defaults) == len(argspec.args)
    ):
        raise InvalidArgument(
            'A default value for initial argument will never be used')
    if len(argspec.args) == 0 and not argspec.varargs:
        raise InvalidArgument(
            'Functions wrapped with composite must take at least one '
            'positional argument.'
        )

    new_argspec = inspect.ArgSpec(
        args=argspec.args[1:], varargs=argspec.varargs,
        keywords=argspec.keywords, defaults=argspec.defaults
    )

    base_strategy = streaming(MorpherStrategy())

    @defines_strategy
    @copy_argspec(f.__name__, new_argspec)
    def accept(*args, **kwargs):
        def call_with_draw(morphers):
            index = [0]

            def draw(strategy):
                i = index[0]
                index[0] += 1
                return morphers[i].become(strategy)
            return f(*((draw,) + args), **kwargs)
        return base_strategy.map(call_with_draw)
    return accept
예제 #3
0
            booleans(), lists(complex_numbers())
        )).flatmap(lambda x: x)
    )

    def integers_from(x):
        return integers(min_value=x)

    TestManyFlatmaps = strategy_test_suite(
        integers()
        .flatmap(integers_from)
        .flatmap(integers_from)
        .flatmap(integers_from)
        .flatmap(integers_from)
    )

    TestBareMorphers = strategy_test_suite(MorpherStrategy())
    TestMasqueradingMorphers = strategy_test_suite(
        MorpherStrategy().map(lambda m: m.become(lists(integers()))))

    TestIntStreams = strategy_test_suite(streaming(integers()))
    TestStreamLists = strategy_test_suite(streaming(integers()))
    TestIntStreamStreams = strategy_test_suite(
        streaming(streaming(integers())))

    TestBoringBitfieldsClass = strategy_test_suite(basic(BoringBitfields))
    TestBitfieldsClass = strategy_test_suite(basic(Bitfields))
    TestBitfieldsInstance = strategy_test_suite(basic(Bitfields()))

    TestBitfields = strategy_test_suite(lists(
        basic(
            generate=lambda r, p: r.getrandbits(128),
예제 #4
0
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at http://mozilla.org/MPL/2.0/.
#
# END HEADER

from __future__ import division, print_function, absolute_import

from random import Random

import hypothesis.strategies as s
from flaky import flaky
from hypothesis import find, given, example, Settings
from hypothesis.control import BuildContext
from hypothesis.searchstrategy.morphers import MorpherStrategy

morphers = MorpherStrategy()
intlists = s.lists(s.integers())


def test_can_simplify_through_a_morpher():
    m = find(morphers, lambda x: bool(x.become(intlists)))
    assert m.become(intlists) == [0]


@example(Random(187))
@example(Random(0))
@given(s.randoms(), settings=Settings(max_examples=10))
def test_can_simplify_text_through_a_morpher(rnd):
    m = find(morphers,
             lambda x: bool(x.become(s.text())),
             random=rnd,
예제 #5
0
TestDiverseFlatmap = strategy_test_suite(
    sampled_from((lists(integers(), average_size=5.0),
                  lists(text(), average_size=5.0), tuples(text(), text()),
                  booleans(), lists(complex_numbers()))).flatmap(lambda x: x))


def integers_from(x):
    return integers(min_value=x)


TestManyFlatmaps = strategy_test_suite(
    integers().flatmap(integers_from).flatmap(integers_from).flatmap(
        integers_from).flatmap(integers_from))

TestBareMorphers = strategy_test_suite(MorpherStrategy())
TestMasqueradingMorphers = strategy_test_suite(MorpherStrategy().map(
    lambda m: m.become(lists(integers(), average_size=5.0))))

TestIntStreams = strategy_test_suite(streaming(integers()))
TestStreamLists = strategy_test_suite(streaming(integers()))
TestIntStreamStreams = strategy_test_suite(streaming(streaming(integers())))

TestRecursiveLowLeaves = strategy_test_suite(
    recursive(
        booleans(),
        lambda x: tuples(x, x),
        max_leaves=3,
    ))

TestRecursiveHighLeaves = strategy_test_suite(