Esempio n. 1
0
def test_will_use_instance_if_no_static():
    f = ExtMethod()

    @f.extend(object)
    def foo(x):
        return x

    assert f(int) == int
Esempio n. 2
0
def test_can_add_static():
    f = ExtMethod()

    @f.extend_static(object)
    def fs(x):
        return 1

    @f.extend_static(int)
    def fi(x):
        return 2

    assert f(object) == 1
    assert f(int) == 2
    assert f(str) == 1
Esempio n. 3
0
def test_will_use_tightest_class():
    f = ExtMethod()

    @f.extend(object)
    def foo(x):
        return 0

    @f.extend(int)
    def bar(x):
        return 1

    assert f(object()) == 0
    assert f(u'') == 0
    assert f(10) == 1
Esempio n. 4
0

@strategy.extend(TemplatesFor)
def templates_for_strategy(specifier, settings):
    return TemplatesStrategy(strategy(specifier.base, settings))


def templates_for(strat):
    return TemplatesStrategy(strategy(strat))


class Rejected(Exception):
    pass


mess_with_basic_data = ExtMethod()


def mutate_basic(basic, random):
    if not random.randint(0, 2):
        if isinstance(basic, text_type):
            return list(basic)
        elif isinstance(basic, integer_types):
            try:
                return float(basic)
            except OverflowError:
                return -basic
        else:
            return text_type(repr(basic))
    return mess_with_basic_data(basic, random)
Esempio n. 5
0
# full list of people who may hold copyright, and consult the git log if you
# need to determine who owns an individual contribution.

# 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 http://mozilla.org/MPL/2.0/.

# END HEADER

from __future__ import division, print_function, absolute_import

from hypothesis.specifiers import IntegerRange
from hypothesis.internal.compat import integer_types
from hypothesis.utils.extmethod import ExtMethod

matches_type = ExtMethod()


@matches_type.extend(type)
def type_matches(typ, value):
    return isinstance(value, typ)


@matches_type.extend(tuple)
def tuple_matches(tup, value):
    if not isinstance(value, type(tup)):
        return False
    if len(tup) != len(value):
        return False
    return all(matches_type(t, v) for t, v in zip(tup, value))
Esempio n. 6
0

@strategy.extend(TemplatesFor)
def templates_for_strategy(specifier, settings):
    return TemplatesStrategy(strategy(specifier.base, settings))


def templates_for(strat):
    return TemplatesStrategy(strategy(strat))


class Rejected(Exception):
    pass


mess_with_basic_data = ExtMethod()


def mutate_basic(basic, random):
    if not random.randint(0, 2):
        if isinstance(basic, text_type):
            return list(basic)
        elif isinstance(basic, integer_types):
            try:
                return float(basic)
            except OverflowError:
                return -basic
        else:
            return text_type(repr(basic))
    return mess_with_basic_data(basic, random)
Esempio n. 7
0
def test_will_error_on_missing():
    f = ExtMethod()
    with pytest.raises(NotImplementedError):
        f(1)
Esempio n. 8
0
# https://github.com/DRMacIver/hypothesis/blob/master/CONTRIBUTING.rst for a
# full list of people who may hold copyright, and consult the git log if you
# need to determine who owns an individual contribution.

# 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 http://mozilla.org/MPL/2.0/.

# END HEADER

from __future__ import division, print_function, absolute_import, \
    unicode_literals

from hypothesis.utils.extmethod import ExtMethod

executor = ExtMethod()


def default_executor(function):
    return function()


def setup_teardown_executor(setup, teardown):
    setup = setup or (lambda: None)
    teardown = teardown or (lambda ex: None)

    def execute(function):
        token = None
        try:
            token = setup()
            return function()