def test_can_use_recursive_data_in_sets(rnd):
    nested_sets = st.recursive(
        st.booleans(),
        lambda js: st.frozensets(js, average_size=2.0),
        max_leaves=10
    )
    nested_sets.example(rnd)

    def flatten(x):
        if isinstance(x, bool):
            return frozenset((x,))
        else:
            result = frozenset()
            for t in x:
                result |= flatten(t)
                if len(result) == 2:
                    break
            return result
    assert rnd is not None
    x = find(
        nested_sets, lambda x: len(flatten(x)) == 2, random=rnd,
        settings=settings(database=None, max_shrinks=1000, max_examples=1000))
    assert x in (
        frozenset((False, True)),
        frozenset((False, frozenset((True,)))),
        frozenset((frozenset((False, True)),))
    )
def test_can_find_nested():
    x = find(
        st.recursive(st.booleans(), lambda x: st.tuples(x, x)),
        lambda x: isinstance(x, tuple) and isinstance(x[0], tuple)
    )

    assert x == ((False, False), False)
Esempio n. 3
0
def test_can_form_sets_of_recursive_data():
    trees = st.sets(st.recursive(
        st.booleans(), lambda x: st.lists(x).map(tuple), max_leaves=4))
    xs = find(trees, lambda x: len(x) >= 10)
    assert len(xs) == 10
    assert False in xs
    assert True in xs
Esempio n. 4
0
def values():
    """Returns a strategy that unifies all strategies that produced valid JSON
    serializable values.
    """
    def extend(children):
        return arrays(children) | objects(children)
    simple_values = nulls() | booleans() | numbers() | strings()
    return simple_values | st.recursive(simple_values, extend)
Esempio n. 5
0
def test_drawing_many_near_boundary():
    ls = find(
        st.lists(st.recursive(
            st.booleans(),
            lambda x: st.lists(x, min_size=8, max_size=10).map(tuple),
            max_leaves=9)),
        lambda x: len(set(x)) >= 5)
    assert len(ls) == 5
def test_can_form_sets_of_recursive_data():
    size = 3

    trees = st.sets(st.recursive(
        st.booleans(),
        lambda x: st.lists(x, min_size=size).map(tuple),
        max_leaves=20))
    xs = minimal(trees, lambda x: len(x) >= size, timeout_after=None)
    assert len(xs) == size
Esempio n. 7
0
def test_can_find_quite_broad_lists():
    def breadth(x):
        if isinstance(x, list):
            return sum(map(breadth, x))
        else:
            return 1

    broad = find(st.recursive(st.booleans(), lambda x: st.lists(x, max_size=10)), lambda x: breadth(x) >= 20)
    assert breadth(broad) == 20
Esempio n. 8
0
def test_can_find_quite_deep_lists():
    def depth(x):
        if x and isinstance(x, list):
            return 1 + max(map(depth, x))
        else:
            return 1

    deep = find(st.recursive(st.booleans(), lambda x: st.lists(x, max_size=3)), lambda x: depth(x) >= 5)
    assert deep == [[[[False]]]]
Esempio n. 9
0
def test_can_generate_some_depth_with_large_branching():
    def depth(x):
        if x and isinstance(x, list):
            return 1 + max(map(depth, x))
        else:
            return 1

    xs = find(st.recursive(st.integers(), lambda x: st.lists(x, average_size=100)), lambda x: depth(x) > 1)
    assert xs == [0]
Esempio n. 10
0
def test_can_form_sets_of_recursive_data():
    trees = st.sets(st.recursive(
        st.booleans(),
        lambda x: st.lists(x, min_size=5).map(tuple),
        max_leaves=20))
    xs = find(trees, lambda x: len(x) >= 10, settings=settings(
        database=None, timeout=20, max_shrinks=1000, max_examples=1000
    ))
    assert len(xs) == 10
def nested_checkboxes_list():
    def create_options_with_children(list_strategy):
        return st.lists(nested_checkboxes(options_list_strategy=list_strategy))

    return st.recursive(
        st.lists(nested_checkboxes()),
        create_options_with_children,
        max_leaves=15
    )
Esempio n. 12
0
def not_test():
    """
    not_test: 'not' not_test | comparison
    """
    @st.composite
    def _not_test(draw, other_not_test):
        return 'not ' + draw(st.one_of(comparison(), other_not_test))

    return st.recursive(STRING(), _not_test)
Esempio n. 13
0
def comparison():
    """
    comparison: expr (comp_op expr)*
    """
    @st.composite
    def _comparison(draw, sub_expr):
        return _expr_builder(draw, not_test, 'and')

    return st.recursive(STRING(), _comparison)
Esempio n. 14
0
def test_drawing_many_near_boundary():
    ls = find(
        st.lists(st.recursive(
            st.booleans(),
            lambda x: st.lists(x, min_size=8, max_size=10).map(tuple),
            max_leaves=9)),
        lambda x: len(set(x)) >= 5,
        settings=settings(max_examples=10000, database=None, max_shrinks=2000)
    )
    assert len(ls) == 5
Esempio n. 15
0
def test_can_generate_some_depth_with_large_branching():
    def depth(x):
        if x and isinstance(x, list):
            return 1 + max(map(depth, x))
        else:
            return 1
    xs = minimal(
        st.recursive(st.integers(), st.lists),
        lambda x: depth(x) > 1,
        timeout_after=None,
    )
    assert xs in ([0], [[]])
Esempio n. 16
0
def test_broad_recursive_data_will_fail_a_health_check():
    r = st.recursive(
        st.integers(), lambda s: st.tuples(*((s,) * 10)),
        max_leaves=10,
    )

    @given(st.tuples(r, r, r, r, r, r, r))
    def test(x):
        pass

    with raises(FailedHealthCheck):
        test()
Esempio n. 17
0
def test_can_form_sets_of_recursive_data():
    trees = st.sets(st.recursive(
        st.booleans(),
        lambda x: st.lists(x, min_size=5).map(tuple),
        max_leaves=10))
    xs = find(trees, lambda x: len(x) >= 10, settings=settings(
        database=None
    ))
    print(xs)
    assert len(xs) == 10
    assert False in xs
    assert True in xs
def mutated_commands(commands):
    args = st.sampled_from([b'withscores', b'xx', b'nx', b'ex', b'px', b'weights', b'aggregate',
                            b'', b'0', b'-1', b'nan', b'inf', b'-inf']) | command_args(commands)
    affixes = st.sampled_from([b'\0', b'-', b'+', b'\t', b'\n', b'0000']) | st.binary()
    return st.recursive(
        commands,
        lambda x:
            delete_arg(x)
            | replace_arg(x, args)
            | uppercase_arg(x)
            | prefix_arg(x, affixes)
            | suffix_arg(x, affixes)
            | add_arg(x, args)
            | swap_args(x))
Esempio n. 19
0
def test_can_generate_with_large_branching():
    def flatten(x):
        if isinstance(x, list):
            return sum(map(flatten, x), [])
        else:
            return [x]

    xs = find(
        st.recursive(
            st.integers(), lambda x: st.lists(x, average_size=50),
            max_leaves=100),
        lambda x: isinstance(x, list) and len(flatten(x)) >= 50
    )
    assert flatten(xs) == [0] * 50
def monoids():
    base = many_one_of(
        strat.integers(),
        strat.lists(strat.integers()),
        strat.lists(strat.integers()).map(tuple),
        strat.text(),
        strat.integers().map(MonoidProduct),
        strat.dictionaries(strat.integers(), strat.integers()),
    )

    def recurse(substrat):
        return stream_apply_strat(maybes(), substrat)

    return strat.recursive(base, recurse)
Esempio n. 21
0
def test_drawing_many_near_boundary():
    target = 4

    ls = minimal(
        st.lists(st.recursive(
            st.booleans(),
            lambda x: st.lists(
                x, min_size=2 * (target - 1), max_size=2 * target
            ).map(tuple),
            max_leaves=2 * target - 1)),
        lambda x: len(set(x)) >= target,
        timeout_after=None
    )
    assert len(ls) == target
Esempio n. 22
0
def nested_dtypes(subtype_strategy=scalar_dtypes(),
                  max_leaves=10, max_itemsize=None):
    """Return the most-general dtype strategy.

    Elements drawn from this strategy may be simple (from the
    subtype_strategy), or several such values drawn from
    :func:`array_dtypes` with ``allow_subarrays=True``. Subdtypes in an
    array dtype may be nested to any depth, subject to the max_leaves
    argument.

    """
    return st.recursive(subtype_strategy,
                        lambda x: array_dtypes(x, allow_subarrays=True),
                        max_leaves).filter(
        lambda d: max_itemsize is None or d.itemsize <= max_itemsize)
Esempio n. 23
0
def action_structures(draw):
    """
    A Hypothesis strategy that creates a tree of L{ActionStructure} and
    L{unicode}.
    """
    tree = draw(st.recursive(labels, st.lists, max_leaves=50))

    def to_structure(tree_or_message):
        if isinstance(tree_or_message, list):
            return ActionStructure(
                type=draw(labels),
                failed=draw(st.booleans()),
                children=[to_structure(o) for o in tree_or_message])
        else:
            return tree_or_message
    return to_structure(tree)
def test_can_find_quite_broad_lists():
    def breadth(x):
        if isinstance(x, list):
            return sum(map(breadth, x))
        else:
            return 1

    target = 10

    broad = minimal(
        st.recursive(st.booleans(), lambda x: st.lists(x, max_size=target // 2)),
        lambda x: breadth(x) >= target,
        settings=settings(max_examples=10000),
        timeout_after=None,
    )
    assert breadth(broad) == target
Esempio n. 25
0
def test_can_use_recursive_data_in_sets(rnd):
    nested_sets = st.recursive(st.booleans(), lambda js: st.frozensets(js), max_leaves=10)
    nested_sets.example()

    def flatten(x):
        if isinstance(x, bool):
            return frozenset((x,))
        else:
            result = frozenset()
            for t in x:
                result |= flatten(t)
                if len(result) == 2:
                    break
            return result

    x = find(nested_sets, lambda x: len(flatten(x)) == 2, random=rnd, settings=Settings(database=None))
    assert x == frozenset((False, True))
Esempio n. 26
0
def test_can_generate_with_large_branching():
    def flatten(x):
        if isinstance(x, list):
            return sum(map(flatten, x), [])
        else:
            return [x]

    size = 20

    xs = minimal(
        st.recursive(
            st.integers(), lambda x: st.lists(x, min_size=size // 2),
            max_leaves=size * 2),
        lambda x: isinstance(x, list) and len(flatten(x)) >= size,
        timeout_after=None,
    )
    assert flatten(xs) == [0] * size
Esempio n. 27
0
def factor():
    """
    factor: ('+'|'-'|'~') factor | atom trailer*
    """
    @st.composite
    def _multi_atom_trailer(draw):
        result = [draw(atom())]
        iter_count = range(draw(st.integers(1, 5)))
        result += [draw(trailer()) for _ in iter_count]
        return ' '.join(result)

    @st.composite
    def _factor(draw, other_factor):
        unary = st.sampled_from('+-~ ')
        strategy = st.one_of(other_factor, _multi_atom_trailer())
        return unary + draw(strategy)

    return st.recursive(st.just(''), _factor)
Esempio n. 28
0
def test_can_flatmap_to_recursive_data(rnd):
    stuff = st.lists(st.integers(), min_size=1).flatmap(
        lambda elts: st.recursive(st.sampled_from(elts), lambda x: st.lists(x, average_size=25), max_leaves=25)
    )

    def flatten(x):
        if isinstance(x, integer_types):
            return [x]
        else:
            return sum(map(flatten, x), [])

    tree = find(
        stuff,
        lambda x: sum(flatten(x)) >= 100,
        settings=Settings(database=None, max_shrinks=1000, max_examples=1000),
        random=rnd,
    )
    flat = flatten(tree)
    assert (sum(flat) == 1000) or (len(set(flat)) == 1)
def test_can_use_recursive_data_in_sets(rnd):
    nested_sets = st.recursive(st.booleans(), st.frozensets, max_leaves=3)
    find_any(nested_sets, random=rnd)

    def flatten(x):
        if isinstance(x, bool):
            return frozenset((x,))
        else:
            result = frozenset()
            for t in x:
                result |= flatten(t)
                if len(result) == 2:
                    break
            return result

    assert rnd is not None
    x = minimal(nested_sets, lambda x: len(flatten(x)) == 2, random=rnd)
    assert x in (
        frozenset((False, True)),
        frozenset((False, frozenset((True,)))),
        frozenset((frozenset((False, True)),)),
    )
Esempio n. 30
0
    assert headers_to_scrapy([('Content-Type', 'text/html')]) == html_headers
    assert headers_to_scrapy([{'name': 'Content-Type', 'value': 'text/html'}]) == html_headers


_primitive = (
    st.floats(allow_infinity=False, allow_nan=False) |
    st.booleans() |
    st.text() |
    st.none() |
    st.integers()
)
_data = st.recursive(_primitive,
    lambda children: (
        children |
        st.lists(children) |
        st.tuples(children) |
        st.dictionaries(st.text(), children) |
        st.tuples(st.just('h'), children)
    ),
    max_leaves=5,
)
_data_notuples = st.recursive(_primitive,
    lambda children: (
        children |
        st.lists(children) |
        st.dictionaries(st.text(), children)
    ),
    max_leaves=5,
)


@given(_data, _data)