]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_contains.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_contains.py
CommitLineData
4710c53d 1from test.test_support import have_unicode, run_unittest\r
2import unittest\r
3\r
4\r
5class base_set:\r
6 def __init__(self, el):\r
7 self.el = el\r
8\r
9class set(base_set):\r
10 def __contains__(self, el):\r
11 return self.el == el\r
12\r
13class seq(base_set):\r
14 def __getitem__(self, n):\r
15 return [self.el][n]\r
16\r
17\r
18class TestContains(unittest.TestCase):\r
19 def test_common_tests(self):\r
20 a = base_set(1)\r
21 b = set(1)\r
22 c = seq(1)\r
23 self.assertIn(1, b)\r
24 self.assertNotIn(0, b)\r
25 self.assertIn(1, c)\r
26 self.assertNotIn(0, c)\r
27 self.assertRaises(TypeError, lambda: 1 in a)\r
28 self.assertRaises(TypeError, lambda: 1 not in a)\r
29\r
30 # test char in string\r
31 self.assertIn('c', 'abc')\r
32 self.assertNotIn('d', 'abc')\r
33\r
34 self.assertIn('', '')\r
35 self.assertIn('', 'abc')\r
36\r
37 self.assertRaises(TypeError, lambda: None in 'abc')\r
38\r
39 if have_unicode:\r
40 def test_char_in_unicode(self):\r
41 self.assertIn('c', unicode('abc'))\r
42 self.assertNotIn('d', unicode('abc'))\r
43\r
44 self.assertIn('', unicode(''))\r
45 self.assertIn(unicode(''), '')\r
46 self.assertIn(unicode(''), unicode(''))\r
47 self.assertIn('', unicode('abc'))\r
48 self.assertIn(unicode(''), 'abc')\r
49 self.assertIn(unicode(''), unicode('abc'))\r
50\r
51 self.assertRaises(TypeError, lambda: None in unicode('abc'))\r
52\r
53 # test Unicode char in Unicode\r
54 self.assertIn(unicode('c'), unicode('abc'))\r
55 self.assertNotIn(unicode('d'), unicode('abc'))\r
56\r
57 # test Unicode char in string\r
58 self.assertIn(unicode('c'), 'abc')\r
59 self.assertNotIn(unicode('d'), 'abc')\r
60\r
61 def test_builtin_sequence_types(self):\r
62 # a collection of tests on builtin sequence types\r
63 a = range(10)\r
64 for i in a:\r
65 self.assertIn(i, a)\r
66 self.assertNotIn(16, a)\r
67 self.assertNotIn(a, a)\r
68\r
69 a = tuple(a)\r
70 for i in a:\r
71 self.assertIn(i, a)\r
72 self.assertNotIn(16, a)\r
73 self.assertNotIn(a, a)\r
74\r
75 class Deviant1:\r
76 """Behaves strangely when compared\r
77\r
78 This class is designed to make sure that the contains code\r
79 works when the list is modified during the check.\r
80 """\r
81 aList = range(15)\r
82 def __cmp__(self, other):\r
83 if other == 12:\r
84 self.aList.remove(12)\r
85 self.aList.remove(13)\r
86 self.aList.remove(14)\r
87 return 1\r
88\r
89 self.assertNotIn(Deviant1(), Deviant1.aList)\r
90\r
91 class Deviant2:\r
92 """Behaves strangely when compared\r
93\r
94 This class raises an exception during comparison. That in\r
95 turn causes the comparison to fail with a TypeError.\r
96 """\r
97 def __cmp__(self, other):\r
98 if other == 4:\r
99 raise RuntimeError, "gotcha"\r
100\r
101 try:\r
102 self.assertNotIn(Deviant2(), a)\r
103 except TypeError:\r
104 pass\r
105\r
106\r
107def test_main():\r
108 run_unittest(TestContains)\r
109\r
110if __name__ == '__main__':\r
111 test_main()\r