rich/tests/test_spinner.py

75 lines
1.8 KiB
Python
Raw Normal View History

2020-12-10 17:07:11 +00:00
import pytest
from rich.console import Console
from rich.measure import Measurement
2021-03-29 17:10:43 +00:00
from rich.rule import Rule
2020-12-10 17:07:11 +00:00
from rich.spinner import Spinner
2021-05-19 15:50:09 +00:00
from rich.text import Text
2020-12-10 17:07:11 +00:00
def test_spinner_create():
Spinner("dots")
2020-12-10 17:07:11 +00:00
with pytest.raises(KeyError):
Spinner("foobar")
def test_spinner_render():
time = 0.0
def get_time():
nonlocal time
return time
console = Console(
width=80, color_system=None, force_terminal=True, get_time=get_time
)
console.begin_capture()
spinner = Spinner("dots", "Foo")
console.print(spinner)
time += 80 / 1000
console.print(spinner)
result = console.end_capture()
print(repr(result))
expected = "⠋ Foo\n⠙ Foo\n"
assert result == expected
def test_spinner_update():
time = 0.0
def get_time():
nonlocal time
return time
2021-03-29 17:10:43 +00:00
console = Console(width=20, force_terminal=True, get_time=get_time)
console.begin_capture()
2021-03-29 16:51:34 +00:00
spinner = Spinner("dots")
console.print(spinner)
2021-03-29 16:51:34 +00:00
spinner.update(text="Bar", style="green", speed=2)
time += 80 / 1000
console.print(spinner)
2021-03-29 16:51:34 +00:00
2021-03-29 17:10:43 +00:00
spinner.update(text=Rule("Bar"))
time += 80 / 1000
console.print(spinner)
2021-03-29 16:51:34 +00:00
result = console.end_capture()
print(repr(result))
2021-03-29 17:10:43 +00:00
expected = f"\n\x1b[32m⠙\x1b[0m Bar\n\x1b[32m⠸\x1b[0m \x1b[92m────── \x1b[0mBar\x1b[92m ───────\x1b[0m\n"
assert result == expected
2020-12-10 17:07:11 +00:00
def test_rich_measure():
console = Console(width=80, color_system=None, force_terminal=True)
spinner = Spinner("dots", "Foo")
2021-03-25 21:08:36 +00:00
min_width, max_width = Measurement.get(console, console.options, spinner)
2020-12-10 17:07:11 +00:00
assert min_width == 3
assert max_width == 5
2021-05-19 15:50:09 +00:00
def test_spinner_markup():
spinner = Spinner("dots", "[bold]spinning[/bold]")
assert isinstance(spinner.text, Text)
2021-05-19 16:02:53 +00:00
assert str(spinner.text) == "spinning"