~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/test/test_compare.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
from test import support
 
3
 
 
4
class Empty:
 
5
    def __repr__(self):
 
6
        return '<Empty>'
 
7
 
 
8
class 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
 
 
18
class Anything:
 
19
    def __eq__(self, other):
 
20
        return True
 
21
 
 
22
    def __ne__(self, other):
 
23
        return False
 
24
 
 
25
class ComparisonTest(unittest.TestCase):
 
26
    set1 = [2, 2.0, 2, 2+0j, Cmp(2.0)]
 
27
    set2 = [[1], (3,), None, Empty()]
 
28
    candidates = set1 + set2
 
29
 
 
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)
 
35
                else:
 
36
                    self.assertNotEqual(a, b)
 
37
 
 
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:
 
45
                self.assertEqual(a == b, id(a) == id(b),
 
46
                                 'a=%r, b=%r' % (a, b))
 
47
 
 
48
    def test_ne_defaults_to_not_eq(self):
 
49
        a = Cmp(1)
 
50
        b = Cmp(1)
 
51
        self.assertTrue(a == b)
 
52
        self.assertFalse(a != b)
 
53
 
 
54
    def test_issue_1393(self):
 
55
        x = lambda: None
 
56
        self.assertEqual(x, Anything())
 
57
        self.assertEqual(Anything(), x)
 
58
        y = object()
 
59
        self.assertEqual(y, Anything())
 
60
        self.assertEqual(Anything(), y)
 
61
 
 
62
 
 
63
def test_main():
 
64
    support.run_unittest(ComparisonTest)
 
65
 
 
66
if __name__ == '__main__':
 
67
    test_main()