summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib/test_abc.py
blob: c620c3771b4aff5b7ba5a20f317da148f7c6485f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 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,
                    machinery.FrozenImporter, machinery.ExtensionFileLoader]


class ExecutionLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.InspectLoader]
    subclasses = [abc.PyLoader]


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]


class PyLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.Loader, abc.ResourceLoader, abc.ExecutionLoader]


class PyPycLoader(InheritanceTests, unittest.TestCase):

    superclasses = [abc.PyLoader]


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()