File: test_pretty_repr.py

package info (click to toggle)
python-hypothesis 6.138.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,272 kB
  • sloc: python: 62,853; ruby: 1,107; sh: 253; makefile: 41; javascript: 6
file content (116 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

from collections import OrderedDict

import pytest

from hypothesis import given, strategies as st
from hypothesis.control import reject
from hypothesis.errors import HypothesisDeprecationWarning, InvalidArgument


def foo(x):
    pass


def bar(x):
    pass


def baz(x):
    pass


fns = [foo, bar, baz]


def builds_ignoring_invalid(target, *args, **kwargs):
    def splat(value):
        try:
            result = target(*value[0], **value[1])
            result.validate()
            return result
        except (HypothesisDeprecationWarning, InvalidArgument):
            reject()

    return st.tuples(st.tuples(*args), st.fixed_dictionaries(kwargs)).map(splat)


size_strategies = {
    "min_size": st.integers(min_value=0, max_value=100),
    "max_size": st.integers(min_value=0, max_value=100) | st.none(),
}

values = st.integers() | st.text()


Strategies = st.recursive(
    st.one_of(
        st.sampled_from(
            [
                st.none(),
                st.booleans(),
                st.randoms(use_true_random=True),
                st.complex_numbers(),
                st.randoms(use_true_random=True),
                st.fractions(),
                st.decimals(),
            ]
        ),
        st.builds(st.just, values),
        st.builds(st.sampled_from, st.lists(values, min_size=1)),
        builds_ignoring_invalid(st.floats, st.floats(), st.floats()),
    ),
    lambda x: st.one_of(
        builds_ignoring_invalid(st.lists, x, **size_strategies),
        builds_ignoring_invalid(st.sets, x, **size_strategies),
        builds_ignoring_invalid(lambda v: st.tuples(*v), st.lists(x)),
        builds_ignoring_invalid(lambda v: st.one_of(*v), st.lists(x, min_size=1)),
        builds_ignoring_invalid(
            st.dictionaries,
            x,
            x,
            dict_class=st.sampled_from([dict, OrderedDict]),
            **size_strategies,
        ),
        st.builds(lambda s, f: s.map(f), x, st.sampled_from(fns)),
    ),
)


strategy_globals = {k: getattr(st, k) for k in dir(st)}

strategy_globals["OrderedDict"] = OrderedDict
strategy_globals["inf"] = float("inf")
strategy_globals["nan"] = float("nan")
strategy_globals["foo"] = foo
strategy_globals["bar"] = bar
strategy_globals["baz"] = baz


@given(Strategies)
def test_repr_evals_to_thing_with_same_repr(strategy):
    r = repr(strategy)
    via_eval = eval(r, strategy_globals)
    r2 = repr(via_eval)
    assert r == r2


@pytest.mark.parametrize(
    "r",
    [
        "none().filter(foo).map(bar)",
        "just(1).filter(foo).map(bar)",
        "sampled_from([1, 2, 3]).filter(foo).map(bar)",
    ],
)
def test_sampled_transform_reprs(r):
    assert repr(eval(r, strategy_globals)) == r