Esempio n. 1
0
def test_can_falsify_complex_numbers():
    falsify(lambda x: x == (x**2)**0.5, complex)

    with pytest.raises(Unfalsifiable):
        falsify(
            lambda x, y: (x * y).conjugate() == x.conjugate() * y.conjugate(),
            complex, complex)
Esempio n. 2
0
def test_can_produce_long_mixed_lists_with_only_a_subset():
    def is_good(xs):
        if len(xs) < 20: return True 
        if any((isinstance(x, int) for x in xs)): return True
        return False
        
    falsify(is_good, [int,str])
Esempio n. 3
0
def test_can_produce_long_mixed_lists_with_only_a_subset():
    def is_good(xs):
        if len(xs) < 20: return True
        if any((isinstance(x, int) for x in xs)): return True
        return False

    falsify(is_good, [int, str])
Esempio n. 4
0
def test_can_go_deep_into_recursive_descriptors():
    foo = [str]
    foo.append(foo)
    def depth(x):
        if isinstance(x, str): return 1
        elif not x: return 0
        else: return max(map(depth, x)) + 1
    falsify(lambda x: depth(x) <= 5, foo)
Esempio n. 5
0
def test_can_falsify_complex_numbers():
    falsify(lambda x: assume(abs(x) <= 1000) and x == (x**2)**0.5, complex)

    with pytest.raises(Unfalsifiable):
        falsify(
            lambda x, y: actually_equal((x * y).conjugate(),
                                        x.conjugate() * y.conjugate()),
            complex, complex)
Esempio n. 6
0
def test_can_generate_non_ascii():
    def is_ascii(s):
        try:
            s.encode('ascii')
            return True
        except UnicodeEncodeError:
            return False

    falsify(is_ascii, text_type)
Esempio n. 7
0
def test_can_produce_things_that_are_not_utf8():
    def is_utf8(x):
        try:
            x.decode('utf-8')
            return True
        except UnicodeDecodeError:
            return False

    falsify(is_utf8, binary_type)
Esempio n. 8
0
def test_detects_flaky_failure():
    calls = [0]

    def flaky(x):
        result = calls != [0]
        calls[0] = 1
        return result

    with pytest.raises(Flaky):
        falsify(flaky, int)
Esempio n. 9
0
def test_does_not_call_twice_with_same_passing_parameter():
    calls = [0]

    def count_calls(x):
        calls[0] += 1
        return True

    with pytest.raises(Exhausted):
        falsify(count_calls, bool)
    assert calls == [2]
Esempio n. 10
0
def test_can_produce_long_mixed_lists_with_only_a_subset():
    def short_or_includes(t):
        def is_good(xs):
            if len(xs) < 20:
                return True
            return any(isinstance(x, t) for x in xs)

        return is_good

    falsify(short_or_includes(text_type), [int, text_type])
    falsify(short_or_includes(int), [int, text_type])
Esempio n. 11
0
def test_gravitates_towards_good_parameter_values():
    good_value_counts = [0]
    all_value_counts = [0]

    def just_being_awkward(xs):
        assume(len(xs) >= 10)
        all_value_counts[0] += 1
        assume(all(x >= 0 for x in xs))
        good_value_counts[0] += 1
        return True

    with pytest.raises(Unfalsifiable):
        falsify(just_being_awkward, [float])

    assert all_value_counts[0] >= 300
    assert good_value_counts[0] >= 0.6 * all_value_counts[0]
Esempio n. 12
0
def test_can_falsify_tuples():
    def out_of_order_positive_tuple(x):
        a,b = x
        assume(a > 0 and b > 0)
        assert a >= b
        return True
    assert falsify(out_of_order_positive_tuple, (int,int))[0] == (1,2)
Esempio n. 13
0
def test_can_falsify_methods_which_mutate_data_without_proving_flaky():
    def pop_single(xs):
        if len(xs) == 1:
            xs.pop()
            return False
        return True

    assert falsify(pop_single, [int]) == ([0], )
Esempio n. 14
0
def test_can_falsify_tuples():
    def out_of_order_positive_tuple(x):
        a, b = x
        assume(a > 0 and b > 0)
        assert a >= b
        return True

    assert falsify(out_of_order_positive_tuple, (int, int))[0] == (1, 2)
Esempio n. 15
0
def test_can_falsify_string_commutativity():
    def commutes(x, y):
        return x + y == y + x

    non_commuting = falsify(commutes, text_type, text_type)
    x, y = sorted(non_commuting)
    assert x == '0'
    assert y == '1'
Esempio n. 16
0
def test_only_generates_valid_unicode():
    def is_valid(x):
        try:
            x.encode('utf-8')
            return True
        except UnicodeEncodeError:
            return False

    with pytest.raises(Unfalsifiable):
        print(falsify(is_valid, text_type))
Esempio n. 17
0
def test_can_falsify_dicts():
    def is_good(x):
        assume("foo" in x)
        assume("bar" in x)
        return x["foo"] < x["bar"]

    assert falsify(is_good, {
        "foo": int,
        "bar": int
    })[0] == {
        "foo": 0,
        "bar": 0
    }
Esempio n. 18
0
def test_can_falsify_dicts():
    def is_good(x):
        assume('foo' in x)
        assume('bar' in x)
        return x['foo'] < x['bar']

    assert falsify(is_good, {
        'foo': int,
        'bar': int
    })[0] == {
        'foo': 0,
        'bar': 0
    }
Esempio n. 19
0
def test_can_produce_and_minimize_long_lists_of_only_one_element():
    def is_a_monoculture(xs):
        assume(len(xs) >= 10)
        return len(set(xs)) > 1

    falsify(is_a_monoculture, [descriptors.integers_in_range(0, 10)])
Esempio n. 20
0
def test_can_falsify_with_true_boolean():
    assert falsify(lambda x: not x, bool)[0] is True
Esempio n. 21
0
def test_can_make_assumptions():
    def is_good(x):
        assume(x > 5)
        return x % 2 == 0

    assert falsify(is_good, int)[0] == 7
Esempio n. 22
0
def test_can_falsify_list_inclusion():
    assert falsify(lambda x,y: x not in y, int, [int]) == (0,[0])
Esempio n. 23
0
def test_can_falsify_empty_tuples():
    assert falsify(lambda x: False, ())[0] == ()
Esempio n. 24
0
def test_stops_loop_pretty_quickly():
    with pytest.raises(Unfalsifiable):
        falsify(lambda x: x == x, int)
Esempio n. 25
0
def test_falsifies_integer_keyed_dictionary():
    falsify(always_false, {1: int})
Esempio n. 26
0
def test_minimizes_strings_to_zeroes():
    assert falsify(lambda x: len(x) < 3, str)[0] == "000"
Esempio n. 27
0
def test_can_find_short_strings():
    assert falsify(lambda x: len(x) > 0, str)[0] == ""
    assert len(falsify(lambda x: len(x) <= 1, str)[0]) == 2
    assert falsify(lambda x : len(x) < 10, [str])[0] == [""] * 10
Esempio n. 28
0
def test_can_falsify_string_matching():
    # Note that just doing a match("foo",x) will never find a good solution
    # because the state space is too large
    assert falsify(lambda x: not re.search("a.*b",x), str)[0] == "ab"
Esempio n. 29
0
def test_can_make_assumptions():
    def is_good(x):
        assume(x > 5)
        return x % 2 == 0
    assert falsify(is_good, int)[0] == 7    
Esempio n. 30
0
def test_can_falsify_mixed_lists():
    xs = falsify(is_pure, [int,str])[0]
    assert len(xs) == 2
    assert 0 in xs
    assert "" in xs
Esempio n. 31
0
def test_can_produce_long_lists_with_floats_at_right():
    def is_small_given_large(xs):
        assume(len(xs) >= 15)
        return any(x <= 0.8 for x in xs)

    falsify(is_small_given_large, [descriptors.floats_in_range(0, 1)])
Esempio n. 32
0
def test_good_errors_on_bad_values():
    some_string = "I am the very model of a modern major general"
    with pytest.raises(MissingSpecification) as e:
        falsify(lambda x: False, some_string)

    assert some_string in e.value.args[0]
Esempio n. 33
0
def test_works_with_zero_arguments():
    with pytest.raises(Unfalsifiable):
        falsify(lambda: True)
    falsify(lambda: False)
Esempio n. 34
0
def test_can_falsify_bools():
    assert falsify(lambda x: x, bool)[0] == False
Esempio n. 35
0
def test_can_falsify_alternating_types():
    falsify(lambda x: isinstance(x, int), one_of([int, str]))[0] == ""
Esempio n. 36
0
def test_can_falsify_lists_of_bools():
    falsify(lambda x : len([y for y in x if not y]) <= 5, [bool])
Esempio n. 37
0
def test_can_falsify_set_inclusion():
    assert falsify(lambda x,y: x not in y, int, {int}) == (0,{0})
Esempio n. 38
0
def test_can_falsify_empty_tuples():
    assert falsify(lambda x: False, ())[0] == ()
Esempio n. 39
0
def test_can_falsify_named_tuples():
    pair = falsify(lambda x: x.kitten1 < x.kitten2,
                   Litter(text_type, text_type))[0]
    assert isinstance(pair, Litter)
    assert pair == Litter('', '')
Esempio n. 40
0
def test_can_find_unsorted_lists():
    unsorted = falsify(lambda x: sorted(x) == x, [int])[0] 
    assert unsorted == [1,0] or unsorted == [0,-1]
Esempio n. 41
0
def test_raises_on_unsatisfiable_assumption():
    with pytest.raises(Unsatisfiable):
        falsify(lambda x: assume(False), int)
Esempio n. 42
0
def test_can_falsify_long_lists():
    assert falsify(lambda x: len(x) < 20, [int],warming_rate=0.5)[0] == [0] * 20 
Esempio n. 43
0
def test_can_falsify_sets():
    assert falsify(lambda x: not x, {int})[0] == {0}
Esempio n. 44
0
def test_can_randomize_random():
    falsify(lambda x: x.randint(0, 10) != 10, Random)
Esempio n. 45
0
def test_falsification_contains_function_string():
    with pytest.raises(Unfalsifiable) as e:
        assert falsify(lambda x: True, int)
    assert 'lambda x: True' in e.value.args[0]
Esempio n. 46
0
def test_can_find_negative_floats():
    assert falsify(lambda x : x > -1.0, float)[0] == -1.0
Esempio n. 47
0
def test_can_falsify_string_commutativity():
    assert tuple(sorted(falsify(lambda x,y: x + y == y + x,str,str))) == ('0','1')
Esempio n. 48
0
def test_can_falsify_dicts():
    def is_good(x):
        assume("foo" in x)
        assume("bar" in x) 
        return x["foo"] < x["bar"]
    assert falsify(is_good, {"foo": int, "bar" : int})[0] == {"foo" : 0, "bar" : 0}
Esempio n. 49
0
def test_can_falsify_int_pairs():
    assert falsify(lambda x,y: x > y, int,int) == (0,0)
Esempio n. 50
0
def test_can_falsify_assertions():
    def is_good(x):
        assert x < 3
        return True
    assert falsify(is_good, int)[0] == 3
Esempio n. 51
0
def test_minimizes_to_empty(desc):
    x = falsify(always_false, desc)[0]
    s = StrategyTable.default().strategy(desc)
    assert not list(s.simplify(x))
Esempio n. 52
0
def test_can_falsify_lists():
    assert falsify(lambda x: len(x) < 3, [int])[0] == [0] * 3
Esempio n. 53
0
def test_falsifies_sets_of_union_types():
    assert falsify(always_false,
                   {one_of([text_type, binary_type])})[0] == set()
Esempio n. 54
0
def test_can_falsify_ints():
    assert falsify(lambda x: x != 0, int)[0] == 0
Esempio n. 55
0
def test_can_find_an_element_in_a_list():
    falsify(lambda x, ys: x not in ys, int, [int])
Esempio n. 56
0
def test_can_falsify_types_without_minimizers():
    assert isinstance(falsify(lambda x: False, Foo)[0], Foo)
Esempio n. 57
0
def test_can_produce_deep_binary_trees():
    falsify(lambda x: x.depth() <= 2, BinaryTree)
Esempio n. 58
0
def test_can_find_negative_ints():
    assert falsify(lambda x: x >= 0, int)[0] == -1 
Esempio n. 59
0
def test_can_falsify_types_without_minimizers():
    assert isinstance(falsify(lambda x: False, Foo)[0], Foo)
Esempio n. 60
0
def test_can_falsify_named_tuples():
    pair = falsify(lambda x: x.kitten1 < x.kitten2, Litter(str,str))[0]
    assert isinstance(pair,Litter)
    assert pair == Litter("","")