rich/tests/test_tools.py

39 lines
1.4 KiB
Python
Raw Permalink Normal View History

2020-03-28 11:56:43 +00:00
from rich._loop import loop_first, loop_last, loop_first_last
2020-06-06 12:11:29 +00:00
from rich._ratio import ratio_distribute
2019-12-11 14:57:04 +00:00
2020-04-02 15:59:44 +00:00
def test_loop_first():
2020-03-28 11:56:43 +00:00
assert list(loop_first([])) == []
iterable = loop_first(["apples", "oranges", "pears", "lemons"])
2019-12-11 14:57:04 +00:00
assert next(iterable) == (True, "apples")
assert next(iterable) == (False, "oranges")
assert next(iterable) == (False, "pears")
assert next(iterable) == (False, "lemons")
2020-04-02 15:59:44 +00:00
def test_loop_last():
2020-03-28 11:56:43 +00:00
assert list(loop_last([])) == []
iterable = loop_last(["apples", "oranges", "pears", "lemons"])
2019-12-11 14:57:04 +00:00
assert next(iterable) == (False, "apples")
assert next(iterable) == (False, "oranges")
assert next(iterable) == (False, "pears")
assert next(iterable) == (True, "lemons")
2020-04-02 15:59:44 +00:00
def test_loop_first_last():
2020-03-28 11:56:43 +00:00
assert list(loop_first_last([])) == []
iterable = loop_first_last(["apples", "oranges", "pears", "lemons"])
2019-12-11 14:57:04 +00:00
assert next(iterable) == (True, False, "apples")
assert next(iterable) == (False, False, "oranges")
assert next(iterable) == (False, False, "pears")
assert next(iterable) == (False, True, "lemons")
2020-06-06 12:11:29 +00:00
def test_ratio_distribute():
assert ratio_distribute(10, [1]) == [10]
assert ratio_distribute(10, [1, 1]) == [5, 5]
assert ratio_distribute(12, [1, 3]) == [3, 9]
assert ratio_distribute(0, [1, 3]) == [0, 0]
assert ratio_distribute(0, [1, 3], [1, 1]) == [1, 1]
assert ratio_distribute(10, [1, 0]) == [10, 0]