rich/tests/test_block_bar.py

56 lines
1.6 KiB
Python
Raw Normal View History

2020-10-16 09:56:08 +00:00
from rich.bar import Bar
2021-03-25 21:08:36 +00:00
from rich.console import Console
2020-10-08 03:16:05 +00:00
from .render import render
expected = [
2020-10-16 09:56:08 +00:00
"\x1b[39;49m ▐█████████████████████████ \x1b[0m\n",
"\x1b[39;49m ██████████████████████▌ \x1b[0m\n",
"\x1b[39;49m \x1b[0m\n",
2020-10-08 03:16:05 +00:00
]
2020-10-10 05:09:17 +00:00
def test_repr():
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=11, end=62, width=50)
assert repr(bar) == "Bar(100, 11, 62)"
2020-10-10 05:09:17 +00:00
2020-10-08 03:16:05 +00:00
def test_render():
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=11, end=62, width=50)
2020-10-08 03:16:05 +00:00
bar_render = render(bar)
assert bar_render == expected[0]
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=12, end=57, width=50)
2020-10-08 03:16:05 +00:00
bar_render = render(bar)
assert bar_render == expected[1]
2020-10-09 04:18:40 +00:00
# begin after end
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=60, end=40, width=50)
2020-10-09 04:18:40 +00:00
bar_render = render(bar)
assert bar_render == expected[2]
2020-10-08 03:16:05 +00:00
def test_measure():
2021-03-25 21:08:36 +00:00
console = Console(width=120)
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=11, end=62)
2021-03-25 21:08:36 +00:00
measurement = bar.__rich_measure__(console, console.options)
2020-10-08 03:16:05 +00:00
assert measurement.minimum == 4
assert measurement.maximum == 120
def test_zero_total():
# Shouldn't throw zero division error
2020-10-16 09:56:08 +00:00
bar = Bar(size=0, begin=0, end=0)
2020-10-08 03:16:05 +00:00
render(bar)
if __name__ == "__main__":
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=11, end=62, width=50)
2020-10-08 03:16:05 +00:00
bar_render = render(bar)
print(repr(bar_render))
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=12, end=57, width=50)
2020-10-08 03:16:05 +00:00
bar_render = render(bar)
print(repr(bar_render))
2020-10-16 09:56:08 +00:00
bar = Bar(size=100, begin=60, end=40, width=50)
2020-10-08 03:16:05 +00:00
bar_render = render(bar)
print(repr(bar_render))