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