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
.assert_(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
.assert_(isabstract(D
))
55 self
.assertEqual(E
.__abstractmethods
__, set())
56 E() # now foo is concrete, too
57 self
.failIf(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
.assert_(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
.assertEqual(isinstance(b
, A
), False)
93 self
.assertEqual(isinstance(b
, (A
,)), False)
95 self
.assertEqual(issubclass(B
, A
), True)
96 self
.assertEqual(issubclass(B
, (A
,)), True)
97 self
.assertEqual(isinstance(b
, A
), True)
98 self
.assertEqual(isinstance(b
, (A
,)), True)
102 self
.assertEqual(issubclass(C
, A
), True)
103 self
.assertEqual(issubclass(C
, (A
,)), True)
104 self
.assertEqual(isinstance(c
, A
), True)
105 self
.assertEqual(isinstance(c
, (A
,)), True)
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
.assertEqual(isinstance(42, A
), True)
124 self
.assertEqual(isinstance(42, (A
,)), True)
125 self
.assertEqual(issubclass(int, A
), True)
126 self
.assertEqual(issubclass(int, (A
,)), True)
129 B
.register(basestring
)
130 self
.assertEqual(isinstance("", A
), True)
131 self
.assertEqual(isinstance("", (A
,)), True)
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_registration_transitiveness(self
):
154 __metaclass__
= abc
.ABCMeta
155 self
.failUnless(issubclass(A
, A
))
156 self
.failUnless(issubclass(A
, (A
,)))
158 __metaclass__
= abc
.ABCMeta
159 self
.failIf(issubclass(A
, B
))
160 self
.failIf(issubclass(A
, (B
,)))
161 self
.failIf(issubclass(B
, A
))
162 self
.failIf(issubclass(B
, (A
,)))
164 __metaclass__
= abc
.ABCMeta
168 self
.failUnless(issubclass(B1
, A
))
169 self
.failUnless(issubclass(B1
, (A
,)))
173 self
.failIf(issubclass(C
, B
))
174 self
.failIf(issubclass(C
, (B
,)))
175 self
.failIf(issubclass(C
, B1
))
176 self
.failIf(issubclass(C
, (B1
,)))
177 self
.failUnless(issubclass(C1
, A
))
178 self
.failUnless(issubclass(C1
, (A
,)))
179 self
.failUnless(issubclass(C1
, B
))
180 self
.failUnless(issubclass(C1
, (B
,)))
181 self
.failUnless(issubclass(C1
, B1
))
182 self
.failUnless(issubclass(C1
, (B1
,)))
186 self
.failUnless(issubclass(MyInt
, A
))
187 self
.failUnless(issubclass(MyInt
, (A
,)))
188 self
.failUnless(isinstance(42, A
))
189 self
.failUnless(isinstance(42, (A
,)))
191 def test_all_new_methods_are_called(self
):
193 __metaclass__
= abc
.ABCMeta
198 return super(B
, cls
).__new
__(cls
)
201 self
.assertEqual(B
.counter
, 0)
203 self
.assertEqual(B
.counter
, 1)
207 test_support
.run_unittest(TestABC
)
210 if __name__
== "__main__":