Ejemplo n.º 1
0
def test_dispatch_method__list_of():
    f = Dispatcher('f')

    @f.register(list_of(int))
    def rev(x):
        return x[::-1]

    @f.register(int, int)
    def add(x, y):
        return x + y

    class MyList(list):
        pass

    assert f.dispatch(list_of(int)) is rev
    assert f.dispatch(MyList) is None # MyList is considered by list_of() as an independent type
    assert f.dispatch(int, int) is add
Ejemplo n.º 2
0
def test_register_stacking__list_of__tuple_of():
    f = Dispatcher('f')

    @f.register(list_of(int))
    @f.register(tuple_of(int))
    def rev(x):
        return x[::-1]

    assert f((1, 2, 3)) == (3, 2, 1)
    assert f([1, 2, 3]) == [3, 2, 1]

    assert raises(UnavailableSignatureError, lambda: f('hello'))
    assert rev('hello') == 'olleh'
Ejemplo n.º 3
0
def test_source_copy():
    def one(x, y):
        """ Docstring number one """
        return x + y

    def two(x, y):
        """ Docstring number two """
        return x - y

    master_doc = 'Doc of the multimethod itself'

    f = Dispatcher('f', doc=master_doc)
    f.add((int, int), one)
    f.add((float, float), two)

    assert 'x + y' in f._source(1, 1)
    assert 'x - y' in f._source(1.0, 1.0)