Skip to content

Commit edc3f27

Browse files
committed
Add tests for ChainMap (issue #226)
1 parent a6bd305 commit edc3f27

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed

tests/test_future/test_chainmap.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
"""
2+
Tests for the future.standard_library module
3+
"""
4+
5+
from __future__ import absolute_import, print_function
6+
from future import standard_library
7+
from future import utils
8+
from future.tests.base import unittest, CodeHandler, expectedFailurePY2
9+
10+
import sys
11+
import tempfile
12+
import os
13+
import copy
14+
import textwrap
15+
from subprocess import CalledProcessError
16+
17+
18+
class TestChainMap(CodeHandler):
19+
20+
def setUp(self):
21+
self.interpreter = sys.executable
22+
standard_library.install_aliases()
23+
super(TestChainMap, self).setUp()
24+
25+
def tearDown(self):
26+
# standard_library.remove_hooks()
27+
pass
28+
29+
@staticmethod
30+
def simple_cm():
31+
from collections import ChainMap
32+
c = ChainMap()
33+
c['one'] = 1
34+
c['two'] = 2
35+
36+
cc = c.new_child()
37+
cc['one'] = 'one'
38+
39+
return c, cc
40+
41+
42+
def test_repr(self):
43+
c, cc = TestChainMap.simple_cm()
44+
45+
order1 = "ChainMap({'one': 'one'}, {'one': 1, 'two': 2})"
46+
order2 = "ChainMap({'one': 'one'}, {'two': 2, 'one': 1})"
47+
assert repr(cc) in [order1, order2]
48+
49+
50+
def test_recursive_repr(self):
51+
"""
52+
Test for degnerative recursive cases. Very unlikely in
53+
ChainMaps. But all must bow before the god of testing coverage.
54+
"""
55+
from collections import ChainMap
56+
c = ChainMap()
57+
c['one'] = c
58+
assert repr(c) == "ChainMap({'one': ...})"
59+
60+
61+
def test_get(self):
62+
c, cc = TestChainMap.simple_cm()
63+
64+
assert cc.get('two') == 2
65+
assert cc.get('three') == None
66+
assert cc.get('three', 'notthree') == 'notthree'
67+
68+
69+
def test_bool(self):
70+
from collections import ChainMap
71+
c = ChainMap()
72+
assert not(bool(c))
73+
74+
c['one'] = 1
75+
c['two'] = 2
76+
assert bool(c)
77+
78+
cc = c.new_child()
79+
cc['one'] = 'one'
80+
assert cc
81+
82+
83+
def test_fromkeys(self):
84+
from collections import ChainMap
85+
keys = 'a b c'.split()
86+
c = ChainMap.fromkeys(keys)
87+
assert len(c) == 3
88+
assert c['a'] == None
89+
assert c['b'] == None
90+
assert c['c'] == None
91+
92+
93+
def test_copy(self):
94+
c, cc = TestChainMap.simple_cm()
95+
new_cc = cc.copy()
96+
assert new_cc is not cc
97+
assert sorted(new_cc.items()) == sorted(cc.items())
98+
99+
100+
def test_parents(self):
101+
c, cc = TestChainMap.simple_cm()
102+
103+
new_c = cc.parents
104+
assert c is not new_c
105+
assert len(new_c) == 2
106+
assert new_c['one'] == c['one']
107+
assert new_c['two'] == c['two']
108+
109+
110+
def test_delitem(self):
111+
c, cc = TestChainMap.simple_cm()
112+
113+
with self.assertRaises(KeyError):
114+
del cc['two']
115+
116+
del cc['one']
117+
assert len(cc) == 2
118+
assert cc['one'] == 1
119+
assert cc['two'] == 2
120+
121+
122+
def test_popitem(self):
123+
c, cc = TestChainMap.simple_cm()
124+
125+
assert cc.popitem() == ('one', 'one')
126+
127+
with self.assertRaises(KeyError):
128+
cc.popitem()
129+
130+
131+
def test_pop(self):
132+
c, cc = TestChainMap.simple_cm()
133+
134+
assert cc.pop('one') == 'one'
135+
136+
with self.assertRaises(KeyError):
137+
cc.pop('two')
138+
139+
assert len(cc) == 2
140+
141+
142+
def test_clear(self):
143+
c, cc = TestChainMap.simple_cm()
144+
145+
cc.clear()
146+
assert len(cc) == 2
147+
assert cc['one'] == 1
148+
assert cc['two'] == 2
149+
150+
151+
def test_missing(self):
152+
153+
c, cc = TestChainMap.simple_cm()
154+
155+
with self.assertRaises(KeyError):
156+
cc['clown']
157+
158+
159+
if __name__ == '__main__':
160+
unittest.main()

tests/test_future/test_standard_library.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ def test_itertools_zip_longest(self):
247247
self.assertEqual(list(zip_longest(a, b)),
248248
[(1, 2), (2, 4), (None, 6)])
249249

250+
def test_ChainMap(self):
251+
"""
252+
Tests whether collections.ChainMap is available.
253+
"""
254+
from collections import ChainMap
255+
cm = ChainMap()
256+
250257
@unittest.expectedFailure
251258
@unittest.skipIf(utils.PY3, 'generic import tests are for Py2 only')
252259
def test_import_failure_from_module(self):

0 commit comments

Comments
 (0)