1import copy 2import pickle 3import unittest 4import collections 5from test import test_support 6 7class DictSetTest(unittest.TestCase): 8 9 def test_constructors_not_callable(self): 10 kt = type({}.viewkeys()) 11 self.assertRaises(TypeError, kt, {}) 12 self.assertRaises(TypeError, kt) 13 it = type({}.viewitems()) 14 self.assertRaises(TypeError, it, {}) 15 self.assertRaises(TypeError, it) 16 vt = type({}.viewvalues()) 17 self.assertRaises(TypeError, vt, {}) 18 self.assertRaises(TypeError, vt) 19 20 def test_dict_keys(self): 21 d = {1: 10, "a": "ABC"} 22 keys = d.viewkeys() 23 self.assertEqual(len(keys), 2) 24 self.assertEqual(set(keys), set([1, "a"])) 25 self.assertEqual(keys, set([1, "a"])) 26 self.assertNotEqual(keys, set([1, "a", "b"])) 27 self.assertNotEqual(keys, set([1, "b"])) 28 self.assertNotEqual(keys, set([1])) 29 self.assertNotEqual(keys, 42) 30 self.assertIn(1, keys) 31 self.assertIn("a", keys) 32 self.assertNotIn(10, keys) 33 self.assertNotIn("Z", keys) 34 self.assertEqual(d.viewkeys(), d.viewkeys()) 35 e = {1: 11, "a": "def"} 36 self.assertEqual(d.viewkeys(), e.viewkeys()) 37 del e["a"] 38 self.assertNotEqual(d.viewkeys(), e.viewkeys()) 39 40 def test_dict_items(self): 41 d = {1: 10, "a": "ABC"} 42 items = d.viewitems() 43 self.assertEqual(len(items), 2) 44 self.assertEqual(set(items), set([(1, 10), ("a", "ABC")])) 45 self.assertEqual(items, set([(1, 10), ("a", "ABC")])) 46 self.assertNotEqual(items, set([(1, 10), ("a", "ABC"), "junk"])) 47 self.assertNotEqual(items, set([(1, 10), ("a", "def")])) 48 self.assertNotEqual(items, set([(1, 10)])) 49 self.assertNotEqual(items, 42) 50 self.assertIn((1, 10), items) 51 self.assertIn(("a", "ABC"), items) 52 self.assertNotIn((1, 11), items) 53 self.assertNotIn(1, items) 54 self.assertNotIn((), items) 55 self.assertNotIn((1,), items) 56 self.assertNotIn((1, 2, 3), items) 57 self.assertEqual(d.viewitems(), d.viewitems()) 58 e = d.copy() 59 self.assertEqual(d.viewitems(), e.viewitems()) 60 e["a"] = "def" 61 self.assertNotEqual(d.viewitems(), e.viewitems()) 62 63 def test_dict_mixed_keys_items(self): 64 d = {(1, 1): 11, (2, 2): 22} 65 e = {1: 1, 2: 2} 66 self.assertEqual(d.viewkeys(), e.viewitems()) 67 self.assertNotEqual(d.viewitems(), e.viewkeys()) 68 69 def test_dict_values(self): 70 d = {1: 10, "a": "ABC"} 71 values = d.viewvalues() 72 self.assertEqual(set(values), set([10, "ABC"])) 73 self.assertEqual(len(values), 2) 74 75 def test_dict_repr(self): 76 d = {1: 10, "a": "ABC"} 77 self.assertIsInstance(repr(d), str) 78 r = repr(d.viewitems()) 79 self.assertIsInstance(r, str) 80 self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or 81 r == "dict_items([(1, 10), ('a', 'ABC')])") 82 r = repr(d.viewkeys()) 83 self.assertIsInstance(r, str) 84 self.assertTrue(r == "dict_keys(['a', 1])" or 85 r == "dict_keys([1, 'a'])") 86 r = repr(d.viewvalues()) 87 self.assertIsInstance(r, str) 88 self.assertTrue(r == "dict_values(['ABC', 10])" or 89 r == "dict_values([10, 'ABC'])") 90 91 def test_keys_set_operations(self): 92 d1 = {'a': 1, 'b': 2} 93 d2 = {'b': 3, 'c': 2} 94 d3 = {'d': 4, 'e': 5} 95 self.assertEqual(d1.viewkeys() & d1.viewkeys(), {'a', 'b'}) 96 self.assertEqual(d1.viewkeys() & d2.viewkeys(), {'b'}) 97 self.assertEqual(d1.viewkeys() & d3.viewkeys(), set()) 98 self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), {'a', 'b'}) 99 self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), {'b'}) 100 self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set()) 101 self.assertEqual(d1.viewkeys() & tuple(d1.viewkeys()), {'a', 'b'}) 102 103 self.assertEqual(d1.viewkeys() | d1.viewkeys(), {'a', 'b'}) 104 self.assertEqual(d1.viewkeys() | d2.viewkeys(), {'a', 'b', 'c'}) 105 self.assertEqual(d1.viewkeys() | d3.viewkeys(), {'a', 'b', 'd', 'e'}) 106 self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), {'a', 'b'}) 107 self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), {'a', 'b', 'c'}) 108 self.assertEqual(d1.viewkeys() | set(d3.viewkeys()), 109 {'a', 'b', 'd', 'e'}) 110 self.assertEqual(d1.viewkeys() | (1, 2), {'a', 'b', 1, 2}) 111 112 self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set()) 113 self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), {'a', 'c'}) 114 self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), {'a', 'b', 'd', 'e'}) 115 self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set()) 116 self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), {'a', 'c'}) 117 self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()), 118 {'a', 'b', 'd', 'e'}) 119 self.assertEqual(d1.viewkeys() ^ tuple(d2.keys()), {'a', 'c'}) 120 121 self.assertEqual(d1.viewkeys() - d1.viewkeys(), set()) 122 self.assertEqual(d1.viewkeys() - d2.viewkeys(), {'a'}) 123 self.assertEqual(d1.viewkeys() - d3.viewkeys(), {'a', 'b'}) 124 self.assertEqual(d1.viewkeys() - set(d1.viewkeys()), set()) 125 self.assertEqual(d1.viewkeys() - set(d2.viewkeys()), {'a'}) 126 self.assertEqual(d1.viewkeys() - set(d3.viewkeys()), {'a', 'b'}) 127 self.assertEqual(d1.viewkeys() - (0, 1), {'a', 'b'}) 128 129 def test_items_set_operations(self): 130 d1 = {'a': 1, 'b': 2} 131 d2 = {'a': 2, 'b': 2} 132 d3 = {'d': 4, 'e': 5} 133 self.assertEqual( 134 d1.viewitems() & d1.viewitems(), {('a', 1), ('b', 2)}) 135 self.assertEqual(d1.viewitems() & d2.viewitems(), {('b', 2)}) 136 self.assertEqual(d1.viewitems() & d3.viewitems(), set()) 137 self.assertEqual(d1.viewitems() & set(d1.viewitems()), 138 {('a', 1), ('b', 2)}) 139 self.assertEqual(d1.viewitems() & set(d2.viewitems()), {('b', 2)}) 140 self.assertEqual(d1.viewitems() & set(d3.viewitems()), set()) 141 142 self.assertEqual(d1.viewitems() | d1.viewitems(), 143 {('a', 1), ('b', 2)}) 144 self.assertEqual(d1.viewitems() | d2.viewitems(), 145 {('a', 1), ('a', 2), ('b', 2)}) 146 self.assertEqual(d1.viewitems() | d3.viewitems(), 147 {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) 148 self.assertEqual(d1.viewitems() | set(d1.viewitems()), 149 {('a', 1), ('b', 2)}) 150 self.assertEqual(d1.viewitems() | set(d2.viewitems()), 151 {('a', 1), ('a', 2), ('b', 2)}) 152 self.assertEqual(d1.viewitems() | set(d3.viewitems()), 153 {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) 154 155 self.assertEqual(d1.viewitems() ^ d1.viewitems(), set()) 156 self.assertEqual(d1.viewitems() ^ d2.viewitems(), 157 {('a', 1), ('a', 2)}) 158 self.assertEqual(d1.viewitems() ^ d3.viewitems(), 159 {('a', 1), ('b', 2), ('d', 4), ('e', 5)}) 160 161 self.assertEqual(d1.viewitems() - d1.viewitems(), set()) 162 self.assertEqual(d1.viewitems() - d2.viewitems(), {('a', 1)}) 163 self.assertEqual(d1.viewitems() - d3.viewitems(), {('a', 1), ('b', 2)}) 164 self.assertEqual(d1.viewitems() - set(d1.viewitems()), set()) 165 self.assertEqual(d1.viewitems() - set(d2.viewitems()), {('a', 1)}) 166 self.assertEqual(d1.viewitems() - set(d3.viewitems()), 167 {('a', 1), ('b', 2)}) 168 169 def test_recursive_repr(self): 170 d = {} 171 d[42] = d.viewvalues() 172 self.assertRaises(RuntimeError, repr, d) 173 174 def test_abc_registry(self): 175 d = dict(a=1) 176 177 self.assertIsInstance(d.viewkeys(), collections.KeysView) 178 self.assertIsInstance(d.viewkeys(), collections.MappingView) 179 self.assertIsInstance(d.viewkeys(), collections.Set) 180 self.assertIsInstance(d.viewkeys(), collections.Sized) 181 self.assertIsInstance(d.viewkeys(), collections.Iterable) 182 self.assertIsInstance(d.viewkeys(), collections.Container) 183 184 self.assertIsInstance(d.viewvalues(), collections.ValuesView) 185 self.assertIsInstance(d.viewvalues(), collections.MappingView) 186 self.assertIsInstance(d.viewvalues(), collections.Sized) 187 188 self.assertIsInstance(d.viewitems(), collections.ItemsView) 189 self.assertIsInstance(d.viewitems(), collections.MappingView) 190 self.assertIsInstance(d.viewitems(), collections.Set) 191 self.assertIsInstance(d.viewitems(), collections.Sized) 192 self.assertIsInstance(d.viewitems(), collections.Iterable) 193 self.assertIsInstance(d.viewitems(), collections.Container) 194 195 def test_copy(self): 196 d = {1: 10, "a": "ABC"} 197 self.assertRaises(TypeError, copy.copy, d.viewkeys()) 198 self.assertRaises(TypeError, copy.copy, d.viewvalues()) 199 self.assertRaises(TypeError, copy.copy, d.viewitems()) 200 201 def test_pickle(self): 202 d = {1: 10, "a": "ABC"} 203 for proto in range(pickle.HIGHEST_PROTOCOL + 1): 204 self.assertRaises((TypeError, pickle.PicklingError), 205 pickle.dumps, d.viewkeys(), proto) 206 self.assertRaises((TypeError, pickle.PicklingError), 207 pickle.dumps, d.viewvalues(), proto) 208 self.assertRaises((TypeError, pickle.PicklingError), 209 pickle.dumps, d.viewitems(), proto) 210 211 212def test_main(): 213 test_support.run_unittest(DictSetTest) 214 215if __name__ == "__main__": 216 test_main() 217