summaryrefslogtreecommitdiffstats
path: root/tests/test_tools.py
blob: 2e6fef7f5a1bf5552dc63f7d5078b19a847419da (plain)
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
from rich._loop import loop_first, loop_last, loop_first_last
from rich._ratio import ratio_distribute


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_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]