ยปCore Development>Code coverage>Lib/test/test_compare.py

Python code coverage for Lib/test/test_compare.py

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