Mercurial > cpython-lru_cache-weakref
annotate Lib/test/mapping_tests.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 4a31f6b11e7a |
| children |
| rev | line source |
|---|---|
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
1 # tests common to dict and UserDict |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
2 import unittest |
|
45185
8ef3c7c00470
Migrate remaining tests from UserDict.UserDict to collections.UserDict.
Raymond Hettinger <python@rcn.com>
parents:
41501
diff
changeset
|
3 import collections |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
4 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
5 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
6 class BasicTestMappingProtocol(unittest.TestCase): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
7 # This base class can be used to check that an object conforms to the |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
8 # mapping protocol |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
9 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
10 # Functions that can be useful to override to adapt to dictionary |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
11 # semantics |
|
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
|
12 type2test = None # which class is being tested (overwrite in subclasses) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
13 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
14 def _reference(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
15 """Return a dictionary of values which are invariant by storage |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
16 in the object under test.""" |
|
75066
f4b7ecf8a5f8
Issue #13703: add a way to randomize the hash values of basic types (str, bytes, datetime)
Georg Brandl <georg@python.org>
parents:
55593
diff
changeset
|
17 return {"1": "2", "key1":"value1", "key2":(1,2,3)} |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
18 def _empty_mapping(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
19 """Return an empty mapping object""" |
|
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
|
20 return self.type2test() |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
21 def _full_mapping(self, data): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
22 """Return a mapping object with the value contained in data |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
23 dictionary""" |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
24 x = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
25 for key, value in data.items(): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
26 x[key] = value |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
27 return x |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
28 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
29 def __init__(self, *args, **kw): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
30 unittest.TestCase.__init__(self, *args, **kw) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
31 self.reference = self._reference().copy() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
32 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
33 # A (key, value) pair not in the mapping |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
34 key, value = self.reference.popitem() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
35 self.other = {key:value} |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
36 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
37 # A (key, value) pair in the mapping |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
38 key, value = self.reference.popitem() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
39 self.inmapping = {key:value} |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
40 self.reference[key] = value |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
41 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
42 def test_read(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
43 # Test for read only operations on mapping |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
44 p = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
45 p1 = dict(p) #workaround for singleton objects |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
46 d = self._full_mapping(self.reference) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
47 if d is p: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
48 p = p1 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
49 #Indexing |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
50 for key, value in self.reference.items(): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
51 self.assertEqual(d[key], value) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
52 knownkey = list(self.other.keys())[0] |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
53 self.assertRaises(KeyError, lambda:d[knownkey]) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
54 #len |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
55 self.assertEqual(len(p), 0) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
56 self.assertEqual(len(d), len(self.reference)) |
|
39575
63237fcefd99
Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents:
32204
diff
changeset
|
57 #__contains__ |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
58 for k in self.reference: |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
59 self.assertIn(k, d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
60 for k in self.other: |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
61 self.assertNotIn(k, d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
62 #cmp |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
63 self.assertEqual(p, p) |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
64 self.assertEqual(d, d) |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
65 self.assertNotEqual(p, d) |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
66 self.assertNotEqual(d, p) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
67 #__non__zero__ |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
68 if p: self.fail("Empty mapping must compare to False") |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
69 if not d: self.fail("Full mapping must compare to True") |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
70 # keys(), items(), iterkeys() ... |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
71 def check_iterandlist(iter, lst, ref): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
72 self.assertTrue(hasattr(iter, '__next__')) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
73 self.assertTrue(hasattr(iter, '__iter__')) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
74 x = list(iter) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
75 self.assertTrue(set(x)==set(lst)==set(ref)) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
76 check_iterandlist(iter(d.keys()), list(d.keys()), |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
77 self.reference.keys()) |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
78 check_iterandlist(iter(d), list(d.keys()), self.reference.keys()) |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
79 check_iterandlist(iter(d.values()), list(d.values()), |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
80 self.reference.values()) |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
81 check_iterandlist(iter(d.items()), list(d.items()), |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
82 self.reference.items()) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
83 #get |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
84 key, value = next(iter(d.items())) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
85 knownkey, knownvalue = next(iter(self.other.items())) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
86 self.assertEqual(d.get(key, knownvalue), value) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
87 self.assertEqual(d.get(knownkey, knownvalue), knownvalue) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
88 self.assertNotIn(knownkey, d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
89 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
90 def test_write(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
91 # Test for write operations on mapping |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
92 p = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
93 #Indexing |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
94 for key, value in self.reference.items(): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
95 p[key] = value |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
96 self.assertEqual(p[key], value) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
97 for key in self.reference.keys(): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
98 del p[key] |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
99 self.assertRaises(KeyError, lambda:p[key]) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
100 p = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
101 #update |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
102 p.update(self.reference) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
103 self.assertEqual(dict(p), self.reference) |
|
40880
55deb1da4cb8
Fix test_os from breakage due to dict views.
Brett Cannon <bcannon@gmail.com>
parents:
40848
diff
changeset
|
104 items = list(p.items()) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
105 p = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
106 p.update(items) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
107 self.assertEqual(dict(p), self.reference) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
108 d = self._full_mapping(self.reference) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
109 #setdefault |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
110 key, value = next(iter(d.items())) |
|
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
111 knownkey, knownvalue = next(iter(self.other.items())) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
112 self.assertEqual(d.setdefault(key, knownvalue), value) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
113 self.assertEqual(d[key], value) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
114 self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
115 self.assertEqual(d[knownkey], knownvalue) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
116 #pop |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
117 self.assertEqual(d.pop(knownkey), knownvalue) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
118 self.assertNotIn(knownkey, d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
119 self.assertRaises(KeyError, d.pop, knownkey) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
120 default = 909 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
121 d[knownkey] = knownvalue |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
122 self.assertEqual(d.pop(knownkey, default), knownvalue) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
123 self.assertNotIn(knownkey, d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
124 self.assertEqual(d.pop(knownkey, default), default) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
125 #popitem |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
126 key, value = d.popitem() |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
127 self.assertNotIn(key, d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
128 self.assertEqual(value, self.reference[key]) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
129 p=self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
130 self.assertRaises(KeyError, p.popitem) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
131 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
132 def test_constructor(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
133 self.assertEqual(self._empty_mapping(), self._empty_mapping()) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
134 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
135 def test_bool(self): |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
136 self.assertTrue(not self._empty_mapping()) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
137 self.assertTrue(self.reference) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
138 self.assertTrue(bool(self._empty_mapping()) is False) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
139 self.assertTrue(bool(self.reference) is True) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
140 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
141 def test_keys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
142 d = self._empty_mapping() |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
143 self.assertEqual(list(d.keys()), []) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
144 d = self.reference |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
145 self.assertIn(list(self.inmapping.keys())[0], d.keys()) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
146 self.assertNotIn(list(self.other.keys())[0], d.keys()) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
147 self.assertRaises(TypeError, d.keys, None) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
148 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
149 def test_values(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
150 d = self._empty_mapping() |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
151 self.assertEqual(list(d.values()), []) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
152 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
153 self.assertRaises(TypeError, d.values, None) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
154 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
155 def test_items(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
156 d = self._empty_mapping() |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
157 self.assertEqual(list(d.items()), []) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
158 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
159 self.assertRaises(TypeError, d.items, None) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
160 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
161 def test_len(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
162 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
163 self.assertEqual(len(d), 0) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
164 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
165 def test_getitem(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
166 d = self.reference |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
167 self.assertEqual(d[list(self.inmapping.keys())[0]], |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
168 list(self.inmapping.values())[0]) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
169 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
170 self.assertRaises(TypeError, d.__getitem__) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
171 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
172 def test_update(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
173 # mapping argument |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
174 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
175 d.update(self.other) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
176 self.assertEqual(list(d.items()), list(self.other.items())) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
177 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
178 # No argument |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
179 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
180 d.update() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
181 self.assertEqual(d, self._empty_mapping()) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
182 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
183 # item sequence |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
184 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
185 d.update(self.other.items()) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
186 self.assertEqual(list(d.items()), list(self.other.items())) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
187 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
188 # Iterator |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
189 d = self._empty_mapping() |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
190 d.update(self.other.items()) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
191 self.assertEqual(list(d.items()), list(self.other.items())) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
192 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
193 # FIXME: Doesn't work with UserDict |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
194 # self.assertRaises((TypeError, AttributeError), d.update, None) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
195 self.assertRaises((TypeError, AttributeError), d.update, 42) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
196 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
197 outerself = self |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
198 class SimpleUserDict: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
199 def __init__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
200 self.d = outerself.reference |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
201 def keys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
202 return self.d.keys() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
203 def __getitem__(self, i): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
204 return self.d[i] |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
205 d.clear() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
206 d.update(SimpleUserDict()) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
207 i1 = sorted(d.items()) |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
208 i2 = sorted(self.reference.items()) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
209 self.assertEqual(i1, i2) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
210 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
211 class Exc(Exception): pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
212 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
213 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
214 class FailingUserDict: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
215 def keys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
216 raise Exc |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
217 self.assertRaises(Exc, d.update, FailingUserDict()) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
218 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
219 d.clear() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
220 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
221 class FailingUserDict: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
222 def keys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
223 class BogonIter: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
224 def __init__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
225 self.i = 1 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
226 def __iter__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
227 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
228 def __next__(self): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
229 if self.i: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
230 self.i = 0 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
231 return 'a' |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
232 raise Exc |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
233 return BogonIter() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
234 def __getitem__(self, key): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
235 return key |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
236 self.assertRaises(Exc, d.update, FailingUserDict()) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
237 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
238 class FailingUserDict: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
239 def keys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
240 class BogonIter: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
241 def __init__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
242 self.i = ord('a') |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
243 def __iter__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
244 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
245 def __next__(self): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
246 if self.i <= ord('z'): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
247 rtn = chr(self.i) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
248 self.i += 1 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
249 return rtn |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
250 raise StopIteration |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
251 return BogonIter() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
252 def __getitem__(self, key): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
253 raise Exc |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
254 self.assertRaises(Exc, d.update, FailingUserDict()) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
255 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
256 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
257 class badseq(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
258 def __iter__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
259 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
260 def __next__(self): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
261 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
262 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
263 self.assertRaises(Exc, d.update, badseq()) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
264 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
265 self.assertRaises(ValueError, d.update, [(1, 2, 3)]) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
266 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
267 # no test_fromkeys or test_copy as both os.environ and selves don't support it |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
268 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
269 def test_get(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
270 d = self._empty_mapping() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
271 self.assertTrue(d.get(list(self.other.keys())[0]) is None) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
272 self.assertEqual(d.get(list(self.other.keys())[0], 3), 3) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
273 d = self.reference |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
274 self.assertTrue(d.get(list(self.other.keys())[0]) is None) |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
275 self.assertEqual(d.get(list(self.other.keys())[0], 3), 3) |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
276 self.assertEqual(d.get(list(self.inmapping.keys())[0]), |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
277 list(self.inmapping.values())[0]) |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
278 self.assertEqual(d.get(list(self.inmapping.keys())[0], 3), |
|
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
279 list(self.inmapping.values())[0]) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
280 self.assertRaises(TypeError, d.get) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
281 self.assertRaises(TypeError, d.get, None, None, None) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
282 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
283 def test_setdefault(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
284 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
285 self.assertRaises(TypeError, d.setdefault) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
286 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
287 def test_popitem(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
288 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
289 self.assertRaises(KeyError, d.popitem) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
290 self.assertRaises(TypeError, d.popitem, 42) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
291 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
292 def test_pop(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
293 d = self._empty_mapping() |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
294 k, v = list(self.inmapping.items())[0] |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
295 d[k] = v |
|
40818
a84460e53160
Nailed test_weakref.py. Pfew, messy!
Guido van Rossum <guido@python.org>
parents:
40809
diff
changeset
|
296 self.assertRaises(KeyError, d.pop, list(self.other.keys())[0]) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
297 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
298 self.assertEqual(d.pop(k), v) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
299 self.assertEqual(len(d), 0) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
300 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
301 self.assertRaises(KeyError, d.pop, k) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
302 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
303 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
304 class TestMappingProtocol(BasicTestMappingProtocol): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
305 def test_constructor(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
306 BasicTestMappingProtocol.test_constructor(self) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
307 self.assertTrue(self._empty_mapping() is not self._empty_mapping()) |
|
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
|
308 self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2}) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
309 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
310 def test_bool(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
311 BasicTestMappingProtocol.test_bool(self) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
312 self.assertTrue(not self._empty_mapping()) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
313 self.assertTrue(self._full_mapping({"x": "y"})) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
314 self.assertTrue(bool(self._empty_mapping()) is False) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
315 self.assertTrue(bool(self._full_mapping({"x": "y"})) is True) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
316 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
317 def test_keys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
318 BasicTestMappingProtocol.test_keys(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
319 d = self._empty_mapping() |
|
40848
502de079ab3a
Fix the damage to UserDict and its tests.
Guido van Rossum <guido@python.org>
parents:
40818
diff
changeset
|
320 self.assertEqual(list(d.keys()), []) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
321 d = self._full_mapping({'a': 1, 'b': 2}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
322 k = d.keys() |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
323 self.assertIn('a', k) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
324 self.assertIn('b', k) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
325 self.assertNotIn('c', k) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
326 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
327 def test_values(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
328 BasicTestMappingProtocol.test_values(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
329 d = self._full_mapping({1:2}) |
|
40848
502de079ab3a
Fix the damage to UserDict and its tests.
Guido van Rossum <guido@python.org>
parents:
40818
diff
changeset
|
330 self.assertEqual(list(d.values()), [2]) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
331 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
332 def test_items(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
333 BasicTestMappingProtocol.test_items(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
334 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
335 d = self._full_mapping({1:2}) |
|
40848
502de079ab3a
Fix the damage to UserDict and its tests.
Guido van Rossum <guido@python.org>
parents:
40818
diff
changeset
|
336 self.assertEqual(list(d.items()), [(1, 2)]) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
337 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
338 def test_contains(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
339 d = self._empty_mapping() |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
340 self.assertNotIn('a', d) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
341 self.assertTrue(not ('a' in d)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
342 self.assertTrue('a' not in d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
343 d = self._full_mapping({'a': 1, 'b': 2}) |
|
58456
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
344 self.assertIn('a', d) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
345 self.assertIn('b', d) |
|
f0f221e1a7c0
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57582
diff
changeset
|
346 self.assertNotIn('c', d) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
347 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
348 self.assertRaises(TypeError, d.__contains__) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
349 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
350 def test_len(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
351 BasicTestMappingProtocol.test_len(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
352 d = self._full_mapping({'a': 1, 'b': 2}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
353 self.assertEqual(len(d), 2) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
354 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
355 def test_getitem(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
356 BasicTestMappingProtocol.test_getitem(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
357 d = self._full_mapping({'a': 1, 'b': 2}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
358 self.assertEqual(d['a'], 1) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
359 self.assertEqual(d['b'], 2) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
360 d['c'] = 3 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
361 d['a'] = 4 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
362 self.assertEqual(d['c'], 3) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
363 self.assertEqual(d['a'], 4) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
364 del d['b'] |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
365 self.assertEqual(d, {'a': 4, 'c': 3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
366 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
367 self.assertRaises(TypeError, d.__getitem__) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
368 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
369 def test_clear(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
370 d = self._full_mapping({1:1, 2:2, 3:3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
371 d.clear() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
372 self.assertEqual(d, {}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
373 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
374 self.assertRaises(TypeError, d.clear, None) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
375 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
376 def test_update(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
377 BasicTestMappingProtocol.test_update(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
378 # mapping argument |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
379 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
380 d.update({1:100}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
381 d.update({2:20}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
382 d.update({1:1, 2:2, 3:3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
383 self.assertEqual(d, {1:1, 2:2, 3:3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
384 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
385 # no argument |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
386 d.update() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
387 self.assertEqual(d, {1:1, 2:2, 3:3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
388 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
389 # keyword arguments |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
390 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
391 d.update(x=100) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
392 d.update(y=20) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
393 d.update(x=1, y=2, z=3) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
394 self.assertEqual(d, {"x":1, "y":2, "z":3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
395 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
396 # item sequence |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
397 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
398 d.update([("x", 100), ("y", 20)]) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
399 self.assertEqual(d, {"x":100, "y":20}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
400 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
401 # Both item sequence and keyword arguments |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
402 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
403 d.update([("x", 100), ("y", 20)], x=1, y=2) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
404 self.assertEqual(d, {"x":1, "y":2}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
405 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
406 # iterator |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
407 d = self._full_mapping({1:3, 2:4}) |
|
40809
f340cb045bf9
- PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
408 d.update(self._full_mapping({1:2, 3:4, 5:6}).items()) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
409 self.assertEqual(d, {1:2, 2:4, 3:4, 5:6}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
410 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
411 class SimpleUserDict: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
412 def __init__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
413 self.d = {1:1, 2:2, 3:3} |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
414 def keys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
415 return self.d.keys() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
416 def __getitem__(self, i): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
417 return self.d[i] |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
418 d.clear() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
419 d.update(SimpleUserDict()) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
420 self.assertEqual(d, {1:1, 2:2, 3:3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
421 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
422 def test_fromkeys(self): |
|
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
|
423 self.assertEqual(self.type2test.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
424 d = self._empty_mapping() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
425 self.assertTrue(not(d.fromkeys('abc') is d)) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
426 self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
427 self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
428 self.assertEqual(d.fromkeys([]), {}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
429 def g(): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
430 yield 1 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
431 self.assertEqual(d.fromkeys(g()), {1:None}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
432 self.assertRaises(TypeError, {}.fromkeys, 3) |
|
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
|
433 class dictlike(self.type2test): pass |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
434 self.assertEqual(dictlike.fromkeys('a'), {'a':None}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
435 self.assertEqual(dictlike().fromkeys('a'), {'a':None}) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
436 self.assertTrue(dictlike.fromkeys('a').__class__ is dictlike) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
437 self.assertTrue(dictlike().fromkeys('a').__class__ is dictlike) |
|
58473
71acd4482e65
Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58456
diff
changeset
|
438 self.assertTrue(type(dictlike.fromkeys('a')) is dictlike) |
|
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
|
439 class mydict(self.type2test): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
440 def __new__(cls): |
|
45185
8ef3c7c00470
Migrate remaining tests from UserDict.UserDict to collections.UserDict.
Raymond Hettinger <python@rcn.com>
parents:
41501
diff
changeset
|
441 return collections.UserDict() |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
442 ud = mydict.fromkeys('ab') |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
443 self.assertEqual(ud, {'a':None, 'b':None}) |
|
58473
71acd4482e65
Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58456
diff
changeset
|
444 self.assertIsInstance(ud, collections.UserDict) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
445 self.assertRaises(TypeError, dict.fromkeys) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
446 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
447 class Exc(Exception): pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
448 |
|
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
|
449 class baddict1(self.type2test): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
450 def __init__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
451 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
452 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
453 self.assertRaises(Exc, baddict1.fromkeys, [1]) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
454 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
455 class BadSeq(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
456 def __iter__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
457 return self |
|
41501
0c5019afc55b
PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents:
40880
diff
changeset
|
458 def __next__(self): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
459 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
460 |
|
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
|
461 self.assertRaises(Exc, self.type2test.fromkeys, BadSeq()) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
462 |
|
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
|
463 class baddict2(self.type2test): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
464 def __setitem__(self, key, value): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
465 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
466 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
467 self.assertRaises(Exc, baddict2.fromkeys, [1]) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
468 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
469 def test_copy(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
470 d = self._full_mapping({1:1, 2:2, 3:3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
471 self.assertEqual(d.copy(), {1:1, 2:2, 3:3}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
472 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
473 self.assertEqual(d.copy(), d) |
|
58473
71acd4482e65
Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58456
diff
changeset
|
474 self.assertIsInstance(d.copy(), d.__class__) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
475 self.assertRaises(TypeError, d.copy, None) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
476 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
477 def test_get(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
478 BasicTestMappingProtocol.test_get(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
479 d = self._empty_mapping() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
480 self.assertTrue(d.get('c') is None) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
481 self.assertEqual(d.get('c', 3), 3) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
482 d = self._full_mapping({'a' : 1, 'b' : 2}) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
483 self.assertTrue(d.get('c') is None) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
484 self.assertEqual(d.get('c', 3), 3) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
485 self.assertEqual(d.get('a'), 1) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
486 self.assertEqual(d.get('a', 3), 1) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
487 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
488 def test_setdefault(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
489 BasicTestMappingProtocol.test_setdefault(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
490 d = self._empty_mapping() |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
491 self.assertTrue(d.setdefault('key0') is None) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
492 d.setdefault('key0', []) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
493 self.assertTrue(d.setdefault('key0') is None) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
494 d.setdefault('key', []).append(3) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
495 self.assertEqual(d['key'][0], 3) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
496 d.setdefault('key', []).append(4) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
497 self.assertEqual(len(d['key']), 2) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
498 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
499 def test_popitem(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
500 BasicTestMappingProtocol.test_popitem(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
501 for copymode in -1, +1: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
502 # -1: b has same structure as a |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
503 # +1: b is a.copy() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
504 for log2size in range(12): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
505 size = 2**log2size |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
506 a = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
507 b = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
508 for i in range(size): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
509 a[repr(i)] = i |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
510 if copymode < 0: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
511 b[repr(i)] = i |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
512 if copymode > 0: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
513 b = a.copy() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
514 for i in range(size): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
515 ka, va = ta = a.popitem() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
516 self.assertEqual(va, int(ka)) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
517 kb, vb = tb = b.popitem() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
518 self.assertEqual(vb, int(kb)) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
519 self.assertTrue(not(copymode < 0 and ta != tb)) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
520 self.assertTrue(not a) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
45185
diff
changeset
|
521 self.assertTrue(not b) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
522 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
523 def test_pop(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
524 BasicTestMappingProtocol.test_pop(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
525 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
526 # Tests for pop with specified key |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
527 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
528 k, v = 'abc', 'def' |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
529 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
530 self.assertEqual(d.pop(k, v), v) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
531 d[k] = v |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
532 self.assertEqual(d.pop(k, 1), v) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
533 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
534 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
535 class TestHashMappingProtocol(TestMappingProtocol): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
536 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
537 def test_getitem(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
538 TestMappingProtocol.test_getitem(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
539 class Exc(Exception): pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
540 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
541 class BadEq(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
542 def __eq__(self, other): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
543 raise Exc() |
|
39623
c6d9fa81f20f
Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
544 def __hash__(self): |
|
c6d9fa81f20f
Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
545 return 24 |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
546 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
547 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
548 d[BadEq()] = 42 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
549 self.assertRaises(KeyError, d.__getitem__, 23) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
550 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
551 class BadHash(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
552 fail = False |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
553 def __hash__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
554 if self.fail: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
555 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
556 else: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
557 return 42 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
558 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
559 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
560 x = BadHash() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
561 d[x] = 42 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
562 x.fail = True |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
563 self.assertRaises(Exc, d.__getitem__, x) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
564 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
565 def test_fromkeys(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
566 TestMappingProtocol.test_fromkeys(self) |
|
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
|
567 class mydict(self.type2test): |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
568 def __new__(cls): |
|
45185
8ef3c7c00470
Migrate remaining tests from UserDict.UserDict to collections.UserDict.
Raymond Hettinger <python@rcn.com>
parents:
41501
diff
changeset
|
569 return collections.UserDict() |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
570 ud = mydict.fromkeys('ab') |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
571 self.assertEqual(ud, {'a':None, 'b':None}) |
|
58473
71acd4482e65
Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58456
diff
changeset
|
572 self.assertIsInstance(ud, collections.UserDict) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
573 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
574 def test_pop(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
575 TestMappingProtocol.test_pop(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
576 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
577 class Exc(Exception): pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
578 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
579 class BadHash(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
580 fail = False |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
581 def __hash__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
582 if self.fail: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
583 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
584 else: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
585 return 42 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
586 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
587 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
588 x = BadHash() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
589 d[x] = 42 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
590 x.fail = True |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
591 self.assertRaises(Exc, d.pop, x) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
592 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
593 def test_mutatingiteration(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
594 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
595 d[1] = 1 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
596 try: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
597 for i in d: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
598 d[i+1] = 1 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
599 except RuntimeError: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
600 pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
601 else: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
602 self.fail("changing dict size during iteration doesn't raise Error") |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
603 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
604 def test_repr(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
605 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
606 self.assertEqual(repr(d), '{}') |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
607 d[1] = 2 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
608 self.assertEqual(repr(d), '{1: 2}') |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
609 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
610 d[1] = d |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
611 self.assertEqual(repr(d), '{1: {...}}') |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
612 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
613 class Exc(Exception): pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
614 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
615 class BadRepr(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
616 def __repr__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
617 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
618 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
619 d = self._full_mapping({1: BadRepr()}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
620 self.assertRaises(Exc, repr, d) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
621 |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
622 def test_eq(self): |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
623 self.assertEqual(self._empty_mapping(), self._empty_mapping()) |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
624 self.assertEqual(self._full_mapping({1: 2}), |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
625 self._full_mapping({1: 2})) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
626 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
627 class Exc(Exception): pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
628 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
629 class BadCmp(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
630 def __eq__(self, other): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
631 raise Exc() |
|
39623
c6d9fa81f20f
Change the way __hash__ is inherited; when __eq__ or __cmp__ is overridden
Guido van Rossum <guido@python.org>
parents:
39575
diff
changeset
|
632 def __hash__(self): |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
633 return 1 |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
634 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
635 d1 = self._full_mapping({BadCmp(): 1}) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
636 d2 = self._full_mapping({1: 1}) |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
637 self.assertRaises(Exc, lambda: BadCmp()==1) |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39623
diff
changeset
|
638 self.assertRaises(Exc, lambda: d1==d2) |
|
32163
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
639 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
640 def test_setdefault(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
641 TestMappingProtocol.test_setdefault(self) |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
642 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
643 class Exc(Exception): pass |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
644 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
645 class BadHash(object): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
646 fail = False |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
647 def __hash__(self): |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
648 if self.fail: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
649 raise Exc() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
650 else: |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
651 return 42 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
652 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
653 d = self._empty_mapping() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
654 x = BadHash() |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
655 d[x] = 42 |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
656 x.fail = True |
|
fad52ffd8892
Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff
changeset
|
657 self.assertRaises(Exc, d.setdefault, x, []) |
