1 # Copyright 2007 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
4 """Unit tests for abc.py."""
7 from test
import test_support
10 from inspect
import isabstract
13 class TestABC(unittest
.TestCase
):
15 def test_abstractmethod_basics(self
):
18 self
.assertEqual(foo
.__isabstractmethod
__, True)
20 self
.assertEqual(hasattr(bar
, "__isabstractmethod__"), False)
22 def test_abstractproperty_basics(self
):
25 self
.assertEqual(foo
.__isabstractmethod
__, True)
27 self
.assertEqual(hasattr(bar
, "__isabstractmethod__"), False)
30 __metaclass__
= abc
.ABCMeta
32 def foo(self
): return 3
35 def foo(self
): return super(D
, self
).foo
36 self
.assertEqual(D().foo
, 3)
38 def test_abstractmethod_integration(self
):
39 for abstractthing
in [abc
.abstractmethod
, abc
.abstractproperty
]:
41 __metaclass__
= abc
.ABCMeta
43 def foo(self
): pass # abstract
44 def bar(self
): pass # concrete
45 self
.assertEqual(C
.__abstractmethods
__, set(["foo"]))
46 self
.assertRaises(TypeError, C
) # because foo is abstract
47 self
.assertTrue(isabstract(C
))
49 def bar(self
): pass # concrete override of concrete
50 self
.assertEqual(D
.__abstractmethods
__, set(["foo"]))
51 self
.assertRaises(TypeError, D
) # because foo is still abstract
52 self
.assertTrue(isabstract(D
))
55 self
.assertEqual(E
.__abstractmethods
__, set())
56 E() # now foo is concrete, too
57 self
.assertFalse(isabstract(E
))
60 def bar(self
): pass # abstract override of concrete
61 self
.assertEqual(F
.__abstractmethods
__, set(["bar"]))
62 self
.assertRaises(TypeError, F
) # because bar is abstract now
63 self
.assertTrue(isabstract(F
))
65 def test_subclass_oldstyle_class(self
):
67 __metaclass__
= abc
.ABCMeta
70 self
.assertFalse(issubclass(OldstyleClass
, A
))
71 self
.assertFalse(issubclass(A
, OldstyleClass
))
73 def test_isinstance_class(self
):
75 __metaclass__
= abc
.ABCMeta
78 self
.assertFalse(isinstance(OldstyleClass
, A
))
79 self
.assertTrue(isinstance(OldstyleClass
, type(OldstyleClass
)))
80 self
.assertFalse(isinstance(A
, OldstyleClass
))
81 # This raises a recursion depth error, but is low-priority:
82 # self.assertTrue(isinstance(A, abc.ABCMeta))
84 def test_registration_basics(self
):
86 __metaclass__
= abc
.ABCMeta
90 self
.assertEqual(issubclass(B
, A
), False)
91 self
.assertEqual(issubclass(B
, (A
,)), False)
92 self
.assertNotIsInstance(b
, A
)
93 self
.assertNotIsInstance(b
, (A
,))
95 self
.assertEqual(issubclass(B
, A
), True)
96 self
.assertEqual(issubclass(B
, (A
,)), True)
97 self
.assertIsInstance(b
, A
)
98 self
.assertIsInstance(b
, (A
,))
102 self
.assertEqual(issubclass(C
, A
), True)
103 self
.assertEqual(issubclass(C
, (A
,)), True)
104 self
.assertIsInstance(c
, A
)
105 self
.assertIsInstance(c
, (A
,))
107 def test_isinstance_invalidation(self
):
109 __metaclass__
= abc
.ABCMeta
113 self
.assertEqual(isinstance(b
, A
), False)
114 self
.assertEqual(isinstance(b
, (A
,)), False)
116 self
.assertEqual(isinstance(b
, A
), True)
117 self
.assertEqual(isinstance(b
, (A
,)), True)
119 def test_registration_builtins(self
):
121 __metaclass__
= abc
.ABCMeta
123 self
.assertIsInstance(42, A
)
124 self
.assertIsInstance(42, (A
,))
125 self
.assertEqual(issubclass(int, A
), True)
126 self
.assertEqual(issubclass(int, (A
,)), True)
129 B
.register(basestring
)
130 self
.assertIsInstance("", A
)
131 self
.assertIsInstance("", (A
,))
132 self
.assertEqual(issubclass(str, A
), True)
133 self
.assertEqual(issubclass(str, (A
,)), True)
135 def test_registration_edge_cases(self
):
137 __metaclass__
= abc
.ABCMeta
138 A
.register(A
) # should pass silently
141 self
.assertRaises(RuntimeError, A1
.register
, A
) # cycles not allowed
145 A1
.register(B
) # should pass silently
148 A
.register(C
) # should pass silently
149 self
.assertRaises(RuntimeError, C
.register
, A
) # cycles not allowed
152 def test_register_non_class(self
):
154 __metaclass__
= abc
.ABCMeta
155 self
.assertRaisesRegexp(TypeError, "Can only register classes",
158 def test_registration_transitiveness(self
):
160 __metaclass__
= abc
.ABCMeta
161 self
.assertTrue(issubclass(A
, A
))
162 self
.assertTrue(issubclass(A
, (A
,)))
164 __metaclass__
= abc
.ABCMeta
165 self
.assertFalse(issubclass(A
, B
))
166 self
.assertFalse(issubclass(A
, (B
,)))
167 self
.assertFalse(issubclass(B
, A
))
168 self
.assertFalse(issubclass(B
, (A
,)))
170 __metaclass__
= abc
.ABCMeta
174 self
.assertTrue(issubclass(B1
, A
))
175 self
.assertTrue(issubclass(B1
, (A
,)))
179 self
.assertFalse(issubclass(C
, B
))
180 self
.assertFalse(issubclass(C
, (B
,)))
181 self
.assertFalse(issubclass(C
, B1
))
182 self
.assertFalse(issubclass(C
, (B1
,)))
183 self
.assertTrue(issubclass(C1
, A
))
184 self
.assertTrue(issubclass(C1
, (A
,)))
185 self
.assertTrue(issubclass(C1
, B
))
186 self
.assertTrue(issubclass(C1
, (B
,)))
187 self
.assertTrue(issubclass(C1
, B1
))
188 self
.assertTrue(issubclass(C1
, (B1
,)))
192 self
.assertTrue(issubclass(MyInt
, A
))
193 self
.assertTrue(issubclass(MyInt
, (A
,)))
194 self
.assertIsInstance(42, A
)
195 self
.assertIsInstance(42, (A
,))
197 def test_all_new_methods_are_called(self
):
199 __metaclass__
= abc
.ABCMeta
204 return super(B
, cls
).__new
__(cls
)
207 self
.assertEqual(B
.counter
, 0)
209 self
.assertEqual(B
.counter
, 1)
213 test_support
.run_unittest(TestABC
)
216 if __name__
== "__main__":