Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 1 | # Check every path through every method of UserDict |
| 2 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 3 | from 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 | |
| 22 | # Test __repr__ |
| 23 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 24 | verify(str(u0) == str(d0)) |
| 25 | verify(repr(u1) == repr(d1)) |
| 26 | verify(`u2` == `d2`) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 27 | |
| 28 | # Test __cmp__ and __len__ |
| 29 | |
| 30 | all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2] |
| 31 | for a in all: |
| 32 | for b in all: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 33 | verify(cmp(a, b) == cmp(len(a), len(b))) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 34 | |
| 35 | # Test __getitem__ |
| 36 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 37 | verify(u2["one"] == 1) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 38 | try: |
| 39 | u1["two"] |
| 40 | except KeyError: |
| 41 | pass |
| 42 | else: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 43 | verify(0, "u1['two'] shouldn't exist") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 44 | |
| 45 | # Test __setitem__ |
| 46 | |
| 47 | u3 = UserDict(u2) |
| 48 | u3["two"] = 2 |
| 49 | u3["three"] = 3 |
| 50 | |
| 51 | # Test __delitem__ |
| 52 | |
| 53 | del u3["three"] |
| 54 | try: |
| 55 | del u3["three"] |
| 56 | except KeyError: |
| 57 | pass |
| 58 | else: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 59 | verify(0, "u3['three'] shouldn't exist") |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 60 | |
| 61 | # Test clear |
| 62 | |
| 63 | u3.clear() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 64 | verify(u3 == {}) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 65 | |
| 66 | # Test copy() |
| 67 | |
| 68 | u2a = u2.copy() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 69 | verify(u2a == u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 70 | |
| 71 | class MyUserDict(UserDict): |
| 72 | def display(self): print self |
| 73 | |
| 74 | m2 = MyUserDict(u2) |
| 75 | m2a = m2.copy() |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 76 | verify(m2a == m2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 77 | |
Fred Drake | 5262867 | 2001-11-05 17:41:48 +0000 | [diff] [blame] | 78 | # SF bug #476616 -- copy() of UserDict subclass shared data |
| 79 | m2['foo'] = 'bar' |
| 80 | verify(m2a != m2) |
| 81 | |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 82 | # Test keys, items, values |
| 83 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 84 | verify(u2.keys() == d2.keys()) |
| 85 | verify(u2.items() == d2.items()) |
| 86 | verify(u2.values() == d2.values()) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 87 | |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 88 | # Test has_key and "in". |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 89 | |
| 90 | for i in u2.keys(): |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 91 | verify(u2.has_key(i) == 1) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 92 | verify((i in u2) == 1) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 93 | verify(u1.has_key(i) == d1.has_key(i)) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 94 | verify((i in u1) == (i in d1)) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 95 | verify(u0.has_key(i) == d0.has_key(i)) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 96 | verify((i in u0) == (i in d0)) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 97 | |
| 98 | # Test update |
| 99 | |
| 100 | t = UserDict() |
| 101 | t.update(u2) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 102 | verify(t == u2) |
Guido van Rossum | 3eccc48 | 1999-03-26 15:32:05 +0000 | [diff] [blame] | 103 | |
| 104 | # Test get |
| 105 | |
| 106 | for i in u2.keys(): |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 107 | verify(u2.get(i) == u2[i]) |
| 108 | verify(u1.get(i) == d1.get(i)) |
| 109 | verify(u0.get(i) == d0.get(i)) |
Tim Peters | a3f98d6 | 2001-04-21 09:13:15 +0000 | [diff] [blame] | 110 | |
| 111 | # Test "in" iteration. |
| 112 | for i in xrange(20): |
| 113 | u2[i] = str(i) |
| 114 | ikeys = [] |
| 115 | for k in u2: |
| 116 | ikeys.append(k) |
| 117 | ikeys.sort() |
| 118 | keys = u2.keys() |
| 119 | keys.sort() |
| 120 | verify(ikeys == keys) |