Back to index

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

List of all members.

Public Member Functions

def test_constructor
def test_bool
def test_keys
def test_values
def test_items
def test_contains
def test_len
def test_getitem
def test_clear
def test_update
def test_fromkeys
def test_copy
def test_get
def test_setdefault
def test_popitem
def test_pop
def test_read
def test_write

Public Attributes

 d
 reference
 other
 inmapping
 i

Static Public Attributes

 type2test = None

Detailed Description

Definition at line 304 of file mapping_tests.py.


Member Function Documentation

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:

Reimplemented in test.mapping_tests.TestHashMappingProtocol.

Definition at line 422 of file mapping_tests.py.

00422 
00423     def test_fromkeys(self):
00424         self.assertEqual(self.type2test.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
00425         d = self._empty_mapping()
00426         self.assertTrue(not(d.fromkeys('abc') is d))
00427         self.assertEqual(d.fromkeys('abc'), {'a':None, 'b':None, 'c':None})
00428         self.assertEqual(d.fromkeys((4,5),0), {4:0, 5:0})
00429         self.assertEqual(d.fromkeys([]), {})
00430         def g():
00431             yield 1
00432         self.assertEqual(d.fromkeys(g()), {1:None})
00433         self.assertRaises(TypeError, {}.fromkeys, 3)
00434         class dictlike(self.type2test): pass
00435         self.assertEqual(dictlike.fromkeys('a'), {'a':None})
00436         self.assertEqual(dictlike().fromkeys('a'), {'a':None})
00437         self.assertTrue(dictlike.fromkeys('a').__class__ is dictlike)
00438         self.assertTrue(dictlike().fromkeys('a').__class__ is dictlike)
00439         self.assertTrue(type(dictlike.fromkeys('a')) is dictlike)
00440         class mydict(self.type2test):
00441             def __new__(cls):
00442                 return collections.UserDict()
00443         ud = mydict.fromkeys('ab')
00444         self.assertEqual(ud, {'a':None, 'b':None})
00445         self.assertIsInstance(ud, collections.UserDict)
00446         self.assertRaises(TypeError, dict.fromkeys)
00447 
00448         class Exc(Exception): pass
00449 
00450         class baddict1(self.type2test):
00451             def __init__(self):
00452                 raise Exc()
00453 
00454         self.assertRaises(Exc, baddict1.fromkeys, [1])
00455 
00456         class BadSeq(object):
00457             def __iter__(self):
00458                 return self
00459             def __next__(self):
00460                 raise Exc()
00461 
00462         self.assertRaises(Exc, self.type2test.fromkeys, BadSeq())
00463 
00464         class baddict2(self.type2test):
00465             def __setitem__(self, key, value):
00466                 raise Exc()
00467 
00468         self.assertRaises(Exc, baddict2.fromkeys, [1])

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.BasicTestMappingProtocol.

Reimplemented in test.mapping_tests.TestHashMappingProtocol.

Definition at line 355 of file mapping_tests.py.

00355 
00356     def test_getitem(self):
00357         BasicTestMappingProtocol.test_getitem(self)
00358         d = self._full_mapping({'a': 1, 'b': 2})
00359         self.assertEqual(d['a'], 1)
00360         self.assertEqual(d['b'], 2)
00361         d['c'] = 3
00362         d['a'] = 4
00363         self.assertEqual(d['c'], 3)
00364         self.assertEqual(d['a'], 4)
00365         del d['b']
00366         self.assertEqual(d, {'a': 4, 'c': 3})
00367 
00368         self.assertRaises(TypeError, d.__getitem__)

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:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Reimplemented in test.mapping_tests.TestHashMappingProtocol.

Definition at line 523 of file mapping_tests.py.

00523 
00524     def test_pop(self):
00525         BasicTestMappingProtocol.test_pop(self)
00526 
00527         # Tests for pop with specified key
00528         d = self._empty_mapping()
00529         k, v = 'abc', 'def'
00530 
00531         self.assertEqual(d.pop(k, v), v)
00532         d[k] = v
00533         self.assertEqual(d.pop(k, 1), v)
00534 

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:

Reimplemented from test.mapping_tests.BasicTestMappingProtocol.

Reimplemented in test.mapping_tests.TestHashMappingProtocol.

Definition at line 488 of file mapping_tests.py.

00488 
00489     def test_setdefault(self):
00490         BasicTestMappingProtocol.test_setdefault(self)
00491         d = self._empty_mapping()
00492         self.assertTrue(d.setdefault('key0') is None)
00493         d.setdefault('key0', [])
00494         self.assertTrue(d.setdefault('key0') is None)
00495         d.setdefault('key', []).append(3)
00496         self.assertEqual(d['key'][0], 3)
00497         d.setdefault('key', []).append(4)
00498         self.assertEqual(len(d['key']), 2)

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

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.


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