cpython / Lib / test / test_importlib / test_abc.py

from importlib import abc
from importlib import machinery
import inspect
import unittest


class InheritanceTests:

    """Test that the specified class is a subclass/superclass of the expected
    classes."""

    subclasses = []
    superclasses = []

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        assert self.subclasses or self.superclasses, self.__class__
        self.__test = getattr(abc, self.__class__.__name__)

    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 MetaPathFinder(InheritanceTests, unittest.TestCase):

    superclasses = [abc.Finder]
    subclasses = [machinery.BuiltinImporter, machinery.FrozenImporter,
                    machinery.PathFinder, machinery.WindowsRegistryFinder]


class PathEntryFinder(InheritanceTests, unittest.TestCase):

    superclasses = [abc.Finder]
    subclasses = [machinery.FileFinder]


class ResourceLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.Loader]


class InspectLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.Loader]
    subclasses = [machinery.BuiltinImporter,
                    machinery.FrozenImporter, machinery.ExtensionFileLoader]


class ExecutionLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.InspectLoader]


class FileLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.ResourceLoader, abc.ExecutionLoader]
    subclasses = [machinery.SourceFileLoader, machinery.SourcelessFileLoader]


class SourceLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.ResourceLoader, abc.ExecutionLoader]
    subclasses = [machinery.SourceFileLoader]


def test_main():
    from test.support import run_unittest
    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)


if __name__ == '__main__':
    test_main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.