def test_GuardOnRecursiveInclusion(self): MM = ModulesManager() scopeM = Scope("MAIN", MM, exports=[ ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ]) scope1 = Scope("FIRST", MM, exports=[ ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ], imports=[ ScopeImport("MAIN", Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL), ]) scope2 = Scope("SECOND", MM, exports=[ ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ], imports=[ ScopeImport("MAIN", Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL), ScopeImport("FIRST", Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ]) scopeM.globalsvars.addDefinition( GlobalVarDefinition(scopeM.moduleName, "?*A*", object())) self.assertTrue(scopeM.globalsvars.has("?*A*")) self.assertTrue(scope1.globalsvars.has("?*A*")) self.assertTrue(scope2.globalsvars.has("?*A*")) self.assertEqual( scopeM.globalsvars.getDefinition("?*A*").moduleName, "MAIN") self.assertEqual( scope1.globalsvars.getDefinition("?*A*").moduleName, "MAIN") self.assertEqual( scope2.globalsvars.getDefinition("?*A*").moduleName, "MAIN") self.assertEqual(scopeM.globalsvars.getDefinition("?*A*"), scope1.globalsvars.getDefinition("?*A*")) self.assertEqual(scopeM.globalsvars.getDefinition("?*A*"), scope2.globalsvars.getDefinition("?*A*")) self.assertEqual(scope1.globalsvars.getDefinition("?*A*"), scope2.globalsvars.getDefinition("?*A*"))
def test_ImportAListOfConstructsIsPossible(self): MM = ModulesManager() scopeM = Scope("MAIN", MM, exports=[ ScopeExport(Scope.PROMISE_TYPE_GLOBAL, ["?*a*", "?*b*", "?*c*"]) ]) scopeM.globalsvars.addDefinition( GlobalVarDefinition(scopeM.moduleName, "?*a*", object())) scopeM.globalsvars.addDefinition( GlobalVarDefinition(scopeM.moduleName, "?*b*", object())) scopeM.globalsvars.addDefinition( GlobalVarDefinition(scopeM.moduleName, "?*c*", object())) scope1 = Scope("FIRST", MM, imports=[ ScopeImport("MAIN", Scope.PROMISE_TYPE_GLOBAL, ["?*a*", "?*b*"]) ]) self.assertTrue(scope1.globalsvars.has("?*a*")) self.assertTrue(scope1.globalsvars.has("?*b*")) self.assertFalse(scope1.globalsvars.has("?*c*"))
def test_ListenerCleanupOnScopeCreationFail(self): MM = ModulesManager() scopeM = Scope("MAIN", MM, exports=[ ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ]) scopeM.globalsvars.addDefinition( GlobalVarDefinition(scopeM.moduleName, "?*A*", object())) scope1 = Scope("FIRST", MM, exports=[ ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ]) scope1.globalsvars.addDefinition( GlobalVarDefinition(scope1.moduleName, "?*A*", object())) self.assertRaises(ScopeDefinitionConflict, Scope, "SECOND", MM, exports=[ ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ], imports=[ ScopeImport("MAIN", Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL), ScopeImport("FIRST", Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ]) self.assertEqual( len( scope1.globalsvars.getObservers( scope1.globalsvars.EVENT_NEW_DEFINITION)), 0) self.assertEqual( len( scopeM.globalsvars.getObservers( scopeM.globalsvars.EVENT_NEW_DEFINITION)), 0)
def test_ErrorOnInclusionOfNotDefinedModule(self): MM = ModulesManager() self.assertRaises(ScopeDefinitionNotFound, Scope, "MAIN", MM, imports=[ ScopeImport("FIRST", Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ])
def __init__(self, moduleName, modulesManager, specifications=None, comment=None): moduleName = moduleName.evaluate() if isinstance( moduleName, BaseParsedType) else moduleName comment = comment.evaluate().strip('"') if isinstance( comment, BaseParsedType) else comment specifications = specifications if isinstance(specifications, list) else [] ParsedType.__init__(self, moduleName) self.moduleName = moduleName self.comment = comment self.specifications = specifications # time to create che new Scope try: Scope(moduleName, modulesManager, imports=[ ScopeImport(imp.moduleName, imp.item.portType, imp.item.portNames) for imp in self.specifications if isinstance(imp, ImportSpecification) ], exports=[ ScopeExport(exp.item.portType, exp.item.portNames) for exp in self.specifications if isinstance(exp, ExportSpecification) ]) except Exception, e: # causes of failure: # moduleName already exists # import from unknown module # import of an unknown construct # name conflicts # i can use the original name raise TypeInstanceCreationError(e.args[0])
def test_ImportDefinitionFromAnotherScopeWithObserver(self): MM = ModulesManager() scope1 = Scope("MAIN", MM, exports=[ ScopeExport(Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ]) scope2 = Scope("SECOND", MM, imports=[ ScopeImport("MAIN", Scope.PROMISE_NAME_ALL, Scope.PROMISE_NAME_ALL) ]) scope1.globalsvars.addDefinition( GlobalVarDefinition(scope1.moduleName, "?*A*", object())) self.assertTrue(scope2.globalsvars.has("?*A*")) self.assertEqual( scope2.globalsvars.getDefinition("?*A*").moduleName, "MAIN")
def test_CanRedefineFunctionUntilDefinitionIsImported(self): self.scope.functions.addDefinition( FunctionDefinition(self.scope.moduleName, "NuovaFunzione", object(), Symbol) ) self.assertIsInstance(self.scope.functions.getDefinition("NuovaFunzione"), FunctionDefinition) self.assertEqual(self.scope.functions.getDefinition("NuovaFunzione").returnTypes, tuple([Symbol])) self.scope.functions.addDefinition( FunctionDefinition(self.scope.moduleName, "NuovaFunzione", object(), Integer) ) self.assertEqual(self.scope.functions.getDefinition("NuovaFunzione").returnTypes, tuple([Integer])) Scope("OTHER", self.MM, imports=[ ScopeImport("MAIN", Scope.PROMISE_TYPE_FUNCTION, "NuovaFunzione") ]) self.assertRaisesRegexp(MultipleDefinitionError, "Cannot redefine deffunction \w+::\w+ while it is in use", self.scope.functions.addDefinition, FunctionDefinition(self.scope.moduleName, "NuovaFunzione", object(), Integer) )