diff --git a/src/qwlist/qwlist.py b/src/qwlist/qwlist.py index 93f9e87..9f72cd2 100644 --- a/src/qwlist/qwlist.py +++ b/src/qwlist/qwlist.py @@ -1,6 +1,5 @@ from typing import TypeVar, Generic, Iterable, Callable, overload, Optional, Iterator, Type, Tuple, List from collections import deque -from unittest.mock import right T = TypeVar('T') K = TypeVar('K') @@ -720,13 +719,10 @@ def split_when(self, pred: Callable[[T], bool]) -> Optional[Tuple["QList[T]", "L if the element is the split point. Returns: - A tuple where: - - The first element is a fully evaluated `QList` containing all elements up to and - including the split point. - - The second element is a lazily evaluated sequence of all elements after the split point. - - Returns `None` if `self` is empty. - - If no element satisfies the predicate, the left part contains all elements from `self` and - the right part is an empty lazy sequence. + A tuple where the first element is a fully evaluated `QList` containing all elements up to and + including the split point, and the second element is a lazily evaluated sequence of all + elements after the split point. Returns `None` if `self` is empty. If no element satisfies the + predicate, the left part contains all elements from `self` and the right part is an empty lazy sequence. """ left = QList() it = self.iter() diff --git a/tests/test_lazy.py b/tests/test_lazy.py index 9b36965..27996f0 100644 --- a/tests/test_lazy.py +++ b/tests/test_lazy.py @@ -1,10 +1,12 @@ -import sys; +import sys; sys.path.append('../src') +from src.qwlist.qwlist import QList, Lazy -from src.qwlist.eager import EagerQList -sys.path.append('../src') -from src.qwlist.qwlist import QList, Lazy -import pytest +def naturals(): + current = 0 + while True: + yield current + current += 1 def test_lazy_from_iterable_constructor(): @@ -524,4 +526,52 @@ def test_get(): assert Lazy([]).get(10, default=100) == 100 assert Lazy(range(1, 11)).get(0, default=100) == 1 assert Lazy(range(1, 11)).get(9, default=100) == 10 - assert Lazy(range(1, 11)).get(10, default=100) == 100 \ No newline at end of file + assert Lazy(range(1, 11)).get(10, default=100) == 100 + + +def test_uncons(): + assert Lazy([]).uncons() is None + + expected = (0, QList([1, 2, 3])) + head, tail = Lazy(range(4)).uncons() + res = (head, tail.collect()) + assert res == expected + + expected = (0, QList()) + head, tail = Lazy([0]).uncons() + res = (head, tail.collect()) + assert res == expected + + assert Lazy(range(4)).filter(lambda x: x > 5).uncons() is None + + nat = Lazy(naturals()) + head, tail = nat.uncons() + assert head == 0 + head, tail = tail.uncons() + assert head == 1 + head, tail = tail.uncons() + assert head == 2 + + +def test_split_when(): + assert Lazy([]).split_when(lambda x: True) is None + + expected = (QList([0]), QList([1, 2, 3])) + left, right = Lazy(range(4)).split_when(lambda x: True) + res = (left, right.collect()) + assert res == expected + + expected = (QList([0, 1, 2]), QList([3, 4, 5])) + left, right = Lazy(range(6)).split_when(lambda x: x == 2) + res = (left, right.collect()) + assert res == expected + + expected = (QList([0, 1, 2]), QList()) + left, right = Lazy(range(3)).split_when(lambda x: x == 2) + res = (left, right.collect()) + assert res == expected + + left, right = Lazy(naturals()).split_when(lambda x: x == 2) + assert left == QList([0, 1, 2]) + left, right = right.split_when(lambda x: x == 5) + assert left == QList([3, 4, 5]) \ No newline at end of file