OLD | NEW |
1 """Tests for binary operators on subtypes of built-in types.""" | 1 """Tests for binary operators on subtypes of built-in types.""" |
2 | 2 |
3 import unittest | 3 import unittest |
4 from test import support | 4 from test import support |
5 from operator import eq, le | 5 from operator import eq, le |
6 from abc import ABCMeta | 6 from abc import ABCMeta |
7 | 7 |
8 def gcd(a, b): | 8 def gcd(a, b): |
9 """Greatest common divisor using Euclid's algorithm.""" | 9 """Greatest common divisor using Euclid's algorithm.""" |
10 while a: | 10 while a: |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 self.assertEqual(op_sequence(le, A, A), ['A.__le__', 'A.__ge__']) | 381 self.assertEqual(op_sequence(le, A, A), ['A.__le__', 'A.__ge__']) |
382 self.assertEqual(op_sequence(le, A, B), ['A.__le__', 'B.__ge__']) | 382 self.assertEqual(op_sequence(le, A, B), ['A.__le__', 'B.__ge__']) |
383 self.assertEqual(op_sequence(le, B, A), ['B.__le__', 'A.__ge__']) | 383 self.assertEqual(op_sequence(le, B, A), ['B.__le__', 'A.__ge__']) |
384 self.assertEqual(op_sequence(le, B, C), ['C.__ge__', 'B.__le__']) | 384 self.assertEqual(op_sequence(le, B, C), ['C.__ge__', 'B.__le__']) |
385 self.assertEqual(op_sequence(le, C, B), ['C.__le__', 'B.__ge__']) | 385 self.assertEqual(op_sequence(le, C, B), ['C.__le__', 'B.__ge__']) |
386 | 386 |
387 self.assertTrue(issubclass(V, B)) | 387 self.assertTrue(issubclass(V, B)) |
388 self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__']) | 388 self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__']) |
389 self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__']) | 389 self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__']) |
390 | 390 |
| 391 class SupEq(object): |
| 392 """Class that can test equality""" |
| 393 def __eq__(self, other): |
| 394 return True |
| 395 |
| 396 class S(SupEq): |
| 397 """Subclass of SupEq that should fail""" |
| 398 __eq__ = None |
| 399 |
| 400 class F(object): |
| 401 """Independent class that should fall back""" |
| 402 |
| 403 class X(object): |
| 404 """Independent class that should fail""" |
| 405 __eq__ = None |
| 406 |
| 407 class SN(SupEq): |
| 408 """Subclass of SupEq that can test equality, but not non-equality""" |
| 409 __ne__ = None |
| 410 |
| 411 class XN: |
| 412 """Independent class that can test equality, but not non-equality""" |
| 413 def __eq__(self, other): |
| 414 return True |
| 415 __ne__ = None |
| 416 |
| 417 class FallbackBlockingTests(unittest.TestCase): |
| 418 """Unit tests for None method blocking""" |
| 419 |
| 420 def test_fallback_rmethod_blocking(self): |
| 421 e, f, s, x = SupEq(), F(), S(), X() |
| 422 self.assertEqual(e, e) |
| 423 self.assertEqual(e, f) |
| 424 self.assertEqual(f, e) |
| 425 # left operand is checked first |
| 426 self.assertEqual(e, x) |
| 427 self.assertRaises(TypeError, eq, x, e) |
| 428 # S is a subclass, so it's always checked first |
| 429 self.assertRaises(TypeError, eq, e, s) |
| 430 self.assertRaises(TypeError, eq, s, e) |
| 431 |
| 432 def test_fallback_ne_blocking(self): |
| 433 e, sn, xn = SupEq(), SN(), XN() |
| 434 self.assertFalse(e != e) |
| 435 self.assertRaises(TypeError, ne, e, sn) |
| 436 self.assertRaises(TypeError, ne, sn, e) |
| 437 self.assertFalse(e != xn) |
| 438 self.assertRaises(TypeError, ne, xn, e) |
391 | 439 |
392 if __name__ == "__main__": | 440 if __name__ == "__main__": |
393 unittest.main() | 441 unittest.main() |
OLD | NEW |