Debian Codegraph @

tests/test_frozenlist.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # FIXME:
2 # mypy: disable-error-code="misc"
3
4 from collections.abc import MutableSequence
5 from copy import deepcopy
6
7 import pytest
8
9 from frozenlist import FrozenList, PyFrozenList
10
11
12 class FrozenListMixin:
13 FrozenList = NotImplemented
14
15 SKIP_METHODS = {
16 "__abstractmethods__",
17 "__slots__",
18 "__static_attributes__",
19 "__firstlineno__",
20 }
21
22 def test___class_getitem__(self) -> None:
23 assert self.FrozenList[str] is not None
24
25 def test_subclass(self) -> None:
26 assert issubclass(self.FrozenList, MutableSequence)
27
28 def test_iface(self) -> None:
29 for name in set(dir(MutableSequence)) - self.SKIP_METHODS:
30 if name.startswith("_") and not name.endswith("_"):
31 continue
32 assert hasattr(self.FrozenList, name)
33
34 def test_ctor_default(self) -> None:
35 _list = self.FrozenList([])
36 assert not _list.frozen
37
38 def test_ctor(self) -> None:
39 _list = self.FrozenList([1])
40 assert not _list.frozen
41
42 def test_ctor_copy_list(self) -> None:
43 orig = [1]
44 _list = self.FrozenList(orig)
45 del _list[0]
46 assert _list != orig
47
48 def test_freeze(self) -> None:
49 _list = self.FrozenList()
50 _list.freeze()
51 assert _list.frozen
52
53 def test_repr(self) -> None:
54 _list = self.FrozenList([1])
55 assert repr(_list) == "<FrozenList(frozen=False, [1])>"
56 _list.freeze()
57 assert repr(_list) == "<FrozenList(frozen=True, [1])>"
58
59 def test_getitem(self) -> None:
60 _list = self.FrozenList([1, 2])
61 assert _list[1] == 2
62
63 def test_setitem(self) -> None:
64 _list = self.FrozenList([1, 2])
65 _list[1] = 3
66 assert _list[1] == 3
67
68 def test_delitem(self) -> None:
69 _list = self.FrozenList([1, 2])
70 del _list[0]
71 assert len(_list) == 1
72 assert _list[0] == 2
73
74 def test_len(self) -> None:
75 _list = self.FrozenList([1])
76 assert len(_list) == 1
77
78 def test_iter(self) -> None:
79 _list = self.FrozenList([1, 2])
80 assert list(iter(_list)) == [1, 2]
81
82 def test_reversed(self) -> None:
83 _list = self.FrozenList([1, 2])
84 assert list(reversed(_list)) == [2, 1]
85
86 def test_eq(self) -> None:
87 _list = self.FrozenList([1])
88 assert _list == [1]
89
90 def test_ne(self) -> None:
91 _list = self.FrozenList([1])
92 assert _list != [2]
93
94 def test_le(self) -> None:
95 _list = self.FrozenList([1])
96 assert _list <= [1]
97
98 def test_lt(self) -> None:
99 _list = self.FrozenList([1])
100 assert _list < [3]
101
102 def test_ge(self) -> None:
103 _list = self.FrozenList([1])
104 assert _list >= [1]
105
106 def test_gt(self) -> None:
107 _list = self.FrozenList([2])
108 assert _list > [1]
109
110 def test_insert(self) -> None:
111 _list = self.FrozenList([2])
112 _list.insert(0, 1)
113 assert _list == [1, 2]
114
115 def test_frozen_setitem(self) -> None:
116 _list = self.FrozenList([1])
117 _list.freeze()
118 with pytest.raises(RuntimeError):
119 _list[0] = 2
120
121 def test_frozen_delitem(self) -> None:
122 _list = self.FrozenList([1])
123 _list.freeze()
124 with pytest.raises(RuntimeError):
125 del _list[0]
126
127 def test_frozen_insert(self) -> None:
128 _list = self.FrozenList([1])
129 _list.freeze()
130 with pytest.raises(RuntimeError):
131 _list.insert(0, 2)
132
133 def test_contains(self) -> None:
134 _list = self.FrozenList([2])
135 assert 2 in _list
136
137 def test_iadd(self) -> None:
138 _list = self.FrozenList([1])
139 _list += [2]
140 assert _list == [1, 2]
141
142 def test_iadd_frozen(self) -> None:
143 _list = self.FrozenList([1])
144 _list.freeze()
145 with pytest.raises(RuntimeError):
146 _list += [2]
147 assert _list == [1]
148
149 def test_index(self) -> None:
150 _list = self.FrozenList([1])
151 assert _list.index(1) == 0
152
153 def test_remove(self) -> None:
154 _list = self.FrozenList([1])
155 _list.remove(1)
156 assert len(_list) == 0
157
158 def test_remove_frozen(self) -> None:
159 _list = self.FrozenList([1])
160 _list.freeze()
161 with pytest.raises(RuntimeError):
162 _list.remove(1)
163 assert _list == [1]
164
165 def test_clear(self) -> None:
166 _list = self.FrozenList([1])
167 _list.clear()
168 assert len(_list) == 0
169
170 def test_clear_frozen(self) -> None:
171 _list = self.FrozenList([1])
172 _list.freeze()
173 with pytest.raises(RuntimeError):
174 _list.clear()
175 assert _list == [1]
176
177 def test_extend(self) -> None:
178 _list = self.FrozenList([1])
179 _list.extend([2])
180 assert _list == [1, 2]
181
182 def test_extend_frozen(self) -> None:
183 _list = self.FrozenList([1])
184 _list.freeze()
185 with pytest.raises(RuntimeError):
186 _list.extend([2])
187 assert _list == [1]
188
189 def test_reverse(self) -> None:
190 _list = self.FrozenList([1, 2])
191 _list.reverse()
192 assert _list == [2, 1]
193
194 def test_reverse_frozen(self) -> None:
195 _list = self.FrozenList([1, 2])
196 _list.freeze()
197 with pytest.raises(RuntimeError):
198 _list.reverse()
199 assert _list == [1, 2]
200
201 def test_pop(self) -> None:
202 _list = self.FrozenList([1, 2])
203 assert _list.pop(0) == 1
204 assert _list == [2]
205
206 def test_pop_default(self) -> None:
207 _list = self.FrozenList([1, 2])
208 assert _list.pop() == 2
209 assert _list == [1]
210
211 def test_pop_frozen(self) -> None:
212 _list = self.FrozenList([1, 2])
213 _list.freeze()
214 with pytest.raises(RuntimeError):
215 _list.pop()
216 assert _list == [1, 2]
217
218 def test_append(self) -> None:
219 _list = self.FrozenList([1, 2])
220 _list.append(3)
221 assert _list == [1, 2, 3]
222
223 def test_append_frozen(self) -> None:
224 _list = self.FrozenList([1, 2])
225 _list.freeze()
226 with pytest.raises(RuntimeError):
227 _list.append(3)
228 assert _list == [1, 2]
229
230 def test_hash(self) -> None:
231 _list = self.FrozenList([1, 2])
232 with pytest.raises(RuntimeError):
233 hash(_list)
234
235 def test_hash_frozen(self) -> None:
236 _list = self.FrozenList([1, 2])
237 _list.freeze()
238 h = hash(_list)
239 assert h == hash((1, 2))
240
241 def test_dict_key(self) -> None:
242 _list = self.FrozenList([1, 2])
243 with pytest.raises(RuntimeError):
244 {_list: "hello"}
245 _list.freeze()
246 {_list: "hello"}
247
248 def test_count(self) -> None:
249 _list = self.FrozenList([1, 2])
250 assert _list.count(1) == 1
251
252 def test_deepcopy_unfrozen(self) -> None:
253 orig = self.FrozenList([1, 2, 3])
254 copied = deepcopy(orig)
255 assert copied == orig
256 assert copied is not orig
257 assert list(copied) == list(orig)
258 assert not copied.frozen
259 # Verify the copy is mutable
260 copied.append(4)
261 assert len(copied) == 4
262 assert len(orig) == 3
263
264 def test_deepcopy_frozen(self) -> None:
265 orig = self.FrozenList([1, 2, 3])
266 orig.freeze()
267 copied = deepcopy(orig)
268 assert copied == orig
269 assert copied is not orig
270 assert list(copied) == list(orig)
271 assert copied.frozen
272 # Verify the copy is also frozen
273 with pytest.raises(RuntimeError):
274 copied.append(4)
275
276 def test_deepcopy_nested(self) -> None:
277 inner = self.FrozenList([1, 2])
278 orig = self.FrozenList([inner, 3])
279 copied = deepcopy(orig)
280 assert copied == orig
281 assert copied[0] is not orig[0]
282 assert isinstance(copied[0], self.FrozenList)
283 # Modify the inner list in the copy
284 copied[0].append(3)
285 assert len(copied[0]) == 3
286 assert len(orig[0]) == 2
287
288 def test_deepcopy_circular(self) -> None:
289 orig = self.FrozenList([1, 2])
290 orig.append(orig) # Create circular reference
291
292 copied = deepcopy(orig)
293
294 # Check structure is preserved
295 assert len(copied) == 3
296 assert copied[0] == 1
297 assert copied[1] == 2
298 assert copied[2] is copied # Circular reference preserved
299
300 # Verify they are different objects
301 assert copied is not orig
302 assert copied[2] is not orig
303
304 # Modify the copy
305 copied.append(3)
306 assert len(copied) == 4
307 assert len(orig) == 3
308
309 def test_deepcopy_circular_frozen(self) -> None:
310 orig = self.FrozenList([1, 2])
311 orig.append(orig) # Create circular reference
312 orig.freeze()
313
314 copied = deepcopy(orig)
315
316 # Check structure is preserved
317 assert len(copied) == 3
318 assert copied[0] == 1
319 assert copied[1] == 2
320 assert copied[2] is copied # Circular reference preserved
321 assert copied.frozen
322
323 # Verify frozen state
324 with pytest.raises(RuntimeError):
325 copied.append(3)
326
327 def test_deepcopy_nested_circular(self) -> None:
328 # Create a complex nested structure with circular references
329 inner1 = self.FrozenList([1, 2])
330 inner2 = self.FrozenList([3, 4])
331 orig = self.FrozenList([inner1, inner2])
332
333 # Add circular references
334 inner1.append(inner2) # inner1 -> inner2
335 inner2.append(orig) # inner2 -> orig (outer list)
336 orig.append(orig) # orig -> orig (self reference)
337
338 copied = deepcopy(orig)
339
340 # Verify structure
341 assert len(copied) == 3
342 assert isinstance(copied[0], self.FrozenList)
343 assert isinstance(copied[1], self.FrozenList)
344 assert copied[2] is copied # Self reference preserved
345
346 # Verify nested circular references
347 assert len(copied[0]) == 3
348 assert copied[0][2] is copied[1] # inner1 -> inner2 preserved
349 assert len(copied[1]) == 3
350 assert copied[1][2] is copied # inner2 -> orig preserved
351
352 # All objects should be new instances
353 assert copied is not orig
354 assert copied[0] is not orig[0]
355 assert copied[1] is not orig[1]
356
357 def test_deepcopy_multiple_references(self) -> None:
358 # Test that multiple references to the same object are preserved
359 shared = self.FrozenList([1, 2])
360 orig = self.FrozenList([shared, shared, 3])
361
362 copied = deepcopy(orig)
363
364 # Both references should point to the same copied object
365 assert copied[0] is copied[1]
366 assert copied[0] is not shared
367 assert isinstance(copied[0], self.FrozenList)
368
369 # Modify through one reference
370 copied[0].append(3)
371 assert len(copied[0]) == 3
372 assert len(copied[1]) == 3 # Should see the change
373 assert len(shared) == 2 # Original unchanged
374
375
376 class TestFrozenList(FrozenListMixin):
377 FrozenList = FrozenList # type: ignore[assignment] # FIXME
378
379
380 class TestFrozenListPy(FrozenListMixin):
381 FrozenList = PyFrozenList # type: ignore[assignment] # FIXME