(self)
| 25 | type2test = collections.UserDict |
| 26 | |
| 27 | def test_all(self): |
| 28 | # Test constructors |
| 29 | u = collections.UserDict() |
| 30 | u0 = collections.UserDict(d0) |
| 31 | u1 = collections.UserDict(d1) |
| 32 | u2 = collections.UserDict(d2) |
| 33 | |
| 34 | uu = collections.UserDict(u) |
| 35 | uu0 = collections.UserDict(u0) |
| 36 | uu1 = collections.UserDict(u1) |
| 37 | uu2 = collections.UserDict(u2) |
| 38 | |
| 39 | # keyword arg constructor |
| 40 | self.assertEqual(collections.UserDict(one=1, two=2), d2) |
| 41 | # item sequence constructor |
| 42 | self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2) |
| 43 | self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), |
| 44 | {'dict': [('one', 1), ('two', 2)]}) |
| 45 | # both together |
| 46 | self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3) |
| 47 | |
| 48 | # alternate constructor |
| 49 | self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4) |
| 50 | self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4) |
| 51 | self.assertEqual(collections.UserDict.fromkeys('one two'.split(), 1), d5) |
| 52 | self.assertEqual(collections.UserDict().fromkeys('one two'.split(), 1), d5) |
| 53 | self.assertTrue(u1.fromkeys('one two'.split()) is not u1) |
| 54 | self.assertIsInstance(u1.fromkeys('one two'.split()), collections.UserDict) |
| 55 | self.assertIsInstance(u2.fromkeys('one two'.split()), collections.UserDict) |
| 56 | |
| 57 | # Test __repr__ |
| 58 | self.assertEqual(str(u0), str(d0)) |
| 59 | self.assertEqual(repr(u1), repr(d1)) |
| 60 | self.assertIn(repr(u2), ("{'one': 1, 'two': 2}", |
| 61 | "{'two': 2, 'one': 1}")) |
| 62 | |
| 63 | # Test rich comparison and __len__ |
| 64 | all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] |
| 65 | for a in all: |
| 66 | for b in all: |
| 67 | self.assertEqual(a == b, len(a) == len(b)) |
| 68 | |
| 69 | # Test __getitem__ |
| 70 | self.assertEqual(u2["one"], 1) |
| 71 | self.assertRaises(KeyError, u1.__getitem__, "two") |
| 72 | |
| 73 | # Test __setitem__ |
| 74 | u3 = collections.UserDict(u2) |
| 75 | u3["two"] = 2 |
| 76 | u3["three"] = 3 |
| 77 | |
| 78 | # Test __delitem__ |
| 79 | del u3["three"] |
| 80 | self.assertRaises(KeyError, u3.__delitem__, "three") |
| 81 | |
| 82 | # Test clear |
| 83 | u3.clear() |
| 84 | self.assertEqual(u3, {}) |
nothing calls this directly
no test coverage detected