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