| OLD | NEW |
| 1 """Test descriptors, binary ops, etc. | 1 """Test descriptors, binary ops, etc. |
| 2 | 2 |
| 3 Made for Jython. | 3 Made for Jython. |
| 4 """ | 4 """ |
| 5 import types | 5 import types |
| 6 import unittest | 6 import unittest |
| 7 from test import test_support | 7 from test import test_support |
| 8 | 8 |
| 9 class Old: | 9 class Old: |
| 10 pass | 10 pass |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 except TypeError: | 65 except TypeError: |
| 66 pass | 66 pass |
| 67 else: | 67 else: |
| 68 self.assert_(False, "should have raised TypeError") | 68 self.assert_(False, "should have raised TypeError") |
| 69 try: | 69 try: |
| 70 foo = C(None) | 70 foo = C(None) |
| 71 except TypeError: | 71 except TypeError: |
| 72 pass | 72 pass |
| 73 else: | 73 else: |
| 74 self.assert_(False, "should have raised TypeError") | 74 self.assert_(False, "should have raised TypeError") |
| 75 |
| 76 def test_raising_custom_attribute_error(self): |
| 77 class RaisesCustomMsg(object): |
| 78 def __get__(self, instance, type): |
| 79 raise AttributeError("Custom message") |
| 80 |
| 81 |
| 82 class CustomAttributeError(AttributeError): pass |
| 83 |
| 84 class RaisesCustomErr(object): |
| 85 def __get__(self, instance, type): |
| 86 raise CustomAttributeError |
| 87 |
| 88 class Foo(object): |
| 89 custom_msg = RaisesCustomMsg() |
| 90 custom_err = RaisesCustomErr() |
| 91 |
| 92 self.assertRaises(CustomAttributeError, lambda: Foo().custom_err) |
| 93 try: |
| 94 Foo().custom_msg |
| 95 self.assert_(False) # Previous line should raise AttributteError |
| 96 except AttributeError, e: |
| 97 self.assertEquals("Custom message", str(e)) |
| 75 | 98 |
| 76 class SubclassDescrTestCase(unittest.TestCase): | 99 class SubclassDescrTestCase(unittest.TestCase): |
| 77 | 100 |
| 78 def test_subclass_cmp_right_op(self): | 101 def test_subclass_cmp_right_op(self): |
| 79 # Case 1: subclass of int | 102 # Case 1: subclass of int |
| 80 | 103 |
| 81 class B(int): | 104 class B(int): |
| 82 def __ge__(self, other): | 105 def __ge__(self, other): |
| 83 return "B.__ge__" | 106 return "B.__ge__" |
| 84 def __le__(self, other): | 107 def __le__(self, other): |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 160 |
| 138 class C(object): | 161 class C(object): |
| 139 def __radd__(self, o): | 162 def __radd__(self, o): |
| 140 return '%r + C()' % (o,) | 163 return '%r + C()' % (o,) |
| 141 | 164 |
| 142 def __rmul__(self, o): | 165 def __rmul__(self, o): |
| 143 return '%r * C()' % (o,) | 166 return '%r * C()' % (o,) |
| 144 | 167 |
| 145 # Test strs, unicode, lists and tuples | 168 # Test strs, unicode, lists and tuples |
| 146 mapping = [] | 169 mapping = [] |
| 147 | 170 |
| 148 # + binop | 171 # + binop |
| 149 mapping.append((lambda o: 'foo' + o, | 172 mapping.append((lambda o: 'foo' + o, |
| 150 TypeError, "cannot concatenate 'str' and 'B' objects", | 173 TypeError, "cannot concatenate 'str' and 'B' objects", |
| 151 "'foo' + C()")) | 174 "'foo' + C()")) |
| 152 # XXX: There's probably work to be done here besides just emulating this | 175 # XXX: There's probably work to be done here besides just emulating this |
| 153 # message | 176 # message |
| 154 if test_support.is_jython: | 177 if test_support.is_jython: |
| 155 mapping.append((lambda o: u'foo' + o, | 178 mapping.append((lambda o: u'foo' + o, |
| 156 TypeError, "cannot concatenate 'unicode' and 'B' obj
ects", | 179 TypeError, "cannot concatenate 'unicode' and 'B' obj
ects", |
| 157 "u'foo' + C()")) | 180 "u'foo' + C()")) |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 eval('%s(new)' % op) | 309 eval('%s(new)' % op) |
| 287 except TypeError: | 310 except TypeError: |
| 288 pass | 311 pass |
| 289 else: | 312 else: |
| 290 self._assert(False, 'Expected a TypeError, op: %s' % op) | 313 self._assert(False, 'Expected a TypeError, op: %s' % op) |
| 291 | 314 |
| 292 def _test(self, func): | 315 def _test(self, func): |
| 293 self.assertRaises(AttributeError, func, old) | 316 self.assertRaises(AttributeError, func, old) |
| 294 self.assertRaises(TypeError, func, new) | 317 self.assertRaises(TypeError, func, new) |
| 295 | 318 |
| 319 class GetAttrTestCase(unittest.TestCase): |
| 320 def test_raising_custom_attribute_error(self): |
| 321 # Very similar to |
| 322 # test_descr_jy.TestDescrTestCase.test_raising_custom_attribute_error |
| 323 class BarAttributeError(AttributeError): pass |
| 324 |
| 325 class Bar(object): |
| 326 def __getattr__(self, name): |
| 327 raise BarAttributeError |
| 328 |
| 329 class Foo(object): |
| 330 def __getattr__(self, name): |
| 331 raise AttributeError("Custom message") |
| 332 self.assertRaises(BarAttributeError, lambda: Bar().x) |
| 333 try: |
| 334 Foo().x |
| 335 self.assert_(False) # Previous line should raise AttributteError |
| 336 except AttributeError, e: |
| 337 self.assertEquals("Custom message", str(e)) |
| 296 | 338 |
| 297 def test_main(): | 339 def test_main(): |
| 298 test_support.run_unittest(TestDescrTestCase, | 340 test_support.run_unittest(TestDescrTestCase, |
| 299 SubclassDescrTestCase, | 341 SubclassDescrTestCase, |
| 300 InPlaceTestCase, | 342 InPlaceTestCase, |
| 301 DescrExceptionsTestCase) | 343 DescrExceptionsTestCase, |
| 344 GetAttrTestCase) |
| 302 | 345 |
| 303 if __name__ == '__main__': | 346 if __name__ == '__main__': |
| 304 test_main() | 347 test_main() |
| OLD | NEW |