示例#1
0
def test_extension_reg():
    clear_registry()
    from giblets import Component, ComponentManager, ExtensionPoint, ExtensionInterface, implements


    # a Train is extended by a bunch of train cars...
    class ITrainCar(ExtensionInterface):
        pass

    class Train(Component):
        cars = ExtensionPoint(ITrainCar)

    class EngineCar(Component):
        implements(ITrainCar)
        
    class PassengerCar(Component):
        implements(ITrainCar)
        
    class CafeCar(Component):
        implements(ITrainCar)
    
    class CabooseCar(Component):
        implements(ITrainCar)
        

    # an irrelevant interface and implementation...
    class IZooAnimal(ExtensionInterface):
        pass
        
    class Zebra(Component):
        implements(IZooAnimal)

    # create a component manager and the train associated with it 
    mgr1 = ComponentManager()
    train1 = Train(mgr1)
    
    # components are singletons w/ respect to manager
    assert id(train1) == id(Train(mgr1))
    
    # check that all of the extensions for the extension point were registered
    for cls in (EngineCar, PassengerCar, CafeCar, CabooseCar):
        assert has_exactly(1, cls, train1.cars)
        assert has_exactly(1, cls, mgr1.get_all(ITrainCar))
    
    # check that the Zebra is not a part of the train
    assert has_exactly(0, Zebra, train1.cars)
    assert has_exactly(0, Zebra, mgr1.get_all(ITrainCar))
    
    # create a different component manager and the train associated with it, 
    # check the same stuff...
    mgr2 = ComponentManager()
    train2 = Train(mgr2)
    assert id(train2) == id(Train(mgr2))
    for cls in (EngineCar, PassengerCar, CafeCar, CabooseCar):
        assert has_exactly(1, cls, train2.cars)
        assert has_exactly(1, cls, mgr2.get_all(ITrainCar))

    assert has_exactly(0, Zebra, train2.cars)
    assert has_exactly(0, Zebra, mgr2.get_all(ITrainCar))

    # now check that the trains and extensions produced by the
    # different component managers are different.
    assert train1 != train2
    for cls in (EngineCar, PassengerCar, CafeCar, CabooseCar):
        i1 = (x for x in train1.cars if isinstance(x, cls)).next()
        i2 = (x for x in train2.cars if isinstance(x, cls)).next()
        assert id(i1) != id(i2)
        
        i1 = (x for x in mgr1.get_all(ITrainCar) if isinstance(x, cls)).next()
        i2 = (x for x in mgr2.get_all(ITrainCar) if isinstance(x, cls)).next()

        assert id(i1) != id(i2)