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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
import pytest
from pytest import approx
from rich.console import Console
from rich.text import Text
from textual.app import App
from textual.color import Gradient
from textual.css.query import NoMatches
from textual.renderables.bar import _apply_gradient
from textual.widget import Widget
from textual.widgets import ProgressBar
def test_initial_status():
pb = ProgressBar()
assert pb.total is None
assert pb.progress == 0
assert pb.percentage is None
pb = ProgressBar(total=100)
assert pb.total == 100
assert pb.progress == 0
assert pb.percentage == 0
def test_advance():
pb = ProgressBar(total=100)
pb.advance(10)
assert pb.progress == 10
assert pb.percentage == approx(0.1)
pb.advance(42)
assert pb.progress == 52
assert pb.percentage == approx(0.52)
pb.advance(0.0625)
assert pb.progress == 52.0625
assert pb.percentage == approx(0.520625)
def test_advance_backwards():
pb = ProgressBar(total=100)
pb.progress = 50
pb.advance(-10)
assert pb.progress == 40
def test_progress_overflow():
pb = ProgressBar(total=100)
pb.advance(999_999)
assert pb.percentage == 1
pb.update(total=50)
assert pb.percentage == 1
def test_progress_underflow():
pb = ProgressBar(total=100)
pb.advance(-999_999)
assert pb.percentage == 0
def test_non_negative_total():
pb = ProgressBar(total=-100)
assert pb.total == 0
def test_update_total():
pb = ProgressBar()
pb.update(total=100)
assert pb.total == 100
pb.update(total=1000)
assert pb.total == 1000
pb.update(total=None)
assert pb.total is None
pb.update(total=100)
assert pb.total == 100
def test_update_progress():
pb = ProgressBar(total=100)
pb.update(progress=10)
assert pb.progress == 10
pb.update(progress=73)
assert pb.progress == 73
pb.update(progress=40)
assert pb.progress == 40
def test_update_advance():
pb = ProgressBar(total=100)
pb.update(advance=10)
assert pb.progress == 10
pb.update(advance=10)
assert pb.progress == 20
pb.update(advance=10)
assert pb.progress == 30
def test_update():
pb = ProgressBar()
pb.update(total=100, progress=30, advance=20)
assert pb.total == 100
assert pb.progress == 50
def test_go_back_to_indeterminate():
pb = ProgressBar()
pb.total = 100
assert pb.percentage == 0
pb.total = None
assert pb.percentage is None
@pytest.mark.parametrize(
["show_bar", "show_percentage", "show_eta"],
[
(True, True, True),
(True, True, False),
(True, False, True),
(True, False, False),
(False, True, True),
(False, True, False),
(False, False, True),
(False, False, False),
],
)
async def test_show_sub_widgets(show_bar: bool, show_percentage: bool, show_eta: bool):
class PBApp(App[None]):
def compose(self):
self.pb = ProgressBar(
show_bar=show_bar, show_percentage=show_percentage, show_eta=show_eta
)
yield self.pb
app = PBApp()
async with app.run_test():
if show_bar:
bar = app.pb.query_one("#bar")
assert isinstance(bar, Widget)
else:
with pytest.raises(NoMatches):
app.pb.query_one("#bar")
if show_percentage:
percentage = app.pb.query_one("#percentage")
assert isinstance(percentage, Widget)
else:
with pytest.raises(NoMatches):
app.pb.query_one("#percentage")
if show_eta:
eta = app.pb.query_one("#eta")
assert isinstance(eta, Widget)
else:
with pytest.raises(NoMatches):
app.pb.query_one("#eta")
def test_apply_gradient():
text = Text("foo")
gradient = Gradient.from_colors("red", "blue")
_apply_gradient(text, gradient, 1)
console = Console()
assert text.get_style_at_offset(console, 0).color.triplet == (255, 0, 0)
|