Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(268021)

Delta Between Two Patch Sets: Lib/test/test_abc.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/pickletester.py ('k') | Lib/test/test_ast.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Copyright 2007 Google, Inc. All Rights Reserved. 1 # Copyright 2007 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 3
4 """Unit tests for abc.py.""" 4 """Unit tests for abc.py."""
5 5
6 import unittest 6 import unittest
7 from test import support 7 from test import support
8 8
9 import abc 9 import abc
10 from inspect import isabstract 10 from inspect import isabstract
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 def test_abstractstaticmethod_basics(self): 50 def test_abstractstaticmethod_basics(self):
51 @abc.abstractstaticmethod 51 @abc.abstractstaticmethod
52 def foo(): pass 52 def foo(): pass
53 self.assertTrue(foo.__isabstractmethod__) 53 self.assertTrue(foo.__isabstractmethod__)
54 @staticmethod 54 @staticmethod
55 def bar(): pass 55 def bar(): pass
56 self.assertFalse(getattr(bar, "__isabstractmethod__", False)) 56 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
57 57
58 class C(metaclass=abc.ABCMeta): 58 class C(metaclass=abc.ABCMeta):
59 @abc.abstractstaticmethod 59 @abc.abstractstaticmethod
60 def foo(): return 3
61 self.assertRaises(TypeError, C)
62 class D(C):
63 @staticmethod
64 def foo(): return 4
65 self.assertEqual(D.foo(), 4)
66 self.assertEqual(D().foo(), 4)
67
68
69 class TestABC(unittest.TestCase):
70
71 def test_ABC_helper(self):
72 # create an ABC using the helper class and perform basic checks
73 class C(abc.ABC):
74 @classmethod
75 @abc.abstractmethod
76 def foo(cls): return cls.__name__
77 self.assertEqual(type(C), abc.ABCMeta)
78 self.assertRaises(TypeError, C)
79 class D(C):
80 @classmethod
81 def foo(cls): return super().foo()
82 self.assertEqual(D.foo(), 'D')
83
84 def test_abstractmethod_basics(self):
85 @abc.abstractmethod
86 def foo(self): pass
87 self.assertTrue(foo.__isabstractmethod__)
88 def bar(self): pass
89 self.assertFalse(hasattr(bar, "__isabstractmethod__"))
90
91 def test_abstractproperty_basics(self):
92 @property
93 @abc.abstractmethod
94 def foo(self): pass
95 self.assertTrue(foo.__isabstractmethod__)
96 def bar(self): pass
97 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
98
99 class C(metaclass=abc.ABCMeta):
100 @property
101 @abc.abstractmethod
102 def foo(self): return 3
103 self.assertRaises(TypeError, C)
104 class D(C):
105 @C.foo.getter
106 def foo(self): return super().foo
107 self.assertEqual(D().foo, 3)
108
109 def test_abstractclassmethod_basics(self):
110 @classmethod
111 @abc.abstractmethod
112 def foo(cls): pass
113 self.assertTrue(foo.__isabstractmethod__)
114 @classmethod
115 def bar(cls): pass
116 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
117
118 class C(metaclass=abc.ABCMeta):
119 @classmethod
120 @abc.abstractmethod
121 def foo(cls): return cls.__name__
122 self.assertRaises(TypeError, C)
123 class D(C):
124 @classmethod
125 def foo(cls): return super().foo()
126 self.assertEqual(D.foo(), 'D')
127 self.assertEqual(D().foo(), 'D')
128
129 def test_abstractstaticmethod_basics(self):
130 @staticmethod
131 @abc.abstractmethod
132 def foo(): pass
133 self.assertTrue(foo.__isabstractmethod__)
134 @staticmethod
135 def bar(): pass
136 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
137
138 class C(metaclass=abc.ABCMeta):
139 @staticmethod
140 @abc.abstractmethod
60 def foo(): return 3 141 def foo(): return 3
61 self.assertRaises(TypeError, C) 142 self.assertRaises(TypeError, C)
62 class D(C): 143 class D(C):
63 @staticmethod 144 @staticmethod
64 def foo(): return 4 145 def foo(): return 4
65 self.assertEqual(D.foo(), 4) 146 self.assertEqual(D.foo(), 4)
66 self.assertEqual(D().foo(), 4) 147 self.assertEqual(D().foo(), 4)
67 148
68 def test_abstractmethod_integration(self): 149 def test_abstractmethod_integration(self):
69 for abstractthing in [abc.abstractmethod, abc.abstractproperty, 150 for abstractthing in [abc.abstractmethod, abc.abstractproperty,
(...skipping 16 matching lines...) Expand all
86 self.assertEqual(E.__abstractmethods__, set()) 167 self.assertEqual(E.__abstractmethods__, set())
87 E() # now foo is concrete, too 168 E() # now foo is concrete, too
88 self.assertFalse(isabstract(E)) 169 self.assertFalse(isabstract(E))
89 class F(E): 170 class F(E):
90 @abstractthing 171 @abstractthing
91 def bar(self): pass # abstract override of concrete 172 def bar(self): pass # abstract override of concrete
92 self.assertEqual(F.__abstractmethods__, {"bar"}) 173 self.assertEqual(F.__abstractmethods__, {"bar"})
93 self.assertRaises(TypeError, F) # because bar is abstract now 174 self.assertRaises(TypeError, F) # because bar is abstract now
94 self.assertTrue(isabstract(F)) 175 self.assertTrue(isabstract(F))
95 176
96
97 class TestABC(unittest.TestCase):
98
99 def test_ABC_helper(self):
100 # create an ABC using the helper class and perform basic checks
101 class C(abc.ABC):
102 @classmethod
103 @abc.abstractmethod
104 def foo(cls): return cls.__name__
105 self.assertEqual(type(C), abc.ABCMeta)
106 self.assertRaises(TypeError, C)
107 class D(C):
108 @classmethod
109 def foo(cls): return super().foo()
110 self.assertEqual(D.foo(), 'D')
111
112 def test_abstractmethod_basics(self):
113 @abc.abstractmethod
114 def foo(self): pass
115 self.assertTrue(foo.__isabstractmethod__)
116 def bar(self): pass
117 self.assertFalse(hasattr(bar, "__isabstractmethod__"))
118
119 def test_abstractproperty_basics(self):
120 @property
121 @abc.abstractmethod
122 def foo(self): pass
123 self.assertTrue(foo.__isabstractmethod__)
124 def bar(self): pass
125 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
126
127 class C(metaclass=abc.ABCMeta):
128 @property
129 @abc.abstractmethod
130 def foo(self): return 3
131 self.assertRaises(TypeError, C)
132 class D(C):
133 @C.foo.getter
134 def foo(self): return super().foo
135 self.assertEqual(D().foo, 3)
136
137 def test_abstractclassmethod_basics(self):
138 @classmethod
139 @abc.abstractmethod
140 def foo(cls): pass
141 self.assertTrue(foo.__isabstractmethod__)
142 @classmethod
143 def bar(cls): pass
144 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
145
146 class C(metaclass=abc.ABCMeta):
147 @classmethod
148 @abc.abstractmethod
149 def foo(cls): return cls.__name__
150 self.assertRaises(TypeError, C)
151 class D(C):
152 @classmethod
153 def foo(cls): return super().foo()
154 self.assertEqual(D.foo(), 'D')
155 self.assertEqual(D().foo(), 'D')
156
157 def test_abstractstaticmethod_basics(self):
158 @staticmethod
159 @abc.abstractmethod
160 def foo(): pass
161 self.assertTrue(foo.__isabstractmethod__)
162 @staticmethod
163 def bar(): pass
164 self.assertFalse(getattr(bar, "__isabstractmethod__", False))
165
166 class C(metaclass=abc.ABCMeta):
167 @staticmethod
168 @abc.abstractmethod
169 def foo(): return 3
170 self.assertRaises(TypeError, C)
171 class D(C):
172 @staticmethod
173 def foo(): return 4
174 self.assertEqual(D.foo(), 4)
175 self.assertEqual(D().foo(), 4)
176
177 def test_abstractmethod_integration(self):
178 for abstractthing in [abc.abstractmethod, abc.abstractproperty,
179 abc.abstractclassmethod,
180 abc.abstractstaticmethod]:
181 class C(metaclass=abc.ABCMeta):
182 @abstractthing
183 def foo(self): pass # abstract
184 def bar(self): pass # concrete
185 self.assertEqual(C.__abstractmethods__, {"foo"})
186 self.assertRaises(TypeError, C) # because foo is abstract
187 self.assertTrue(isabstract(C))
188 class D(C):
189 def bar(self): pass # concrete override of concrete
190 self.assertEqual(D.__abstractmethods__, {"foo"})
191 self.assertRaises(TypeError, D) # because foo is still abstract
192 self.assertTrue(isabstract(D))
193 class E(D):
194 def foo(self): pass
195 self.assertEqual(E.__abstractmethods__, set())
196 E() # now foo is concrete, too
197 self.assertFalse(isabstract(E))
198 class F(E):
199 @abstractthing
200 def bar(self): pass # abstract override of concrete
201 self.assertEqual(F.__abstractmethods__, {"bar"})
202 self.assertRaises(TypeError, F) # because bar is abstract now
203 self.assertTrue(isabstract(F))
204
205 def test_descriptors_with_abstractmethod(self): 177 def test_descriptors_with_abstractmethod(self):
206 class C(metaclass=abc.ABCMeta): 178 class C(metaclass=abc.ABCMeta):
207 @property 179 @property
208 @abc.abstractmethod 180 @abc.abstractmethod
209 def foo(self): return 3 181 def foo(self): return 3
210 @foo.setter 182 @foo.setter
211 @abc.abstractmethod 183 @abc.abstractmethod
212 def foo(self, val): pass 184 def foo(self, val): pass
213 self.assertRaises(TypeError, C) 185 self.assertRaises(TypeError, C)
214 class D(C): 186 class D(C):
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 self.assertIs(C, A.register(C)) 294 self.assertIs(C, A.register(C))
323 295
324 def test_isinstance_invalidation(self): 296 def test_isinstance_invalidation(self):
325 class A(metaclass=abc.ABCMeta): 297 class A(metaclass=abc.ABCMeta):
326 pass 298 pass
327 class B: 299 class B:
328 pass 300 pass
329 b = B() 301 b = B()
330 self.assertNotIsInstance(b, A) 302 self.assertNotIsInstance(b, A)
331 self.assertNotIsInstance(b, (A,)) 303 self.assertNotIsInstance(b, (A,))
304 token_old = abc.get_cache_token()
332 A.register(B) 305 A.register(B)
306 token_new = abc.get_cache_token()
307 self.assertNotEqual(token_old, token_new)
333 self.assertIsInstance(b, A) 308 self.assertIsInstance(b, A)
334 self.assertIsInstance(b, (A,)) 309 self.assertIsInstance(b, (A,))
335 310
336 def test_registration_builtins(self): 311 def test_registration_builtins(self):
337 class A(metaclass=abc.ABCMeta): 312 class A(metaclass=abc.ABCMeta):
338 pass 313 pass
339 A.register(int) 314 A.register(int)
340 self.assertIsInstance(42, A) 315 self.assertIsInstance(42, A)
341 self.assertIsInstance(42, (A,)) 316 self.assertIsInstance(42, (A,))
342 self.assertTrue(issubclass(int, A)) 317 self.assertTrue(issubclass(int, A))
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 def __new__(cls): 397 def __new__(cls):
423 B.counter += 1 398 B.counter += 1
424 return super().__new__(cls) 399 return super().__new__(cls)
425 class C(A, B): 400 class C(A, B):
426 pass 401 pass
427 self.assertEqual(B.counter, 0) 402 self.assertEqual(B.counter, 0)
428 C() 403 C()
429 self.assertEqual(B.counter, 1) 404 self.assertEqual(B.counter, 1)
430 405
431 406
432 def test_main():
433 support.run_unittest(TestABC)
434
435
436 if __name__ == "__main__": 407 if __name__ == "__main__":
437 unittest.main() 408 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+