def testGetMethodDescription(self): """ Tests the ipopo.decorators.get_method_description() method """ bundle_name = "tests.framework.simple_bundle" bundle = install_bundle(self.framework, bundle_name) descr = decorators.get_method_description(bundle.ActivatorTest.start) # Assert we found sufficient data self.assertTrue(descr.startswith("'start'"), "Method name not found") self.assertIn(bundle_name.replace(".", os.sep) + ".py", descr, "File couldn't determined") # Some methods are unreadable self.assertEqual("'getpid'", decorators.get_method_description(os.getpid), "Invalid description of getpid()")
def __call__(self, clazz): """ Stores the configuration of the handler in the component factory context Do not forget to return the given class if no exception is raised :param clazz: Manipulated class :return: The (manipulated) class """ # Ensure that the decorator is applied on a class if not inspect.isclass(clazz): raise TypeError( "@Logger can decorate only classes, not '{0}'".format( type(clazz).__name__)) # Retrieve the Factory context context = decorators.get_factory_context(clazz) if context.completed: # Do nothing if the class has already been manipulated _logger.warning("@Logger: Already manipulated class: %s", decorators.get_method_description(clazz)) return clazz # Store the handler information context.set_handler(constants.HANDLER_LOGGER, self._field) # Inject the logger field in the class setattr(clazz, self._field, None) return clazz
def __call__(self, clazz): """ Stores the configuration of the handler in the component factory context Do not forget to return the given class if no exception is raised :param clazz: Manipulated class :return: The (manipulated) class """ # Ensure that the decorator is applied on a class if not inspect.isclass(clazz): raise TypeError( "@Logger can decorate only classes, not '{0}'".format( type(clazz).__name__ ) ) # Retrieve the Factory context context = decorators.get_factory_context(clazz) if context.completed: # Do nothing if the class has already been manipulated _logger.warning( "@Logger: Already manipulated class: %s", decorators.get_method_description(clazz), ) return clazz # Store the handler information context.set_handler(constants.HANDLER_LOGGER, self._field) # Inject the logger field in the class setattr(clazz, self._field, None) return clazz
def testGetMethodDescription(self): """ Tests the ipopo.decorators.get_method_description() method """ bundle_name = "tests.framework.simple_bundle" bundle = install_bundle(self.framework, bundle_name) descr = decorators.get_method_description(bundle.ActivatorTest.start) # Assert we found sufficient data self.assertTrue(descr.startswith("'start'"), "Method name not found") self.assertIn(bundle_name.replace(".", os.sep) + ".py", descr, "File couldn't determined") # Some methods are unreadable self.assertEqual("'getpid'", decorators.get_method_description(os.getpid), "Invalid description of getpid()")
def test_method_description(self): """ Tests get_method_description() """ description = decorators.get_method_description(self._dummy) self.assertNotIn(":-1", description) self.assertIn(__file__[:-2], description) self.assertIn("_dummy", description) description = decorators.get_method_description(self._some_args_dummy) self.assertNotIn(":-1", description) self.assertIn(__file__[:-2], description) self.assertIn("_some_args_dummy", description) # Try with a compiled method local_vars = {} mod = code.compile_command("def foobar():\n pass\n", "<generated>") exec(mod, {}, local_vars) foobar = local_vars['foobar'] description = decorators.get_method_description(foobar) self.assertIn(":-1", description) self.assertNotIn(__file__[:-2], description) self.assertIn("<generated>", description) self.assertIn("foobar", description) # Try with any object with a name class Foo: pass # __name__ is missing in instances self.assertRaises( AttributeError, decorators.get_method_description, Foo()) # ... not on type self.assertIn( repr(Foo.__name__), decorators.get_method_description(Foo)) class Bar: __name__ = "<Bar>" # Name exists in instance description = decorators.get_method_description(Bar()) self.assertIn(repr(Bar().__name__), description)
def test_method_description(self): """ Tests get_method_description() """ description = decorators.get_method_description(self._dummy) self.assertNotIn(":-1", description) self.assertIn(__file__[:-2], description) self.assertIn("_dummy", description) description = decorators.get_method_description(self._some_args_dummy) self.assertNotIn(":-1", description) self.assertIn(__file__[:-2], description) self.assertIn("_some_args_dummy", description) # Try with a compiled method local_vars = {} mod = code.compile_command("def foobar():\n pass\n", "<generated>") exec(mod, {}, local_vars) foobar = local_vars['foobar'] description = decorators.get_method_description(foobar) self.assertIn(":-1", description) self.assertNotIn(__file__[:-2], description) self.assertIn("<generated>", description) self.assertIn("foobar", description) # Try with any object with a name class Foo: pass # __name__ is missing in instances self.assertRaises( AttributeError, decorators.get_method_description, Foo()) # ... not on type self.assertIn( repr(Foo.__name__), decorators.get_method_description(Foo)) class Bar: __name__ = "<Bar>" # Name exists in instance description = decorators.get_method_description(Bar()) self.assertIn(repr(Bar().__name__), description)