Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 3 | |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 4 | class Empty: |
| 5 | def __repr__(self): |
| 6 | return '<Empty>' |
| 7 | |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 8 | class Cmp: |
| 9 | def __init__(self,arg): |
| 10 | self.arg = arg |
| 11 | |
| 12 | def __repr__(self): |
| 13 | return '<Cmp %s>' % self.arg |
| 14 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 15 | def __eq__(self, other): |
| 16 | return self.arg == other |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | ab078dd | 2008-01-06 00:09:11 +0000 | [diff] [blame] | 18 | class Anything: |
| 19 | def __eq__(self, other): |
| 20 | return True |
| 21 | |
| 22 | def __ne__(self, other): |
| 23 | return False |
| 24 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 25 | class ComparisonTest(unittest.TestCase): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 26 | set1 = [2, 2.0, 2, 2+0j, Cmp(2.0)] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | set2 = [[1], (3,), None, Empty()] |
| 28 | candidates = set1 + set2 |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 29 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 30 | def test_comparisons(self): |
| 31 | for a in self.candidates: |
| 32 | for b in self.candidates: |
| 33 | if ((a in self.set1) and (b in self.set1)) or a is b: |
| 34 | self.assertEqual(a, b) |
Neil Schemenauer | e7e694f | 2001-01-03 02:13:26 +0000 | [diff] [blame] | 35 | else: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | self.assertNotEqual(a, b) |
Neil Schemenauer | fd288c7 | 2001-01-02 16:30:31 +0000 | [diff] [blame] | 37 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | def test_id_comparisons(self): |
| 39 | # Ensure default comparison compares id() of args |
| 40 | L = [] |
| 41 | for i in range(10): |
| 42 | L.insert(len(L)//2, Empty()) |
| 43 | for a in L: |
| 44 | for b in L: |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 45 | self.assertEqual(a == b, id(a) == id(b), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 46 | 'a=%r, b=%r' % (a, b)) |
| 47 | |
Guido van Rossum | e27dc72 | 2007-03-27 22:37:34 +0000 | [diff] [blame] | 48 | def test_ne_defaults_to_not_eq(self): |
| 49 | a = Cmp(1) |
| 50 | b = Cmp(1) |
Serhiy Storchaka | f4b7a02 | 2015-01-26 09:57:07 +0200 | [diff] [blame^] | 51 | c = Cmp(2) |
| 52 | self.assertIs(a == b, True) |
| 53 | self.assertIs(a != b, False) |
| 54 | self.assertIs(a != c, True) |
| 55 | |
| 56 | def test_ne_high_priority(self): |
| 57 | """object.__ne__() should allow reflected __ne__() to be tried""" |
| 58 | calls = [] |
| 59 | class Left: |
| 60 | # Inherits object.__ne__() |
| 61 | def __eq__(*args): |
| 62 | calls.append('Left.__eq__') |
| 63 | return NotImplemented |
| 64 | class Right: |
| 65 | def __eq__(*args): |
| 66 | calls.append('Right.__eq__') |
| 67 | return NotImplemented |
| 68 | def __ne__(*args): |
| 69 | calls.append('Right.__ne__') |
| 70 | return NotImplemented |
| 71 | Left() != Right() |
| 72 | self.assertSequenceEqual(calls, ['Left.__eq__', 'Right.__ne__']) |
| 73 | |
| 74 | def test_ne_low_priority(self): |
| 75 | """object.__ne__() should not invoke reflected __eq__()""" |
| 76 | calls = [] |
| 77 | class Base: |
| 78 | # Inherits object.__ne__() |
| 79 | def __eq__(*args): |
| 80 | calls.append('Base.__eq__') |
| 81 | return NotImplemented |
| 82 | class Derived(Base): # Subclassing forces higher priority |
| 83 | def __eq__(*args): |
| 84 | calls.append('Derived.__eq__') |
| 85 | return NotImplemented |
| 86 | def __ne__(*args): |
| 87 | calls.append('Derived.__ne__') |
| 88 | return NotImplemented |
| 89 | Base() != Derived() |
| 90 | self.assertSequenceEqual(calls, ['Derived.__ne__', 'Base.__eq__']) |
| 91 | |
| 92 | def test_other_delegation(self): |
| 93 | """No default delegation between operations except __ne__()""" |
| 94 | ops = ( |
| 95 | ('__eq__', lambda a, b: a == b), |
| 96 | ('__lt__', lambda a, b: a < b), |
| 97 | ('__le__', lambda a, b: a <= b), |
| 98 | ('__gt__', lambda a, b: a > b), |
| 99 | ('__ge__', lambda a, b: a >= b), |
| 100 | ) |
| 101 | for name, func in ops: |
| 102 | with self.subTest(name): |
| 103 | def unexpected(*args): |
| 104 | self.fail('Unexpected operator method called') |
| 105 | class C: |
| 106 | __ne__ = unexpected |
| 107 | for other, _ in ops: |
| 108 | if other != name: |
| 109 | setattr(C, other, unexpected) |
| 110 | if name == '__eq__': |
| 111 | self.assertIs(func(C(), object()), False) |
| 112 | else: |
| 113 | self.assertRaises(TypeError, func, C(), object()) |
Guido van Rossum | e27dc72 | 2007-03-27 22:37:34 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | ab078dd | 2008-01-06 00:09:11 +0000 | [diff] [blame] | 115 | def test_issue_1393(self): |
| 116 | x = lambda: None |
| 117 | self.assertEqual(x, Anything()) |
| 118 | self.assertEqual(Anything(), x) |
| 119 | y = object() |
| 120 | self.assertEqual(y, Anything()) |
| 121 | self.assertEqual(Anything(), y) |
| 122 | |
| 123 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 124 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 125 | support.run_unittest(ComparisonTest) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 126 | |
| 127 | if __name__ == '__main__': |
| 128 | test_main() |