1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
from textual._loop import loop_first, loop_first_last, loop_from_index, loop_last
def test_loop_first():
assert list(loop_first([])) == []
iterable = loop_first(["apples", "oranges", "pears", "lemons"])
assert next(iterable) == (True, "apples")
assert next(iterable) == (False, "oranges")
assert next(iterable) == (False, "pears")
assert next(iterable) == (False, "lemons")
def test_loop_last():
assert list(loop_last([])) == []
iterable = loop_last(["apples", "oranges", "pears", "lemons"])
assert next(iterable) == (False, "apples")
assert next(iterable) == (False, "oranges")
assert next(iterable) == (False, "pears")
assert next(iterable) == (True, "lemons")
def test_loop_first_last():
assert list(loop_first_last([])) == []
iterable = loop_first_last(["apples", "oranges", "pears", "lemons"])
assert next(iterable) == (True, False, "apples")
assert next(iterable) == (False, False, "oranges")
assert next(iterable) == (False, False, "pears")
assert next(iterable) == (False, True, "lemons")
def test_loop_from_index():
assert list(loop_from_index("abcdefghij", 3)) == [
(4, "e"),
(5, "f"),
(6, "g"),
(7, "h"),
(8, "i"),
(9, "j"),
(0, "a"),
(1, "b"),
(2, "c"),
(3, "d"),
]
assert list(loop_from_index("abcdefghij", 3, direction=-1)) == [
(2, "c"),
(1, "b"),
(0, "a"),
(9, "j"),
(8, "i"),
(7, "h"),
(6, "g"),
(5, "f"),
(4, "e"),
(3, "d"),
]
assert list(loop_from_index("abcdefghij", 3, wrap=False)) == [
(4, "e"),
(5, "f"),
(6, "g"),
(7, "h"),
(8, "i"),
(9, "j"),
]
|