test_can_produce_floats_near_right = define_test(
    descriptors.floats_in_range(0, 1), 0.1, lambda t: t > 0.8)

test_can_produce_floats_in_middle = define_test(
    descriptors.floats_in_range(0, 1), 0.3, lambda t: 0.2 <= t <= 0.8)

test_can_produce_long_lists = define_test([int], 0.5, long_list)

test_can_produce_short_lists = define_test([int], 0.2, lambda x: len(x) <= 10)

test_can_produce_lists_bunched_near_left = define_test(
    [descriptors.floats_in_range(0, 1)],
    0.1,
    lambda ts: all(t < 0.2 for t in ts),
    condition=long_list,
)

test_can_produce_lists_bunched_near_right = define_test(
    [descriptors.floats_in_range(0, 1)],
    0.1,
    lambda ts: all(t > 0.8 for t in ts),
    condition=long_list,
)

test_can_produce_the_same_int_twice = define_test(
    ([int], int), 0.05, lambda t: len([x for x in t[0] if x == t[1]]) > 1)

test_non_empty_subset_of_two_is_usually_large = define_test(
    {descriptors.sampled_from((1, 2))}, 0.6, lambda t: len(t) == 2)
Beispiel #2
0
 ((int, ), (1, )),
 (complex, complex(1, 1)),
 ({int}, {1}),
 (text_type, ''),
 (binary_type, b''),
 (Random, RandomWithSeed(1)),
 (Just(frozenset({False})), frozenset({False})),
 (({str}, bool), (set(), True)),
 ({
     '\x9aLLLL\x1c': {bool}
 }, {
     '\x9aLLLL\x1c': {False}
 }),
 (one_of([int, str]), 1),
 ([[{int}], [[{int}]]], [[[]]]),
 (sampled_from(elements=(1, )), 1),
 (one_of(({
     1: int
 }, {
     1: bool
 })), {
     1: 2
 }),
 (one_of(({
     1: int
 }, {
     1: bool
 })), {
     1: False
 }),
 ({float}, {0.0460563451184767, -0.19420794805570227}),
Beispiel #3
0
    assume(y in xs)
    xs.remove(y)
    assert y not in xs


def test_errors_even_if_does_not_error_on_final_call():
    @given(int)
    def rude(x):
        assert not any(t[3] == 'falsify'
                       for t in inspect.getouterframes(inspect.currentframe()))

    with pytest.raises(Flaky):
        rude()


@given(set([sampled_from(list(range(10)))]))
def test_can_test_sets_sampled_from(xs):
    assert all(isinstance(x, int) for x in xs)
    assert all(0 <= x < 10 for x in xs)


mix = one_of((sampled_from([1, 2, 3]), str))


@fails
@given(mix, mix)
def test_can_mix_sampling_with_generating(x, y):
    assert type(x) == type(y)


@fails
Beispiel #4
0
all_test_classes = [
    A, AA, AAA, AAB, AB, ABA, ABB, B, BA, BAA, BAB, BB, BBA, BBB
]


def find_most_specific(x, classes):
    best = None
    for c in classes:
        if isinstance(x, c):
            if best is None or issubclass(c, best):
                best = c
    return best


@given({sampled_from(all_test_classes)}, random.Random)
def test_chooses_most_specific_subclass(classes, r):
    classes = list(classes)
    random.shuffle(classes)
    classes = tuple(classes)
    mapper = SpecificationMapper()
    for i in hrange(len(classes)):
        mapper.define_specification_for_instances(
            classes[i],
            const(i),
        )
    for c in all_test_classes:
        if issubclass(c, classes):
            x = c()
            correct_choice = find_most_specific(x, classes)
            i = mapper.specification_for(x)
Beispiel #5
0
def test_has_strategy_for_samples():
    table = st.StrategyTable.default()
    sampling = descriptors.sampled_from([1, 2, 3])
    assert table.has_specification_for(sampling)
    assert table.has_specification_for([sampling])
    assert table.has_specification_for(set([sampling]))