Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 1 | # Check every path through every method of UserDict |
| 2 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test.test_support import verify, verbose |
Guido van Rossum | 288cd2c | 2001-08-07 17:50:06 +0000 | [diff] [blame] | 4 | from UserDict import UserDict, IterableUserDict |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 5 | |
| 6 | d0 = {} |
| 7 | d1 = {"one": 1} |
| 8 | d2 = {"one": 1, "two": 2} |
| 9 | |
| 10 | # Test constructors |
| 11 | |
| 12 | u = UserDict() |
| 13 | u0 = UserDict(d0) |
| 14 | u1 = UserDict(d1) |
Guido van Rossum | 288cd2c | 2001-08-07 17:50:06 +0000 | [diff] [blame] | 15 | u2 = IterableUserDict(d2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 16 | |
| 17 | uu = UserDict(u) |
| 18 | uu0 = UserDict(u0) |
| 19 | uu1 = UserDict(u1) |
| 20 | uu2 = UserDict(u2) |
| 21 | |
Raymond Hettinger | 5440545 | 2002-11-22 00:07:40 +0000 | [diff] [blame] | 22 | verify(UserDict(one=1, two=2) == d2) # keyword arg constructor |
| 23 | verify(UserDict([('one',1), ('two',2)]) == d2) # item sequence constructor |
| 24 | |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 25 | # Test __repr__ |
| 26 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 27 | verify(str(u0) == str(d0)) |
| 28 | verify(repr(u1) == repr(d1)) |
| 29 | verify(`u2` == `d2`) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 30 | |
| 31 | # Test __cmp__ and __len__ |
| 32 | |
| 33 | all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] |
| 34 | for a in all: |
| 35 | for b in all: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 36 | verify(cmp(a, b) == cmp(len(a), len(b))) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 37 | |
| 38 | # Test __getitem__ |
| 39 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 40 | verify(u2["one"] == 1) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 41 | try: |
| 42 | u1["two"] |
| 43 | except KeyError: |
| 44 | pass |
| 45 | else: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 46 | verify(0, "u1['two'] shouldn't exist") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 47 | |
| 48 | # Test __setitem__ |
| 49 | |
| 50 | u3 = UserDict(u2) |
| 51 | u3["two"] = 2 |
| 52 | u3["three"] = 3 |
| 53 | |
| 54 | # Test __delitem__ |
| 55 | |
| 56 | del u3["three"] |
| 57 | try: |
| 58 | del u3["three"] |
| 59 | except KeyError: |
| 60 | pass |
| 61 | else: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 62 | verify(0, "u3['three'] shouldn't exist") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 63 | |
| 64 | # Test clear |
| 65 | |
| 66 | u3.clear() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 67 | verify(u3 == {}) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 68 | |
| 69 | # Test copy() |
| 70 | |
| 71 | u2a = u2.copy() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 72 | verify(u2a == u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 73 | |
| 74 | class MyUserDict(UserDict): |
| 75 | def display(self): print self |
| 76 | |
| 77 | m2 = MyUserDict(u2) |
| 78 | m2a = m2.copy() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 79 | verify(m2a == m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 80 | |
Fred Drake | 5262867 | 2001-11-05 17:41:48 +0000 | [diff] [blame] | 81 | # SF bug #476616 -- copy() of UserDict subclass shared data |
| 82 | m2['foo'] = 'bar' |
| 83 | verify(m2a != m2) |
| 84 | |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 85 | # Test keys, items, values |
| 86 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 87 | verify(u2.keys() == d2.keys()) |
| 88 | verify(u2.items() == d2.items()) |
| 89 | verify(u2.values() == d2.values()) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 90 | |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 91 | # Test has_key and "in". |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 92 | |
| 93 | for i in u2.keys(): |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 94 | verify(u2.has_key(i) == 1) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 95 | verify((i in u2) == 1) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 96 | verify(u1.has_key(i) == d1.has_key(i)) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 97 | verify((i in u1) == (i in d1)) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 98 | verify(u0.has_key(i) == d0.has_key(i)) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 99 | verify((i in u0) == (i in d0)) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 100 | |
| 101 | # Test update |
| 102 | |
| 103 | t = UserDict() |
| 104 | t.update(u2) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 105 | verify(t == u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 106 | |
| 107 | # Test get |
| 108 | |
| 109 | for i in u2.keys(): |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 110 | verify(u2.get(i) == u2[i]) |
| 111 | verify(u1.get(i) == d1.get(i)) |
| 112 | verify(u0.get(i) == d0.get(i)) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 113 | |
| 114 | # Test "in" iteration. |
| 115 | for i in xrange(20): |
| 116 | u2[i] = str(i) |
| 117 | ikeys = [] |
| 118 | for k in u2: |
| 119 | ikeys.append(k) |
| 120 | ikeys.sort() |
| 121 | keys = u2.keys() |
| 122 | keys.sort() |
| 123 | verify(ikeys == keys) |
Raymond Hettinger | 903bf90 | 2002-11-15 08:39:40 +0000 | [diff] [blame] | 124 | |
| 125 | ########################## |
| 126 | # Test Dict Mixin |
| 127 | |
| 128 | from UserDict import DictMixin |
| 129 | |
| 130 | class SeqDict(DictMixin): |
| 131 | """Dictionary lookalike implemented with lists. |
| 132 | |
| 133 | Used to test and demonstrate DictMixin |
| 134 | """ |
| 135 | def __init__(self): |
| 136 | self.keylist = [] |
| 137 | self.valuelist = [] |
| 138 | def __getitem__(self, key): |
| 139 | try: |
| 140 | i = self.keylist.index(key) |
| 141 | except ValueError: |
| 142 | raise KeyError |
| 143 | return self.valuelist[i] |
| 144 | def __setitem__(self, key, value): |
| 145 | self.keylist.append(key) |
| 146 | self.valuelist.append(value) |
| 147 | def __delitem__(self, key): |
| 148 | try: |
| 149 | i = self.keylist.index(key) |
| 150 | except ValueError: |
| 151 | raise KeyError |
| 152 | self.keylist.pop(i) |
| 153 | self.valuelist.pop(i) |
| 154 | def keys(self): |
| 155 | return list(self.keylist) |
| 156 | |
| 157 | ## Setup test and verify working of the test class |
| 158 | s = SeqDict() # check init |
| 159 | s[10] = 'ten' # exercise setitem |
| 160 | s[20] = 'twenty' |
| 161 | s[30] = 'thirty' |
| 162 | del s[20] # exercise delitem |
| 163 | verify(s[10] == 'ten') # check getitem and setitem |
| 164 | verify(s.keys() == [10, 30]) # check keys() and delitem |
| 165 | |
| 166 | ## Now, test the DictMixin methods one by one |
| 167 | verify(s.has_key(10)) # has_key |
| 168 | verify(not s.has_key(20)) |
| 169 | |
| 170 | verify(10 in s) # __contains__ |
| 171 | verify(20 not in s) |
| 172 | |
| 173 | verify([k for k in s] == [10, 30]) # __iter__ |
| 174 | |
| 175 | verify(len(s) == 2) # __len__ |
| 176 | |
| 177 | verify(list(s.iteritems()) == [(10,'ten'), (30, 'thirty')]) # iteritems |
| 178 | |
| 179 | verify(list(s.iterkeys()) == [10, 30]) # iterkeys |
| 180 | |
| 181 | verify(list(s.itervalues()) == ['ten', 'thirty']) # itervalues |
| 182 | |
| 183 | verify(s.values() == ['ten', 'thirty']) # values |
| 184 | |
| 185 | verify(s.items() == [(10,'ten'), (30, 'thirty')]) # items |
| 186 | |
| 187 | verify(s.get(10) == 'ten') # get |
| 188 | verify(s.get(15,'fifteen') == 'fifteen') |
| 189 | verify(s.get(15) == None) |
| 190 | |
| 191 | verify(s.setdefault(40, 'forty') == 'forty') # setdefault |
| 192 | verify(s.setdefault(10, 'null') == 'ten') |
| 193 | del s[40] |
| 194 | |
| 195 | verify(s.pop(10) == 'ten') # pop |
| 196 | verify(10 not in s) |
| 197 | s[10] = 'ten' |
| 198 | |
| 199 | k, v = s.popitem() # popitem |
| 200 | verify(k not in s) |
| 201 | s[k] = v |
| 202 | |
| 203 | s.clear() # clear |
| 204 | verify(len(s) == 0) |
| 205 | |
| 206 | try: # empty popitem |
| 207 | s.popitem() |
| 208 | except KeyError: |
| 209 | pass |
| 210 | else: |
| 211 | verify(0, "popitem from an empty list should raise KeyError") |
| 212 | |
| 213 | s.update({10: 'ten', 20:'twenty'}) # update |
| 214 | verify(s[10]=='ten' and s[20]=='twenty') |
| 215 | |
Raymond Hettinger | 8ddc176e | 2002-11-18 04:34:10 +0000 | [diff] [blame] | 216 | verify(s == {10: 'ten', 20:'twenty'}) # cmp |
| 217 | t = SeqDict() |
| 218 | t[20] = 'twenty' |
| 219 | t[10] = 'ten' |
| 220 | verify(s == t) |