• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zope.interface / 16098775349

04 Mar 2025 10:16PM UTC coverage: 99.073% (+0.05%) from 99.022%
16098775349

push

github

web-flow
Merge pull request #338 from zopefoundation/config-with-c-code-template-3c1c588c

Apply latest meta templates, drop support for Python 3.8

2436 of 2464 branches covered (98.86%)

Branch coverage included in aggregate %.

12209 of 12318 relevant lines covered (99.12%)

6.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/src/zope/interface/tests/__init__.py
1
from zope.interface._compat import _should_attempt_c_optimizations
7✔
2

3

4
class OptimizationTestMixin:
7✔
5
    """Mixin testing that C optimizations are used when appropriate.
6
    """
7

8
    def _getTargetClass(self):
7✔
9
        """Return the implementation in use, without 'Py' or 'Fallback' suffix.
10
        """
11
        raise NotImplementedError
12

13
    def _getFallbackClass(self):
7✔
14
        """Return the fallback Python implementation.
15
        """
16
        # Is there an algorithmic way to do this? The C
17
        # objects all come from the same module so I don't see how we can
18
        # get the Python object from that.
19
        raise NotImplementedError
20

21
    def test_optimizations(self):
7✔
22
        used = self._getTargetClass()
7✔
23
        fallback = self._getFallbackClass()
7✔
24

25
        if _should_attempt_c_optimizations():
7✔
26
            self.assertIsNot(used, fallback)
6✔
27
        else:
28
            self.assertIs(used, fallback)
7✔
29

30

31
class SubclassableMixin:
7✔
32

33
    def _getTargetClass(self):
7✔
34
        """Return the implementation in use without 'Py' or 'Fallback' suffix.
35
        """
36
        raise NotImplementedError
37

38
    def test_can_subclass(self):
7✔
39
        klass = self._getTargetClass()
7✔
40

41
        class Derived(klass):  # no raise
7✔
42
            pass
7✔
43

44

45
class MissingSomeAttrs:
7✔
46
    """
47
    Helper for tests that raises a specific exception
48
    for attributes that are missing. This is usually not
49
    an AttributeError, and this object is used to test that
50
    those errors are not improperly caught and treated like
51
    an AttributeError.
52
    """
53

54
    def __init__(self, exc_kind, **other_attrs):
7✔
55
        self.__exc_kind = exc_kind
7✔
56
        d = object.__getattribute__(self, '__dict__')
7✔
57
        d.update(other_attrs)
7✔
58

59
    def __getattribute__(self, name):
7✔
60
        # Note that we ignore objects found in the class dictionary.
61
        d = object.__getattribute__(self, '__dict__')
7✔
62
        try:
7✔
63
            return d[name]
7✔
64
        except KeyError:
7✔
65
            raise d['_MissingSomeAttrs__exc_kind'](name)
7✔
66

67
    EXCEPTION_CLASSES = (
7✔
68
        TypeError,
69
        RuntimeError,
70
        BaseException,
71
        ValueError,
72
    )
73

74
    @classmethod
7✔
75
    def test_raises(cls, unittest, test_func, expected_missing, **other_attrs):
7✔
76
        """
77
        Loop through various exceptions, calling *test_func* inside a
78
        ``assertRaises`` block.
79

80
        :param test_func: A callable of one argument, the instance of this
81
            class.
82
        :param str expected_missing: The attribute that should fail with the
83
            exception.  This is used to ensure that we're testing the path we
84
            think we are.
85
        :param other_attrs: Attributes that should be provided on the test
86
            object.  Must not contain *expected_missing*.
87
        """
88
        assert isinstance(expected_missing, str)
7✔
89
        assert expected_missing not in other_attrs
7✔
90
        for exc in cls.EXCEPTION_CLASSES:
7✔
91
            ob = cls(exc, **other_attrs)
7✔
92
            with unittest.assertRaises(exc) as ex:
7✔
93
                test_func(ob)
7✔
94

95
            unittest.assertEqual(ex.exception.args[0], expected_missing)
7✔
96

97
        # Now test that the AttributeError for that expected_missing is *not*
98
        # raised.
99
        ob = cls(AttributeError, **other_attrs)
7✔
100
        try:
7✔
101
            test_func(ob)
7✔
102
        except AttributeError as e:
7!
103
            unittest.assertNotIn(expected_missing, str(e))
×
104
        except Exception:  # pylint:disable=broad-except
7✔
105
            pass
7✔
106

107
# Be sure cleanup functionality is available; classes that use the adapter hook
108
# need to be sure to subclass ``CleanUp``.
109
#
110
# If zope.component is installed and imported when we run our tests
111
# (import chain:
112
# zope.testrunner->zope.security->zope.location->zope.component.api)
113
# it adds an adapter hook that uses its global site manager. That can cause
114
# leakage from one test to another unless its cleanup hooks are run. The
115
# symptoms can be odd, especially if one test used C objects and the next used
116
# the Python implementation. (For example, you can get strange TypeErrors or
117
# find inexplicable comparisons being done.)
118

119

120
try:
7✔
121
    from zope.testing import cleanup
7✔
122
except ImportError:
123

124
    class CleanUp:
125
        def cleanUp(self):
126
            pass
127

128
        setUp = tearDown = cleanUp
129
else:
130
    CleanUp = cleanup.CleanUp
7✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc