Example #1
0
def test_Scope_register_subclasses(include_subclasses):
    scope = Scope()

    supported = [BaseA, DerBaseA, DerDerBaseA]
    if include_subclasses:
        included = [cls.__name__ for cls in supported]
    else:
        included = [BaseA.__name__]
    excluded = [cls.__name__ for cls in supported if not cls.__name__ in included]

    for cls in supported:
        assert not scope.supports(cls)
    for name in included + excluded:
        assert name not in scope
    
    scope.register(BaseA, subclasses=include_subclasses)

    for cls in supported:
        assert scope.supports(cls)
    for name in included:
        assert name in scope
    for name in excluded:
        assert name not in scope
    
    scope.unregister(BaseA, subclasses=include_subclasses)

    for cls in supported:
        assert not scope.supports(cls)
    for name in included + excluded:
        assert name not in scope
Example #2
0
def test_Scope_len_register():
    scope = Scope()
    scope.register(str)
    assert len(scope) == 1
    assert tuple(scope.keys()) == (str.__name__, )
    assert tuple(scope.values()) == (str, )
    scope.unregister(str)
    assert len(scope) == 0
Example #3
0
def test_Scope_len_register_with_name():
    scope = Scope()
    scope.register(string=str)
    assert len(scope) == 1
    assert tuple(scope.keys()) == ("string", )
    assert tuple(scope.values()) == (str, )
    assert scope.supports(str)
    scope.unregister(string=str)
    assert len(scope) == 0
    assert not scope.supports(str)
Example #4
0
def test_Scope_register_str():
    scope = Scope()
    assert not scope.supports(str)
    scope.register(str)
    assert scope.supports(str)
    assert scope.supports("10")
    assert not scope.supports(int)
    assert not scope.supports(10)
    scope.unregister(str)
    assert not scope.supports(str)
    assert 'str' not in scope.globals_d()
Example #5
0
def test_Scope_register_BaseA():
    scope = Scope()
    assert not scope.supports(BaseA)
    assert not scope.supports(DerBaseA)
    scope.register(BaseA)
    assert scope.supports(BaseA)
    assert BaseA.__name__ in scope
    assert scope.supports(DerBaseA)
    assert not DerBaseA.__name__ in scope
    scope.unregister(BaseA)
    assert not scope.supports(BaseA)
    assert not scope.supports(DerBaseA)
    assert not BaseA.__name__ in scope
    assert not DerBaseA.__name__ in scope
Example #6
0
def test_Scope_register_objects():
    scope = Scope()
    a = 10
    b = BaseA()
    assert 'a' not in scope
    assert 'b' not in scope
    assert not scope.supports(a)
    assert not scope.supports(int)
    assert not scope.supports(b)
    assert not scope.supports(BaseA)
    scope.register(a=a, b=b)
    assert scope.supports(a)
    assert scope.supports(int)
    assert scope.supports(b)
    assert scope.supports(BaseA)
    assert 'a' in scope
    assert 'b' in scope
    scope.unregister(a=a, b=b)
    assert 'a' not in scope
    assert 'b' not in scope
    assert not scope.supports(a)
    assert not scope.supports(int)
    assert not scope.supports(b)
    assert not scope.supports(BaseA)
Example #7
0
def test_Scope_unregister_type_error():
    scope = Scope()
    with pytest.raises(TypeError):
        scope.unregister(10)