1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Check every path through every method of UserDict 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_support, mapping_tests 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport UserDict 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd0 = {} 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd1 = {"one": 1} 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd2 = {"one": 1, "two": 2} 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd3 = {"one": 1, "two": 3, "three": 5} 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd4 = {"one": None, "two": None} 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepd5 = {"one": 1, "two": 1} 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass UserDictTest(mapping_tests.TestHashMappingProtocol): 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep type2test = UserDict.IterableUserDict 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_all(self): 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test constructors 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u = UserDict.UserDict() 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u0 = UserDict.UserDict(d0) 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u1 = UserDict.UserDict(d1) 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u2 = UserDict.IterableUserDict(d2) 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep uu = UserDict.UserDict(u) 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep uu0 = UserDict.UserDict(u0) 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep uu1 = UserDict.UserDict(u1) 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep uu2 = UserDict.UserDict(u2) 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # keyword arg constructor 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict(one=1, two=2), d2) 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # item sequence constructor 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict([('one',1), ('two',2)]), d2) 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict(dict=[('one',1), ('two',2)]), d2) 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # both together 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict([('one',1), ('two',2)], two=3, three=5), d3) 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # alternate constructor 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict.fromkeys('one two'.split()), d4) 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict().fromkeys('one two'.split()), d4) 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict.fromkeys('one two'.split(), 1), d5) 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(UserDict.UserDict().fromkeys('one two'.split(), 1), d5) 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(u1.fromkeys('one two'.split()) is not u1) 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsInstance(u1.fromkeys('one two'.split()), UserDict.UserDict) 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIsInstance(u2.fromkeys('one two'.split()), UserDict.IterableUserDict) 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test __repr__ 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(str(u0), str(d0)) 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(repr(u1), repr(d1)) 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(repr(u2), repr(d2)) 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test __cmp__ and __len__ 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for a in all: 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for b in all: 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(cmp(a, b), cmp(len(a), len(b))) 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test __getitem__ 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u2["one"], 1) 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(KeyError, u1.__getitem__, "two") 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test __setitem__ 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u3 = UserDict.UserDict(u2) 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u3["two"] = 2 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u3["three"] = 3 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test __delitem__ 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep del u3["three"] 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(KeyError, u3.__delitem__, "three") 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test clear 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u3.clear() 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u3, {}) 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test copy() 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u2a = u2.copy() 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u2a, u2) 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u2b = UserDict.UserDict(x=42, y=23) 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u2c = u2b.copy() # making a copy of a UserDict is special cased 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u2b, u2c) 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class MyUserDict(UserDict.UserDict): 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def display(self): print self 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep m2 = MyUserDict(u2) 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep m2a = m2.copy() 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(m2a, m2) 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # SF bug #476616 -- copy() of UserDict subclass shared data 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep m2['foo'] = 'bar' 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertNotEqual(m2a, m2) 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test keys, items, values 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u2.keys(), d2.keys()) 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u2.items(), d2.items()) 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u2.values(), d2.values()) 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test has_key and "in". 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in u2.keys(): 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIn(i, u2) 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(i in u1, i in d1) 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(i in u0, i in d0) 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep with test_support.check_py3k_warnings(): 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(u2.has_key(i)) 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u1.has_key(i), d1.has_key(i)) 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u0.has_key(i), d0.has_key(i)) 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test update 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t = UserDict.UserDict() 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t.update(u2) 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t, u2) 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class Items: 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def items(self): 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (("x", 42), ("y", 23)) 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t = UserDict.UserDict() 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t.update(Items()) 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t, {"x": 42, "y": 23}) 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test get 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in u2.keys(): 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u2.get(i), u2[i]) 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u1.get(i), d1.get(i)) 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(u0.get(i), d0.get(i)) 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test "in" iteration. 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in xrange(20): 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep u2[i] = str(i) 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ikeys = [] 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for k in u2: 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ikeys.append(k) 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep keys = u2.keys() 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(set(ikeys), set(keys)) 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test setdefault 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t = UserDict.UserDict() 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t.setdefault("x", 42), 42) 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(t.has_key("x")) 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t.setdefault("x", 23), 42) 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test pop 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t = UserDict.UserDict(x=42) 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t.pop("x"), 42) 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(KeyError, t.pop, "x") 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t.pop("x", 1), 1) 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t["x"] = 42 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t.pop("x", 1), 42) 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test popitem 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t = UserDict.UserDict(x=42) 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(t.popitem(), ("x", 42)) 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(KeyError, t.popitem) 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_missing(self): 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Make sure UserDict doesn't have a __missing__ method 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(hasattr(UserDict, "__missing__"), False) 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Test several cases: 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # (D) subclass defines __missing__ method returning a value 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # (E) subclass defines __missing__ method raising RuntimeError 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # (F) subclass sets __missing__ instance variable (no effect) 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # (G) subclass doesn't define __missing__ at a all 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class D(UserDict.UserDict): 160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __missing__(self, key): 161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return 42 162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep d = D({1: 2, 3: 4}) 163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(d[1], 2) 164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(d[3], 4) 165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertNotIn(2, d) 166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertNotIn(2, d.keys()) 167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(d[2], 42) 168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class E(UserDict.UserDict): 169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __missing__(self, key): 170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise RuntimeError(key) 171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e = E() 172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep e[42] 174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except RuntimeError, err: 175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(err.args, (42,)) 176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.fail("e[42] didn't raise RuntimeError") 178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class F(UserDict.UserDict): 179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self): 180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # An instance variable __missing__ should have no effect 181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.__missing__ = lambda key: None 182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UserDict.UserDict.__init__(self) 183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep f = F() 184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep f[42] 186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except KeyError, err: 187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(err.args, (42,)) 188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.fail("f[42] didn't raise KeyError") 190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class G(UserDict.UserDict): 191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep g = G() 193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep g[42] 195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except KeyError, err: 196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(err.args, (42,)) 197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.fail("g[42] didn't raise KeyError") 199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep########################## 201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Test Dict Mixin 202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass SeqDict(UserDict.DictMixin): 204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Dictionary lookalike implemented with lists. 205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Used to test and demonstrate DictMixin 207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, other=None, **kwargs): 209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.keylist = [] 210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.valuelist = [] 211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if other is not None: 212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for (key, value) in other: 213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self[key] = value 214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for (key, value) in kwargs.iteritems(): 215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self[key] = value 216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __getitem__(self, key): 217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = self.keylist.index(key) 219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except ValueError: 220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise KeyError 221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.valuelist[i] 222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __setitem__(self, key, value): 223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = self.keylist.index(key) 225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.valuelist[i] = value 226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except ValueError: 227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.keylist.append(key) 228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.valuelist.append(value) 229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __delitem__(self, key): 230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep i = self.keylist.index(key) 232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except ValueError: 233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise KeyError 234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.keylist.pop(i) 235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.valuelist.pop(i) 236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def keys(self): 237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return list(self.keylist) 238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def copy(self): 239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep d = self.__class__() 240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for key, value in self.iteritems(): 241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep d[key] = value 242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return d 243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep @classmethod 244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def fromkeys(cls, keys, value=None): 245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep d = cls() 246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for key in keys: 247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep d[key] = value 248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return d 249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass UserDictMixinTest(mapping_tests.TestMappingProtocol): 251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep type2test = SeqDict 252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_all(self): 254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ## Setup test and verify working of the test class 255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # check init 257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s = SeqDict() 258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # exercise setitem 260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s[10] = 'ten' 261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s[20] = 'twenty' 262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s[30] = 'thirty' 263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # exercise delitem 265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep del s[20] 266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # check getitem and setitem 267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s[10], 'ten') 268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # check keys() and delitem 269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.keys(), [10, 30]) 270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ## Now, test the DictMixin methods one by one 272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # has_key 273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(s.has_key(10)) 274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertTrue(not s.has_key(20)) 275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # __contains__ 277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertIn(10, s) 278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertNotIn(20, s) 279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # __iter__ 281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual([k for k in s], [10, 30]) 282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # __len__ 284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(s), 2) 285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # iteritems 287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(s.iteritems()), [(10,'ten'), (30, 'thirty')]) 288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # iterkeys 290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(s.iterkeys()), [10, 30]) 291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # itervalues 293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(list(s.itervalues()), ['ten', 'thirty']) 294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # values 296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.values(), ['ten', 'thirty']) 297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # items 299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.items(), [(10,'ten'), (30, 'thirty')]) 300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # get 302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.get(10), 'ten') 303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.get(15,'fifteen'), 'fifteen') 304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.get(15), None) 305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # setdefault 307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.setdefault(40, 'forty'), 'forty') 308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.setdefault(10, 'null'), 'ten') 309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep del s[40] 310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # pop 312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.pop(10), 'ten') 313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertNotIn(10, s) 314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s[10] = 'ten' 315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.pop("x", 1), 1) 316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s["x"] = 42 317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s.pop("x", 1), 42) 318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # popitem 320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep k, v = s.popitem() 321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertNotIn(k, s) 322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s[k] = v 323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # clear 325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s.clear() 326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(len(s), 0) 327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # empty popitem 329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertRaises(KeyError, s.popitem) 330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # update 332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep s.update({10: 'ten', 20:'twenty'}) 333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s[10], 'ten') 334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s[20], 'twenty') 335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # cmp 337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s, {10: 'ten', 20:'twenty'}) 338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t = SeqDict() 339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t[20] = 'twenty' 340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t[10] = 'ten' 341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(s, t) 342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(): 344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_support.run_unittest( 345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UserDictTest, 346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep UserDictMixinTest 347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ) 348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__": 350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main() 351