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

« back to all changes in this revision

Viewing changes to Lib/test/test_contains.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
from collections import deque
 
2
from test.support import run_unittest
 
3
import unittest
 
4
 
 
5
 
 
6
class base_set:
 
7
    def __init__(self, el):
 
8
        self.el = el
 
9
 
 
10
class myset(base_set):
 
11
    def __contains__(self, el):
 
12
        return self.el == el
 
13
 
 
14
class seq(base_set):
 
15
    def __getitem__(self, n):
 
16
        return [self.el][n]
 
17
 
 
18
class TestContains(unittest.TestCase):
 
19
    def test_common_tests(self):
 
20
        a = base_set(1)
 
21
        b = myset(1)
 
22
        c = seq(1)
 
23
        self.assertIn(1, b)
 
24
        self.assertNotIn(0, b)
 
25
        self.assertIn(1, c)
 
26
        self.assertNotIn(0, c)
 
27
        self.assertRaises(TypeError, lambda: 1 in a)
 
28
        self.assertRaises(TypeError, lambda: 1 not in a)
 
29
 
 
30
        # test char in string
 
31
        self.assertIn('c', 'abc')
 
32
        self.assertNotIn('d', 'abc')
 
33
 
 
34
        self.assertIn('', '')
 
35
        self.assertIn('', 'abc')
 
36
 
 
37
        self.assertRaises(TypeError, lambda: None in 'abc')
 
38
 
 
39
    def test_builtin_sequence_types(self):
 
40
        # a collection of tests on builtin sequence types
 
41
        a = range(10)
 
42
        for i in a:
 
43
            self.assertIn(i, a)
 
44
        self.assertNotIn(16, a)
 
45
        self.assertNotIn(a, a)
 
46
 
 
47
        a = tuple(a)
 
48
        for i in a:
 
49
            self.assertIn(i, a)
 
50
        self.assertNotIn(16, a)
 
51
        self.assertNotIn(a, a)
 
52
 
 
53
        class Deviant1:
 
54
            """Behaves strangely when compared
 
55
 
 
56
            This class is designed to make sure that the contains code
 
57
            works when the list is modified during the check.
 
58
            """
 
59
            aList = list(range(15))
 
60
            def __eq__(self, other):
 
61
                if other == 12:
 
62
                    self.aList.remove(12)
 
63
                    self.aList.remove(13)
 
64
                    self.aList.remove(14)
 
65
                return 0
 
66
 
 
67
        self.assertNotIn(Deviant1(), Deviant1.aList)
 
68
 
 
69
    def test_nonreflexive(self):
 
70
        # containment and equality tests involving elements that are
 
71
        # not necessarily equal to themselves
 
72
 
 
73
        class MyNonReflexive(object):
 
74
            def __eq__(self, other):
 
75
                return False
 
76
            def __hash__(self):
 
77
                return 28
 
78
 
 
79
        values = float('nan'), 1, None, 'abc', MyNonReflexive()
 
80
        constructors = list, tuple, dict.fromkeys, set, frozenset, deque
 
81
        for constructor in constructors:
 
82
            container = constructor(values)
 
83
            for elem in container:
 
84
                self.assertIn(elem, container)
 
85
            self.assertTrue(container == constructor(values))
 
86
            self.assertTrue(container == container)
 
87
 
 
88
 
 
89
def test_main():
 
90
    run_unittest(TestContains)
 
91
 
 
92
if __name__ == '__main__':
 
93
    test_main()