Exemplo n.º 1
0
def test_map_ancestor():
    m = Map()

    a = MapKey('a')
    b = MapKey('b', parents=[a])

    m = Map()
    m[a] = u'Value for A'
    assert m[b] == u'Value for A'
Exemplo n.º 2
0
def singledispatch(func):
    registry = Map()

    def dispatch(typ):
        """generic_func.dispatch(type) -> <function implementation>

        Runs the dispatch algorithm to return the best available implementation
        for the given `type` registered on `generic_func`.

        """
        return registry[ClassMapKey(typ)]

    def register(typ, func=None):
        """generic_func.register(type, func) -> func

        Registers a new implementation for the given `type` on a
        `generic_func`.
        """
        if func is None:
            return lambda f: register(typ, f)
        registry[ClassMapKey(typ)] = func
        return func

    def wrapper(*args, **kw):
        return dispatch(args[0].__class__)(*args, **kw)

    registry[ClassMapKey(object)] = func
    wrapper.register = register
    wrapper.dispatch = dispatch
    update_wrapper(wrapper, func)
    return wrapper
Exemplo n.º 3
0
def test_map_deletion():
    m = Map()
    a = MapKey('a')
    m[a] = u'Value for A'
    del m[a]
    with pytest.raises(KeyError):
        m[a]
Exemplo n.º 4
0
def test_map_exact_get():
    m = Map()
    a = MapKey('a')
    b = MapKey('b', parents=[a])

    m[a] = u"Value for A"

    assert m.exact_get(b) is None
    assert m.exact_get(b, u'default') == u'default'
    assert m.exact_get(a) == u'Value for A'
Exemplo n.º 5
0
def test_map_exact_getitem():
    m = Map()
    a = MapKey('a')
    b = MapKey('b', parents=[a])

    m[a] = u"Value for A"

    with pytest.raises(KeyError):
        m.exact_getitem(b)
    assert m.exact_getitem(a) == u'Value for A'
Exemplo n.º 6
0
def test_map_parent():
    m = Map()
    a = MapKey('a')
    b = MapKey('b', parents=[a])
    c = MapKey('c', parents=[a])
    m[b] = u'Value for B'
    assert m[b] == u'Value for B'
    with pytest.raises(KeyError):
        m[c]
    with pytest.raises(KeyError):
        m[a]
Exemplo n.º 7
0
def test_map_all_empty():
    m = Map()
    a = MapKey('a')
    b = MapKey('b', parents=[a])
    c = MapKey('c', parents=[a])
    d = MapKey('d', parents=[b, c])

    m[b] = u'Value for B'
    m[c] = u'Value for C'
    m[d] = u'Value for D'
    assert list(m.all(d)) == [u'Value for D', u'Value for B', u'Value for C']
Exemplo n.º 8
0
def test_map_ancestor_mro2():
    m = Map()
    a = MapKey('a')
    b = MapKey('b', parents=[a])
    c = MapKey('c', parents=[a])
    d = MapKey('d', parents=[b, c])

    m[c] = u'Value for C'

    # now we do get C
    assert m[d] == u'Value for C'
Exemplo n.º 9
0
def test_map_ancestor_mro():
    m = Map()
    a = MapKey('a')
    b = MapKey('b', parents=[a])
    c = MapKey('c', parents=[a])
    d = MapKey('d', parents=[b, c])

    m[b] = u'Value for B'
    m[c] = u'Value for C'

    # b comes first in mro
    assert m[d] == u'Value for B'
Exemplo n.º 10
0
def test_map_ancestor_direct_key_wins():
    m = Map()
    a = MapKey('a')
    b = MapKey('b', parents=[a])
    c = MapKey('c', parents=[a])
    d = MapKey('d', parents=[b, c])

    m[b] = u'Value for B'
    m[c] = u'Value for C'
    m[d] = u'Value for D'

    assert m[d] == u'Value for D'
Exemplo n.º 11
0
def test_map_same_underlying_key_is_same():
    m = Map()
    a = MapKey('a')
    a_another = MapKey('a')
    m[a] = u'Value for A'
    assert m[a_another] == u'Value for A'
Exemplo n.º 12
0
def test_map_get_default():
    m = Map()
    a = MapKey('a')
    assert m.get(a, 'default') == 'default'
Exemplo n.º 13
0
def test_map_get():
    m = Map()
    a = MapKey('a')
    m[a] = u'Value for A'
    assert m.get(a) == u'Value for A'
Exemplo n.º 14
0
def test_map_simple_key():
    m = Map()
    a = MapKey('a')
    m[a] = u'Value for A'
    assert m[a] == u'Value for A'