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 | aec5fd1 | 2010-10-02 00:03:31 +0000 | [diff] [blame^] | 101 | def test_type_has_no_abstractmethods(self): |
| 102 | # type pretends not to have __abstractmethods__. |
| 103 | self.assertRaises(AttributeError, getattr, type, "__abstractmethods__") |
| 104 | class meta(type): |
| 105 | pass |
| 106 | self.assertRaises(AttributeError, getattr, meta, "__abstractmethods__") |
| 107 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 108 | def test_registration_basics(self): |
| 109 | class A(metaclass=abc.ABCMeta): |
| 110 | pass |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 111 | class B(object): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 112 | pass |
| 113 | b = B() |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 114 | self.assertFalse(issubclass(B, A)) |
| 115 | self.assertFalse(issubclass(B, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 116 | self.assertNotIsInstance(b, A) |
| 117 | self.assertNotIsInstance(b, (A,)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 118 | A.register(B) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 119 | self.assertTrue(issubclass(B, A)) |
| 120 | self.assertTrue(issubclass(B, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 121 | self.assertIsInstance(b, A) |
| 122 | self.assertIsInstance(b, (A,)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 123 | class C(B): |
| 124 | pass |
| 125 | c = C() |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 126 | self.assertTrue(issubclass(C, A)) |
| 127 | self.assertTrue(issubclass(C, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 128 | self.assertIsInstance(c, A) |
| 129 | self.assertIsInstance(c, (A,)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 130 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 131 | def test_isinstance_invalidation(self): |
| 132 | class A(metaclass=abc.ABCMeta): |
| 133 | pass |
| 134 | class B: |
| 135 | pass |
| 136 | b = B() |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 137 | self.assertFalse(isinstance(b, A)) |
| 138 | self.assertFalse(isinstance(b, (A,))) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 139 | A.register(B) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 140 | self.assertTrue(isinstance(b, A)) |
| 141 | self.assertTrue(isinstance(b, (A,))) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 143 | def test_registration_builtins(self): |
| 144 | class A(metaclass=abc.ABCMeta): |
| 145 | pass |
| 146 | A.register(int) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 147 | self.assertIsInstance(42, A) |
| 148 | self.assertIsInstance(42, (A,)) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 149 | self.assertTrue(issubclass(int, A)) |
| 150 | self.assertTrue(issubclass(int, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 151 | class B(A): |
| 152 | pass |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 153 | B.register(str) |
| 154 | class C(str): pass |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 155 | self.assertIsInstance("", A) |
| 156 | self.assertIsInstance("", (A,)) |
Benjamin Peterson | 83cd3b9 | 2010-08-17 01:07:53 +0000 | [diff] [blame] | 157 | self.assertTrue(issubclass(str, A)) |
| 158 | self.assertTrue(issubclass(str, (A,))) |
| 159 | self.assertTrue(issubclass(C, A)) |
| 160 | self.assertTrue(issubclass(C, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 161 | |
| 162 | def test_registration_edge_cases(self): |
| 163 | class A(metaclass=abc.ABCMeta): |
| 164 | pass |
| 165 | A.register(A) # should pass silently |
| 166 | class A1(A): |
| 167 | pass |
| 168 | self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 169 | class B(object): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 170 | pass |
| 171 | A1.register(B) # ok |
| 172 | A1.register(B) # should pass silently |
| 173 | class C(A): |
| 174 | pass |
| 175 | A.register(C) # should pass silently |
| 176 | self.assertRaises(RuntimeError, C.register, A) # cycles not allowed |
| 177 | C.register(B) # ok |
| 178 | |
Benjamin Peterson | d632664 | 2010-01-27 02:25:58 +0000 | [diff] [blame] | 179 | def test_register_non_class(self): |
| 180 | class A(metaclass=abc.ABCMeta): |
| 181 | pass |
| 182 | self.assertRaisesRegexp(TypeError, "Can only register classes", |
| 183 | A.register, 4) |
| 184 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 185 | def test_registration_transitiveness(self): |
| 186 | class A(metaclass=abc.ABCMeta): |
| 187 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 188 | self.assertTrue(issubclass(A, A)) |
| 189 | self.assertTrue(issubclass(A, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 190 | class B(metaclass=abc.ABCMeta): |
| 191 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 192 | self.assertFalse(issubclass(A, B)) |
| 193 | self.assertFalse(issubclass(A, (B,))) |
| 194 | self.assertFalse(issubclass(B, A)) |
| 195 | self.assertFalse(issubclass(B, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 196 | class C(metaclass=abc.ABCMeta): |
| 197 | pass |
| 198 | A.register(B) |
| 199 | class B1(B): |
| 200 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 201 | self.assertTrue(issubclass(B1, A)) |
| 202 | self.assertTrue(issubclass(B1, (A,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 203 | class C1(C): |
| 204 | pass |
| 205 | B1.register(C1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 206 | self.assertFalse(issubclass(C, B)) |
| 207 | self.assertFalse(issubclass(C, (B,))) |
| 208 | self.assertFalse(issubclass(C, B1)) |
| 209 | self.assertFalse(issubclass(C, (B1,))) |
| 210 | self.assertTrue(issubclass(C1, A)) |
| 211 | self.assertTrue(issubclass(C1, (A,))) |
| 212 | self.assertTrue(issubclass(C1, B)) |
| 213 | self.assertTrue(issubclass(C1, (B,))) |
| 214 | self.assertTrue(issubclass(C1, B1)) |
| 215 | self.assertTrue(issubclass(C1, (B1,))) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 216 | C1.register(int) |
| 217 | class MyInt(int): |
| 218 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 219 | self.assertTrue(issubclass(MyInt, A)) |
| 220 | self.assertTrue(issubclass(MyInt, (A,))) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 221 | self.assertIsInstance(42, A) |
| 222 | self.assertIsInstance(42, (A,)) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | 894d35e | 2007-09-11 20:42:30 +0000 | [diff] [blame] | 224 | def test_all_new_methods_are_called(self): |
| 225 | class A(metaclass=abc.ABCMeta): |
| 226 | pass |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 227 | class B(object): |
Guido van Rossum | 894d35e | 2007-09-11 20:42:30 +0000 | [diff] [blame] | 228 | counter = 0 |
| 229 | def __new__(cls): |
| 230 | B.counter += 1 |
| 231 | return super().__new__(cls) |
| 232 | class C(A, B): |
| 233 | pass |
| 234 | self.assertEqual(B.counter, 0) |
| 235 | C() |
| 236 | self.assertEqual(B.counter, 1) |
| 237 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 238 | |
| 239 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 240 | support.run_unittest(TestABC) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 241 | |
| 242 | |
| 243 | if __name__ == "__main__": |
| 244 | unittest.main() |