File: test_chainmap.py

package info (click to toggle)
python-future 0.18.2-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,264 kB
  • sloc: python: 43,246; makefile: 136; sh: 29
file content (160 lines) | stat: -rw-r--r-- 3,721 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Tests for the future.standard_library module
"""

from __future__ import absolute_import, print_function
from future import standard_library
from future import utils
from future.tests.base import unittest, CodeHandler, expectedFailurePY2

import sys
import tempfile
import os
import copy
import textwrap
from subprocess import CalledProcessError


class TestChainMap(CodeHandler):

    def setUp(self):
        self.interpreter = sys.executable
        standard_library.install_aliases()
        super(TestChainMap, self).setUp()

    def tearDown(self):
        # standard_library.remove_hooks()
        pass

    @staticmethod
    def simple_cm():
        from collections import ChainMap
        c = ChainMap()
        c['one'] = 1
        c['two'] = 2
    
        cc = c.new_child()
        cc['one'] = 'one'
    
        return c, cc
    
    
    def test_repr(self):
        c, cc = TestChainMap.simple_cm()
    
        order1 = "ChainMap({'one': 'one'}, {'one': 1, 'two': 2})"
        order2 = "ChainMap({'one': 'one'}, {'two': 2, 'one': 1})"
        assert repr(cc) in [order1, order2]
    
    
    def test_recursive_repr(self):
        """
        Test for degnerative recursive cases. Very unlikely in
        ChainMaps. But all must bow before the god of testing coverage.
        """
        from collections import ChainMap
        c = ChainMap()
        c['one'] = c
        assert repr(c) == "ChainMap({'one': ...})"
    
    
    def test_get(self):
        c, cc = TestChainMap.simple_cm()
    
        assert cc.get('two') == 2
        assert cc.get('three') == None
        assert cc.get('three', 'notthree') == 'notthree'
    
    
    def test_bool(self):
        from collections import ChainMap
        c = ChainMap()
        assert not(bool(c))
    
        c['one'] = 1
        c['two'] = 2
        assert bool(c)
    
        cc = c.new_child()
        cc['one'] = 'one'
        assert cc
    
    
    def test_fromkeys(self):
        from collections import ChainMap
        keys = 'a b c'.split()
        c = ChainMap.fromkeys(keys)
        assert len(c) == 3
        assert c['a'] == None
        assert c['b'] == None
        assert c['c'] == None
    
    
    def test_copy(self):
        c, cc = TestChainMap.simple_cm()
        new_cc = cc.copy()
        assert new_cc is not cc
        assert sorted(new_cc.items()) == sorted(cc.items())
    
    
    def test_parents(self):
        c, cc = TestChainMap.simple_cm()
    
        new_c = cc.parents
        assert c is not new_c
        assert len(new_c) == 2
        assert new_c['one'] == c['one']
        assert new_c['two'] == c['two']
    
    
    def test_delitem(self):
        c, cc = TestChainMap.simple_cm()
    
        with self.assertRaises(KeyError):
            del cc['two']
    
        del cc['one']
        assert len(cc) == 2
        assert cc['one'] == 1
        assert cc['two'] == 2
    
    
    def test_popitem(self):
        c, cc = TestChainMap.simple_cm()
    
        assert cc.popitem() == ('one', 'one')
    
        with self.assertRaises(KeyError):
            cc.popitem()
    
    
    def test_pop(self):
        c, cc = TestChainMap.simple_cm()
    
        assert cc.pop('one') == 'one'
    
        with self.assertRaises(KeyError):
            cc.pop('two')
    
        assert len(cc) == 2
    
    
    def test_clear(self):
        c, cc = TestChainMap.simple_cm()
    
        cc.clear()
        assert len(cc) == 2
        assert cc['one'] == 1
        assert cc['two'] == 2
    
    
    def test_missing(self):
    
        c, cc = TestChainMap.simple_cm()
    
        with self.assertRaises(KeyError):
            cc['clown']


if __name__ == '__main__':
    unittest.main()