MCPcopy Index your code
hub / github.com/python/cpython / test_union

Method test_union

Lib/test/test_defaultdict.py:151–187  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

149 self.assertEqual(d, o)
150
151 def test_union(self):
152 i = defaultdict(int, {1: 1, 2: 2})
153 s = defaultdict(str, {0: "zero", 1: "one"})
154
155 i_s = i | s
156 self.assertIs(i_s.default_factory, int)
157 self.assertDictEqual(i_s, {1: "one", 2: 2, 0: "zero"})
158 self.assertEqual(list(i_s), [1, 2, 0])
159
160 s_i = s | i
161 self.assertIs(s_i.default_factory, str)
162 self.assertDictEqual(s_i, {0: "zero", 1: 1, 2: 2})
163 self.assertEqual(list(s_i), [0, 1, 2])
164
165 i_ds = i | dict(s)
166 self.assertIs(i_ds.default_factory, int)
167 self.assertDictEqual(i_ds, {1: "one", 2: 2, 0: "zero"})
168 self.assertEqual(list(i_ds), [1, 2, 0])
169
170 ds_i = dict(s) | i
171 self.assertIs(ds_i.default_factory, int)
172 self.assertDictEqual(ds_i, {0: "zero", 1: 1, 2: 2})
173 self.assertEqual(list(ds_i), [0, 1, 2])
174
175 with self.assertRaises(TypeError):
176 i | list(s.items())
177 with self.assertRaises(TypeError):
178 list(s.items()) | i
179
180 # We inherit a fine |= from dict, so just a few sanity checks here:
181 i |= list(s.items())
182 self.assertIs(i.default_factory, int)
183 self.assertDictEqual(i, {1: "one", 2: 2, 0: "zero"})
184 self.assertEqual(list(i), [1, 2, 0])
185
186 with self.assertRaises(TypeError):
187 i |= None
188
189 def test_factory_conflict_with_set_value(self):
190 key = "conflict_test"

Callers

nothing calls this directly

Calls 7

defaultdictClass · 0.85
listClass · 0.85
assertDictEqualMethod · 0.80
assertIsMethod · 0.45
assertEqualMethod · 0.45
assertRaisesMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected