ยปCore Development>Code coverage>Lib/test/test_abc.py

Python code coverage for Lib/test/test_abc.py

#countcontent
1n/a# Copyright 2007 Google, Inc. All Rights Reserved.
2n/a# Licensed to PSF under a Contributor Agreement.
3n/a
4n/a"""Unit tests for abc.py."""
5n/a
6n/aimport unittest
7n/afrom test import support
8n/a
9n/aimport abc
10n/afrom inspect import isabstract
11n/a
12n/a
13n/aclass TestLegacyAPI(unittest.TestCase):
14n/a
15n/a def test_abstractproperty_basics(self):
16n/a @abc.abstractproperty
17n/a def foo(self): pass
18n/a self.assertTrue(foo.__isabstractmethod__)
19n/a def bar(self): pass
20n/a self.assertFalse(hasattr(bar, "__isabstractmethod__"))
21n/a
22n/a class C(metaclass=abc.ABCMeta):
23n/a @abc.abstractproperty
24n/a def foo(self): return 3
25n/a self.assertRaises(TypeError, C)
26n/a class D(C):
27n/a @property
28n/a def foo(self): return super().foo
29n/a self.assertEqual(D().foo, 3)
30n/a self.assertFalse(getattr(D.foo, "__isabstractmethod__", False))
31n/a
32n/a def test_abstractclassmethod_basics(self):
33n/a @abc.abstractclassmethod
34n/a def foo(cls): pass
35n/a self.assertTrue(foo.__isabstractmethod__)
36n/a @classmethod
37n/a def bar(cls): pass
38n/a self.assertFalse(getattr(bar, "__isabstractmethod__", False))
39n/a
40n/a class C(metaclass=abc.ABCMeta):
41n/a @abc.abstractclassmethod
42n/a def foo(cls): return cls.__name__
43n/a self.assertRaises(TypeError, C)
44n/a class D(C):
45n/a @classmethod
46n/a def foo(cls): return super().foo()
47n/a self.assertEqual(D.foo(), 'D')
48n/a self.assertEqual(D().foo(), 'D')
49n/a
50n/a def test_abstractstaticmethod_basics(self):
51n/a @abc.abstractstaticmethod
52n/a def foo(): pass
53n/a self.assertTrue(foo.__isabstractmethod__)
54n/a @staticmethod
55n/a def bar(): pass
56n/a self.assertFalse(getattr(bar, "__isabstractmethod__", False))
57n/a
58n/a class C(metaclass=abc.ABCMeta):
59n/a @abc.abstractstaticmethod
60n/a def foo(): return 3
61n/a self.assertRaises(TypeError, C)
62n/a class D(C):
63n/a @staticmethod
64n/a def foo(): return 4
65n/a self.assertEqual(D.foo(), 4)
66n/a self.assertEqual(D().foo(), 4)
67n/a
68n/a
69n/aclass TestABC(unittest.TestCase):
70n/a
71n/a def test_ABC_helper(self):
72n/a # create an ABC using the helper class and perform basic checks
73n/a class C(abc.ABC):
74n/a @classmethod
75n/a @abc.abstractmethod
76n/a def foo(cls): return cls.__name__
77n/a self.assertEqual(type(C), abc.ABCMeta)
78n/a self.assertRaises(TypeError, C)
79n/a class D(C):
80n/a @classmethod
81n/a def foo(cls): return super().foo()
82n/a self.assertEqual(D.foo(), 'D')
83n/a
84n/a def test_abstractmethod_basics(self):
85n/a @abc.abstractmethod
86n/a def foo(self): pass
87n/a self.assertTrue(foo.__isabstractmethod__)
88n/a def bar(self): pass
89n/a self.assertFalse(hasattr(bar, "__isabstractmethod__"))
90n/a
91n/a def test_abstractproperty_basics(self):
92n/a @property
93n/a @abc.abstractmethod
94n/a def foo(self): pass
95n/a self.assertTrue(foo.__isabstractmethod__)
96n/a def bar(self): pass
97n/a self.assertFalse(getattr(bar, "__isabstractmethod__", False))
98n/a
99n/a class C(metaclass=abc.ABCMeta):
100n/a @property
101n/a @abc.abstractmethod
102n/a def foo(self): return 3
103n/a self.assertRaises(TypeError, C)
104n/a class D(C):
105n/a @C.foo.getter
106n/a def foo(self): return super().foo
107n/a self.assertEqual(D().foo, 3)
108n/a
109n/a def test_abstractclassmethod_basics(self):
110n/a @classmethod
111n/a @abc.abstractmethod
112n/a def foo(cls): pass
113n/a self.assertTrue(foo.__isabstractmethod__)
114n/a @classmethod
115n/a def bar(cls): pass
116n/a self.assertFalse(getattr(bar, "__isabstractmethod__", False))
117n/a
118n/a class C(metaclass=abc.ABCMeta):
119n/a @classmethod
120n/a @abc.abstractmethod
121n/a def foo(cls): return cls.__name__
122n/a self.assertRaises(TypeError, C)
123n/a class D(C):
124n/a @classmethod
125n/a def foo(cls): return super().foo()
126n/a self.assertEqual(D.foo(), 'D')
127n/a self.assertEqual(D().foo(), 'D')
128n/a
129n/a def test_abstractstaticmethod_basics(self):
130n/a @staticmethod
131n/a @abc.abstractmethod
132n/a def foo(): pass
133n/a self.assertTrue(foo.__isabstractmethod__)
134n/a @staticmethod
135n/a def bar(): pass
136n/a self.assertFalse(getattr(bar, "__isabstractmethod__", False))
137n/a
138n/a class C(metaclass=abc.ABCMeta):
139n/a @staticmethod
140n/a @abc.abstractmethod
141n/a def foo(): return 3
142n/a self.assertRaises(TypeError, C)
143n/a class D(C):
144n/a @staticmethod
145n/a def foo(): return 4
146n/a self.assertEqual(D.foo(), 4)
147n/a self.assertEqual(D().foo(), 4)
148n/a
149n/a def test_abstractmethod_integration(self):
150n/a for abstractthing in [abc.abstractmethod, abc.abstractproperty,
151n/a abc.abstractclassmethod,
152n/a abc.abstractstaticmethod]:
153n/a class C(metaclass=abc.ABCMeta):
154n/a @abstractthing
155n/a def foo(self): pass # abstract
156n/a def bar(self): pass # concrete
157n/a self.assertEqual(C.__abstractmethods__, {"foo"})
158n/a self.assertRaises(TypeError, C) # because foo is abstract
159n/a self.assertTrue(isabstract(C))
160n/a class D(C):
161n/a def bar(self): pass # concrete override of concrete
162n/a self.assertEqual(D.__abstractmethods__, {"foo"})
163n/a self.assertRaises(TypeError, D) # because foo is still abstract
164n/a self.assertTrue(isabstract(D))
165n/a class E(D):
166n/a def foo(self): pass
167n/a self.assertEqual(E.__abstractmethods__, set())
168n/a E() # now foo is concrete, too
169n/a self.assertFalse(isabstract(E))
170n/a class F(E):
171n/a @abstractthing
172n/a def bar(self): pass # abstract override of concrete
173n/a self.assertEqual(F.__abstractmethods__, {"bar"})
174n/a self.assertRaises(TypeError, F) # because bar is abstract now
175n/a self.assertTrue(isabstract(F))
176n/a
177n/a def test_descriptors_with_abstractmethod(self):
178n/a class C(metaclass=abc.ABCMeta):
179n/a @property
180n/a @abc.abstractmethod
181n/a def foo(self): return 3
182n/a @foo.setter
183n/a @abc.abstractmethod
184n/a def foo(self, val): pass
185n/a self.assertRaises(TypeError, C)
186n/a class D(C):
187n/a @C.foo.getter
188n/a def foo(self): return super().foo
189n/a self.assertRaises(TypeError, D)
190n/a class E(D):
191n/a @D.foo.setter
192n/a def foo(self, val): pass
193n/a self.assertEqual(E().foo, 3)
194n/a # check that the property's __isabstractmethod__ descriptor does the
195n/a # right thing when presented with a value that fails truth testing:
196n/a class NotBool(object):
197n/a def __bool__(self):
198n/a raise ValueError()
199n/a __len__ = __bool__
200n/a with self.assertRaises(ValueError):
201n/a class F(C):
202n/a def bar(self):
203n/a pass
204n/a bar.__isabstractmethod__ = NotBool()
205n/a foo = property(bar)
206n/a
207n/a
208n/a def test_customdescriptors_with_abstractmethod(self):
209n/a class Descriptor:
210n/a def __init__(self, fget, fset=None):
211n/a self._fget = fget
212n/a self._fset = fset
213n/a def getter(self, callable):
214n/a return Descriptor(callable, self._fget)
215n/a def setter(self, callable):
216n/a return Descriptor(self._fget, callable)
217n/a @property
218n/a def __isabstractmethod__(self):
219n/a return (getattr(self._fget, '__isabstractmethod__', False)
220n/a or getattr(self._fset, '__isabstractmethod__', False))
221n/a class C(metaclass=abc.ABCMeta):
222n/a @Descriptor
223n/a @abc.abstractmethod
224n/a def foo(self): return 3
225n/a @foo.setter
226n/a @abc.abstractmethod
227n/a def foo(self, val): pass
228n/a self.assertRaises(TypeError, C)
229n/a class D(C):
230n/a @C.foo.getter
231n/a def foo(self): return super().foo
232n/a self.assertRaises(TypeError, D)
233n/a class E(D):
234n/a @D.foo.setter
235n/a def foo(self, val): pass
236n/a self.assertFalse(E.foo.__isabstractmethod__)
237n/a
238n/a def test_metaclass_abc(self):
239n/a # Metaclasses can be ABCs, too.
240n/a class A(metaclass=abc.ABCMeta):
241n/a @abc.abstractmethod
242n/a def x(self):
243n/a pass
244n/a self.assertEqual(A.__abstractmethods__, {"x"})
245n/a class meta(type, A):
246n/a def x(self):
247n/a return 1
248n/a class C(metaclass=meta):
249n/a pass
250n/a
251n/a def test_registration_basics(self):
252n/a class A(metaclass=abc.ABCMeta):
253n/a pass
254n/a class B(object):
255n/a pass
256n/a b = B()
257n/a self.assertFalse(issubclass(B, A))
258n/a self.assertFalse(issubclass(B, (A,)))
259n/a self.assertNotIsInstance(b, A)
260n/a self.assertNotIsInstance(b, (A,))
261n/a B1 = A.register(B)
262n/a self.assertTrue(issubclass(B, A))
263n/a self.assertTrue(issubclass(B, (A,)))
264n/a self.assertIsInstance(b, A)
265n/a self.assertIsInstance(b, (A,))
266n/a self.assertIs(B1, B)
267n/a class C(B):
268n/a pass
269n/a c = C()
270n/a self.assertTrue(issubclass(C, A))
271n/a self.assertTrue(issubclass(C, (A,)))
272n/a self.assertIsInstance(c, A)
273n/a self.assertIsInstance(c, (A,))
274n/a
275n/a def test_register_as_class_deco(self):
276n/a class A(metaclass=abc.ABCMeta):
277n/a pass
278n/a @A.register
279n/a class B(object):
280n/a pass
281n/a b = B()
282n/a self.assertTrue(issubclass(B, A))
283n/a self.assertTrue(issubclass(B, (A,)))
284n/a self.assertIsInstance(b, A)
285n/a self.assertIsInstance(b, (A,))
286n/a @A.register
287n/a class C(B):
288n/a pass
289n/a c = C()
290n/a self.assertTrue(issubclass(C, A))
291n/a self.assertTrue(issubclass(C, (A,)))
292n/a self.assertIsInstance(c, A)
293n/a self.assertIsInstance(c, (A,))
294n/a self.assertIs(C, A.register(C))
295n/a
296n/a def test_isinstance_invalidation(self):
297n/a class A(metaclass=abc.ABCMeta):
298n/a pass
299n/a class B:
300n/a pass
301n/a b = B()
302n/a self.assertFalse(isinstance(b, A))
303n/a self.assertFalse(isinstance(b, (A,)))
304n/a token_old = abc.get_cache_token()
305n/a A.register(B)
306n/a token_new = abc.get_cache_token()
307n/a self.assertNotEqual(token_old, token_new)
308n/a self.assertTrue(isinstance(b, A))
309n/a self.assertTrue(isinstance(b, (A,)))
310n/a
311n/a def test_registration_builtins(self):
312n/a class A(metaclass=abc.ABCMeta):
313n/a pass
314n/a A.register(int)
315n/a self.assertIsInstance(42, A)
316n/a self.assertIsInstance(42, (A,))
317n/a self.assertTrue(issubclass(int, A))
318n/a self.assertTrue(issubclass(int, (A,)))
319n/a class B(A):
320n/a pass
321n/a B.register(str)
322n/a class C(str): pass
323n/a self.assertIsInstance("", A)
324n/a self.assertIsInstance("", (A,))
325n/a self.assertTrue(issubclass(str, A))
326n/a self.assertTrue(issubclass(str, (A,)))
327n/a self.assertTrue(issubclass(C, A))
328n/a self.assertTrue(issubclass(C, (A,)))
329n/a
330n/a def test_registration_edge_cases(self):
331n/a class A(metaclass=abc.ABCMeta):
332n/a pass
333n/a A.register(A) # should pass silently
334n/a class A1(A):
335n/a pass
336n/a self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed
337n/a class B(object):
338n/a pass
339n/a A1.register(B) # ok
340n/a A1.register(B) # should pass silently
341n/a class C(A):
342n/a pass
343n/a A.register(C) # should pass silently
344n/a self.assertRaises(RuntimeError, C.register, A) # cycles not allowed
345n/a C.register(B) # ok
346n/a
347n/a def test_register_non_class(self):
348n/a class A(metaclass=abc.ABCMeta):
349n/a pass
350n/a self.assertRaisesRegex(TypeError, "Can only register classes",
351n/a A.register, 4)
352n/a
353n/a def test_registration_transitiveness(self):
354n/a class A(metaclass=abc.ABCMeta):
355n/a pass
356n/a self.assertTrue(issubclass(A, A))
357n/a self.assertTrue(issubclass(A, (A,)))
358n/a class B(metaclass=abc.ABCMeta):
359n/a pass
360n/a self.assertFalse(issubclass(A, B))
361n/a self.assertFalse(issubclass(A, (B,)))
362n/a self.assertFalse(issubclass(B, A))
363n/a self.assertFalse(issubclass(B, (A,)))
364n/a class C(metaclass=abc.ABCMeta):
365n/a pass
366n/a A.register(B)
367n/a class B1(B):
368n/a pass
369n/a self.assertTrue(issubclass(B1, A))
370n/a self.assertTrue(issubclass(B1, (A,)))
371n/a class C1(C):
372n/a pass
373n/a B1.register(C1)
374n/a self.assertFalse(issubclass(C, B))
375n/a self.assertFalse(issubclass(C, (B,)))
376n/a self.assertFalse(issubclass(C, B1))
377n/a self.assertFalse(issubclass(C, (B1,)))
378n/a self.assertTrue(issubclass(C1, A))
379n/a self.assertTrue(issubclass(C1, (A,)))
380n/a self.assertTrue(issubclass(C1, B))
381n/a self.assertTrue(issubclass(C1, (B,)))
382n/a self.assertTrue(issubclass(C1, B1))
383n/a self.assertTrue(issubclass(C1, (B1,)))
384n/a C1.register(int)
385n/a class MyInt(int):
386n/a pass
387n/a self.assertTrue(issubclass(MyInt, A))
388n/a self.assertTrue(issubclass(MyInt, (A,)))
389n/a self.assertIsInstance(42, A)
390n/a self.assertIsInstance(42, (A,))
391n/a
392n/a def test_all_new_methods_are_called(self):
393n/a class A(metaclass=abc.ABCMeta):
394n/a pass
395n/a class B(object):
396n/a counter = 0
397n/a def __new__(cls):
398n/a B.counter += 1
399n/a return super().__new__(cls)
400n/a class C(A, B):
401n/a pass
402n/a self.assertEqual(B.counter, 0)
403n/a C()
404n/a self.assertEqual(B.counter, 1)
405n/a
406n/a
407n/aif __name__ == "__main__":
408n/a unittest.main()