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 | |
| 6 | import sys |
| 7 | import unittest |
| 8 | from test import test_support |
| 9 | |
| 10 | import abc |
| 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 | |
| 22 | def test_abstractmethod_integration(self): |
| 23 | class C(metaclass=abc.ABCMeta): |
| 24 | @abc.abstractmethod |
| 25 | def foo(self): pass # abstract |
| 26 | def bar(self): pass # concrete |
| 27 | self.assertEqual(C.__abstractmethods__, {"foo"}) |
| 28 | self.assertRaises(TypeError, C) # because foo is abstract |
| 29 | class D(C): |
| 30 | def bar(self): pass # concrete override of concrete |
| 31 | self.assertEqual(D.__abstractmethods__, {"foo"}) |
| 32 | self.assertRaises(TypeError, D) # because foo is still abstract |
| 33 | class E(D): |
| 34 | def foo(self): pass |
| 35 | self.assertEqual(E.__abstractmethods__, set()) |
| 36 | E() # now foo is concrete, too |
| 37 | class F(E): |
| 38 | @abc.abstractmethod |
| 39 | def bar(self): pass # abstract override of concrete |
| 40 | self.assertEqual(F.__abstractmethods__, {"bar"}) |
| 41 | self.assertRaises(TypeError, F) # because bar is abstract now |
| 42 | |
| 43 | def test_registration_basics(self): |
| 44 | class A(metaclass=abc.ABCMeta): |
| 45 | pass |
| 46 | class B: |
| 47 | pass |
| 48 | b = B() |
| 49 | self.assertEqual(issubclass(B, A), False) |
| 50 | self.assertEqual(isinstance(b, A), False) |
| 51 | A.register(B) |
| 52 | self.assertEqual(issubclass(B, A), True) |
| 53 | self.assertEqual(isinstance(b, A), True) |
| 54 | class C(B): |
| 55 | pass |
| 56 | c = C() |
| 57 | self.assertEqual(issubclass(C, A), True) |
| 58 | self.assertEqual(isinstance(c, A), True) |
| 59 | |
| 60 | def test_registration_builtins(self): |
| 61 | class A(metaclass=abc.ABCMeta): |
| 62 | pass |
| 63 | A.register(int) |
| 64 | self.assertEqual(isinstance(42, A), True) |
| 65 | self.assertEqual(issubclass(int, A), True) |
| 66 | class B(A): |
| 67 | pass |
| 68 | B.register(basestring) |
| 69 | self.assertEqual(isinstance("", A), True) |
| 70 | self.assertEqual(issubclass(str, A), True) |
| 71 | |
| 72 | def test_registration_edge_cases(self): |
| 73 | class A(metaclass=abc.ABCMeta): |
| 74 | pass |
| 75 | A.register(A) # should pass silently |
| 76 | class A1(A): |
| 77 | pass |
| 78 | self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed |
| 79 | class B: |
| 80 | pass |
| 81 | A1.register(B) # ok |
| 82 | A1.register(B) # should pass silently |
| 83 | class C(A): |
| 84 | pass |
| 85 | A.register(C) # should pass silently |
| 86 | self.assertRaises(RuntimeError, C.register, A) # cycles not allowed |
| 87 | C.register(B) # ok |
| 88 | |
| 89 | def test_registration_transitiveness(self): |
| 90 | class A(metaclass=abc.ABCMeta): |
| 91 | pass |
| 92 | self.failUnless(issubclass(A, A)) |
| 93 | class B(metaclass=abc.ABCMeta): |
| 94 | pass |
| 95 | self.failIf(issubclass(A, B)) |
| 96 | self.failIf(issubclass(B, A)) |
| 97 | class C(metaclass=abc.ABCMeta): |
| 98 | pass |
| 99 | A.register(B) |
| 100 | class B1(B): |
| 101 | pass |
| 102 | self.failUnless(issubclass(B1, A)) |
| 103 | class C1(C): |
| 104 | pass |
| 105 | B1.register(C1) |
| 106 | self.failIf(issubclass(C, B)) |
| 107 | self.failIf(issubclass(C, B1)) |
| 108 | self.failUnless(issubclass(C1, A)) |
| 109 | self.failUnless(issubclass(C1, B)) |
| 110 | self.failUnless(issubclass(C1, B1)) |
| 111 | C1.register(int) |
| 112 | class MyInt(int): |
| 113 | pass |
| 114 | self.failUnless(issubclass(MyInt, A)) |
| 115 | self.failUnless(isinstance(42, A)) |
| 116 | |
| 117 | |
| 118 | def test_main(): |
| 119 | test_support.run_unittest(TestABC) |
| 120 | |
| 121 | |
| 122 | if __name__ == "__main__": |
| 123 | unittest.main() |