test_frozenlist.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # FIXME:
  2. # mypy: disable-error-code="misc"
  3. from collections.abc import MutableSequence
  4. import pytest
  5. from frozenlist import FrozenList, PyFrozenList
  6. class FrozenListMixin:
  7. FrozenList = NotImplemented
  8. SKIP_METHODS = {"__abstractmethods__", "__slots__"}
  9. def test_subclass(self) -> None:
  10. assert issubclass(self.FrozenList, MutableSequence)
  11. def test_iface(self) -> None:
  12. for name in set(dir(MutableSequence)) - self.SKIP_METHODS:
  13. if name.startswith("_") and not name.endswith("_"):
  14. continue
  15. assert hasattr(self.FrozenList, name)
  16. def test_ctor_default(self) -> None:
  17. _list = self.FrozenList([])
  18. assert not _list.frozen
  19. def test_ctor(self) -> None:
  20. _list = self.FrozenList([1])
  21. assert not _list.frozen
  22. def test_ctor_copy_list(self) -> None:
  23. orig = [1]
  24. _list = self.FrozenList(orig)
  25. del _list[0]
  26. assert _list != orig
  27. def test_freeze(self) -> None:
  28. _list = self.FrozenList()
  29. _list.freeze()
  30. assert _list.frozen
  31. def test_repr(self) -> None:
  32. _list = self.FrozenList([1])
  33. assert repr(_list) == "<FrozenList(frozen=False, [1])>"
  34. _list.freeze()
  35. assert repr(_list) == "<FrozenList(frozen=True, [1])>"
  36. def test_getitem(self) -> None:
  37. _list = self.FrozenList([1, 2])
  38. assert _list[1] == 2
  39. def test_setitem(self) -> None:
  40. _list = self.FrozenList([1, 2])
  41. _list[1] = 3
  42. assert _list[1] == 3
  43. def test_delitem(self) -> None:
  44. _list = self.FrozenList([1, 2])
  45. del _list[0]
  46. assert len(_list) == 1
  47. assert _list[0] == 2
  48. def test_len(self) -> None:
  49. _list = self.FrozenList([1])
  50. assert len(_list) == 1
  51. def test_iter(self) -> None:
  52. _list = self.FrozenList([1, 2])
  53. assert list(iter(_list)) == [1, 2]
  54. def test_reversed(self) -> None:
  55. _list = self.FrozenList([1, 2])
  56. assert list(reversed(_list)) == [2, 1]
  57. def test_eq(self) -> None:
  58. _list = self.FrozenList([1])
  59. assert _list == [1]
  60. def test_ne(self) -> None:
  61. _list = self.FrozenList([1])
  62. assert _list != [2]
  63. def test_le(self) -> None:
  64. _list = self.FrozenList([1])
  65. assert _list <= [1]
  66. def test_lt(self) -> None:
  67. _list = self.FrozenList([1])
  68. assert _list < [3]
  69. def test_ge(self) -> None:
  70. _list = self.FrozenList([1])
  71. assert _list >= [1]
  72. def test_gt(self) -> None:
  73. _list = self.FrozenList([2])
  74. assert _list > [1]
  75. def test_insert(self) -> None:
  76. _list = self.FrozenList([2])
  77. _list.insert(0, 1)
  78. assert _list == [1, 2]
  79. def test_frozen_setitem(self) -> None:
  80. _list = self.FrozenList([1])
  81. _list.freeze()
  82. with pytest.raises(RuntimeError):
  83. _list[0] = 2
  84. def test_frozen_delitem(self) -> None:
  85. _list = self.FrozenList([1])
  86. _list.freeze()
  87. with pytest.raises(RuntimeError):
  88. del _list[0]
  89. def test_frozen_insert(self) -> None:
  90. _list = self.FrozenList([1])
  91. _list.freeze()
  92. with pytest.raises(RuntimeError):
  93. _list.insert(0, 2)
  94. def test_contains(self) -> None:
  95. _list = self.FrozenList([2])
  96. assert 2 in _list
  97. def test_iadd(self) -> None:
  98. _list = self.FrozenList([1])
  99. _list += [2]
  100. assert _list == [1, 2]
  101. def test_iadd_frozen(self) -> None:
  102. _list = self.FrozenList([1])
  103. _list.freeze()
  104. with pytest.raises(RuntimeError):
  105. _list += [2]
  106. assert _list == [1]
  107. def test_index(self) -> None:
  108. _list = self.FrozenList([1])
  109. assert _list.index(1) == 0
  110. def test_remove(self) -> None:
  111. _list = self.FrozenList([1])
  112. _list.remove(1)
  113. assert len(_list) == 0
  114. def test_remove_frozen(self) -> None:
  115. _list = self.FrozenList([1])
  116. _list.freeze()
  117. with pytest.raises(RuntimeError):
  118. _list.remove(1)
  119. assert _list == [1]
  120. def test_clear(self) -> None:
  121. _list = self.FrozenList([1])
  122. _list.clear()
  123. assert len(_list) == 0
  124. def test_clear_frozen(self) -> None:
  125. _list = self.FrozenList([1])
  126. _list.freeze()
  127. with pytest.raises(RuntimeError):
  128. _list.clear()
  129. assert _list == [1]
  130. def test_extend(self) -> None:
  131. _list = self.FrozenList([1])
  132. _list.extend([2])
  133. assert _list == [1, 2]
  134. def test_extend_frozen(self) -> None:
  135. _list = self.FrozenList([1])
  136. _list.freeze()
  137. with pytest.raises(RuntimeError):
  138. _list.extend([2])
  139. assert _list == [1]
  140. def test_reverse(self) -> None:
  141. _list = self.FrozenList([1, 2])
  142. _list.reverse()
  143. assert _list == [2, 1]
  144. def test_reverse_frozen(self) -> None:
  145. _list = self.FrozenList([1, 2])
  146. _list.freeze()
  147. with pytest.raises(RuntimeError):
  148. _list.reverse()
  149. assert _list == [1, 2]
  150. def test_pop(self) -> None:
  151. _list = self.FrozenList([1, 2])
  152. assert _list.pop(0) == 1
  153. assert _list == [2]
  154. def test_pop_default(self) -> None:
  155. _list = self.FrozenList([1, 2])
  156. assert _list.pop() == 2
  157. assert _list == [1]
  158. def test_pop_frozen(self) -> None:
  159. _list = self.FrozenList([1, 2])
  160. _list.freeze()
  161. with pytest.raises(RuntimeError):
  162. _list.pop()
  163. assert _list == [1, 2]
  164. def test_append(self) -> None:
  165. _list = self.FrozenList([1, 2])
  166. _list.append(3)
  167. assert _list == [1, 2, 3]
  168. def test_append_frozen(self) -> None:
  169. _list = self.FrozenList([1, 2])
  170. _list.freeze()
  171. with pytest.raises(RuntimeError):
  172. _list.append(3)
  173. assert _list == [1, 2]
  174. def test_hash(self) -> None:
  175. _list = self.FrozenList([1, 2])
  176. with pytest.raises(RuntimeError):
  177. hash(_list)
  178. def test_hash_frozen(self) -> None:
  179. _list = self.FrozenList([1, 2])
  180. _list.freeze()
  181. h = hash(_list)
  182. assert h == hash((1, 2))
  183. def test_dict_key(self) -> None:
  184. _list = self.FrozenList([1, 2])
  185. with pytest.raises(RuntimeError):
  186. {_list: "hello"}
  187. _list.freeze()
  188. {_list: "hello"}
  189. def test_count(self) -> None:
  190. _list = self.FrozenList([1, 2])
  191. assert _list.count(1) == 1
  192. class TestFrozenList(FrozenListMixin):
  193. FrozenList = FrozenList # type: ignore[assignment] # FIXME
  194. class TestFrozenListPy(FrozenListMixin):
  195. FrozenList = PyFrozenList # type: ignore[assignment] # FIXME