Back to index

python3.2  3.2.2
Public Member Functions
test.test_collections.TestChainMap Class Reference

List of all members.

Public Member Functions

def test_basics
def test_contructor
def test_bool
def test_missing
def test_dict_coercion

Detailed Description

Definition at line 28 of file test_collections.py.


Member Function Documentation

Definition at line 30 of file test_collections.py.

00030 
00031     def test_basics(self):
00032         c = ChainMap()
00033         c['a'] = 1
00034         c['b'] = 2
00035         d = c.new_child()
00036         d['b'] = 20
00037         d['c'] = 30
00038         self.assertEqual(d.maps, [{'b':20, 'c':30}, {'a':1, 'b':2}])  # check internal state
00039         self.assertEqual(d.items(), dict(a=1, b=20, c=30).items())    # check items/iter/getitem
00040         self.assertEqual(len(d), 3)                                   # check len
00041         for key in 'abc':                                             # check contains
00042             self.assertIn(key, d)
00043         for k, v in dict(a=1, b=20, c=30, z=100).items():             # check get
00044             self.assertEqual(d.get(k, 100), v)
00045 
00046         del d['b']                                                    # unmask a value
00047         self.assertEqual(d.maps, [{'c':30}, {'a':1, 'b':2}])          # check internal state
00048         self.assertEqual(d.items(), dict(a=1, b=2, c=30).items())     # check items/iter/getitem
00049         self.assertEqual(len(d), 3)                                   # check len
00050         for key in 'abc':                                             # check contains
00051             self.assertIn(key, d)
00052         for k, v in dict(a=1, b=2, c=30, z=100).items():              # check get
00053             self.assertEqual(d.get(k, 100), v)
00054         self.assertIn(repr(d), [                                      # check repr
00055             type(d).__name__ + "({'c': 30}, {'a': 1, 'b': 2})",
00056             type(d).__name__ + "({'c': 30}, {'b': 2, 'a': 1})"
00057         ])
00058 
00059         for e in d.copy(), copy.copy(d):                               # check shallow copies
00060             self.assertEqual(d, e)
00061             self.assertEqual(d.maps, e.maps)
00062             self.assertIsNot(d, e)
00063             self.assertIsNot(d.maps[0], e.maps[0])
00064             for m1, m2 in zip(d.maps[1:], e.maps[1:]):
00065                 self.assertIs(m1, m2)
00066 
00067         for e in [pickle.loads(pickle.dumps(d)),
00068                   copy.deepcopy(d),
00069                   eval(repr(d))
00070                 ]:                                                    # check deep copies
00071             self.assertEqual(d, e)
00072             self.assertEqual(d.maps, e.maps)
00073             self.assertIsNot(d, e)
00074             for m1, m2 in zip(d.maps, e.maps):
00075                 self.assertIsNot(m1, m2, e)
00076 
00077         f = d.new_child()
00078         f['b'] = 5
00079         self.assertEqual(f.maps, [{'b': 5}, {'c':30}, {'a':1, 'b':2}])
00080         self.assertEqual(f.parents.maps, [{'c':30}, {'a':1, 'b':2}])   # check parents
00081         self.assertEqual(f['b'], 5)                                    # find first in chain
00082         self.assertEqual(f.parents['b'], 2)                            # look beyond maps[0]

Here is the call graph for this function:

Definition at line 87 of file test_collections.py.

00087 
00088     def test_bool(self):
00089         self.assertFalse(ChainMap())
00090         self.assertFalse(ChainMap({}, {}))
00091         self.assertTrue(ChainMap({1:2}, {}))
00092         self.assertTrue(ChainMap({}, {1:2}))

Here is the call graph for this function:

Definition at line 83 of file test_collections.py.

00083 
00084     def test_contructor(self):
00085         self.assertEqual(ChainMap().maps, [{}])                        # no-args --> one new dict
00086         self.assertEqual(ChainMap({1:2}).maps, [{1:2}])                # 1 arg --> list

Here is the call graph for this function:

Definition at line 110 of file test_collections.py.

00110 
00111     def test_dict_coercion(self):
00112         d = ChainMap(dict(a=1, b=2), dict(b=20, c=30))
00113         self.assertEqual(dict(d), dict(a=1, b=2, c=30))
00114         self.assertEqual(dict(d.items()), dict(a=1, b=2, c=30))
00115 

Here is the call graph for this function:

Definition at line 93 of file test_collections.py.

00093 
00094     def test_missing(self):
00095         class DefaultChainMap(ChainMap):
00096             def __missing__(self, key):
00097                 return 999
00098         d = DefaultChainMap(dict(a=1, b=2), dict(b=20, c=30))
00099         for k, v in dict(a=1, b=2, c=30, d=999).items():
00100             self.assertEqual(d[k], v)                                  # check __getitem__ w/missing
00101         for k, v in dict(a=1, b=2, c=30, d=77).items():
00102             self.assertEqual(d.get(k, 77), v)                          # check get() w/ missing
00103         for k, v in dict(a=True, b=True, c=True, d=False).items():
00104             self.assertEqual(k in d, v)                                # check __contains__ w/missing
00105         self.assertEqual(d.pop('a', 1001), 1, d)
00106         self.assertEqual(d.pop('a', 1002), 1002)                       # check pop() w/missing
00107         self.assertEqual(d.popitem(), ('b', 2))                        # check popitem() w/missing
00108         with self.assertRaises(KeyError):
00109             d.popitem()

Here is the call graph for this function:


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