| LEFT | RIGHT |
| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 | 75 |
| 76 def test_raising_custom_attribute_error(self): | 76 def test_raising_custom_attribute_error(self): |
| 77 class Desc(object): | 77 class RaisesCustomMsg(object): |
| 78 def __get__(self, instance, type): | 78 def __get__(self, instance, type): |
| 79 raise AttributeError("Custom message") | 79 raise AttributeError("Custom message") |
| 80 class Foo(object): | 80 |
| 81 desc = Desc() | 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) |
| 82 try: | 93 try: |
| 83 Foo().desc | 94 Foo().custom_msg |
| 84 self.assert_(False) # Previous line should raise AttributteError | 95 self.assert_(False) # Previous line should raise AttributteError |
| 85 except AttributeError, e: | 96 except AttributeError, e: |
| 86 self.assertEquals("Custom message", str(e)) | 97 self.assertEquals("Custom message", str(e)) |
| 87 | 98 |
| 88 class SubclassDescrTestCase(unittest.TestCase): | 99 class SubclassDescrTestCase(unittest.TestCase): |
| 89 | 100 |
| 90 def test_subclass_cmp_right_op(self): | 101 def test_subclass_cmp_right_op(self): |
| 91 # Case 1: subclass of int | 102 # Case 1: subclass of int |
| 92 | 103 |
| 93 class B(int): | 104 class B(int): |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 160 |
| 150 class C(object): | 161 class C(object): |
| 151 def __radd__(self, o): | 162 def __radd__(self, o): |
| 152 return '%r + C()' % (o,) | 163 return '%r + C()' % (o,) |
| 153 | 164 |
| 154 def __rmul__(self, o): | 165 def __rmul__(self, o): |
| 155 return '%r * C()' % (o,) | 166 return '%r * C()' % (o,) |
| 156 | 167 |
| 157 # Test strs, unicode, lists and tuples | 168 # Test strs, unicode, lists and tuples |
| 158 mapping = [] | 169 mapping = [] |
| 159 | 170 |
| 160 # + binop | 171 # + binop |
| 161 mapping.append((lambda o: 'foo' + o, | 172 mapping.append((lambda o: 'foo' + o, |
| 162 TypeError, "cannot concatenate 'str' and 'B' objects", | 173 TypeError, "cannot concatenate 'str' and 'B' objects", |
| 163 "'foo' + C()")) | 174 "'foo' + C()")) |
| 164 # 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 |
| 165 # message | 176 # message |
| 166 if test_support.is_jython: | 177 if test_support.is_jython: |
| 167 mapping.append((lambda o: u'foo' + o, | 178 mapping.append((lambda o: u'foo' + o, |
| 168 TypeError, "cannot concatenate 'unicode' and 'B' obj
ects", | 179 TypeError, "cannot concatenate 'unicode' and 'B' obj
ects", |
| 169 "u'foo' + C()")) | 180 "u'foo' + C()")) |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 eval('%s(new)' % op) | 309 eval('%s(new)' % op) |
| 299 except TypeError: | 310 except TypeError: |
| 300 pass | 311 pass |
| 301 else: | 312 else: |
| 302 self._assert(False, 'Expected a TypeError, op: %s' % op) | 313 self._assert(False, 'Expected a TypeError, op: %s' % op) |
| 303 | 314 |
| 304 def _test(self, func): | 315 def _test(self, func): |
| 305 self.assertRaises(AttributeError, func, old) | 316 self.assertRaises(AttributeError, func, old) |
| 306 self.assertRaises(TypeError, func, new) | 317 self.assertRaises(TypeError, func, new) |
| 307 | 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)) |
| 308 | 338 |
| 309 def test_main(): | 339 def test_main(): |
| 310 test_support.run_unittest(TestDescrTestCase, | 340 test_support.run_unittest(TestDescrTestCase, |
| 311 SubclassDescrTestCase, | 341 SubclassDescrTestCase, |
| 312 InPlaceTestCase, | 342 InPlaceTestCase, |
| 313 DescrExceptionsTestCase) | 343 DescrExceptionsTestCase, |
| 344 GetAttrTestCase) |
| 314 | 345 |
| 315 if __name__ == '__main__': | 346 if __name__ == '__main__': |
| 316 test_main() | 347 test_main() |
| LEFT | RIGHT |