annotate Lib/test/test_userdict.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 1d84bf5f9478
children 51102abdec50 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Check every path through every method of UserDict
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
3 from test import test_support, mapping_tests
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
4 import UserDict
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 d0 = {}
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 d1 = {"one": 1}
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 d2 = {"one": 1, "two": 2}
26438
d44945b81ffa Bring UserDict in-sync with changes to dict.
Raymond Hettinger <python@rcn.com>
parents: 26332
diff changeset
9 d3 = {"one": 1, "two": 3, "three": 5}
d44945b81ffa Bring UserDict in-sync with changes to dict.
Raymond Hettinger <python@rcn.com>
parents: 26332
diff changeset
10 d4 = {"one": None, "two": None}
d44945b81ffa Bring UserDict in-sync with changes to dict.
Raymond Hettinger <python@rcn.com>
parents: 26332
diff changeset
11 d5 = {"one": 1, "two": 1}
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
13 class UserDictTest(mapping_tests.TestHashMappingProtocol):
32204
9a3901bf6222 Rename class attribute containing the class to be tested, so the name is the
Walter Dörwald <walter@livinglogic.de>
parents: 32163
diff changeset
14 type2test = UserDict.IterableUserDict
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 28211
diff changeset
15
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
16 def test_all(self):
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
17 # Test constructors
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
18 u = UserDict.UserDict()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
19 u0 = UserDict.UserDict(d0)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
20 u1 = UserDict.UserDict(d1)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
21 u2 = UserDict.IterableUserDict(d2)
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
23 uu = UserDict.UserDict(u)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
24 uu0 = UserDict.UserDict(u0)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
25 uu1 = UserDict.UserDict(u1)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
26 uu2 = UserDict.UserDict(u2)
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
28 # keyword arg constructor
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
29 self.assertEqual(UserDict.UserDict(one=1, two=2), d2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
30 # item sequence constructor
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
31 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)]), d2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
32 self.assertEqual(UserDict.UserDict(dict=[('one',1), ('two',2)]), d2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
33 # both together
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
34 self.assertEqual(UserDict.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
26438
d44945b81ffa Bring UserDict in-sync with changes to dict.
Raymond Hettinger <python@rcn.com>
parents: 26332
diff changeset
35
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
36 # alternate constructor
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
37 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split()), d4)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
38 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split()), d4)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
39 self.assertEqual(UserDict.UserDict.fromkeys('one two'.split(), 1), d5)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
40 self.assertEqual(UserDict.UserDict().fromkeys('one two'.split(), 1), d5)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
41 self.assert_(u1.fromkeys('one two'.split()) is not u1)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
42 self.assert_(isinstance(u1.fromkeys('one two'.split()), UserDict.UserDict))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
43 self.assert_(isinstance(u2.fromkeys('one two'.split()), UserDict.IterableUserDict))
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
45 # Test __repr__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
46 self.assertEqual(str(u0), str(d0))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
47 self.assertEqual(repr(u1), repr(d1))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
48 self.assertEqual(`u2`, `d2`)
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
50 # Test __cmp__ and __len__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
51 all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
52 for a in all:
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
53 for b in all:
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
54 self.assertEqual(cmp(a, b), cmp(len(a), len(b)))
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
56 # Test __getitem__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
57 self.assertEqual(u2["one"], 1)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
58 self.assertRaises(KeyError, u1.__getitem__, "two")
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
60 # Test __setitem__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
61 u3 = UserDict.UserDict(u2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
62 u3["two"] = 2
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
63 u3["three"] = 3
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
64
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
65 # Test __delitem__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
66 del u3["three"]
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
67 self.assertRaises(KeyError, u3.__delitem__, "three")
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
69 # Test clear
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
70 u3.clear()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
71 self.assertEqual(u3, {})
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
73 # Test copy()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
74 u2a = u2.copy()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
75 self.assertEqual(u2a, u2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
76 u2b = UserDict.UserDict(x=42, y=23)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
77 u2c = u2b.copy() # making a copy of a UserDict is special cased
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
78 self.assertEqual(u2b, u2c)
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
80 class MyUserDict(UserDict.UserDict):
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
81 def display(self): print self
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
83 m2 = MyUserDict(u2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
84 m2a = m2.copy()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
85 self.assertEqual(m2a, m2)
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
87 # SF bug #476616 -- copy() of UserDict subclass shared data
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
88 m2['foo'] = 'bar'
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
89 self.assertNotEqual(m2a, m2)
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
91 # Test keys, items, values
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
92 self.assertEqual(u2.keys(), d2.keys())
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
93 self.assertEqual(u2.items(), d2.items())
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
94 self.assertEqual(u2.values(), d2.values())
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
95
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
96 # Test has_key and "in".
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
97 for i in u2.keys():
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
98 self.assert_(u2.has_key(i))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
99 self.assert_(i in u2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
100 self.assertEqual(u1.has_key(i), d1.has_key(i))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
101 self.assertEqual(i in u1, i in d1)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
102 self.assertEqual(u0.has_key(i), d0.has_key(i))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
103 self.assertEqual(i in u0, i in d0)
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
105 # Test update
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
106 t = UserDict.UserDict()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
107 t.update(u2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
108 self.assertEqual(t, u2)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
109 class Items:
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
110 def items(self):
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
111 return (("x", 42), ("y", 23))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
112 t = UserDict.UserDict()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
113 t.update(Items())
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
114 self.assertEqual(t, {"x": 42, "y": 23})
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
116 # Test get
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
117 for i in u2.keys():
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
118 self.assertEqual(u2.get(i), u2[i])
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
119 self.assertEqual(u1.get(i), d1.get(i))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
120 self.assertEqual(u0.get(i), d0.get(i))
10478
01250b832374 Test suite for UserDict
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
122 # Test "in" iteration.
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
123 for i in xrange(20):
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
124 u2[i] = str(i)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
125 ikeys = []
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
126 for k in u2:
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
127 ikeys.append(k)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
128 keys = u2.keys()
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 28922
diff changeset
129 self.assertEqual(set(ikeys), set(keys))
17608
22af15f8410a Give UserDict new __contains__ and __iter__ methods.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
130
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
131 # Test setdefault
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
132 t = UserDict.UserDict()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
133 self.assertEqual(t.setdefault("x", 42), 42)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
134 self.assert_(t.has_key("x"))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
135 self.assertEqual(t.setdefault("x", 23), 42)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
136
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
137 # Test pop
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
138 t = UserDict.UserDict(x=42)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
139 self.assertEqual(t.pop("x"), 42)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
140 self.assertRaises(KeyError, t.pop, "x")
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27307
diff changeset
141 self.assertEqual(t.pop("x", 1), 1)
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27307
diff changeset
142 t["x"] = 42
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27307
diff changeset
143 self.assertEqual(t.pop("x", 1), 42)
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
144
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
145 # Test popitem
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
146 t = UserDict.UserDict(x=42)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
147 self.assertEqual(t.popitem(), ("x", 42))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
148 self.assertRaises(KeyError, t.popitem)
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
149
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
150 def test_missing(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
151 # Make sure UserDict doesn't have a __missing__ method
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
152 self.assertEqual(hasattr(UserDict, "__missing__"), False)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
153 # Test several cases:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
154 # (D) subclass defines __missing__ method returning a value
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
155 # (E) subclass defines __missing__ method raising RuntimeError
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
156 # (F) subclass sets __missing__ instance variable (no effect)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
157 # (G) subclass doesn't define __missing__ at a all
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
158 class D(UserDict.UserDict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
159 def __missing__(self, key):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
160 return 42
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
161 d = D({1: 2, 3: 4})
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
162 self.assertEqual(d[1], 2)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
163 self.assertEqual(d[3], 4)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
164 self.assert_(2 not in d)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
165 self.assert_(2 not in d.keys())
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
166 self.assertEqual(d[2], 42)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
167 class E(UserDict.UserDict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
168 def __missing__(self, key):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
169 raise RuntimeError(key)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
170 e = E()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
171 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
172 e[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
173 except RuntimeError, err:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
174 self.assertEqual(err.args, (42,))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
175 else:
41001
2f2b8935a81e Fix a bug in test_dict and test_userdict, found at the PyPy sprint.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
176 self.fail("e[42] didn't raise RuntimeError")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
177 class F(UserDict.UserDict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
178 def __init__(self):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
179 # An instance variable __missing__ should have no effect
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
180 self.__missing__ = lambda key: None
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
181 UserDict.UserDict.__init__(self)
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
182 f = F()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
183 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
184 f[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
185 except KeyError, err:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
186 self.assertEqual(err.args, (42,))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
187 else:
41001
2f2b8935a81e Fix a bug in test_dict and test_userdict, found at the PyPy sprint.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
188 self.fail("f[42] didn't raise KeyError")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
189 class G(UserDict.UserDict):
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
190 pass
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
191 g = G()
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
192 try:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
193 g[42]
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
194 except KeyError, err:
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
195 self.assertEqual(err.args, (42,))
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
196 else:
41001
2f2b8935a81e Fix a bug in test_dict and test_userdict, found at the PyPy sprint.
Georg Brandl <georg@python.org>
parents: 36656
diff changeset
197 self.fail("g[42] didn't raise KeyError")
36656
92ce8bb8b12a - Patch 1433928:
Guido van Rossum <guido@python.org>
parents: 34423
diff changeset
198
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
199 ##########################
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
200 # Test Dict Mixin
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
201
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
202 class SeqDict(UserDict.DictMixin):
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
203 """Dictionary lookalike implemented with lists.
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
204
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
205 Used to test and demonstrate DictMixin
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
206 """
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
207 def __init__(self, other=None, **kwargs):
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
208 self.keylist = []
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
209 self.valuelist = []
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
210 if other is not None:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
211 for (key, value) in other:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
212 self[key] = value
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
213 for (key, value) in kwargs.iteritems():
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
214 self[key] = value
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
215 def __getitem__(self, key):
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
216 try:
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
217 i = self.keylist.index(key)
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
218 except ValueError:
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
219 raise KeyError
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
220 return self.valuelist[i]
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
221 def __setitem__(self, key, value):
26593
211038505e56 Clean-up test class for DictMixin.
Raymond Hettinger <python@rcn.com>
parents: 26438
diff changeset
222 try:
211038505e56 Clean-up test class for DictMixin.
Raymond Hettinger <python@rcn.com>
parents: 26438
diff changeset
223 i = self.keylist.index(key)
211038505e56 Clean-up test class for DictMixin.
Raymond Hettinger <python@rcn.com>
parents: 26438
diff changeset
224 self.valuelist[i] = value
211038505e56 Clean-up test class for DictMixin.
Raymond Hettinger <python@rcn.com>
parents: 26438
diff changeset
225 except ValueError:
211038505e56 Clean-up test class for DictMixin.
Raymond Hettinger <python@rcn.com>
parents: 26438
diff changeset
226 self.keylist.append(key)
211038505e56 Clean-up test class for DictMixin.
Raymond Hettinger <python@rcn.com>
parents: 26438
diff changeset
227 self.valuelist.append(value)
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
228 def __delitem__(self, key):
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
229 try:
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
230 i = self.keylist.index(key)
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
231 except ValueError:
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
232 raise KeyError
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
233 self.keylist.pop(i)
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
234 self.valuelist.pop(i)
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
235 def keys(self):
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
236 return list(self.keylist)
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
237 def copy(self):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
238 d = self.__class__()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
239 for key, value in self.iteritems():
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
240 d[key] = value
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
241 return d
34423
5449b7f806fb Use descriptors.
Guido van Rossum <guido@python.org>
parents: 32204
diff changeset
242 @classmethod
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
243 def fromkeys(cls, keys, value=None):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
244 d = cls()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
245 for key in keys:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
246 d[key] = value
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
247 return d
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
248
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
249 class UserDictMixinTest(mapping_tests.TestMappingProtocol):
32204
9a3901bf6222 Rename class attribute containing the class to be tested, so the name is the
Walter Dörwald <walter@livinglogic.de>
parents: 32163
diff changeset
250 type2test = SeqDict
28240
ea70032a24b1 SF patch #667730: More DictMixin
Raymond Hettinger <python@rcn.com>
parents: 28211
diff changeset
251
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
252 def test_all(self):
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
253 ## Setup test and verify working of the test class
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
254
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
255 # check init
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
256 s = SeqDict()
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
257
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
258 # exercise setitem
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
259 s[10] = 'ten'
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
260 s[20] = 'twenty'
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
261 s[30] = 'thirty'
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
262
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
263 # exercise delitem
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
264 del s[20]
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
265 # check getitem and setitem
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
266 self.assertEqual(s[10], 'ten')
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
267 # check keys() and delitem
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
268 self.assertEqual(s.keys(), [10, 30])
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
269
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
270 ## Now, test the DictMixin methods one by one
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
271 # has_key
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
272 self.assert_(s.has_key(10))
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
273 self.assert_(not s.has_key(20))
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
274
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
275 # __contains__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
276 self.assert_(10 in s)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
277 self.assert_(20 not in s)
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
278
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
279 # __iter__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
280 self.assertEqual([k for k in s], [10, 30])
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
281
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
282 # __len__
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
283 self.assertEqual(len(s), 2)
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
284
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
285 # iteritems
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
286 self.assertEqual(list(s.iteritems()), [(10,'ten'), (30, 'thirty')])
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
287
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
288 # iterkeys
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
289 self.assertEqual(list(s.iterkeys()), [10, 30])
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
290
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
291 # itervalues
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
292 self.assertEqual(list(s.itervalues()), ['ten', 'thirty'])
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
293
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
294 # values
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
295 self.assertEqual(s.values(), ['ten', 'thirty'])
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
296
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
297 # items
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
298 self.assertEqual(s.items(), [(10,'ten'), (30, 'thirty')])
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
299
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
300 # get
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
301 self.assertEqual(s.get(10), 'ten')
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
302 self.assertEqual(s.get(15,'fifteen'), 'fifteen')
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
303 self.assertEqual(s.get(15), None)
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
304
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
305 # setdefault
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
306 self.assertEqual(s.setdefault(40, 'forty'), 'forty')
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
307 self.assertEqual(s.setdefault(10, 'null'), 'ten')
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
308 del s[40]
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
309
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
310 # pop
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
311 self.assertEqual(s.pop(10), 'ten')
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
312 self.assert_(10 not in s)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
313 s[10] = 'ten'
28211
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27307
diff changeset
314 self.assertEqual(s.pop("x", 1), 1)
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27307
diff changeset
315 s["x"] = 42
92e3b611dc9f SF patch #693753: fix for bug 639806: default for dict.pop
Raymond Hettinger <python@rcn.com>
parents: 27307
diff changeset
316 self.assertEqual(s.pop("x", 1), 42)
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
317
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
318 # popitem
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
319 k, v = s.popitem()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
320 self.assert_(k not in s)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
321 s[k] = v
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
322
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
323 # clear
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
324 s.clear()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
325 self.assertEqual(len(s), 0)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
326
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
327 # empty popitem
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
328 self.assertRaises(KeyError, s.popitem)
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
329
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
330 # update
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
331 s.update({10: 'ten', 20:'twenty'})
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
332 self.assertEqual(s[10], 'ten')
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
333 self.assertEqual(s[20], 'twenty')
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
334
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
335 # cmp
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
336 self.assertEqual(s, {10: 'ten', 20:'twenty'})
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
337 t = SeqDict()
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
338 t[20] = 'twenty'
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
339 t[10] = 'ten'
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
340 self.assertEqual(s, t)
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
341
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
342 def test_main():
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents: 31616
diff changeset
343 test_support.run_unittest(
28917
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
344 UserDictTest,
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
345 UserDictMixinTest
1741af48fd9f Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents: 28240
diff changeset
346 )
26241
a4e3021eaa7d SF patch #520382: Expand shelve.py to have a full dictionary interface
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
347
27307
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
348 if __name__ == "__main__":
dadd178b65fa Port test_userdict.py to PyUnit. From SF patch #662807,
Walter Dörwald <walter@livinglogic.de>
parents: 26593
diff changeset
349 test_main()