annotate Lib/test/mapping_tests.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents c8c840162804
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
3 import UserDict
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
4 import test_support
32163
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
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
7 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
8 # 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
9 # 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
10
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
11 # 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
12 # 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
13 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
14
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
15 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
16 """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
17 in the object under test."""
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
18 return {1:2, "key1":"value1", "key2":(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
19 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
20 """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
21 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
22 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
23 """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
24 dictionary"""
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
25 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
26 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
27 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
28 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
29
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
30 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
31 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
32 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
33
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
34 # 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
35 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
36 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
37
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
38 # 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
39 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
40 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
41 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
42
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
43 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
44 # 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
45 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
46 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
47 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
48 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
49 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
50 #Indexing
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
51 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
52 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
53 knownkey = self.other.keys()[0]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
54 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
55 #len
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(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
57 self.assertEqual(len(d), len(self.reference))
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
58 #in
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
59 for k in self.reference:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
60 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
61 for k in self.other:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
62 self.assertNotIn(k, d)
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
63 #has_key
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
64 with test_support.check_py3k_warnings(quiet=True):
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
65 for k in self.reference:
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
66 self.assertTrue(d.has_key(k))
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
67 for k in self.other:
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58472
diff changeset
68 self.assertFalse(d.has_key(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
69 #cmp
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
70 self.assertEqual(cmp(p,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
71 self.assertEqual(cmp(d,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
72 self.assertEqual(cmp(p,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
73 self.assertEqual(cmp(d,p), 1)
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
74 #__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
75 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
76 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
77 # 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
78 def check_iterandlist(iter, lst, ref):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
79 self.assertTrue(hasattr(iter, 'next'))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
80 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
81 x = list(iter)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
82 self.assertTrue(set(x)==set(lst)==set(ref))
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 check_iterandlist(d.iterkeys(), d.keys(), 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
84 check_iterandlist(iter(d), d.keys(), 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
85 check_iterandlist(d.itervalues(), d.values(), self.reference.values())
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
86 check_iterandlist(d.iteritems(), d.items(), 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
87 #get
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
88 key, value = d.iteritems().next()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
89 knownkey, knownvalue = self.other.iteritems().next()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
90 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
91 self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
92 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
93
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
94 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
95 # 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
96 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
97 #Indexing
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
98 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
99 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
100 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
101 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
102 del p[key]
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
103 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
104 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
105 #update
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(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
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 items = p.items()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
109 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
110 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
111 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
112 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
113 #setdefault
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
114 key, value = d.iteritems().next()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
115 knownkey, knownvalue = self.other.iteritems().next()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
116 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
117 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
118 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
119 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
120 #pop
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
121 self.assertEqual(d.pop(knownkey), knownvalue)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
122 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
123 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
124 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
125 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
126 self.assertEqual(d.pop(knownkey, default), knownvalue)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
127 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
128 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
129 #popitem
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
130 key, value = d.popitem()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
131 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
132 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
133 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
134 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
135
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
136 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
137 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
138
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
139 def test_bool(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
140 self.assertTrue(not self._empty_mapping())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
141 self.assertTrue(self.reference)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
142 self.assertTrue(bool(self._empty_mapping()) is False)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
143 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
144
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
145 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
146 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
147 self.assertEqual(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
148 d = self.reference
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
149 self.assertIn(self.inmapping.keys()[0], d.keys())
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
150 self.assertNotIn(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
151 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
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 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
154 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
155 self.assertEqual(d.values(), [])
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
156
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
157 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
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 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
160 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
161 self.assertEqual(d.items(), [])
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
162
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.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
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_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
166 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
167 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
168
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
169 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
170 d = 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
171 self.assertEqual(d[self.inmapping.keys()[0]], self.inmapping.values()[0])
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
172
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
173 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
174
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
175 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
176 # 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
177 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
178 d.update(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
179 self.assertEqual(d.items(), self.other.items())
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
180
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
181 # 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
182 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
183 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
184 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
185
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
186 # 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
187 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
188 d.update(self.other.items())
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
189 self.assertEqual(d.items(), self.other.items())
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
190
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
191 # Iterator
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
192 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
193 d.update(self.other.iteritems())
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.assertEqual(d.items(), self.other.items())
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
195
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
196 # 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
197 # 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
198 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
199
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
200 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
201 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
202 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
203 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
204 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
205 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
206 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
207 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
208 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
209 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
210 i1 = d.items()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
211 i2 = 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
212 i1.sort()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
213 i2.sort()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
214 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
215
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
216 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
217
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
218 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
219 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
220 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
221 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
222 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
223
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
224 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
225
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
226 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
227 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
228 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
229 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
230 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
231 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
232 return self
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
233 def next(self):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
234 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
235 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
236 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
237 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
238 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
239 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
240 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
241 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
242
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
243 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
244 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
245 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
246 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
247 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
248 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
249 return self
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
250 def next(self):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
261 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
262 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
263 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
264 return self
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
265 def next(self):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
266 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
267
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
268 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
269
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
270 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
271
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
272 # 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
273
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
274 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
275 d = self._empty_mapping()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
276 self.assertTrue(d.get(self.other.keys()[0]) 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
277 self.assertEqual(d.get(self.other.keys()[0], 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
278 d = self.reference
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
279 self.assertTrue(d.get(self.other.keys()[0]) 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
280 self.assertEqual(d.get(self.other.keys()[0], 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
281 self.assertEqual(d.get(self.inmapping.keys()[0]), self.inmapping.values()[0])
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
282 self.assertEqual(d.get(self.inmapping.keys()[0], 3), self.inmapping.values()[0])
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
283 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
284 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
285
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
286 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
287 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
288 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
289
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
290 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
291 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
292 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
293 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
294
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
295 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
296 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
297 k, v = self.inmapping.items()[0]
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
298 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
299 self.assertRaises(KeyError, d.pop, self.other.keys()[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.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
302 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
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 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
305
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
306
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
307 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
308 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
309 BasicTestMappingProtocol.test_constructor(self)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
310 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
311 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
312
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
313 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
314 BasicTestMappingProtocol.test_bool(self)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
315 self.assertTrue(not self._empty_mapping())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
316 self.assertTrue(self._full_mapping({"x": "y"}))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
317 self.assertTrue(bool(self._empty_mapping()) is False)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
318 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
319
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
320 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
321 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
322 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
323 self.assertEqual(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
324 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
325 k = d.keys()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
326 self.assertIn('a', k)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
327 self.assertIn('b', k)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
328 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
329
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
330 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
331 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
332 d = self._full_mapping({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
333 self.assertEqual(d.values(), [2])
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 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
336 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
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 d = self._full_mapping({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
339 self.assertEqual(d.items(), [(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
340
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
341 def test_has_key(self):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
342 d = self._empty_mapping()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
343 self.assertTrue(not d.has_key('a'))
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
344 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
345 k = 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
346 k.sort()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
347 self.assertEqual(k, ['a', 'b'])
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
348
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
349 self.assertRaises(TypeError, d.has_key)
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
350
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
351 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
352 d = self._empty_mapping()
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
353 self.assertNotIn('a', d)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
354 self.assertTrue(not ('a' in d))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
355 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
356 d = self._full_mapping({'a': 1, 'b': 2})
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
357 self.assertIn('a', d)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
358 self.assertIn('b', d)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
359 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
360
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
361 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
362
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
363 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
364 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
365 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
366 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
367
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
368 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
369 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
370 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
371 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
372 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
373 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
374 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
375 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
376 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
377 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
378 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
379
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
380 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
381
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
382 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
383 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
384 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
385 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
386
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.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
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 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
390 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
391 # 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
392 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
393 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
394 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
395 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
396 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
397
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
398 # 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
399 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
400 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
401
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
402 # 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
403 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
404 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
405 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
406 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
407 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
408
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
409 # 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
410 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
411 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
412 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
413
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
414 # 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
415 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
416 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
417 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
418
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
419 # iterator
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
420 d = self._full_mapping({1:3, 2:4})
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
421 d.update(self._full_mapping({1:2, 3:4, 5:6}).iteritems())
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
422 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
423
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
424 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
425 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
426 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
427 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
428 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
429 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
430 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
431 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
432 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
433 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
434
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
435 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
436 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
437 d = self._empty_mapping()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
438 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
439 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
440 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
441 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
442 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
443 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
444 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
445 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
446 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
447 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
448 self.assertEqual(dictlike().fromkeys('a'), {'a':None})
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
449 self.assertTrue(dictlike.fromkeys('a').__class__ is dictlike)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
450 self.assertTrue(dictlike().fromkeys('a').__class__ is dictlike)
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
451 # FIXME: the following won't work with UserDict, because it's an old style class
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
452 # 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
453 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
454 def __new__(cls):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
455 return UserDict.UserDict()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
456 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
457 self.assertEqual(ud, {'a':None, 'b':None})
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
458 # FIXME: the following won't work with UserDict, because it's an old style class
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
459 # self.assertIsInstance(ud, UserDict.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
460 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
461
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
462 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
463
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
464 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
465 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
466 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
467
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
468 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
469
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
470 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
471 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
472 return self
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
473 def next(self):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
474 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
475
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
476 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
477
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
478 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
479 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
480 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
481
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
482 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
483
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
484 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
485 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
486 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
487 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
488 self.assertEqual(d.copy(), d)
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
489 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
490 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
491
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
492 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
493 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
494 d = self._empty_mapping()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
495 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
496 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
497 d = self._full_mapping({'a' : 1, 'b' : 2})
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
498 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
499 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
500 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
501 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
502
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
503 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
504 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
505 d = self._empty_mapping()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
506 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
507 d.setdefault('key0', [])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
508 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
509 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
510 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
511 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
512 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
513
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
514 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
515 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
516 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
517 # -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
518 # +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
519 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
520 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
521 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
522 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
523 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
524 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
525 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
526 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
527 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
528 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
529 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
530 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
531 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
532 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
533 self.assertEqual(vb, int(kb))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
534 self.assertTrue(not(copymode < 0 and ta != tb))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
535 self.assertTrue(not a)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
536 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
537
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
538 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
539 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
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 # 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
542 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
543 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
544
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
545 # verify longs/ints get same value when key > 32 bits (for 64-bit archs)
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
546 # see SF bug #689659
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
547 x = 4503599627370496L
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
548 y = 4503599627370496
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
549 h = self._full_mapping({x: 'anything', y: 'something else'})
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
550 self.assertEqual(h[x], h[y])
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
551
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
552 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
553 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
554 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
555
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
556
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
557 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
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 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
560 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
561 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
562
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
563 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
564 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
565 raise Exc()
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 32204
diff changeset
566 def __hash__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 32204
diff changeset
567 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
568
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
569 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
570 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
571 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
572
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
573 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
574 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
575 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
576 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
577 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
578 else:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
579 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
580
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
581 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
582 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
583 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
584 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
585 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
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 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
588 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
589 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
590 def __new__(cls):
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
591 return UserDict.UserDict()
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
592 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
593 self.assertEqual(ud, {'a':None, 'b':None})
58472
6ffcdafa0118 use assert[Not]IsInstance where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58460
diff changeset
594 self.assertIsInstance(ud, UserDict.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
595
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
596 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
597 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
598
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
599 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
600
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
601 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
602 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
603 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
604 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
605 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
606 else:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
607 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
608
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 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
611 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
612 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
613 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
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 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
616 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
617 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
618 try:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
619 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
620 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
621 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
622 pass
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
623 else:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
624 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
625
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
626 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
627 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
628 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
629 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
630 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
631 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
632 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
633 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
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 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
636
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
637 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
638 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
639 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
640
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
641 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
642 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
643
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
644 def test_le(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
645 self.assertTrue(not (self._empty_mapping() < self._empty_mapping()))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 43958
diff changeset
646 self.assertTrue(not (self._full_mapping({1: 2}) < self._full_mapping({1L: 2L})))
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
647
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
648 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
649
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
650 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
651 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
652 raise Exc()
43958
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 32204
diff changeset
653 def __hash__(self):
103b7f955036 Backport of _abccoll.py by Benjamin Arangueren, issue 1383.
Guido van Rossum <guido@python.org>
parents: 32204
diff changeset
654 return 42
32163
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
655
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
656 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
657 d2 = self._full_mapping({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
658 try:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
659 d1 < d2
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
660 except Exc:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
661 pass
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
662 else:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
663 self.fail("< didn't 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
664
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
665 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
666 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
667
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
668 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
669
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
670 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
671 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
672 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
673 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
674 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
675 else:
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
676 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
677
fad52ffd8892 Port the dictionary tests from test_types.py to unittest. Collect as much
Walter Dörwald <walter@livinglogic.de>
parents:
diff changeset
678 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
679 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
680 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
681 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
682 self.assertRaises(Exc, d.setdefault, x, [])