Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Attributes
test.test_userdict.UserDictTest Class Reference
Inheritance diagram for test.test_userdict.UserDictTest:
Inheritance graph
[legend]
Collaboration diagram for test.test_userdict.UserDictTest:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_all
def test_missing
def test_getitem
def test_fromkeys
def test_pop
def test_mutatingiteration
def test_repr
def test_eq
def test_setdefault
def test_constructor
def test_bool
def test_keys
def test_values
def test_items
def test_contains
def test_len
def test_clear
def test_update
def test_copy
def test_get
def test_popitem
def test_read
def test_write

Public Attributes

 d
 reference
 other
 inmapping
 i

Static Public Attributes

 type2test = collections.UserDict

Private Attributes

 __missing__

Detailed Description

Definition at line 13 of file test_userdict.py.


Member Function Documentation

Definition at line 16 of file test_userdict.py.

00016 
00017     def test_all(self):
00018         # Test constructors
00019         u = collections.UserDict()
00020         u0 = collections.UserDict(d0)
00021         u1 = collections.UserDict(d1)
00022         u2 = collections.UserDict(d2)
00023 
00024         uu = collections.UserDict(u)
00025         uu0 = collections.UserDict(u0)
00026         uu1 = collections.UserDict(u1)
00027         uu2 = collections.UserDict(u2)
00028 
00029         # keyword arg constructor
00030         self.assertEqual(collections.UserDict(one=1, two=2), d2)
00031         # item sequence constructor
00032         self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
00033         self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
00034         # both together
00035         self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
00036 
00037         # alternate constructor
00038         self.assertEqual(collections.UserDict.fromkeys('one two'.split()), d4)
00039         self.assertEqual(collections.UserDict().fromkeys('one two'.split()), d4)
00040         self.assertEqual(collections.UserDict.fromkeys('one two'.split(), 1), d5)
00041         self.assertEqual(collections.UserDict().fromkeys('one two'.split(), 1), d5)
00042         self.assertTrue(u1.fromkeys('one two'.split()) is not u1)
00043         self.assertIsInstance(u1.fromkeys('one two'.split()), collections.UserDict)
00044         self.assertIsInstance(u2.fromkeys('one two'.split()), collections.UserDict)
00045 
00046         # Test __repr__
00047         self.assertEqual(str(u0), str(d0))
00048         self.assertEqual(repr(u1), repr(d1))
00049         self.assertEqual(repr(u2), repr(d2))
00050 
00051         # Test rich comparison and __len__
00052         all = [d0, d1, d2, u, u0, u1, u2, uu, uu0, uu1, uu2]
00053         for a in all:
00054             for b in all:
00055                 self.assertEqual(a == b, len(a) == len(b))
00056 
00057         # Test __getitem__
00058         self.assertEqual(u2["one"], 1)
00059         self.assertRaises(KeyError, u1.__getitem__, "two")
00060 
00061         # Test __setitem__
00062         u3 = collections.UserDict(u2)
00063         u3["two"] = 2
00064         u3["three"] = 3
00065 
00066         # Test __delitem__
00067         del u3["three"]
00068         self.assertRaises(KeyError, u3.__delitem__, "three")
00069 
00070         # Test clear
00071         u3.clear()
00072         self.assertEqual(u3, {})
00073 
00074         # Test copy()
00075         u2a = u2.copy()
00076         self.assertEqual(u2a, u2)
00077         u2b = collections.UserDict(x=42, y=23)
00078         u2c = u2b.copy() # making a copy of a UserDict is special cased
00079         self.assertEqual(u2b, u2c)
00080 
00081         class MyUserDict(collections.UserDict):
00082             def display(self): print(self)
00083 
00084         m2 = MyUserDict(u2)
00085         m2a = m2.copy()
00086         self.assertEqual(m2a, m2)
00087 
00088         # SF bug #476616 -- copy() of UserDict subclass shared data
00089         m2['foo'] = 'bar'
00090         self.assertNotEqual(m2a, m2)
00091 
00092         # Test keys, items, values
00093         self.assertEqual(u2.keys(), d2.keys())
00094         self.assertEqual(u2.items(), d2.items())
00095         self.assertEqual(list(u2.values()), list(d2.values()))
00096 
00097         # Test "in".
00098         for i in u2.keys():
00099             self.assertIn(i, u2)
00100             self.assertEqual(i in u1, i in d1)
00101             self.assertEqual(i in u0, i in d0)
00102 
00103         # Test update
00104         t = collections.UserDict()
00105         t.update(u2)
00106         self.assertEqual(t, u2)
00107 
00108         # Test get
00109         for i in u2.keys():
00110             self.assertEqual(u2.get(i), u2[i])
00111             self.assertEqual(u1.get(i), d1.get(i))
00112             self.assertEqual(u0.get(i), d0.get(i))
00113 
00114         # Test "in" iteration.
00115         for i in range(20):
00116             u2[i] = str(i)
00117         ikeys = []
00118         for k in u2:
00119             ikeys.append(k)
00120         keys = u2.keys()
00121         self.assertEqual(set(ikeys), set(keys))
00122 
00123         # Test setdefault
00124         t = collections.UserDict()
00125         self.assertEqual(t.setdefault("x", 42), 42)
00126         self.assertIn("x", t)
00127         self.assertEqual(t.setdefault("x", 23), 42)
00128 
00129         # Test pop
00130         t = collections.UserDict(x=42)
00131         self.assertEqual(t.pop("x"), 42)
00132         self.assertRaises(KeyError, t.pop, "x")
00133         self.assertEqual(t.pop("x", 1), 1)
00134         t["x"] = 42
00135         self.assertEqual(t.pop("x", 1), 42)
00136 
00137         # Test popitem
00138         t = collections.UserDict(x=42)
00139         self.assertEqual(t.popitem(), ("x", 42))
00140         self.assertRaises(KeyError, t.popitem)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 310 of file mapping_tests.py.

00310 
00311     def test_bool(self):
00312         BasicTestMappingProtocol.test_bool(self)
00313         self.assertTrue(not self._empty_mapping())
00314         self.assertTrue(self._full_mapping({"x": "y"}))
00315         self.assertTrue(bool(self._empty_mapping()) is False)
00316         self.assertTrue(bool(self._full_mapping({"x": "y"})) is True)

Here is the call graph for this function:

Definition at line 369 of file mapping_tests.py.

00369 
00370     def test_clear(self):
00371         d = self._full_mapping({1:1, 2:2, 3:3})
00372         d.clear()
00373         self.assertEqual(d, {})
00374 
00375         self.assertRaises(TypeError, d.clear, None)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 305 of file mapping_tests.py.

00305 
00306     def test_constructor(self):
00307         BasicTestMappingProtocol.test_constructor(self)
00308         self.assertTrue(self._empty_mapping() is not self._empty_mapping())
00309         self.assertEqual(self.type2test(x=1, y=2), {"x": 1, "y": 2})

Here is the call graph for this function:

Definition at line 338 of file mapping_tests.py.

00338 
00339     def test_contains(self):
00340         d = self._empty_mapping()
00341         self.assertNotIn('a', d)
00342         self.assertTrue(not ('a' in d))
00343         self.assertTrue('a' not in d)
00344         d = self._full_mapping({'a': 1, 'b': 2})
00345         self.assertIn('a', d)
00346         self.assertIn('b', d)
00347         self.assertNotIn('c', d)
00348 
00349         self.assertRaises(TypeError, d.__contains__)

Here is the call graph for this function:

Definition at line 469 of file mapping_tests.py.

00469 
00470     def test_copy(self):
00471         d = self._full_mapping({1:1, 2:2, 3:3})
00472         self.assertEqual(d.copy(), {1:1, 2:2, 3:3})
00473         d = self._empty_mapping()
00474         self.assertEqual(d.copy(), d)
00475         self.assertIsInstance(d.copy(), d.__class__)
00476         self.assertRaises(TypeError, d.copy, None)

Here is the call graph for this function:

Definition at line 622 of file mapping_tests.py.

00622 
00623     def test_eq(self):
00624         self.assertEqual(self._empty_mapping(), self._empty_mapping())
00625         self.assertEqual(self._full_mapping({1: 2}),
00626                          self._full_mapping({1: 2}))
00627 
00628         class Exc(Exception): pass
00629 
00630         class BadCmp(object):
00631             def __eq__(self, other):
00632                 raise Exc()
00633             def __hash__(self):
00634                 return 1
00635 
00636         d1 = self._full_mapping({BadCmp(): 1})
00637         d2 = self._full_mapping({1: 1})
00638         self.assertRaises(Exc, lambda: BadCmp()==1)
00639         self.assertRaises(Exc, lambda: d1==d2)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 565 of file mapping_tests.py.

00565 
00566     def test_fromkeys(self):
00567         TestMappingProtocol.test_fromkeys(self)
00568         class mydict(self.type2test):
00569             def __new__(cls):
00570                 return collections.UserDict()
00571         ud = mydict.fromkeys('ab')
00572         self.assertEqual(ud, {'a':None, 'b':None})
00573         self.assertIsInstance(ud, collections.UserDict)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 477 of file mapping_tests.py.

00477 
00478     def test_get(self):
00479         BasicTestMappingProtocol.test_get(self)
00480         d = self._empty_mapping()
00481         self.assertTrue(d.get('c') is None)
00482         self.assertEqual(d.get('c', 3), 3)
00483         d = self._full_mapping({'a' : 1, 'b' : 2})
00484         self.assertTrue(d.get('c') is None)
00485         self.assertEqual(d.get('c', 3), 3)
00486         self.assertEqual(d.get('a'), 1)
00487         self.assertEqual(d.get('a', 3), 1)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 537 of file mapping_tests.py.

00537 
00538     def test_getitem(self):
00539         TestMappingProtocol.test_getitem(self)
00540         class Exc(Exception): pass
00541 
00542         class BadEq(object):
00543             def __eq__(self, other):
00544                 raise Exc()
00545             def __hash__(self):
00546                 return 24
00547 
00548         d = self._empty_mapping()
00549         d[BadEq()] = 42
00550         self.assertRaises(KeyError, d.__getitem__, 23)
00551 
00552         class BadHash(object):
00553             fail = False
00554             def __hash__(self):
00555                 if self.fail:
00556                     raise Exc()
00557                 else:
00558                     return 42
00559 
00560         d = self._empty_mapping()
00561         x = BadHash()
00562         d[x] = 42
00563         x.fail = True
00564         self.assertRaises(Exc, d.__getitem__, x)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 332 of file mapping_tests.py.

00332 
00333     def test_items(self):
00334         BasicTestMappingProtocol.test_items(self)
00335 
00336         d = self._full_mapping({1:2})
00337         self.assertEqual(list(d.items()), [(1, 2)])

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 317 of file mapping_tests.py.

00317 
00318     def test_keys(self):
00319         BasicTestMappingProtocol.test_keys(self)
00320         d = self._empty_mapping()
00321         self.assertEqual(list(d.keys()), [])
00322         d = self._full_mapping({'a': 1, 'b': 2})
00323         k = d.keys()
00324         self.assertIn('a', k)
00325         self.assertIn('b', k)
00326         self.assertNotIn('c', k)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 350 of file mapping_tests.py.

00350 
00351     def test_len(self):
00352         BasicTestMappingProtocol.test_len(self)
00353         d = self._full_mapping({'a': 1, 'b': 2})
00354         self.assertEqual(len(d), 2)

Here is the call graph for this function:

Definition at line 141 of file test_userdict.py.

00141 
00142     def test_missing(self):
00143         # Make sure UserDict doesn't have a __missing__ method
00144         self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)
00145         # Test several cases:
00146         # (D) subclass defines __missing__ method returning a value
00147         # (E) subclass defines __missing__ method raising RuntimeError
00148         # (F) subclass sets __missing__ instance variable (no effect)
00149         # (G) subclass doesn't define __missing__ at a all
00150         class D(collections.UserDict):
00151             def __missing__(self, key):
00152                 return 42
00153         d = D({1: 2, 3: 4})
00154         self.assertEqual(d[1], 2)
00155         self.assertEqual(d[3], 4)
00156         self.assertNotIn(2, d)
00157         self.assertNotIn(2, d.keys())
00158         self.assertEqual(d[2], 42)
00159         class E(collections.UserDict):
00160             def __missing__(self, key):
00161                 raise RuntimeError(key)
00162         e = E()
00163         try:
00164             e[42]
00165         except RuntimeError as err:
00166             self.assertEqual(err.args, (42,))
00167         else:
00168             self.fail("e[42] didn't raise RuntimeError")
00169         class F(collections.UserDict):
00170             def __init__(self):
00171                 # An instance variable __missing__ should have no effect
00172                 self.__missing__ = lambda key: None
00173                 collections.UserDict.__init__(self)
00174         f = F()
00175         try:
00176             f[42]
00177         except KeyError as err:
00178             self.assertEqual(err.args, (42,))
00179         else:
00180             self.fail("f[42] didn't raise KeyError")
00181         class G(collections.UserDict):
00182             pass
00183         g = G()
00184         try:
00185             g[42]
00186         except KeyError as err:
00187             self.assertEqual(err.args, (42,))
00188         else:
00189             self.fail("g[42] didn't raise KeyError")
00190 
00191 

Here is the call graph for this function:

Definition at line 593 of file mapping_tests.py.

00593 
00594     def test_mutatingiteration(self):
00595         d = self._empty_mapping()
00596         d[1] = 1
00597         try:
00598             for i in d:
00599                 d[i+1] = 1
00600         except RuntimeError:
00601             pass
00602         else:
00603             self.fail("changing dict size during iteration doesn't raise Error")

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 574 of file mapping_tests.py.

00574 
00575     def test_pop(self):
00576         TestMappingProtocol.test_pop(self)
00577 
00578         class Exc(Exception): pass
00579 
00580         class BadHash(object):
00581             fail = False
00582             def __hash__(self):
00583                 if self.fail:
00584                     raise Exc()
00585                 else:
00586                     return 42
00587 
00588         d = self._empty_mapping()
00589         x = BadHash()
00590         d[x] = 42
00591         x.fail = True
00592         self.assertRaises(Exc, d.pop, x)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 499 of file mapping_tests.py.

00499 
00500     def test_popitem(self):
00501         BasicTestMappingProtocol.test_popitem(self)
00502         for copymode in -1, +1:
00503             # -1: b has same structure as a
00504             # +1: b is a.copy()
00505             for log2size in range(12):
00506                 size = 2**log2size
00507                 a = self._empty_mapping()
00508                 b = self._empty_mapping()
00509                 for i in range(size):
00510                     a[repr(i)] = i
00511                     if copymode < 0:
00512                         b[repr(i)] = i
00513                 if copymode > 0:
00514                     b = a.copy()
00515                 for i in range(size):
00516                     ka, va = ta = a.popitem()
00517                     self.assertEqual(va, int(ka))
00518                     kb, vb = tb = b.popitem()
00519                     self.assertEqual(vb, int(kb))
00520                     self.assertTrue(not(copymode < 0 and ta != tb))
00521                 self.assertTrue(not a)
00522                 self.assertTrue(not b)

Here is the call graph for this function:

Definition at line 42 of file mapping_tests.py.

00042 
00043     def test_read(self):
00044         # Test for read only operations on mapping
00045         p = self._empty_mapping()
00046         p1 = dict(p) #workaround for singleton objects
00047         d = self._full_mapping(self.reference)
00048         if d is p:
00049             p = p1
00050         #Indexing
00051         for key, value in self.reference.items():
00052             self.assertEqual(d[key], value)
00053         knownkey = list(self.other.keys())[0]
00054         self.assertRaises(KeyError, lambda:d[knownkey])
00055         #len
00056         self.assertEqual(len(p), 0)
00057         self.assertEqual(len(d), len(self.reference))
00058         #__contains__
00059         for k in self.reference:
00060             self.assertIn(k, d)
00061         for k in self.other:
00062             self.assertNotIn(k, d)
00063         #cmp
00064         self.assertEqual(p, p)
00065         self.assertEqual(d, d)
00066         self.assertNotEqual(p, d)
00067         self.assertNotEqual(d, p)
00068         #__non__zero__
00069         if p: self.fail("Empty mapping must compare to False")
00070         if not d: self.fail("Full mapping must compare to True")
00071         # keys(), items(), iterkeys() ...
00072         def check_iterandlist(iter, lst, ref):
00073             self.assertTrue(hasattr(iter, '__next__'))
00074             self.assertTrue(hasattr(iter, '__iter__'))
00075             x = list(iter)
00076             self.assertTrue(set(x)==set(lst)==set(ref))
00077         check_iterandlist(iter(d.keys()), list(d.keys()),
00078                           self.reference.keys())
00079         check_iterandlist(iter(d), list(d.keys()), self.reference.keys())
00080         check_iterandlist(iter(d.values()), list(d.values()),
00081                           self.reference.values())
00082         check_iterandlist(iter(d.items()), list(d.items()),
00083                           self.reference.items())
00084         #get
00085         key, value = next(iter(d.items()))
00086         knownkey, knownvalue = next(iter(self.other.items()))
00087         self.assertEqual(d.get(key, knownvalue), value)
00088         self.assertEqual(d.get(knownkey, knownvalue), knownvalue)
00089         self.assertNotIn(knownkey, d)

Here is the call graph for this function:

Definition at line 604 of file mapping_tests.py.

00604 
00605     def test_repr(self):
00606         d = self._empty_mapping()
00607         self.assertEqual(repr(d), '{}')
00608         d[1] = 2
00609         self.assertEqual(repr(d), '{1: 2}')
00610         d = self._empty_mapping()
00611         d[1] = d
00612         self.assertEqual(repr(d), '{1: {...}}')
00613 
00614         class Exc(Exception): pass
00615 
00616         class BadRepr(object):
00617             def __repr__(self):
00618                 raise Exc()
00619 
00620         d = self._full_mapping({1: BadRepr()})
00621         self.assertRaises(Exc, repr, d)

Here is the call graph for this function:

Reimplemented from test.mapping_tests.TestMappingProtocol.

Definition at line 640 of file mapping_tests.py.

00640 
00641     def test_setdefault(self):
00642         TestMappingProtocol.test_setdefault(self)
00643 
00644         class Exc(Exception): pass
00645 
00646         class BadHash(object):
00647             fail = False
00648             def __hash__(self):
00649                 if self.fail:
00650                     raise Exc()
00651                 else:
00652                     return 42
00653 
00654         d = self._empty_mapping()
00655         x = BadHash()
00656         d[x] = 42
00657         x.fail = True
00658         self.assertRaises(Exc, d.setdefault, x, [])

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 376 of file mapping_tests.py.

00376 
00377     def test_update(self):
00378         BasicTestMappingProtocol.test_update(self)
00379         # mapping argument
00380         d = self._empty_mapping()
00381         d.update({1:100})
00382         d.update({2:20})
00383         d.update({1:1, 2:2, 3:3})
00384         self.assertEqual(d, {1:1, 2:2, 3:3})
00385 
00386         # no argument
00387         d.update()
00388         self.assertEqual(d, {1:1, 2:2, 3:3})
00389 
00390         # keyword arguments
00391         d = self._empty_mapping()
00392         d.update(x=100)
00393         d.update(y=20)
00394         d.update(x=1, y=2, z=3)
00395         self.assertEqual(d, {"x":1, "y":2, "z":3})
00396 
00397         # item sequence
00398         d = self._empty_mapping()
00399         d.update([("x", 100), ("y", 20)])
00400         self.assertEqual(d, {"x":100, "y":20})
00401 
00402         # Both item sequence and keyword arguments
00403         d = self._empty_mapping()
00404         d.update([("x", 100), ("y", 20)], x=1, y=2)
00405         self.assertEqual(d, {"x":1, "y":2})
00406 
00407         # iterator
00408         d = self._full_mapping({1:3, 2:4})
00409         d.update(self._full_mapping({1:2, 3:4, 5:6}).items())
00410         self.assertEqual(d, {1:2, 2:4, 3:4, 5:6})
00411 
00412         class SimpleUserDict:
00413             def __init__(self):
00414                 self.d = {1:1, 2:2, 3:3}
00415             def keys(self):
00416                 return self.d.keys()
00417             def __getitem__(self, i):
00418                 return self.d[i]
00419         d.clear()
00420         d.update(SimpleUserDict())
00421         self.assertEqual(d, {1:1, 2:2, 3:3})

Here is the call graph for this function:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 327 of file mapping_tests.py.

00327 
00328     def test_values(self):
00329         BasicTestMappingProtocol.test_values(self)
00330         d = self._full_mapping({1:2})
00331         self.assertEqual(list(d.values()), [2])

Here is the call graph for this function:

Definition at line 90 of file mapping_tests.py.

00090 
00091     def test_write(self):
00092         # Test for write operations on mapping
00093         p = self._empty_mapping()
00094         #Indexing
00095         for key, value in self.reference.items():
00096             p[key] = value
00097             self.assertEqual(p[key], value)
00098         for key in self.reference.keys():
00099             del p[key]
00100             self.assertRaises(KeyError, lambda:p[key])
00101         p = self._empty_mapping()
00102         #update
00103         p.update(self.reference)
00104         self.assertEqual(dict(p), self.reference)
00105         items = list(p.items())
00106         p = self._empty_mapping()
00107         p.update(items)
00108         self.assertEqual(dict(p), self.reference)
00109         d = self._full_mapping(self.reference)
00110         #setdefault
00111         key, value = next(iter(d.items()))
00112         knownkey, knownvalue = next(iter(self.other.items()))
00113         self.assertEqual(d.setdefault(key, knownvalue), value)
00114         self.assertEqual(d[key], value)
00115         self.assertEqual(d.setdefault(knownkey, knownvalue), knownvalue)
00116         self.assertEqual(d[knownkey], knownvalue)
00117         #pop
00118         self.assertEqual(d.pop(knownkey), knownvalue)
00119         self.assertNotIn(knownkey, d)
00120         self.assertRaises(KeyError, d.pop, knownkey)
00121         default = 909
00122         d[knownkey] = knownvalue
00123         self.assertEqual(d.pop(knownkey, default), knownvalue)
00124         self.assertNotIn(knownkey, d)
00125         self.assertEqual(d.pop(knownkey, default), default)
00126         #popitem
00127         key, value = d.popitem()
00128         self.assertNotIn(key, d)
00129         self.assertEqual(value, self.reference[key])
00130         p=self._empty_mapping()
00131         self.assertRaises(KeyError, p.popitem)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 171 of file test_userdict.py.

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 413 of file mapping_tests.py.

Definition at line 225 of file mapping_tests.py.

Definition at line 39 of file mapping_tests.py.

Definition at line 35 of file mapping_tests.py.

Definition at line 31 of file mapping_tests.py.

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Definition at line 14 of file test_userdict.py.


The documentation for this class was generated from the following file: