0001 # Check every path through every method of UserDict 0002 0003 import unittest 0004 from test import test_support, mapping_tests 0005 import UserDict 0006 0007 d0 = {} 0008 d1 = {"one": 1} 0009 d2 = {"one": 1, "two": 2} 0010 d3 = {"one": 1, "two": 3, "three": 5} 0011 d4 = {"one": None, "two": None} 0012 d5 = {"one": 1, "two": 1} 0013 0014 class UserDictTest(mapping_tests.TestHashMappingProtocol): 0015 type2test = UserDict.IterableUserDict 0016 0017 def test_all(self): 0018 # Test constructors 0019 u = UserDict.UserDict() 0020 u0 = UserDict.UserDict(d0) 0021 u1 = UserDict.UserDict(d1) 0022 u2 = UserDict.IterableUserDict(d2) 0023 0024 uu = UserDict.UserDict(u) 0025 uu0 = UserDict.UserDict(u0) 0026 uu1 = UserDict.UserDict(u1) 0027 uu2 = UserDict.UserDict(u2) 0028 0029 # keyword arg constructor 0030 self.assertEqual(UserDict.UserDict(one=1, two=2), d2) 0031 # item sequence constructor 0032 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)]), d2) 0033 self.assertEqual(UserDict.UserDict(dict=[('one',1), ('two',2)]), d2) 0034 # both together 0035 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)], two=3, three=5), d3) 0036 0037 # alternate constructor 0038 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split()), d4) 0039 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split()), d4) 0040 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split(), 1), d5) 0041 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split(), 1), d5) 0042 self.assert_(u1.fromkeys('one two'.split()) is not u1) 0043 self.assert_(isinstance(u1.fromkeys('one two'.split()), UserDict.UserDict)) 0044 self.assert_(isinstance(u2.fromkeys('one two'.split()), UserDict.IterableUserDict)) 0045 0046 # Test __repr__ 0047 self.assertEqual(str(u0), str(d0)) 0048 self.assertEqual(repr(u1), repr(d1)) 0049 self.assertEqual(`u2`, `d2`) 0050 0051 # Test __cmp__ and __len__ 0052 all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] 0053 for a in all: 0054 for b in all: 0055 self.assertEqual(cmp(a, b), cmp(len(a), len(b))) 0056 0057 # Test __getitem__ 0058 self.assertEqual(u2["one"], 1) 0059 self.assertRaises(KeyError, u1.__getitem__, "two") 0060 0061 # Test __setitem__ 0062 u3 = UserDict.UserDict(u2) 0063 u3["two"] = 2 0064 u3["three"] = 3 0065 0066 # Test __delitem__ 0067 del u3["three"] 0068 self.assertRaises(KeyError, u3.__delitem__, "three") 0069 0070 # Test clear 0071 u3.clear() 0072 self.assertEqual(u3, {}) 0073 0074 # Test copy() 0075 u2a = u2.copy() 0076 self.assertEqual(u2a, u2) 0077 u2b = UserDict.UserDict(x=42, y=23) 0078 u2c = u2b.copy() # making a copy of a UserDict is special cased 0079 self.assertEqual(u2b, u2c) 0080 0081 class MyUserDict(UserDict.UserDict): 0082 def display(self): print self 0083 0084 m2 = MyUserDict(u2) 0085 m2a = m2.copy() 0086 self.assertEqual(m2a, m2) 0087 0088 # SF bug #476616 -- copy() of UserDict subclass shared data 0089 m2['foo'] = 'bar' 0090 self.assertNotEqual(m2a, m2) 0091 0092 # Test keys, items, values 0093 self.assertEqual(u2.keys(), d2.keys()) 0094 self.assertEqual(u2.items(), d2.items()) 0095 self.assertEqual(u2.values(), d2.values()) 0096 0097 # Test has_key and "in". 0098 for i in u2.keys(): 0099 self.assert_(u2.has_key(i)) 0100 self.assert_(i in u2) 0101 self.assertEqual(u1.has_key(i), d1.has_key(i)) 0102 self.assertEqual(i in u1, i in d1) 0103 self.assertEqual(u0.has_key(i), d0.has_key(i)) 0104 self.assertEqual(i in u0, i in d0) 0105 0106 # Test update 0107 t = UserDict.UserDict() 0108 t.update(u2) 0109 self.assertEqual(t, u2) 0110 class Items: 0111 def items(self): 0112 return (("x", 42), ("y", 23)) 0113 t = UserDict.UserDict() 0114 t.update(Items()) 0115 self.assertEqual(t, {"x": 42, "y": 23}) 0116 0117 # Test get 0118 for i in u2.keys(): 0119 self.assertEqual(u2.get(i), u2[i]) 0120 self.assertEqual(u1.get(i), d1.get(i)) 0121 self.assertEqual(u0.get(i), d0.get(i)) 0122 0123 # Test "in" iteration. 0124 for i in xrange(20): 0125 u2[i] = str(i) 0126 ikeys = [] 0127 for k in u2: 0128 ikeys.append(k) 0129 keys = u2.keys() 0130 self.assertEqual(set(ikeys), set(keys)) 0131 0132 # Test setdefault 0133 t = UserDict.UserDict() 0134 self.assertEqual(t.setdefault("x", 42), 42) 0135 self.assert_(t.has_key("x")) 0136 self.assertEqual(t.setdefault("x", 23), 42) 0137 0138 # Test pop 0139 t = UserDict.UserDict(x=42) 0140 self.assertEqual(t.pop("x"), 42) 0141 self.assertRaises(KeyError, t.pop, "x") 0142 self.assertEqual(t.pop("x", 1), 1) 0143 t["x"] = 42 0144 self.assertEqual(t.pop("x", 1), 42) 0145 0146 # Test popitem 0147 t = UserDict.UserDict(x=42) 0148 self.assertEqual(t.popitem(), ("x", 42)) 0149 self.assertRaises(KeyError, t.popitem) 0150 0151 ########################## 0152 # Test Dict Mixin 0153 0154 class SeqDict(UserDict.DictMixin): 0155 """Dictionary lookalike implemented with lists. 0156 0157 Used to test and demonstrate DictMixin 0158 """ 0159 def __init__(self, other=None, **kwargs): 0160 self.keylist = [] 0161 self.valuelist = [] 0162 if other is not None: 0163 for (key, value) in other: 0164 self[key] = value 0165 for (key, value) in kwargs.iteritems(): 0166 self[key] = value 0167 def __getitem__(self, key): 0168 try: 0169 i = self.keylist.index(key) 0170 except ValueError: 0171 raise KeyError 0172 return self.valuelist[i] 0173 def __setitem__(self, key, value): 0174 try: 0175 i = self.keylist.index(key) 0176 self.valuelist[i] = value 0177 except ValueError: 0178 self.keylist.append(key) 0179 self.valuelist.append(value) 0180 def __delitem__(self, key): 0181 try: 0182 i = self.keylist.index(key) 0183 except ValueError: 0184 raise KeyError 0185 self.keylist.pop(i) 0186 self.valuelist.pop(i) 0187 def keys(self): 0188 return list(self.keylist) 0189 def copy(self): 0190 d = self.__class__() 0191 for key, value in self.iteritems(): 0192 d[key] = value 0193 return d 0194 def fromkeys(cls, keys, value=None): 0195 d = cls() 0196 for key in keys: 0197 d[key] = value 0198 return d 0199 fromkeys = classmethod(fromkeys) 0200 0201 class UserDictMixinTest(mapping_tests.TestMappingProtocol): 0202 type2test = SeqDict 0203 0204 def test_all(self): 0205 ## Setup test and verify working of the test class 0206 0207 # check init 0208 s = SeqDict() 0209 0210 # exercise setitem 0211 s[10] = 'ten' 0212 s[20] = 'twenty' 0213 s[30] = 'thirty' 0214 0215 # exercise delitem 0216 del s[20] 0217 # check getitem and setitem 0218 self.assertEqual(s[10], 'ten') 0219 # check keys() and delitem 0220 self.assertEqual(s.keys(), [10, 30]) 0221 0222 ## Now, test the DictMixin methods one by one 0223 # has_key 0224 self.assert_(s.has_key(10)) 0225 self.assert_(not s.has_key(20)) 0226 0227 # __contains__ 0228 self.assert_(10 in s) 0229 self.assert_(20 not in s) 0230 0231 # __iter__ 0232 self.assertEqual([k for k in s], [10, 30]) 0233 0234 # __len__ 0235 self.assertEqual(len(s), 2) 0236 0237 # iteritems 0238 self.assertEqual(list(s.iteritems()), [(10,'ten'), (30, 'thirty')]) 0239 0240 # iterkeys 0241 self.assertEqual(list(s.iterkeys()), [10, 30]) 0242 0243 # itervalues 0244 self.assertEqual(list(s.itervalues()), ['ten', 'thirty']) 0245 0246 # values 0247 self.assertEqual(s.values(), ['ten', 'thirty']) 0248 0249 # items 0250 self.assertEqual(s.items(), [(10,'ten'), (30, 'thirty')]) 0251 0252 # get 0253 self.assertEqual(s.get(10), 'ten') 0254 self.assertEqual(s.get(15,'fifteen'), 'fifteen') 0255 self.assertEqual(s.get(15), None) 0256 0257 # setdefault 0258 self.assertEqual(s.setdefault(40, 'forty'), 'forty') 0259 self.assertEqual(s.setdefault(10, 'null'), 'ten') 0260 del s[40] 0261 0262 # pop 0263 self.assertEqual(s.pop(10), 'ten') 0264 self.assert_(10 not in s) 0265 s[10] = 'ten' 0266 self.assertEqual(s.pop("x", 1), 1) 0267 s["x"] = 42 0268 self.assertEqual(s.pop("x", 1), 42) 0269 0270 # popitem 0271 k, v = s.popitem() 0272 self.assert_(k not in s) 0273 s[k] = v 0274 0275 # clear 0276 s.clear() 0277 self.assertEqual(len(s), 0) 0278 0279 # empty popitem 0280 self.assertRaises(KeyError, s.popitem) 0281 0282 # update 0283 s.update({10: 'ten', 20:'twenty'}) 0284 self.assertEqual(s[10], 'ten') 0285 self.assertEqual(s[20], 'twenty') 0286 0287 # cmp 0288 self.assertEqual(s, {10: 'ten', 20:'twenty'}) 0289 t = SeqDict() 0290 t[20] = 'twenty' 0291 t[10] = 'ten' 0292 self.assertEqual(s, t) 0293 0294 def test_main(): 0295 test_support.run_unittest( 0296 UserDictTest, 0297 UserDictMixinTest 0298 ) 0299 0300 if __name__ == "__main__": 0301 test_main() 0302
Generated by PyXR 0.9.4