123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- # FIXME:
- # mypy: disable-error-code="misc"
- from collections.abc import MutableSequence
- import pytest
- from frozenlist import FrozenList, PyFrozenList
- class FrozenListMixin:
- FrozenList = NotImplemented
- SKIP_METHODS = {"__abstractmethods__", "__slots__"}
- def test_subclass(self) -> None:
- assert issubclass(self.FrozenList, MutableSequence)
- def test_iface(self) -> None:
- for name in set(dir(MutableSequence)) - self.SKIP_METHODS:
- if name.startswith("_") and not name.endswith("_"):
- continue
- assert hasattr(self.FrozenList, name)
- def test_ctor_default(self) -> None:
- _list = self.FrozenList([])
- assert not _list.frozen
- def test_ctor(self) -> None:
- _list = self.FrozenList([1])
- assert not _list.frozen
- def test_ctor_copy_list(self) -> None:
- orig = [1]
- _list = self.FrozenList(orig)
- del _list[0]
- assert _list != orig
- def test_freeze(self) -> None:
- _list = self.FrozenList()
- _list.freeze()
- assert _list.frozen
- def test_repr(self) -> None:
- _list = self.FrozenList([1])
- assert repr(_list) == "<FrozenList(frozen=False, [1])>"
- _list.freeze()
- assert repr(_list) == "<FrozenList(frozen=True, [1])>"
- def test_getitem(self) -> None:
- _list = self.FrozenList([1, 2])
- assert _list[1] == 2
- def test_setitem(self) -> None:
- _list = self.FrozenList([1, 2])
- _list[1] = 3
- assert _list[1] == 3
- def test_delitem(self) -> None:
- _list = self.FrozenList([1, 2])
- del _list[0]
- assert len(_list) == 1
- assert _list[0] == 2
- def test_len(self) -> None:
- _list = self.FrozenList([1])
- assert len(_list) == 1
- def test_iter(self) -> None:
- _list = self.FrozenList([1, 2])
- assert list(iter(_list)) == [1, 2]
- def test_reversed(self) -> None:
- _list = self.FrozenList([1, 2])
- assert list(reversed(_list)) == [2, 1]
- def test_eq(self) -> None:
- _list = self.FrozenList([1])
- assert _list == [1]
- def test_ne(self) -> None:
- _list = self.FrozenList([1])
- assert _list != [2]
- def test_le(self) -> None:
- _list = self.FrozenList([1])
- assert _list <= [1]
- def test_lt(self) -> None:
- _list = self.FrozenList([1])
- assert _list < [3]
- def test_ge(self) -> None:
- _list = self.FrozenList([1])
- assert _list >= [1]
- def test_gt(self) -> None:
- _list = self.FrozenList([2])
- assert _list > [1]
- def test_insert(self) -> None:
- _list = self.FrozenList([2])
- _list.insert(0, 1)
- assert _list == [1, 2]
- def test_frozen_setitem(self) -> None:
- _list = self.FrozenList([1])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list[0] = 2
- def test_frozen_delitem(self) -> None:
- _list = self.FrozenList([1])
- _list.freeze()
- with pytest.raises(RuntimeError):
- del _list[0]
- def test_frozen_insert(self) -> None:
- _list = self.FrozenList([1])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list.insert(0, 2)
- def test_contains(self) -> None:
- _list = self.FrozenList([2])
- assert 2 in _list
- def test_iadd(self) -> None:
- _list = self.FrozenList([1])
- _list += [2]
- assert _list == [1, 2]
- def test_iadd_frozen(self) -> None:
- _list = self.FrozenList([1])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list += [2]
- assert _list == [1]
- def test_index(self) -> None:
- _list = self.FrozenList([1])
- assert _list.index(1) == 0
- def test_remove(self) -> None:
- _list = self.FrozenList([1])
- _list.remove(1)
- assert len(_list) == 0
- def test_remove_frozen(self) -> None:
- _list = self.FrozenList([1])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list.remove(1)
- assert _list == [1]
- def test_clear(self) -> None:
- _list = self.FrozenList([1])
- _list.clear()
- assert len(_list) == 0
- def test_clear_frozen(self) -> None:
- _list = self.FrozenList([1])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list.clear()
- assert _list == [1]
- def test_extend(self) -> None:
- _list = self.FrozenList([1])
- _list.extend([2])
- assert _list == [1, 2]
- def test_extend_frozen(self) -> None:
- _list = self.FrozenList([1])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list.extend([2])
- assert _list == [1]
- def test_reverse(self) -> None:
- _list = self.FrozenList([1, 2])
- _list.reverse()
- assert _list == [2, 1]
- def test_reverse_frozen(self) -> None:
- _list = self.FrozenList([1, 2])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list.reverse()
- assert _list == [1, 2]
- def test_pop(self) -> None:
- _list = self.FrozenList([1, 2])
- assert _list.pop(0) == 1
- assert _list == [2]
- def test_pop_default(self) -> None:
- _list = self.FrozenList([1, 2])
- assert _list.pop() == 2
- assert _list == [1]
- def test_pop_frozen(self) -> None:
- _list = self.FrozenList([1, 2])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list.pop()
- assert _list == [1, 2]
- def test_append(self) -> None:
- _list = self.FrozenList([1, 2])
- _list.append(3)
- assert _list == [1, 2, 3]
- def test_append_frozen(self) -> None:
- _list = self.FrozenList([1, 2])
- _list.freeze()
- with pytest.raises(RuntimeError):
- _list.append(3)
- assert _list == [1, 2]
- def test_hash(self) -> None:
- _list = self.FrozenList([1, 2])
- with pytest.raises(RuntimeError):
- hash(_list)
- def test_hash_frozen(self) -> None:
- _list = self.FrozenList([1, 2])
- _list.freeze()
- h = hash(_list)
- assert h == hash((1, 2))
- def test_dict_key(self) -> None:
- _list = self.FrozenList([1, 2])
- with pytest.raises(RuntimeError):
- {_list: "hello"}
- _list.freeze()
- {_list: "hello"}
- def test_count(self) -> None:
- _list = self.FrozenList([1, 2])
- assert _list.count(1) == 1
- class TestFrozenList(FrozenListMixin):
- FrozenList = FrozenList # type: ignore[assignment] # FIXME
- class TestFrozenListPy(FrozenListMixin):
- FrozenList = PyFrozenList # type: ignore[assignment] # FIXME
|