started pytests
This commit is contained in:
70
tests/test_sequence.py
Normal file
70
tests/test_sequence.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from hooked_containers.sequence import HookedList
|
||||
|
||||
|
||||
def get_id(a: Any):
|
||||
return str(hex(id(a)))
|
||||
|
||||
|
||||
class TestHookedList:
|
||||
class TestConstruction:
|
||||
def test_empty(self):
|
||||
lst = HookedList(None, existing=[])
|
||||
assert list(lst) == []
|
||||
|
||||
def test_with_items(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
assert list(lst) == [1, 2, 3]
|
||||
|
||||
def test_recreation(self):
|
||||
initial = [1, 2, 3]
|
||||
initial_id = id(initial)
|
||||
|
||||
lst = HookedList(None, existing=initial)
|
||||
assert id(lst._data) == initial_id
|
||||
|
||||
lst2 = HookedList(None, existing=lst)
|
||||
assert id(lst2._data) == initial_id
|
||||
|
||||
class TestSeqOps:
|
||||
def test_len(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
assert len(lst) == 3
|
||||
|
||||
def test_getitem(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
assert lst[0] == 1
|
||||
assert lst[1] == 2
|
||||
assert lst[-1] == 3
|
||||
|
||||
def test_contains(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
assert 2 in lst
|
||||
assert 4 not in lst
|
||||
|
||||
def test_iter(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
for i, item in enumerate(lst, start=1):
|
||||
assert item == i
|
||||
|
||||
class TestMutableOps:
|
||||
def test_setitem(self):
|
||||
added = []
|
||||
lst = HookedList(lambda e: added.append(e.item), existing=[1, 2, 3])
|
||||
lst[0] = 10
|
||||
lst.append(20)
|
||||
assert list(lst) == [10, 2, 3, 20]
|
||||
assert added == [10, 20]
|
||||
|
||||
def test_delitem(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
del lst[1]
|
||||
assert list(lst) == [1, 3]
|
||||
|
||||
def test_insert(self):
|
||||
lst = HookedList(None, existing=[1, 3])
|
||||
lst.insert(1, 2)
|
||||
assert list(lst) == [1, 2, 3]
|
||||
Reference in New Issue
Block a user