Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1 | # Copyright 2007 Google, Inc. All Rights Reserved. |
| 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | |
| 4 | """Unit tests for abc.py.""" |
| 5 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 8 | |
| 9 | import abc |
Christian Heimes | be5b30b | 2008-03-03 19:18:51 +0000 | [diff] [blame] | 10 | from inspect import isabstract |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 11 | |
| 12 | |
| 13 | class TestABC(unittest.TestCase): |
| 14 | |
| 15 | def test_abstractmethod_basics(self): |
| 16 | @abc.abstractmethod |
| 17 | def foo(self): pass |
| 18 | self.assertEqual(foo.__isabstractmethod__, True) |
| 19 | def bar(self): pass |
| 20 | self.assertEqual(hasattr(bar, "__isabstractmethod__"), False) |
| 21 | |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 22 | def test_abstractproperty_basics(self): |
| 23 | @abc.abstractproperty |
| 24 | def foo(self): pass |
| 25 | self.assertEqual(foo.__isabstractmethod__, True) |
| 26 | def bar(self): pass |
| 27 | self.assertEqual(hasattr(bar, "__isabstractmethod__"), False) |
| 28 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 29 | class C(metaclass=abc.ABCMeta): |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 30 | @abc.abstractproperty |
| 31 | def foo(self): return 3 |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 32 | class D(C): |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 33 | @property |
| 34 | def foo(self): return super().foo |
| 35 | self.assertEqual(D().foo, 3) |
| 36 | |
| 37 | def test_abstractmethod_integration(self): |
| 38 | for abstractthing in [abc.abstractmethod, abc.abstractproperty]: |
| 39 | class C(metaclass=abc.ABCMeta): |
| 40 | @abstractthing |
| 41 | def foo(self): pass # abstract |
| 42 | def bar(self): pass # concrete |
| 43 | self.assertEqual(C.__abstractmethods__, {"foo"}) |
| 44 | self.assertRaises(TypeError, C) # because foo is abstract |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 45 | self.assertTrue(isabstract(C)) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 46 | class D(C): |
| 47 | def bar(self): pass # concrete override of concrete |
| 48 | self.assertEqual(D.__abstractmethods__, {"foo"}) |
| 49 | self.assertRaises(TypeError, D) # because foo is still abstract |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 50 | self.assertTrue(isabstract(D)) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 51 | class E(D): |
| 52 | def foo(self): pass |
| 53 | self.assertEqual(E.__abstractmethods__, set()) |
| 54 | E() # now foo is concrete, too |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 55 | self.assertFalse(isabstract(E)) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 56 | class F(E): |
| 57 | @abstractthing |
| 58 | def bar(self): pass # abstract override of concrete |
| 59 | self.assertEqual(F.__abstractmethods__, {"bar"}) |
| 60 | self.assertRaises(TypeError, F) # because bar is abstract now |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 61 | self.assertTrue(isabstract(F)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 63 | def test_subclass_oldstyle_class(self): |
| 64 | class A: |
| 65 | __metaclass__ = abc.ABCMeta |
| 66 | class OldstyleClass: |
| 67 | pass |
| 68 | self.assertFalse(issubclass(OldstyleClass, A)) |
| 69 | self.assertFalse(issubclass(A, OldstyleClass)) |
| 70 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 71 | def test_registration_basics(self): |
| 72 | class A(metaclass=abc.ABCMeta): |
| 73 | pass |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 74 | class B(object): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 75 | pass |
| 76 | b = B() |
| 77 | self.assertEqual(issubclass(B, A), False) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 78 | self.assertEqual(issubclass(B, (A,)), False) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 79 | self.assertEqual(isinstance(b, A), False) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 80 | self.assertEqual(isinstance(b, (A,)), False) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 81 | A.register(B) |
| 82 | self.assertEqual(issubclass(B, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 83 | self.assertEqual(issubclass(B, (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 84 | self.assertEqual(isinstance(b, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 85 | self.assertEqual(isinstance(b, (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 86 | class C(B): |
| 87 | pass |
| 88 | c = C() |
| 89 | self.assertEqual(issubclass(C, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 90 | self.assertEqual(issubclass(C, (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 91 | self.assertEqual(isinstance(c, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 92 | self.assertEqual(isinstance(c, (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 93 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 94 | def test_isinstance_invalidation(self): |
| 95 | class A(metaclass=abc.ABCMeta): |
| 96 | pass |
| 97 | class B: |
| 98 | pass |
| 99 | b = B() |
| 100 | self.assertEqual(isinstance(b, A), False) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 101 | self.assertEqual(isinstance(b, (A,)), False) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 102 | A.register(B) |
| 103 | self.assertEqual(isinstance(b, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 104 | self.assertEqual(isinstance(b, (A,)), True) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 106 | def test_registration_builtins(self): |
| 107 | class A(metaclass=abc.ABCMeta): |
| 108 | pass |
| 109 | A.register(int) |
| 110 | self.assertEqual(isinstance(42, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 111 | self.assertEqual(isinstance(42, (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 112 | self.assertEqual(issubclass(int, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 113 | self.assertEqual(issubclass(int, (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 114 | class B(A): |
| 115 | pass |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 116 | B.register(str) |
| 117 | class C(str): pass |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 118 | self.assertEqual(isinstance("", A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 119 | self.assertEqual(isinstance("", (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 120 | self.assertEqual(issubclass(str, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 121 | self.assertEqual(issubclass(str, (A,)), True) |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 122 | self.assertEqual(issubclass(C, A), True) |
Antoine Pitrou | ec569b7 | 2008-08-26 22:40:48 +0000 | [diff] [blame] | 123 | self.assertEqual(issubclass(C, (A,)), True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 124 | |
| 125 | def test_registration_edge_cases(self): |
| 126 | class A(metaclass=abc.ABCMeta): |
| 127 | pass |
| 128 | A.register(A) # should pass silently |
| 129 | class A1(A): |
| 130 | pass |
| 131 | self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 132 | class B(object): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 133 | pass |
| 134 | A1.register(B) # ok |
| 135 | A1.register(B) # should pass silently |
| 136 | class C(A): |
| 137 | pass |
| 138 | A.register(C) # should pass silently |
| 139 | self.assertRaises(RuntimeError, C.register, A) # cycles not allowed |
| 140 | C.register(B) # ok |
| 141 | |
| 142 | def test_registration_transitiveness(self): |
| 143 | class A(metaclass=abc.ABCMeta): |
| 144 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 145 | self.assertTrue(issubclass(A, A)) |
| 146 | self.assertTrue(issubclass(A, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 147 | class B(metaclass=abc.ABCMeta): |
| 148 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 149 | self.assertFalse(issubclass(A, B)) |
| 150 | self.assertFalse(issubclass(A, (B,))) |
| 151 | self.assertFalse(issubclass(B, A)) |
| 152 | self.assertFalse(issubclass(B, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 153 | class C(metaclass=abc.ABCMeta): |
| 154 | pass |
| 155 | A.register(B) |
| 156 | class B1(B): |
| 157 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 158 | self.assertTrue(issubclass(B1, A)) |
| 159 | self.assertTrue(issubclass(B1, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 160 | class C1(C): |
| 161 | pass |
| 162 | B1.register(C1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 163 | self.assertFalse(issubclass(C, B)) |
| 164 | self.assertFalse(issubclass(C, (B,))) |
| 165 | self.assertFalse(issubclass(C, B1)) |
| 166 | self.assertFalse(issubclass(C, (B1,))) |
| 167 | self.assertTrue(issubclass(C1, A)) |
| 168 | self.assertTrue(issubclass(C1, (A,))) |
| 169 | self.assertTrue(issubclass(C1, B)) |
| 170 | self.assertTrue(issubclass(C1, (B,))) |
| 171 | self.assertTrue(issubclass(C1, B1)) |
| 172 | self.assertTrue(issubclass(C1, (B1,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 173 | C1.register(int) |
| 174 | class MyInt(int): |
| 175 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 176 | self.assertTrue(issubclass(MyInt, A)) |
| 177 | self.assertTrue(issubclass(MyInt, (A,))) |
| 178 | self.assertTrue(isinstance(42, A)) |
| 179 | self.assertTrue(isinstance(42, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | 894d35e | 2007-09-11 20:42:30 +0000 | [diff] [blame] | 181 | def test_all_new_methods_are_called(self): |
| 182 | class A(metaclass=abc.ABCMeta): |
| 183 | pass |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 184 | class B(object): |
Guido van Rossum | 894d35e | 2007-09-11 20:42:30 +0000 | [diff] [blame] | 185 | counter = 0 |
| 186 | def __new__(cls): |
| 187 | B.counter += 1 |
| 188 | return super().__new__(cls) |
| 189 | class C(A, B): |
| 190 | pass |
| 191 | self.assertEqual(B.counter, 0) |
| 192 | C() |
| 193 | self.assertEqual(B.counter, 1) |
| 194 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 195 | |
| 196 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 197 | support.run_unittest(TestABC) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 198 | |
| 199 | |
| 200 | if __name__ == "__main__": |
| 201 | unittest.main() |