OLD | NEW |
1 # Test case for property | 1 # Test case for property |
2 # more tests are in test_descr | 2 # more tests are in test_descr |
3 | 3 |
4 import sys | 4 import sys |
5 import unittest | 5 import unittest |
6 from test.support import run_unittest | 6 from test.support import run_unittest |
7 | 7 |
8 class PropertyBase(Exception): | 8 class PropertyBase(Exception): |
9 pass | 9 pass |
10 | 10 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 120 |
121 @unittest.skipIf(sys.flags.optimize >= 2, | 121 @unittest.skipIf(sys.flags.optimize >= 2, |
122 "Docstrings are omitted with -O2 and above") | 122 "Docstrings are omitted with -O2 and above") |
123 def test_property_getter_doc_override(self): | 123 def test_property_getter_doc_override(self): |
124 newgettersub = PropertySubNewGetter() | 124 newgettersub = PropertySubNewGetter() |
125 self.assertEqual(newgettersub.spam, 5) | 125 self.assertEqual(newgettersub.spam, 5) |
126 self.assertEqual(newgettersub.__class__.spam.__doc__, "new docstring") | 126 self.assertEqual(newgettersub.__class__.spam.__doc__, "new docstring") |
127 newgetter = PropertyNewGetter() | 127 newgetter = PropertyNewGetter() |
128 self.assertEqual(newgetter.spam, 8) | 128 self.assertEqual(newgetter.spam, 8) |
129 self.assertEqual(newgetter.__class__.spam.__doc__, "new docstring") | 129 self.assertEqual(newgetter.__class__.spam.__doc__, "new docstring") |
| 130 |
| 131 def test_property___isabstractmethod__descriptor(self): |
| 132 for val in (True, False, [], [1], '', '1'): |
| 133 class C(object): |
| 134 def foo(self): |
| 135 pass |
| 136 foo.__isabstractmethod__ = val |
| 137 foo = property(foo) |
| 138 self.assertIs(C.foo.__isabstractmethod__, bool(val)) |
| 139 |
| 140 # check that the property's __isabstractmethod__ descriptor does the |
| 141 # right thing when presented with a value that fails truth testing: |
| 142 class NotBool(object): |
| 143 def __nonzero__(self): |
| 144 raise ValueError() |
| 145 __len__ = __nonzero__ |
| 146 with self.assertRaises(ValueError): |
| 147 class C(object): |
| 148 def foo(self): |
| 149 pass |
| 150 foo.__isabstractmethod__ = NotBool() |
| 151 foo = property(foo) |
| 152 C.foo.__isabstractmethod__ |
130 | 153 |
131 | 154 |
132 # Issue 5890: subclasses of property do not preserve method __doc__ strings | 155 # Issue 5890: subclasses of property do not preserve method __doc__ strings |
133 class PropertySub(property): | 156 class PropertySub(property): |
134 """This is a subclass of property""" | 157 """This is a subclass of property""" |
135 | 158 |
136 class PropertySubSlots(property): | 159 class PropertySubSlots(property): |
137 """This is a subclass of property that defines __slots__""" | 160 """This is a subclass of property that defines __slots__""" |
138 __slots__ = () | 161 __slots__ = () |
139 | 162 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 return 2 | 245 return 2 |
223 self.assertEqual(Foo.spam.__doc__, "a new docstring") | 246 self.assertEqual(Foo.spam.__doc__, "a new docstring") |
224 | 247 |
225 | 248 |
226 | 249 |
227 def test_main(): | 250 def test_main(): |
228 run_unittest(PropertyTests, PropertySubclassTests) | 251 run_unittest(PropertyTests, PropertySubclassTests) |
229 | 252 |
230 if __name__ == '__main__': | 253 if __name__ == '__main__': |
231 test_main() | 254 test_main() |
OLD | NEW |