reorg
This commit is contained in:
@@ -12,27 +12,30 @@ class HookedList(HookedContainer[T], MutableSequence[T]):
|
||||
_data: MutableSequence[T]
|
||||
path: MutableSequence[int]
|
||||
|
||||
def __init__(self, hook: HookFunction, existing: MutableSequence[T], path: MutableSequence[int] | None = None):
|
||||
def __init__(
|
||||
self,
|
||||
existing: MutableSequence[T],
|
||||
hook: HookFunction | None = None,
|
||||
path: MutableSequence[int] | None = None,
|
||||
) -> None:
|
||||
self.hook = hook
|
||||
match existing:
|
||||
case HookedContainer(_data=seq):
|
||||
self._data = seq
|
||||
case MutableSequence() as seq:
|
||||
self._data = seq
|
||||
case HookedContainer(_data=seq):
|
||||
self._data = seq
|
||||
case _ as it:
|
||||
self._data = list(it)
|
||||
self._path = list(path) if path is not None else []
|
||||
|
||||
def __getitem__(self, s):
|
||||
new_path = copy(self._path)
|
||||
new_path.append(s)
|
||||
match self._data[s]:
|
||||
case MutableSequence() as seq:
|
||||
new_path = copy(self._path)
|
||||
new_path.append(s)
|
||||
return type(self)(self.hook, existing=seq, path=new_path)
|
||||
return type(self)(seq, self.hook, path=new_path)
|
||||
case HookedContainer(_data=seq):
|
||||
new_path = copy(self._path)
|
||||
new_path.append(s)
|
||||
return type(self)(self.hook, existing=seq, path=new_path)
|
||||
return type(self)(seq, self.hook, path=new_path)
|
||||
case _ as item:
|
||||
return item
|
||||
|
||||
|
||||
@@ -12,48 +12,48 @@ def get_id(a: Any):
|
||||
class TestHookedList:
|
||||
class TestConstruction:
|
||||
def test_empty(self):
|
||||
lst = HookedList(None, existing=[])
|
||||
lst = HookedList(existing=[])
|
||||
assert list(lst) == []
|
||||
|
||||
def test_with_items(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
lst = HookedList(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)
|
||||
lst = HookedList(existing=initial)
|
||||
assert id(lst._data) == initial_id
|
||||
|
||||
lst2 = HookedList(None, existing=lst)
|
||||
lst2 = HookedList(existing=lst)
|
||||
assert id(lst2._data) == initial_id
|
||||
|
||||
class TestSeqOps:
|
||||
def test_len(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
lst = HookedList(existing=[1, 2, 3])
|
||||
assert len(lst) == 3
|
||||
|
||||
def test_getitem(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
lst = HookedList(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])
|
||||
lst = HookedList(existing=[1, 2, 3])
|
||||
assert 2 in lst
|
||||
assert 4 not in lst
|
||||
|
||||
def test_iter(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
lst = HookedList(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, [4, 5, [6, 7]]])
|
||||
lst = HookedList([1, 2, [4, 5, [6, 7]]], lambda e: added.append(e.item))
|
||||
lst[0] = 10
|
||||
lst.append(20)
|
||||
assert added == [10, 20]
|
||||
@@ -62,11 +62,11 @@ class TestHookedList:
|
||||
assert added == [10, 20, 8]
|
||||
|
||||
def test_delitem(self):
|
||||
lst = HookedList(None, existing=[1, 2, 3])
|
||||
lst = HookedList(existing=[1, 2, 3])
|
||||
del lst[1]
|
||||
assert list(lst) == [1, 3]
|
||||
|
||||
def test_insert(self):
|
||||
lst = HookedList(None, existing=[1, 3])
|
||||
lst = HookedList(existing=[1, 3])
|
||||
lst.insert(1, 2)
|
||||
assert list(lst) == [1, 2, 3]
|
||||
|
||||
Reference in New Issue
Block a user