2009-03-09 00:35:50 -03:00
|
|
|
from importlib import abc
|
|
|
|
from importlib import machinery
|
2009-07-20 00:19:18 -03:00
|
|
|
import inspect
|
2009-03-09 00:35:50 -03:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2009-07-20 00:19:18 -03:00
|
|
|
class InheritanceTests:
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-07-20 00:19:18 -03:00
|
|
|
"""Test that the specified class is a subclass/superclass of the expected
|
|
|
|
classes."""
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-07-20 00:19:18 -03:00
|
|
|
subclasses = []
|
|
|
|
superclasses = []
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-07-20 00:19:18 -03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
assert self.subclasses or self.superclasses, self.__class__
|
|
|
|
self.__test = getattr(abc, self.__class__.__name__)
|
2009-03-09 00:35:50 -03:00
|
|
|
|
2009-07-20 00:19:18 -03:00
|
|
|
def test_subclasses(self):
|
|
|
|
# Test that the expected subclasses inherit.
|
|
|
|
for subclass in self.subclasses:
|
|
|
|
self.assertTrue(issubclass(subclass, self.__test),
|
|
|
|
"{0} is not a subclass of {1}".format(subclass, self.__test))
|
|
|
|
|
|
|
|
def test_superclasses(self):
|
|
|
|
# Test that the class inherits from the expected superclasses.
|
|
|
|
for superclass in self.superclasses:
|
|
|
|
self.assertTrue(issubclass(self.__test, superclass),
|
|
|
|
"{0} is not a superclass of {1}".format(superclass, self.__test))
|
|
|
|
|
|
|
|
|
|
|
|
class Finder(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
subclasses = [machinery.BuiltinImporter, machinery.FrozenImporter,
|
|
|
|
machinery.PathFinder]
|
|
|
|
|
|
|
|
|
|
|
|
class Loader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
subclasses = [abc.PyLoader]
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceLoader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
superclasses = [abc.Loader]
|
|
|
|
|
|
|
|
|
|
|
|
class InspectLoader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
superclasses = [abc.Loader]
|
|
|
|
subclasses = [abc.PyLoader, machinery.BuiltinImporter,
|
2012-04-22 20:58:33 -03:00
|
|
|
machinery.FrozenImporter, machinery.ExtensionFileLoader]
|
2009-07-20 00:19:18 -03:00
|
|
|
|
|
|
|
|
2009-07-20 01:23:48 -03:00
|
|
|
class ExecutionLoader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
superclasses = [abc.InspectLoader]
|
|
|
|
subclasses = [abc.PyLoader]
|
|
|
|
|
|
|
|
|
2012-04-22 20:58:33 -03:00
|
|
|
class FileLoader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
superclasses = [abc.ResourceLoader, abc.ExecutionLoader]
|
2012-04-24 21:31:37 -03:00
|
|
|
subclasses = [machinery.SourceFileLoader, machinery.SourcelessFileLoader]
|
2012-04-22 20:58:33 -03:00
|
|
|
|
|
|
|
|
2010-06-28 02:46:25 -03:00
|
|
|
class SourceLoader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
superclasses = [abc.ResourceLoader, abc.ExecutionLoader]
|
2012-04-22 20:58:33 -03:00
|
|
|
subclasses = [machinery.SourceFileLoader]
|
2010-06-28 02:46:25 -03:00
|
|
|
|
|
|
|
|
2009-07-20 00:19:18 -03:00
|
|
|
class PyLoader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
2009-07-20 01:23:48 -03:00
|
|
|
superclasses = [abc.Loader, abc.ResourceLoader, abc.ExecutionLoader]
|
2009-07-20 00:19:18 -03:00
|
|
|
|
|
|
|
|
|
|
|
class PyPycLoader(InheritanceTests, unittest.TestCase):
|
|
|
|
|
|
|
|
superclasses = [abc.PyLoader]
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
from test.support import run_unittest
|
2009-07-20 00:19:18 -03:00
|
|
|
classes = []
|
|
|
|
for class_ in globals().values():
|
|
|
|
if (inspect.isclass(class_) and
|
|
|
|
issubclass(class_, unittest.TestCase) and
|
|
|
|
issubclass(class_, InheritanceTests)):
|
|
|
|
classes.append(class_)
|
|
|
|
run_unittest(*classes)
|
2009-03-09 00:35:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|