Example #1
0
def test_registry_sources():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    class LineCount(object):
        pass

    reg.register(LineCount, (Document,), 'document line count')
    reg.register(LineCount, (SpecialDocument,), 'special document line count')

    assert (reg.component(LineCount, (Document(),)) ==
            'document line count')

    assert (reg.component(LineCount, (SpecialDocument(),)) ==
            'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(LineCount, (AnotherDocument(),)) ==
            'document line count')

    class Other(object):
        pass

    assert reg.component(LineCount, (Other(),), default=None) is None
Example #2
0
def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class LineCount(object):
        pass

    class SpecialLineCount(LineCount):
        pass

    class SpecialDocument(Document):
        pass

    reg.register(LineCount, (Document,), 'line count')
    reg.register(SpecialLineCount, (Document,), 'special line count')

    assert reg.component(LineCount, (Document(),)) == 'line count'
    assert (reg.component(SpecialLineCount, (Document(),)) ==
            'special line count')

    assert reg.component(LineCount, (SpecialDocument(),)) == 'line count'
    assert (reg.component(SpecialLineCount, (SpecialDocument(),)) ==
            'special line count')
Example #3
0
def test_matcher_inheritance():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    class DocumentMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 1:
                return 'normal'
            else:
                return 'special'

    class SpecialDocumentMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 2:
                return 'extra normal'
            else:
                return None

    reg.register(linecount, [Document], DocumentMatcher())
    reg.register(linecount, [SpecialDocument], SpecialDocumentMatcher())

    assert reg.component(linecount, [Document(1)]) == 'normal'
    assert reg.component(linecount, [Document(2)]) == 'special'
    assert reg.component(linecount, [SpecialDocument(1)]) == 'normal'
    assert reg.component(linecount, [SpecialDocument(2)]) == 'extra normal'
    assert reg.component(linecount, [SpecialDocument(3)]) == 'special'
Example #4
0
def test_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    class LineCount(object):
        pass

    class SpecialLineCount(LineCount):
        pass

    class Matcher(IMatcher):
        def __init__(self, func, value):
            self.func = func
            self.value = value

        def __call__(self, *args, **kw):
            if self.func(*args, **kw):
                return self.value
            else:
                return None

    reg.register(LineCount, (Document,),
                 Matcher(lambda doc: doc.id == 1, 'line count'))

    reg.register(SpecialLineCount, (Document,),
                 Matcher(lambda doc: doc.id != 1, 'special line count'))

    assert reg.component(LineCount, (Document(1),)) == 'line count'
    assert reg.component(LineCount, (Document(2),)) == 'special line count'
Example #5
0
def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    def special_linecount(obj):
        pass

    reg.register(linecount, [Document], 'line count')
    reg.register(special_linecount, [Document], 'special line count')

    assert reg.component(linecount, [Document()]) == 'line count'
    assert (reg.component(special_linecount,
                          [Document()]) == 'special line count')

    assert reg.component(linecount, [SpecialDocument()]) == 'line count'
    assert (reg.component(special_linecount,
                          [SpecialDocument()]) == 'special line count')
Example #6
0
def test_predicate_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    def foo(obj):
        pass

    predicates = [Predicate("a", KeyIndex, lambda doc: doc.a, "A"), Predicate("b", KeyIndex, lambda doc: doc.b, "B")]

    matcher = PredicateMatcher(predicates)
    matcher.register({"a": "A"}, "a = A")
    matcher.register({"b": "B"}, "b = B")
    matcher.register({}, "nothing matches")

    reg.register(foo, [Document], matcher)

    assert reg.component(foo, [Document(a="A", b="C")]) == "a = A"
    assert reg.component(foo, [Document(a="C", b="B")]) == "b = B"
    assert reg.component(foo, [Document(a="A", b="B")]) == "a = A"
    assert reg.component(foo, [Document(a="C", b="C")]) == "nothing matches"

    # we can also override lookup by supplying our own predicates
    assert reg.component(foo, [Document(a="C", b="C")], predicates={"a": "A", "b": "C"}) == "a = A"
    # if we don't supply something in predicates ourselves, a default will
    # be used
    assert reg.component(foo, [Document(a="C", b="C")], predicates={"a": "C"}) == "b = B"
Example #7
0
def test_call_two_sources():
    reg = Registry()

    @generic
    def target(a, b):
        pass

    class Adapted(object):
        def __init__(self, alpha, beta):
            self.alpha = alpha
            self.beta = beta

        def foo(self):
            pass

    reg.register(target, [IAlpha, IBeta], Adapted)

    alpha = Alpha()
    beta = Beta()
    adapted = reg.call(target, [alpha, beta])

    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta

    adapted = target(alpha, beta, lookup=reg)
    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta
Example #8
0
def test_registry_target_find_specific():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    def special_linecount(obj):
        pass

    reg.register(linecount, [Document], 'line count')
    reg.register(special_linecount, [Document], 'special line count')

    assert reg.component(linecount, [Document()]) == 'line count'
    assert (reg.component(special_linecount, [Document()]) ==
            'special line count')

    assert reg.component(linecount, [SpecialDocument()]) == 'line count'
    assert (reg.component(special_linecount, [SpecialDocument()]) ==
            'special line count')
Example #9
0
def test_component_inheritance_old_style_class():
    reg = Registry()
    foo = object()

    class Gamma:
        pass

    class Delta(Gamma):
        pass

    @generic
    def target():
        pass

    reg.register(target, [Gamma], foo)

    gamma = Gamma()
    delta = Delta()

    assert reg.component(target, [gamma]) is foo
    assert target.component(gamma, lookup=reg) is foo

    # inheritance case
    assert reg.component(target, [delta]) is foo
    assert target.component(delta, lookup=reg) is foo
Example #10
0
def test_registry_sources():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [SpecialDocument], 'special document line count')

    assert (reg.component(linecount, [Document()]) ==
            'document line count')

    assert (reg.component(linecount, [SpecialDocument()]) ==
            'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(linecount, [AnotherDocument()]) ==
            'document line count')

    class Other(object):
        pass

    assert reg.component(linecount, [Other()], default=None) is None
Example #11
0
def test_call_two_sources():
    reg = Registry()

    @generic
    def target(a, b):
        pass

    class Adapted(object):
        def __init__(self, alpha, beta):
            self.alpha = alpha
            self.beta = beta

        def foo(self):
            pass

    reg.register(target, [IAlpha, IBeta], Adapted)

    alpha = Alpha()
    beta = Beta()
    adapted = reg.call(target, [alpha, beta])

    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta

    adapted = target(alpha, beta, lookup=reg)
    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta
Example #12
0
def test_registry_sources():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [SpecialDocument], 'special document line count')

    assert (reg.component(linecount, [Document()]) == 'document line count')

    assert (reg.component(
        linecount, [SpecialDocument()]) == 'special document line count')

    class AnotherDocument(Document):
        pass

    assert (reg.component(linecount,
                          [AnotherDocument()]) == 'document line count')

    class Other(object):
        pass

    assert reg.component(linecount, [Other()], default=None) is None
Example #13
0
def test_all():
    class Base(object):
        pass

    class Sub(Base):
        pass

    @generic
    def target(obj):
        pass

    registry = Registry()
    registry.register(target, (Sub,), 'registered for sub')
    registry.register(target, (Base,), 'registered for base')

    base = Base()
    sub = Sub()

    assert list(registry.all(target, [sub])) == [
        'registered for sub', 'registered for base']
    assert list(registry.all(target, [base])) == [
        'registered for base'
        ]
    assert list(target.all(sub, lookup=registry)) == [
        'registered for sub',
        'registered for base']
    assert list(target.all(base, lookup=registry)) == [
        'registered for base']
Example #14
0
def test_all():
    class Base(object):
        pass

    class Sub(Base):
        pass

    @generic
    def target(obj):
        pass

    registry = Registry()
    registry.register(target, (Sub, ), 'registered for sub')
    registry.register(target, (Base, ), 'registered for base')

    base = Base()
    sub = Sub()

    assert list(registry.all(
        target, [sub])) == ['registered for sub', 'registered for base']
    assert list(registry.all(target, [base])) == ['registered for base']
    assert list(target.all(sub, lookup=registry)) == [
        'registered for sub', 'registered for base'
    ]
    assert list(target.all(base, lookup=registry)) == ['registered for base']
Example #15
0
def test_adapter_two_sources():
    reg = Registry()

    class Adapted(ITarget):
        def __init__(self, alpha, beta):
            self.alpha = alpha
            self.beta = beta

        def foo(self):
            pass

    reg.register(ITarget, [IAlpha, IBeta], Adapted)

    alpha = Alpha()
    beta = Beta()
    adapted = reg.adapt(ITarget, [alpha, beta])

    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta

    adapted = ITarget.adapt(alpha, beta, lookup=reg)
    assert isinstance(adapted, Adapted)
    assert adapted.alpha is alpha
    assert adapted.beta is beta
Example #16
0
def test_component_two_sources():
    reg = Registry()
    foo = object()
    reg.register(ITarget, (IAlpha, IBeta), foo)
    alpha = Alpha()
    beta = Beta()
    assert reg.component(ITarget, [alpha, beta]) is foo
    assert ITarget.component(alpha, beta, lookup=reg) is foo
Example #17
0
def test_component_class_based_registration():
    reg = Registry()
    foo = object()
    reg.register(ITarget, (Alpha,), foo)

    alpha = Alpha()
    assert reg.component(ITarget, [alpha]) is foo
    assert ITarget.component(alpha, lookup=reg) is foo
Example #18
0
def test_non_adapter_looked_up_as_adapter():
    reg = Registry()
    foo = object()
    reg.register(ITarget, [Alpha], foo)
    alpha = Alpha()

    with py.test.raises(TypeError):
        ITarget.adapt(alpha, lookup=reg)
Example #19
0
def test_register_twice_without_sources():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    reg.register(linecount, [], 'twice')
    assert reg.component(linecount, []) == 'twice'
Example #20
0
def test_register_twice_without_sources():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    reg.register(linecount, [], 'twice')
    assert reg.component(linecount, []) == 'twice'
Example #21
0
def test_interface_component():
    class IFoo(Interface):
        pass

    registry = Registry()

    registry.register(IFoo, (), "test component")

    assert IFoo.component(lookup=registry) == 'test component'
Example #22
0
def test_adapter_returns_none():
    def adapt(obj):
        return None
    reg = Registry()
    reg.register(ITarget, [Alpha], adapt)
    alpha = Alpha()
    with py.test.raises(ComponentLookupError):
        ITarget.adapt(alpha, lookup=reg)
    assert ITarget.adapt(alpha, lookup=reg, default='default') == 'default'
Example #23
0
def test_component():
    @generic
    def foo():
        pass

    registry = Registry()

    registry.register(foo, (), "test component")

    assert foo.component(lookup=registry) == 'test component'
Example #24
0
def test_extra_kw():
    reg = Registry()
    foo = object()

    reg.register(ITarget, [Alpha], foo)
    alpha = Alpha()

    with py.test.raises(TypeError) as e:
        ITarget.component(alpha, lookup=reg, extra="illegal")
    assert str(e.value) == 'Illegal extra keyword arguments: extra'
Example #25
0
def test_implicit_component_lookup():
    class ITarget(Interface):
        pass

    reg = Registry()

    reg.register(ITarget, (), 'test component')

    implicit.initialize(reg)
    assert ITarget.component() == 'test component'
Example #26
0
def test_component_to_itself():
    reg = Registry()
    alpha = Alpha()

    foo = object()

    reg.register(IAlpha, [IAlpha], foo)

    assert reg.component(IAlpha, [alpha]) is foo
    assert IAlpha.component(alpha, lookup=reg) is foo
Example #27
0
def test_component():
    @generic
    def foo():
        pass

    registry = Registry()

    registry.register(foo, (), "test component")

    assert foo.component(lookup=registry) == 'test component'
Example #28
0
def test_registry_no_sources():
    reg = Registry()

    class Animal(object):
        pass

    def something():
        pass

    reg.register(something, (), 'elephant')
    assert reg.component(something, ()) == 'elephant'
Example #29
0
def test_clear():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    assert reg.component(linecount, []) == 'once'
    reg.clear()
    with pytest.raises(ComponentLookupError):
        reg.component(linecount, [])
Example #30
0
def test_component_no_source():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, (), foo)
    assert reg.component(target, []) is foo
    assert target.component(lookup=reg) is foo
Example #31
0
def test_implicit_component_lookup():
    @generic
    def func():
        pass

    reg = Registry()

    reg.register(func, (), 'test component')

    implicit.initialize(reg)
    assert func.component() == 'test component'
Example #32
0
def test_clear():
    reg = Registry()

    def linecount(obj):
        pass

    reg.register(linecount, [], 'once')
    assert reg.component(linecount, []) == 'once'
    reg.clear()
    with pytest.raises(ComponentLookupError):
        reg.component(linecount, [])
Example #33
0
def test_registry_no_sources():
    reg = Registry()

    class Animal(object):
        pass

    def something():
        pass

    reg.register(something, (), 'elephant')
    assert reg.component(something, ()) == 'elephant'
Example #34
0
def test_component_no_source():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, (), foo)
    assert reg.component(target, []) is foo
    assert target.component(lookup=reg) is foo
Example #35
0
def test_registry_no_sources():
    reg = Registry()

    class Animal(object):
        pass

    class Elephant(Animal):
        pass

    reg.register(Elephant, (), 'elephant')
    assert reg.component(Animal, ()) == 'elephant'
Example #36
0
def test_implicit_component_lookup():
    @generic
    def func():
        pass

    reg = Registry()

    reg.register(func, (), 'test component')

    implicit.initialize(reg)
    assert func.component() == 'test component'
Example #37
0
def test_register_twice_with_sources():
    reg = Registry()

    class Document(object):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [Document], 'another line count')
    assert reg.component(linecount, [Document()]) == 'another line count'
Example #38
0
def test_register_twice_with_sources():
    reg = Registry()

    class Document(object):
        pass

    def linecount(obj):
        pass

    reg.register(linecount, [Document], 'document line count')
    reg.register(linecount, [Document], 'another line count')
    assert reg.component(linecount, [Document()]) == 'another line count'
Example #39
0
def test_func_returns_none():
    @generic
    def target(obj):
        raise NotImplementedError

    def adapt(obj):
        return None
    reg = Registry()
    reg.register(target, [Alpha], adapt)
    alpha = Alpha()
    assert target(alpha, lookup=reg) is None
    assert target(alpha, lookup=reg, default='default') == 'default'
Example #40
0
def test_adapter_no_source():
    reg = Registry()

    foo = object()

    def factory():
        return foo

    reg.register(ITarget, (), factory)

    assert reg.adapt(ITarget, []) is foo
    assert ITarget.adapt(lookup=reg) is foo
Example #41
0
def test_fallback():
    @generic
    def target(obj):
        return 'fallback'

    reg = Registry()

    def specific_target(obj):
        return 'specific'

    reg.register(target, [Alpha], specific_target)
    beta = Beta()
    assert target(beta, lookup=reg) == 'fallback'
Example #42
0
def test_non_function_called():
    reg = Registry()
    foo = object()

    @generic
    def target(obj):
        pass

    reg.register(target, [Alpha], foo)
    alpha = Alpha()

    with pytest.raises(TypeError):
        target(alpha, lookup=reg)
Example #43
0
def test_non_function_called():
    reg = Registry()
    foo = object()

    @generic
    def target(obj):
        pass

    reg.register(target, [Alpha], foo)
    alpha = Alpha()

    with pytest.raises(TypeError):
        target(alpha, lookup=reg)
Example #44
0
def test_func_returns_none():
    @generic
    def target(obj):
        raise NotImplementedError

    def adapt(obj):
        return None

    reg = Registry()
    reg.register(target, [Alpha], adapt)
    alpha = Alpha()
    assert target(alpha, lookup=reg) is None
    assert target(alpha, lookup=reg, default='default') == 'default'
Example #45
0
def test_fallback():
    @generic
    def target(obj):
        return 'fallback'

    reg = Registry()

    def specific_target(obj):
        return 'specific'

    reg.register(target, [Alpha], specific_target)
    beta = Beta()
    assert target(beta, lookup=reg) == 'fallback'
Example #46
0
def test_component_two_sources():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, (IAlpha, IBeta), foo)
    alpha = Alpha()
    beta = Beta()
    assert reg.component(target, [alpha, beta]) is foo
    assert target.component(alpha, beta, lookup=reg) is foo
Example #47
0
def test_component_one_source():
    reg = Registry()
    foo = object()

    @generic
    def target():
        pass

    reg.register(target, [Alpha], foo)

    alpha = Alpha()
    assert reg.component(target, [alpha]) is foo
    assert target.component(alpha, lookup=reg) is foo
Example #48
0
def test_extra_kw_for_component():
    @generic
    def target(obj):
        pass

    reg = Registry()
    foo = object()

    reg.register(target, [Alpha], foo)
    alpha = Alpha()

    with pytest.raises(TypeError) as e:
        target.component(alpha, lookup=reg, extra="illegal")
    assert str(e.value) == ("component() got an unexpected keyword "
                            "argument 'extra'")
Example #49
0
def test_call_with_wrong_args():
    @generic
    def target(obj):
        pass

    class Adapter(object):
        # takes no args
        def __init__(self):
            pass

    reg = Registry()
    reg.register(target, [Alpha], Adapter)
    alpha = Alpha()

    with pytest.raises(TypeError):
        target(alpha, lookup=reg)
Example #50
0
def test_call_no_source():
    reg = Registry()

    foo = object()

    @generic
    def target():
        pass

    def factory():
        return foo

    reg.register(target, (), factory)

    assert reg.call(target, []) is foo
    assert target(lookup=reg) is foo
Example #51
0
def test_calling_twice():
    @generic
    def target(obj):
        return 'fallback'

    reg = Registry()

    def a(obj):
        return 'a'

    def b(obj):
        return 'b'

    reg.register(target, [Alpha], a)
    reg.register(target, [Beta], b)

    assert target(Alpha(), lookup=reg) == 'a'
    assert target(Beta(), lookup=reg) == 'b'
Example #52
0
def test_extra_kw_for_call():
    @generic
    def target(obj, extra):
        return "General: %s" % extra

    reg = Registry()

    def specific(obj, extra):
        return "Specific: %s" % extra

    reg.register(target, [Alpha], specific)
    alpha = Alpha()
    beta = Beta()
    assert target(alpha, lookup=reg, extra="allowed") == 'Specific: allowed'
    assert target(beta, lookup=reg, extra="allowed") == 'General: allowed'
    assert target(alpha, lookup=reg, default='default',
                  extra='allowed') == 'Specific: allowed'
    assert target(beta, lookup=reg, default='default',
                  extra='allowed') == 'default'
Example #53
0
def test_exact():
    reg = Registry()

    class Document(object):
        pass

    class SubDocument(Document):
        pass

    def linecount(obj):
        pass

    def other(obj):
        pass

    reg.register(linecount, [Document], 'once')
    assert reg.exact(linecount, [Document]) == 'once'
    assert reg.exact(linecount, [SubDocument]) is None
    assert reg.exact(other, [Document]) is None
Example #54
0
def test_matcher_predicates():
    reg = Registry()

    class Document(object):
        pass

    class SpecialDocument(Document):
        pass

    def linecount(obj):
        pass

    count = {'predicates': 0, 'called': 0}

    class DocumentMatcher(Matcher):
        def predicates(self, doc):
            count['predicates'] += 1
            return dict(precalculated=100)

        def __call__(self, doc, precalculated):
            count['called'] += 1
            return 'value: %s' % precalculated

    class SpecialDocumentMatcher(Matcher):
        def predicates(self, doc):
            count['predicates'] += 1
            return dict(precalculated=100)

        def __call__(self, doc, precalculated):
            count['called'] += 1
            return None

    reg.register(linecount, [Document], DocumentMatcher())
    reg.register(linecount, [SpecialDocument], SpecialDocumentMatcher())

    assert reg.component(linecount, [Document()]) == 'value: 100'
    assert count['called'] == 1
    assert count['predicates'] == 1
    assert reg.component(linecount, [SpecialDocument()]) == 'value: 100'
    # called two more times, but predicates only called once
    assert count['called'] == 3
    assert count['predicates'] == 2
Example #55
0
def test_component_inheritance():
    reg = Registry()
    foo = object()

    class Gamma(object):
        pass

    class Delta(Gamma):
        pass

    @generic
    def target():
        pass

    reg.register(target, [Gamma], foo)

    delta = Delta()

    assert reg.component(target, [delta]) is foo
    assert target.component(delta, lookup=reg) is foo
Example #56
0
def test_lookup_passed_along():
    @generic
    def g1(obj):
        pass

    @generic
    def g2(obj):
        pass

    reg = Registry()

    def g1_impl(obj, lookup):
        return g2(obj, lookup=lookup)

    def g2_impl(obj):
        return "g2"

    reg.register(g1, [Alpha], g1_impl)
    reg.register(g2, [Alpha], g2_impl)

    assert g1(Alpha(), lookup=reg) == 'g2'
Example #57
0
def test_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, id):
            self.id = id

    def linecount(obj):
        pass

    class MyMatcher(Matcher):
        def __call__(self, doc):
            if doc.id == 1:
                return 'normal'
            else:
                return 'special'

    reg.register(linecount, [Document], MyMatcher())

    assert reg.component(linecount, [Document(1)]) == 'normal'
    assert reg.component(linecount, [Document(2)]) == 'special'
Example #58
0
def test_call():
    @generic
    def foo(obj):
        pass

    class Foo(object):
        def __init__(self, bar):
            self.bar = bar

        def foo(self):
            return "Foo called: " + self.bar.bar()

    class Bar(object):
        def bar(self):
            return "bar's method"

    registry = Registry()

    registry.register(foo, (Bar, ), Foo)

    bar = Bar()
    assert foo(bar, lookup=registry).foo() == "Foo called: bar's method"
Example #59
0
def test_predicate_matcher():
    reg = Registry()

    class Document(object):
        def __init__(self, a, b):
            self.a = a
            self.b = b

    def foo(obj):
        pass

    predicates = [
        Predicate('a', KeyIndex, lambda doc: doc.a, 'A'),
        Predicate('b', KeyIndex, lambda doc: doc.b, 'B')
    ]

    matcher = PredicateMatcher(predicates)
    matcher.register({'a': 'A'}, 'a = A')
    matcher.register({'b': 'B'}, 'b = B')
    matcher.register({}, 'nothing matches')

    reg.register(foo, [Document], matcher)

    assert reg.component(foo, [Document(a='A', b='C')]) == 'a = A'
    assert reg.component(foo, [Document(a='C', b='B')]) == 'b = B'
    assert reg.component(foo, [Document(a='A', b='B')]) == 'a = A'
    assert reg.component(foo, [Document(a='C', b='C')]) == 'nothing matches'

    # we can also override lookup by supplying our own predicates
    assert reg.component(foo, [Document(a='C', b='C')],
                         predicates={
                             'a': 'A',
                             'b': 'C'
                         }) == 'a = A'
    # if we don't supply something in predicates ourselves, a default will
    # be used
    assert reg.component(foo, [Document(a='C', b='C')],
                         predicates={'a': 'C'}) == 'b = B'
Example #60
0
def test_call_one_source():
    reg = Registry()

    @generic
    def target(obj):
        pass

    class Adapted(object):
        def __init__(self, context):
            self.context = context

        def foo(self):
            pass

    reg.register(target, [IAlpha], Adapted)

    alpha = Alpha()
    adapted = reg.call(target, [alpha])
    assert isinstance(adapted, Adapted)
    assert adapted.context is alpha
    adapted = target(alpha, lookup=reg)
    assert isinstance(adapted, Adapted)
    assert adapted.context is alpha