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 |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 18 | self.assertTrue(foo.__isabstractmethod__) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 19 | def bar(self): pass |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 20 | self.assertFalse(hasattr(bar, "__isabstractmethod__")) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 25 | self.assertTrue(foo.__isabstractmethod__) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 26 | def bar(self): pass |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 27 | self.assertFalse(hasattr(bar, "__isabstractmethod__")) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 37 | def test_abstractclassmethod_basics(self): |
| 38 | @abc.abstractclassmethod |
| 39 | def foo(cls): pass |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 40 | self.assertTrue(foo.__isabstractmethod__) |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 41 | @classmethod |
| 42 | def bar(cls): pass |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 43 | self.assertFalse(hasattr(bar, "__isabstractmethod__")) |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 44 | |
| 45 | class C(metaclass=abc.ABCMeta): |
| 46 | @abc.abstractclassmethod |
| 47 | def foo(cls): return cls.__name__ |
| 48 | self.assertRaises(TypeError, C) |
| 49 | class D(C): |
| 50 | @classmethod |
| 51 | def foo(cls): return super().foo() |
| 52 | self.assertEqual(D.foo(), 'D') |
| 53 | self.assertEqual(D().foo(), 'D') |
| 54 | |
| 55 | def test_abstractstaticmethod_basics(self): |
| 56 | @abc.abstractstaticmethod |
| 57 | def foo(): pass |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 58 | self.assertTrue(foo.__isabstractmethod__) |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 59 | @staticmethod |
| 60 | def bar(): pass |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 61 | self.assertFalse(hasattr(bar, "__isabstractmethod__")) |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 62 | |
| 63 | class C(metaclass=abc.ABCMeta): |
| 64 | @abc.abstractstaticmethod |
| 65 | def foo(): return 3 |
| 66 | self.assertRaises(TypeError, C) |
| 67 | class D(C): |
| 68 | @staticmethod |
| 69 | def foo(): return 4 |
| 70 | self.assertEqual(D.foo(), 4) |
| 71 | self.assertEqual(D().foo(), 4) |
| 72 | |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 73 | def test_abstractmethod_integration(self): |
Benjamin Peterson | 45c257f | 2010-08-17 00:52:52 +0000 | [diff] [blame] | 74 | for abstractthing in [abc.abstractmethod, abc.abstractproperty, |
| 75 | abc.abstractclassmethod, |
| 76 | abc.abstractstaticmethod]: |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 77 | class C(metaclass=abc.ABCMeta): |
| 78 | @abstractthing |
| 79 | def foo(self): pass # abstract |
| 80 | def bar(self): pass # concrete |
| 81 | self.assertEqual(C.__abstractmethods__, {"foo"}) |
| 82 | self.assertRaises(TypeError, C) # because foo is abstract |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 83 | self.assertTrue(isabstract(C)) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 84 | class D(C): |
| 85 | def bar(self): pass # concrete override of concrete |
| 86 | self.assertEqual(D.__abstractmethods__, {"foo"}) |
| 87 | self.assertRaises(TypeError, D) # because foo is still abstract |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 88 | self.assertTrue(isabstract(D)) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 89 | class E(D): |
| 90 | def foo(self): pass |
| 91 | self.assertEqual(E.__abstractmethods__, set()) |
| 92 | E() # now foo is concrete, too |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertFalse(isabstract(E)) |
Guido van Rossum | 70d2b89 | 2007-08-01 17:52:23 +0000 | [diff] [blame] | 94 | class F(E): |
| 95 | @abstractthing |
| 96 | def bar(self): pass # abstract override of concrete |
| 97 | self.assertEqual(F.__abstractmethods__, {"bar"}) |
| 98 | self.assertRaises(TypeError, F) # because bar is abstract now |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 99 | self.assertTrue(isabstract(F)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 100 | |
Benjamin Peterson | 970d188 | 2010-10-02 17:55:47 +0000 | [diff] [blame] | 101 | def test_metaclass_abc(self): |
| 102 | # Metaclasses can be ABCs, too. |
| 103 | class A(metaclass=abc.ABCMeta): |
| 104 | @abc.abstractmethod |
| 105 | def x(self): |
| 106 | pass |
| 107 | self.assertEqual(A.__abstractmethods__, {"x"}) |
| 108 | class meta(type, A): |
| 109 | def x(self): |
| 110 | return 1 |
| 111 | class C(metaclass=meta): |
| 112 | pass |
| 113 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 114 | def test_registration_basics(self): |
| 115 | class A(metaclass=abc.ABCMeta): |
| 116 | pass |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 117 | class B(object): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 118 | pass |
| 119 | b = B() |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 120 | self.assertFalse(issubclass(B, A)) |
| 121 | self.assertFalse(issubclass(B, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 122 | self.assertNotIsInstance(b, A) |
| 123 | self.assertNotIsInstance(b, (A,)) |
Éric Araujo | 6c3787c | 2011-02-24 18:03:10 +0000 | [diff] [blame^] | 124 | B1 = A.register(B) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 125 | self.assertTrue(issubclass(B, A)) |
| 126 | self.assertTrue(issubclass(B, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 127 | self.assertIsInstance(b, A) |
| 128 | self.assertIsInstance(b, (A,)) |
Éric Araujo | 6c3787c | 2011-02-24 18:03:10 +0000 | [diff] [blame^] | 129 | self.assertIs(B1, B) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 130 | class C(B): |
| 131 | pass |
| 132 | c = C() |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 133 | self.assertTrue(issubclass(C, A)) |
| 134 | self.assertTrue(issubclass(C, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 135 | self.assertIsInstance(c, A) |
| 136 | self.assertIsInstance(c, (A,)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 137 | |
Éric Araujo | 6c3787c | 2011-02-24 18:03:10 +0000 | [diff] [blame^] | 138 | def test_register_as_class_deco(self): |
| 139 | class A(metaclass=abc.ABCMeta): |
| 140 | pass |
| 141 | @A.register |
| 142 | class B(object): |
| 143 | pass |
| 144 | b = B() |
| 145 | self.assertTrue(issubclass(B, A)) |
| 146 | self.assertTrue(issubclass(B, (A,))) |
| 147 | self.assertIsInstance(b, A) |
| 148 | self.assertIsInstance(b, (A,)) |
| 149 | @A.register |
| 150 | class C(B): |
| 151 | pass |
| 152 | c = C() |
| 153 | self.assertTrue(issubclass(C, A)) |
| 154 | self.assertTrue(issubclass(C, (A,))) |
| 155 | self.assertIsInstance(c, A) |
| 156 | self.assertIsInstance(c, (A,)) |
| 157 | self.assertIs(C, A.register(C)) |
| 158 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 159 | def test_isinstance_invalidation(self): |
| 160 | class A(metaclass=abc.ABCMeta): |
| 161 | pass |
| 162 | class B: |
| 163 | pass |
| 164 | b = B() |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 165 | self.assertFalse(isinstance(b, A)) |
| 166 | self.assertFalse(isinstance(b, (A,))) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 167 | A.register(B) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 168 | self.assertTrue(isinstance(b, A)) |
| 169 | self.assertTrue(isinstance(b, (A,))) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 171 | def test_registration_builtins(self): |
| 172 | class A(metaclass=abc.ABCMeta): |
| 173 | pass |
| 174 | A.register(int) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 175 | self.assertIsInstance(42, A) |
| 176 | self.assertIsInstance(42, (A,)) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 177 | self.assertTrue(issubclass(int, A)) |
| 178 | self.assertTrue(issubclass(int, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 179 | class B(A): |
| 180 | pass |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 181 | B.register(str) |
| 182 | class C(str): pass |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 183 | self.assertIsInstance("", A) |
| 184 | self.assertIsInstance("", (A,)) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 185 | self.assertTrue(issubclass(str, A)) |
| 186 | self.assertTrue(issubclass(str, (A,))) |
| 187 | self.assertTrue(issubclass(C, A)) |
| 188 | self.assertTrue(issubclass(C, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 189 | |
| 190 | def test_registration_edge_cases(self): |
| 191 | class A(metaclass=abc.ABCMeta): |
| 192 | pass |
| 193 | A.register(A) # should pass silently |
| 194 | class A1(A): |
| 195 | pass |
| 196 | self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 197 | class B(object): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 198 | pass |
| 199 | A1.register(B) # ok |
| 200 | A1.register(B) # should pass silently |
| 201 | class C(A): |
| 202 | pass |
| 203 | A.register(C) # should pass silently |
| 204 | self.assertRaises(RuntimeError, C.register, A) # cycles not allowed |
| 205 | C.register(B) # ok |
| 206 | |
Benjamin Peterson | d632664 | 2010-01-27 02:25:58 +0000 | [diff] [blame] | 207 | def test_register_non_class(self): |
| 208 | class A(metaclass=abc.ABCMeta): |
| 209 | pass |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 210 | self.assertRaisesRegex(TypeError, "Can only register classes", |
| 211 | A.register, 4) |
Benjamin Peterson | d632664 | 2010-01-27 02:25:58 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 213 | def test_registration_transitiveness(self): |
| 214 | class A(metaclass=abc.ABCMeta): |
| 215 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 216 | self.assertTrue(issubclass(A, A)) |
| 217 | self.assertTrue(issubclass(A, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 218 | class B(metaclass=abc.ABCMeta): |
| 219 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 220 | self.assertFalse(issubclass(A, B)) |
| 221 | self.assertFalse(issubclass(A, (B,))) |
| 222 | self.assertFalse(issubclass(B, A)) |
| 223 | self.assertFalse(issubclass(B, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 224 | class C(metaclass=abc.ABCMeta): |
| 225 | pass |
| 226 | A.register(B) |
| 227 | class B1(B): |
| 228 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 229 | self.assertTrue(issubclass(B1, A)) |
| 230 | self.assertTrue(issubclass(B1, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 231 | class C1(C): |
| 232 | pass |
| 233 | B1.register(C1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 234 | self.assertFalse(issubclass(C, B)) |
| 235 | self.assertFalse(issubclass(C, (B,))) |
| 236 | self.assertFalse(issubclass(C, B1)) |
| 237 | self.assertFalse(issubclass(C, (B1,))) |
| 238 | self.assertTrue(issubclass(C1, A)) |
| 239 | self.assertTrue(issubclass(C1, (A,))) |
| 240 | self.assertTrue(issubclass(C1, B)) |
| 241 | self.assertTrue(issubclass(C1, (B,))) |
| 242 | self.assertTrue(issubclass(C1, B1)) |
| 243 | self.assertTrue(issubclass(C1, (B1,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 244 | C1.register(int) |
| 245 | class MyInt(int): |
| 246 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 247 | self.assertTrue(issubclass(MyInt, A)) |
| 248 | self.assertTrue(issubclass(MyInt, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 249 | self.assertIsInstance(42, A) |
| 250 | self.assertIsInstance(42, (A,)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | 894d35e | 2007-09-11 20:42:30 +0000 | [diff] [blame] | 252 | def test_all_new_methods_are_called(self): |
| 253 | class A(metaclass=abc.ABCMeta): |
| 254 | pass |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 255 | class B(object): |
Guido van Rossum | 894d35e | 2007-09-11 20:42:30 +0000 | [diff] [blame] | 256 | counter = 0 |
| 257 | def __new__(cls): |
| 258 | B.counter += 1 |
| 259 | return super().__new__(cls) |
| 260 | class C(A, B): |
| 261 | pass |
| 262 | self.assertEqual(B.counter, 0) |
| 263 | C() |
| 264 | self.assertEqual(B.counter, 1) |
| 265 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 266 | |
| 267 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 268 | support.run_unittest(TestABC) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 269 | |
| 270 | |
| 271 | if __name__ == "__main__": |
| 272 | unittest.main() |