当前位置: 首页>>代码示例>>Python>>正文


Python PluginManager._handle_object_instance方法代码示例

本文整理汇总了Python中pluginmanager.PluginManager._handle_object_instance方法的典型用法代码示例。如果您正苦于以下问题:Python PluginManager._handle_object_instance方法的具体用法?Python PluginManager._handle_object_instance怎么用?Python PluginManager._handle_object_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pluginmanager.PluginManager的用法示例。


在下文中一共展示了PluginManager._handle_object_instance方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestPluginManager

# 需要导入模块: from pluginmanager import PluginManager [as 别名]
# 或者: from pluginmanager.PluginManager import _handle_object_instance [as 别名]
class TestPluginManager(unittest.TestCase):
    def setUp(self):
        self.instance = InstanceClass()
        self.plugin_manager = PluginManager()
        self.plugin_manager.add_plugins(self.instance)

    def test_add_instances(self):
        self.plugin_manager.unique_instances = False
        instance = InstanceClass()
        self.plugin_manager.add_plugins(instance)
        instances = self.plugin_manager.get_plugins()
        self.assertIn(instance, instances)
        self.plugin_manager.add_plugins(InstanceClass())
        instances = self.plugin_manager.get_plugins()
        self.assertTrue(len(instances) > 2)
        uniq = self.plugin_manager._unique_class(InstanceClass)
        self.assertFalse(uniq)

    def test_register_class(self):
        class TestClass:
            pass

        self.assertFalse(issubclass(TestClass, IPlugin))
        self.plugin_manager.register_classes(TestClass)
        self.assertTrue(issubclass(TestClass, IPlugin))

    def test_class_in_blacklist(self):
        self.plugin_manager.set_plugins([])
        self.plugin_manager.add_blacklisted_plugins(InstanceClass)
        self.plugin_manager._handle_object_instance(self.instance)
        plugins = self.plugin_manager.get_plugins()
        self.assertEqual(plugins, [])

    def test_blacklist_plugins(self):
        self.plugin_manager.add_blacklisted_plugins(InstanceClass)
        blacklisted = self.plugin_manager.get_blacklisted_plugins()
        self.assertIn(InstanceClass, blacklisted)

    def test_handle_classs_instance(self):
        self.plugin_manager.instantiate_classes = False
        is_none = self.plugin_manager._handle_class_instance(5)
        self.assertIsNone(is_none)

    def test_class_instance_not_unique(self):
        self.plugin_manager.unique_instances = False
        num_plugins = len(self.plugin_manager.plugins)
        self.plugin_manager._handle_class_instance(InstanceClass)
        self.assertTrue(len(self.plugin_manager.plugins) > num_plugins)

    def test_class_instance_unique(self):
        num_plugins = len(self.plugin_manager.plugins)
        self.plugin_manager.unique_instances = True
        self.plugin_manager._handle_class_instance(InstanceClass)
        self.assertTrue(len(self.plugin_manager.plugins) == num_plugins)

    def test_get_plugins(self):
        self.plugin_manager.unique_instances = False
        instance_2 = InstanceClass()
        instance_2.name = 'red'
        self.plugin_manager.add_plugins(instance_2)

        filtered_plugins = self.plugin_manager.get_plugins(_test_filter)
        self.assertNotIn(self.instance, filtered_plugins)
        self.assertIn(instance_2, filtered_plugins)

    def test_set_plugins(self):
        instance_2 = InstanceClass()
        self.plugin_manager.set_plugins(instance_2)
        plugins = self.plugin_manager.get_plugins()
        self.assertIn(instance_2, plugins)
        self.assertNotIn(self.instance, plugins)

    def test_remove_plugin(self):
        self.plugin_manager.remove_plugins(self.instance)
        plugins = self.plugin_manager.get_plugins()
        self.assertNotIn(self.instance, plugins)

    def test_remove_instance(self):
        self.plugin_manager.remove_instance(self.instance)
        plugins = self.plugin_manager.get_plugins()
        self.assertNotIn(self.instance, plugins)

    def test_get_instances(self):
        self.plugin_manager.unique_instances = False
        instance_2 = InstanceClass()
        instance_2.name = 'red'
        self.plugin_manager.add_plugins((instance_2, 5.0))
        instances = self.plugin_manager.get_instances((IPlugin, InstanceClass))
        self.assertIn(instance_2, instances)
        self.assertIn(self.instance, instances)
        self.assertNotIn(5.0, instances)
        filtered_instances = self.plugin_manager.get_instances(_test_filter)
        self.assertIn(instance_2, filtered_instances)
        self.assertNotIn(self.instance, filtered_instances)
        self.assertNotIn(5.0, filtered_instances)
        all_instances = self.plugin_manager.get_instances(None)
        self.assertIn(self.instance, all_instances)
        self.assertIn(instance_2, all_instances)
        self.assertIn(5.0, all_instances)

#.........这里部分代码省略.........
开发者ID:benhoff,项目名称:pluginmanager,代码行数:103,代码来源:test_plugin_manager.py


注:本文中的pluginmanager.PluginManager._handle_object_instance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。