blob: a663832b3da74aae70d817cc74a7032c6cee7a1c [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Neil Schemenauerfd288c72001-01-02 16:30:31 +00003
Neil Schemenauerfd288c72001-01-02 16:30:31 +00004class Empty:
5 def __repr__(self):
6 return '<Empty>'
7
Neil Schemenauerfd288c72001-01-02 16:30:31 +00008class Cmp:
9 def __init__(self,arg):
10 self.arg = arg
11
12 def __repr__(self):
13 return '<Cmp %s>' % self.arg
14
Guido van Rossum47b9ff62006-08-24 00:41:19 +000015 def __eq__(self, other):
16 return self.arg == other
Neil Schemenauerfd288c72001-01-02 16:30:31 +000017
Guido van Rossumab078dd2008-01-06 00:09:11 +000018class Anything:
19 def __eq__(self, other):
20 return True
21
22 def __ne__(self, other):
23 return False
24
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000025class ComparisonTest(unittest.TestCase):
Guido van Rossume2a383d2007-01-15 16:59:06 +000026 set1 = [2, 2.0, 2, 2+0j, Cmp(2.0)]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027 set2 = [[1], (3,), None, Empty()]
28 candidates = set1 + set2
Neil Schemenauerfd288c72001-01-02 16:30:31 +000029
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030 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 Schemenauere7e694f2001-01-03 02:13:26 +000035 else:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036 self.assertNotEqual(a, b)
Neil Schemenauerfd288c72001-01-02 16:30:31 +000037
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038 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 Rossum47b9ff62006-08-24 00:41:19 +000045 self.assertEqual(a == b, id(a) == id(b),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046 'a=%r, b=%r' % (a, b))
47
Guido van Rossume27dc722007-03-27 22:37:34 +000048 def test_ne_defaults_to_not_eq(self):
49 a = Cmp(1)
50 b = Cmp(1)
Serhiy Storchakaf4b7a022015-01-26 09:57:07 +020051 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 Rossume27dc722007-03-27 22:37:34 +0000114
Guido van Rossumab078dd2008-01-06 00:09:11 +0000115 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 Wouters49fd7fa2006-04-21 10:40:58 +0000124def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000125 support.run_unittest(ComparisonTest)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126
127if __name__ == '__main__':
128 test_main()