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