def test_should_find_components_with_same_attributes_in_list(self): componentinstance = ComponentModel(FakeObj, [], LifeStyle.transient()) componentinstance2nd = ComponentModel(FakeObj, [], LifeStyle.transient()) componentset = ComponentSet() componentset.add(componentinstance) assert componentset.has_comp(componentinstance2nd)
def test_should_prevent_storing_duplicate_attributes_of_componentmodel(self): componentset = ComponentSet() componentinstance = ComponentModel(FakeObj, [], LifeStyle.transient()) componentinstance2nd = ComponentModel(FakeObj, [], LifeStyle.transient()) componentset.add(componentinstance) try: componentset.add(componentinstance2nd) self.fail("should not happen") except AttemptToAddDuplicateComponentModelToVisitedSet: pass
def test_should_prevent_storing_duplicate_attributes_of_componentmodel( self): componentset = ComponentSet() componentinstance = ComponentModel(FakeObj, [], LifeStyle.transient()) componentinstance2nd = ComponentModel(FakeObj, [], LifeStyle.transient()) componentset.add(componentinstance) try: componentset.add(componentinstance2nd) self.fail("should not happen") except AttemptToAddDuplicateComponentModelToVisitedSet: pass
def test_should_set_lifestyle(self): self.pinsor.register( Component.oftype(FakeObj).lifestyle(LifeStyle.transient()) ) fake1 = self.pinsor.resolve(FakeObj) fake2 = self.pinsor.resolve(FakeObj) self.assertNotEqual(id(fake1), id(fake2))
def recursewalk(self, graph, key, cls, instances, visitedset): """method responsible for walking the tree, this is doing too much REFACTOR""" clsout = cls classkey = key if cls == None: clsout = self.__get_cls_from_graph(graph, classkey) else: classkey = self.__get_key_from_class_name(key, clsout) instance = self.__find_instance(instances, classkey, clsout) if instance is not None: return instance compmodel = self.__retrieval.get_component_model( graph, classkey, clsout) if visitedset.has_comp(compmodel): raise CircularDependencyException visitedset.add(compmodel) resolveddeps = [] for dep in compmodel.depends: if isinstance(dep, Config): configmodel = graph[dep.comp_key] resolveddeps.append( self.recursewalk(graph, dep.comp_key, configmodel.classtype, instances, visitedset)) elif isinstance(dep, Instance): resolveddeps.append(dep.arg) else: resolveddeps.append( self.recursewalk(graph, None, dep, instances, visitedset)) built = self.__build_class(clsout, resolveddeps) if compmodel.lifestyle is LifeStyle.singleton(): instances[classkey + str(clsout)] = built return built
def oftype(clsobj): """factory for FluentService""" fluent = FluentService() defaultcomponent = ComponentModel(classtype=clsobj, depends=[], lifestyle=LifeStyle.singleton()) fluent.graphnode = GraphNode(key=clsobj.__name__, component=defaultcomponent) return fluent
def oftype(clsobj): """factory for FluentService""" fluent = FluentService() defaultcomponent = ComponentModel( classtype=clsobj, depends=[], lifestyle = LifeStyle.singleton() ) fluent.graphnode = GraphNode(key=clsobj.__name__, component=defaultcomponent ) return fluent
def addcomponent(self, clstype, depends = [], lifestyle = LifeStyle.singleton(), key= None): """alternative way to register components. this is not the preferred way but can be easier to use for those new to IoC containers""" if key is None: key = clstype.__name__ if key in self.__objectgraph: raise KeyError if type(depends) is not types.ListType: raise TypeError( "Depends parameter has to be of type List, not "\ + str(type(depends)) ) self.__objectgraph[key] = ComponentModel(clstype, depends, lifestyle)
def addcomponent(self, clstype, depends=[], lifestyle=LifeStyle.singleton(), key=None): """alternative way to register components. this is not the preferred way but can be easier to use for those new to IoC containers""" if key is None: key = clstype.__name__ if key in self.__objectgraph: raise KeyError if type(depends) is not types.ListType: raise TypeError( "Depends parameter has to be of type List, not "\ + str(type(depends)) ) self.__objectgraph[key] = ComponentModel(clstype, depends, lifestyle)
def recursewalk(self, graph, key, cls, instances, visitedset): """method responsible for walking the tree, this is doing too much REFACTOR""" clsout = cls classkey = key if cls == None: clsout = self._get_cls_from_graph(graph, classkey) else: classkey = self._get_key_from_class_name(key, clsout) instance = self._find_instance(instances, classkey, clsout) if instance is not None: return instance compmodel = self._retrieval.get_component_model( graph, classkey, clsout ) if visitedset.has_comp(compmodel): raise CircularDependencyException visitedset.add(compmodel) resolveddeps = [] for dep in compmodel.depends: if isinstance(dep, Config): configmodel = graph[dep.comp_key] resolveddeps.append( self.recursewalk( graph, dep.comp_key, configmodel.classtype, instances, visitedset ) ) elif isinstance(dep, Instance): resolveddeps.append(dep.arg) else: resolveddeps.append( self.recursewalk( graph, None, dep, instances, visitedset ) ) built = self._build_class(clsout, resolveddeps) for i in compmodel.interceptors: wrappublic(built, i) if compmodel.lifestyle is LifeStyle.singleton(): instances[classkey+str(clsout)] = built return built
def test_should_set_multiple_options_at_once(self): self.pinsor.register( Component.oftype(FakeObj), Component.oftype(NeedsFakeObj).depends([FakeObj]).named("needs").lifestyle(LifeStyle.transient()) ) com = self.pinsor.objectgraph["needs"] self.assertEqual(NeedsFakeObj, com.classtype) self.assertEqual("transient", com.lifestyle) self.assertEqual(FakeObj, com.depends[0])