ClassFinder: fixed unexpected behaviour, add tests for this
This commit is contained in:
parent
4dd0d0b7c3
commit
2106585d4c
@ -5,7 +5,6 @@ from i3pystatus.core.exceptions import ConfigAmbigiousClassesError, ConfigInvali
|
|||||||
|
|
||||||
|
|
||||||
class ClassFinder:
|
class ClassFinder:
|
||||||
|
|
||||||
"""Support class to find classes of specific bases in a module"""
|
"""Support class to find classes of specific bases in a module"""
|
||||||
|
|
||||||
def __init__(self, baseclass):
|
def __init__(self, baseclass):
|
||||||
@ -20,11 +19,13 @@ class ClassFinder:
|
|||||||
)
|
)
|
||||||
return predicate
|
return predicate
|
||||||
|
|
||||||
def search_module(self, module):
|
def get_matching_classes(self, module):
|
||||||
return list(zip(*inspect.getmembers(module, self.predicate_factory(module))))[1]
|
# Transpose [ (name, list), ... ] to ( [name, ...], [list, ...] )
|
||||||
|
classes = list(zip(*inspect.getmembers(module, self.predicate_factory(module))))
|
||||||
|
return classes[1] if classes else []
|
||||||
|
|
||||||
def get_class(self, module):
|
def get_class(self, module):
|
||||||
classes = self.search_module(module)
|
classes = self.get_matching_classes(module)
|
||||||
|
|
||||||
if len(classes) > 1:
|
if len(classes) > 1:
|
||||||
# If there are multiple Module clases bundled in one module,
|
# If there are multiple Module clases bundled in one module,
|
||||||
|
@ -6,7 +6,8 @@ import string
|
|||||||
import random
|
import random
|
||||||
import types
|
import types
|
||||||
|
|
||||||
from i3pystatus.core import util
|
from i3pystatus.core.exceptions import ConfigAmbigiousClassesError, ConfigInvalidModuleError
|
||||||
|
from i3pystatus.core import util, ClassFinder
|
||||||
|
|
||||||
|
|
||||||
def get_random_string(length=6, chars=string.printable):
|
def get_random_string(length=6, chars=string.printable):
|
||||||
@ -170,12 +171,29 @@ class ModuleListTests(unittest.TestCase):
|
|||||||
pymod.some_class = self._create_module_class("some_class")
|
pymod.some_class = self._create_module_class("some_class")
|
||||||
pymod.some_class.__module__ = "other_module"
|
pymod.some_class.__module__ = "other_module"
|
||||||
|
|
||||||
with self.assertRaises(IndexError):
|
with self.assertRaises(ConfigInvalidModuleError):
|
||||||
self.ml.append(pymod)
|
self.ml.append(pymod)
|
||||||
|
|
||||||
assert not pymod.some_class.__init__.called
|
assert not pymod.some_class.__init__.called
|
||||||
assert not pymod.some_class.registered.called
|
assert not pymod.some_class.registered.called
|
||||||
|
|
||||||
|
def test_ambigious_classdef(self):
|
||||||
|
pymod = types.ModuleType("test_mod")
|
||||||
|
pymod.some_class = self._create_module_class("some_class")
|
||||||
|
pymod.some_class.__module__ = "test_mod"
|
||||||
|
pymod.some_other_class = self._create_module_class("some_other_class")
|
||||||
|
pymod.some_other_class.__module__ = "test_mod"
|
||||||
|
|
||||||
|
with self.assertRaises(ConfigAmbigiousClassesError):
|
||||||
|
self.ml.append(pymod)
|
||||||
|
|
||||||
|
def test_invalid_module(self):
|
||||||
|
pymod = types.ModuleType("test_mod")
|
||||||
|
|
||||||
|
with self.assertRaises(ConfigInvalidModuleError):
|
||||||
|
self.ml.append(pymod)
|
||||||
|
|
||||||
|
|
||||||
def test_append_class_inheritance(self):
|
def test_append_class_inheritance(self):
|
||||||
in_between = self._create_module_class("in_between")
|
in_between = self._create_module_class("in_between")
|
||||||
cls = self._create_module_class("cls", (in_between,))
|
cls = self._create_module_class("cls", (in_between,))
|
||||||
|
Loading…
Reference in New Issue
Block a user