File: test_containers_dict.py

package info (click to toggle)
construct 2.10.68%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,812 kB
  • sloc: python: 11,793; makefile: 135
file content (291 lines) | stat: -rw-r--r-- 8,484 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from tests.declarativeunittest import *
from construct import *
from construct.lib import *


def test_getitem():
    c = Container(a=1)
    assert c["a"] == 1
    assert c.a == 1
    assert raises(lambda: c.unknownkey) == AttributeError
    assert raises(lambda: c["unknownkey"]) == KeyError

def test_setitem():
    c = Container()
    c.a = 1
    assert c["a"] == 1
    assert c.a == 1
    c["a"] = 2
    assert c["a"] == 2
    assert c.a == 2

def test_delitem():
    c = Container(a=1, b=2)
    del c.a
    assert "a" not in c
    assert raises(lambda: c.a) == AttributeError
    assert raises(lambda: c["a"]) == KeyError
    del c["b"]
    assert "b" not in c
    assert raises(lambda: c.b) == AttributeError
    assert raises(lambda: c["b"]) == KeyError
    assert c == Container()
    assert list(c) == []

def test_ctor_empty():
    c = Container()
    assert len(c) == 0
    assert list(c.items()) == []
    assert c == Container()
    assert c == Container(c)
    assert c == Container({})
    assert c == Container([])

def test_ctor_chained():
    c = Container(a=1, b=2, c=3, d=4)
    assert c == Container(c)

def test_ctor_dict():
    c = Container(a=1, b=2, c=3, d=4)
    c = Container(c)
    assert len(c) == 4
    assert list(c.items()) == [('a',1),('b',2),('c',3),('d',4)]

def test_ctor_seqoftuples():
    c = Container([('a',1),('b',2),('c',3),('d',4)])
    assert len(c) == 4
    assert list(c.items()) == [('a',1),('b',2),('c',3),('d',4)]

def test_ctor_orderedkw():
    c = Container(a=1, b=2, c=3, d=4)
    d = Container(a=1, b=2, c=3, d=4)
    assert c == d
    assert len(c) == len(d)
    assert list(c.items()) == list(d.items())

def test_keys():
    c = Container(a=1, b=2, c=3, d=4)
    assert list(c.keys()) == ["a","b","c","d"]

def test_values():
    c = Container(a=1, b=2, c=3, d=4)
    assert list(c.values()) == [1,2,3,4]

def test_items():
    c = Container(a=1, b=2, c=3, d=4)
    assert list(c.items()) == [("a",1),("b",2),("c",3),("d",4)]

def test_iter():
    c = Container(a=1, b=2, c=3, d=4)
    assert list(c) == list(c.keys())

def test_clear():
    c = Container(a=1, b=2, c=3, d=4)
    c.clear()
    assert c == Container()
    assert list(c.items()) == []

def test_pop():
    c = Container(a=1, b=2, c=3, d=4)
    assert c.pop("b") == 2
    assert c.pop("d") == 4
    assert c.pop("a") == 1
    assert c.pop("c") == 3
    assert raises(c.pop, "missing") == KeyError
    assert c == Container()

def test_popitem():
    c = Container(a=1, b=2, c=3, d=4)
    assert c.popitem() == ("d",4)
    assert c.popitem() == ("c",3)
    assert c.popitem() == ("b",2)
    assert c.popitem() == ("a",1)
    assert raises(c.popitem) == KeyError

def test_update_dict():
    c = Container(a=1, b=2, c=3, d=4)
    d = Container()
    d.update(c)
    assert d.a == 1
    assert d.b == 2
    assert d.c == 3
    assert d.d == 4
    assert c == d
    assert list(c.items()) == list(d.items())

def test_update_seqoftuples():
    c = Container(a=1, b=2, c=3, d=4)
    d = Container()
    d.update([("a",1),("b",2),("c",3),("d",4)])
    assert d.a == 1
    assert d.b == 2
    assert d.c == 3
    assert d.d == 4
    assert c == d
    assert list(c.items()) == list(d.items())

def test_copy_method():
    c = Container(a=1)
    d = c.copy()
    assert c == d
    assert c is not d

def test_copy():
    from copy import copy, deepcopy

    c = Container(a=1)
    d = copy(c)
    assert c == d
    assert c is not d

def test_deepcopy():
    from copy import copy, deepcopy

    c = Container(a=1)
    d = deepcopy(c)
    d.a = 2
    assert c != d
    assert c is not d

def test_pickling():
    import pickle

    empty = Container()
    empty_unpickled = pickle.loads(pickle.dumps(empty))
    assert empty_unpickled == empty

    nested = Container(a=1,b=Container(),c=3,d=Container(e=4))
    nested_unpickled = pickle.loads(pickle.dumps(nested))
    assert nested_unpickled == nested

def test_eq_issue_818():
    c = Container(a=1, b=2, c=3, d=4, e=5)
    d = Container(a=1, b=2, c=3, d=4, e=5)
    assert c == c
    assert d == d
    assert c == d
    assert d == c

    a = Container(a=1,b=2)
    b = Container(a=1,b=2,c=3)
    assert not a == b
    assert not b == a

    # c contains internal '_io' field, which shouldn't be considered in the comparison
    c = Struct('a' / Int8ul).parse(b'\x01')
    d = {'a': 1}
    assert c == d
    assert d == c

def test_eq_numpy():
    import numpy
    c = Container(arr=numpy.zeros(10, dtype=numpy.uint8))
    d = Container(arr=numpy.zeros(10, dtype=numpy.uint8))
    assert c == d

def test_ne_issue_818():
    c = Container(a=1, b=2, c=3)
    d = Container(a=1, b=2, c=3, d=4, e=5)
    assert c != d
    assert d != c

def test_str_repr_empty():
    c = Container()
    assert str(c) == "Container: "
    assert repr(c) == "Container()"
    assert eval(repr(c)) == c

def test_str_repr():
    c = Container(a=1, b=2, c=3)
    assert str(c) == "Container: \n    a = 1\n    b = 2\n    c = 3"
    assert repr(c) == "Container(a=1, b=2, c=3)"
    assert eval(repr(c)) == c

def test_str_repr_nested():
    c = Container(a=1,b=2,c=Container())
    assert str(c) == "Container: \n    a = 1\n    b = 2\n    c = Container: "
    assert repr(c) == "Container(a=1, b=2, c=Container())"
    assert eval(repr(c)) == c

def test_str_repr_recursive():
    c = Container(a=1,b=2)
    c.c = c
    assert str(c) == "Container: \n    a = 1\n    b = 2\n    c = <recursion detected>"
    assert repr(c) == "Container(a=1, b=2, c=<recursion detected>)"

def test_fullstrings():
    setGlobalPrintFullStrings(True)
    c = Container(data=b"1234567890")
    assert str(c) == "Container: \n    data = b'1234567890' (total 10)"
    assert repr(c) == "Container(data=b'1234567890')"
    c = Container(data=u"1234567890")
    assert str(c) == "Container: \n    data = u'1234567890' (total 10)"
    assert repr(c) == "Container(data=u'1234567890')"
    c = Container(data=b"1234567890123456789012345678901234567890")
    assert str(c) == "Container: \n    data = b'1234567890123456789012345678901234567890' (total 40)"
    assert repr(c) == "Container(data=b'1234567890123456789012345678901234567890')"
    c = Container(data=u"1234567890123456789012345678901234567890")
    assert str(c) == "Container: \n    data = u'1234567890123456789012345678901234567890' (total 40)"
    assert repr(c) == "Container(data=u'1234567890123456789012345678901234567890')"

    setGlobalPrintFullStrings(False)
    c = Container(data=b"1234567890")
    assert str(c) == "Container: \n    data = b'1234567890' (total 10)"
    assert repr(c) == "Container(data=b'1234567890')"
    c = Container(data=u"1234567890")
    assert str(c) == "Container: \n    data = u'1234567890' (total 10)"
    assert repr(c) == "Container(data=u'1234567890')"
    c = Container(data=b"1234567890123456789012345678901234567890")
    assert str(c) == "Container: \n    data = b'1234567890123456'... (truncated, total 40)"
    assert repr(c) == "Container(data=b'1234567890123456789012345678901234567890')"
    c = Container(data=u"1234567890123456789012345678901234567890")
    assert str(c) == "Container: \n    data = u'12345678901234567890123456789012'... (truncated, total 40)"
    assert repr(c) == "Container(data=u'1234567890123456789012345678901234567890')"

    setGlobalPrintFullStrings()

def test_falseflags():
    d = FlagsEnum(Byte, set=1, unset=2)
    c = d.parse(b"\x01")

    setGlobalPrintFalseFlags(True)
    assert str(c) == "Container: \n    set = True\n    unset = False"
    assert repr(c) == "Container(set=True, unset=False)"

    setGlobalPrintFalseFlags(False)
    assert str(c) == "Container: \n    set = True"
    assert repr(c) == "Container(set=True, unset=False)"

    setGlobalPrintFalseFlags()

def test_privateentries():
    c = Container(_private = 1)

    setGlobalPrintPrivateEntries(True)
    assert str(c) == "Container: \n    _private = 1"
    assert repr(c) == "Container()"

    setGlobalPrintPrivateEntries(False)
    assert str(c) == "Container: "
    assert repr(c) == "Container()"

    setGlobalPrintPrivateEntries()

def test_len_bool():
    c = Container(a=1, b=2, c=3, d=4)
    assert len(c) == 4
    assert c
    c = Container()
    assert len(c) == 0
    assert not c

def test_in():
    c = Container(a=1)
    assert "a" in c
    assert "b" not in c

def test_regression_recursionlock():
    print("REGRESSION: recursion_lock() used to leave private keys.")
    c = Container()
    str(c); repr(c)
    assert not c