| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import time |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| •7 |
import progressbar2 |
| 8 |
|
| •9 |
max_values: list[None | type[progressbar2.base.UnknownLength] | int] = [ |
| 10 |
None, |
| 11 |
10, |
| •12 |
progressbar2.UnknownLength, |
| 13 |
] |
| 14 |
|
| 15 |
|
| 16 |
def test_create_wrapper() -> None: |
| 17 |
with pytest.raises(AssertionError): |
| •18 |
progressbar2.widgets.create_wrapper('ab') |
| 19 |
|
| 20 |
with pytest.raises(RuntimeError): |
| •21 |
progressbar2.widgets.create_wrapper(123) |
| 22 |
|
| 23 |
|
| 24 |
def test_widgets_small_values() -> None: |
| 25 |
widgets = [ |
| 26 |
'Test: ', |
| •27 |
progressbar2.Percentage(), |
| 28 |
' ', |
| •29 |
progressbar2.Bar(marker=progressbar2.RotatingMarker()), |
| 30 |
' ', |
| •31 |
progressbar2.ETA(), |
| 32 |
' ', |
| •33 |
progressbar2.AbsoluteETA(), |
| 34 |
' ', |
| •35 |
progressbar2.FileTransferSpeed(), |
| 36 |
] |
| •37 |
p = progressbar2.ProgressBar(widgets=widgets, max_value=10).start() |
| 38 |
p.update(0) |
| 39 |
for i in range(10): |
| 40 |
time.sleep(1) |
| 41 |
p.update(i + 1) |
| 42 |
p.finish() |
| 43 |
|
| 44 |
|
| 45 |
@pytest.mark.parametrize('max_value', [10**6, 10**8]) |
| 46 |
def test_widgets_large_values(max_value) -> None: |
| 47 |
widgets = [ |
| 48 |
'Test: ', |
| •49 |
progressbar2.Percentage(), |
| 50 |
' ', |
| •51 |
progressbar2.Bar(marker=progressbar2.RotatingMarker()), |
| 52 |
' ', |
| •53 |
progressbar2.ETA(), |
| 54 |
' ', |
| •55 |
progressbar2.AbsoluteETA(), |
| 56 |
' ', |
| •57 |
progressbar2.FileTransferSpeed(), |
| 58 |
] |
| •59 |
p = progressbar2.ProgressBar(widgets=widgets, max_value=max_value).start() |
| 60 |
for i in range(0, 10**6, 10**4): |
| 61 |
time.sleep(1) |
| 62 |
p.update(i + 1) |
| 63 |
p.finish() |
| 64 |
|
| 65 |
|
| 66 |
def test_format_widget() -> None: |
| 67 |
widgets = [ |
| •68 |
progressbar2.FormatLabel(f'%({mapping})r') |
| 69 |
for mapping in progressbar2.FormatLabel.mapping |
| 70 |
] |
| •71 |
p = progressbar2.ProgressBar(widgets=widgets) |
| 72 |
for _ in p(range(10)): |
| 73 |
time.sleep(1) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize('max_value', [None, 10]) |
| 77 |
def test_all_widgets_small_values(max_value) -> None: |
| 78 |
widgets = [ |
| •79 |
progressbar2.Timer(), |
| 80 |
progressbar2.ETA(), |
| 81 |
progressbar2.AdaptiveETA(), |
| 82 |
progressbar2.AbsoluteETA(), |
| 83 |
progressbar2.DataSize(), |
| 84 |
progressbar2.FileTransferSpeed(), |
| 85 |
progressbar2.AdaptiveTransferSpeed(), |
| 86 |
progressbar2.AnimatedMarker(), |
| 87 |
progressbar2.Counter(), |
| 88 |
progressbar2.Percentage(), |
| 89 |
progressbar2.FormatLabel('%(value)d'), |
| 90 |
progressbar2.SimpleProgress(), |
| 91 |
progressbar2.Bar(), |
| 92 |
progressbar2.ReverseBar(), |
| 93 |
progressbar2.BouncingBar(), |
| 94 |
progressbar2.CurrentTime(), |
| 95 |
progressbar2.CurrentTime(microseconds=False), |
| 96 |
progressbar2.CurrentTime(microseconds=True), |
| 97 |
] |
| •98 |
p = progressbar2.ProgressBar(widgets=widgets, max_value=max_value) |
| 99 |
for i in range(10): |
| 100 |
time.sleep(1) |
| 101 |
p.update(i + 1) |
| 102 |
p.finish() |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.parametrize('max_value', [10**6, 10**7]) |
| 106 |
def test_all_widgets_large_values(max_value) -> None: |
| 107 |
widgets = [ |
| •108 |
progressbar2.Timer(), |
| 109 |
progressbar2.ETA(), |
| 110 |
progressbar2.AdaptiveETA(), |
| 111 |
progressbar2.AbsoluteETA(), |
| 112 |
progressbar2.DataSize(), |
| 113 |
progressbar2.FileTransferSpeed(), |
| 114 |
progressbar2.AdaptiveTransferSpeed(), |
| 115 |
progressbar2.AnimatedMarker(), |
| 116 |
progressbar2.Counter(), |
| 117 |
progressbar2.Percentage(), |
| 118 |
progressbar2.FormatLabel('%(value)d/%(max_value)d'), |
| 119 |
progressbar2.SimpleProgress(), |
| 120 |
progressbar2.Bar(fill=lambda progress, data, width: '#'), |
| 121 |
progressbar2.ReverseBar(), |
| 122 |
progressbar2.BouncingBar(), |
| 123 |
progressbar2.FormatCustomText('Custom %(text)s', dict(text='text')), |
| 124 |
] |
| •125 |
p = progressbar2.ProgressBar(widgets=widgets, max_value=max_value) |
| 126 |
p.update() |
| 127 |
time.sleep(1) |
| 128 |
p.update() |
| 129 |
|
| 130 |
for i in range(0, 10**6, 10**4): |
| 131 |
time.sleep(1) |
| 132 |
p.update(i) |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize('min_width', [None, 1, 2, 80, 120]) |
| 136 |
@pytest.mark.parametrize('term_width', [1, 2, 80, 120]) |
| 137 |
def test_all_widgets_min_width(min_width, term_width) -> None: |
| 138 |
widgets = [ |
| •139 |
progressbar2.Timer(min_width=min_width), |
| 140 |
progressbar2.ETA(min_width=min_width), |
| 141 |
progressbar2.AdaptiveETA(min_width=min_width), |
| 142 |
progressbar2.AbsoluteETA(min_width=min_width), |
| 143 |
progressbar2.DataSize(min_width=min_width), |
| 144 |
progressbar2.FileTransferSpeed(min_width=min_width), |
| 145 |
progressbar2.AdaptiveTransferSpeed(min_width=min_width), |
| 146 |
progressbar2.AnimatedMarker(min_width=min_width), |
| 147 |
progressbar2.Counter(min_width=min_width), |
| 148 |
progressbar2.Percentage(min_width=min_width), |
| 149 |
progressbar2.FormatLabel('%(value)d', min_width=min_width), |
| 150 |
progressbar2.SimpleProgress(min_width=min_width), |
| 151 |
progressbar2.Bar(min_width=min_width), |
| 152 |
progressbar2.ReverseBar(min_width=min_width), |
| 153 |
progressbar2.BouncingBar(min_width=min_width), |
| 154 |
progressbar2.FormatCustomText( |
| 155 |
'Custom %(text)s', |
| 156 |
dict(text='text'), |
| 157 |
min_width=min_width, |
| 158 |
), |
| •159 |
progressbar2.DynamicMessage('custom', min_width=min_width), |
| 160 |
progressbar2.CurrentTime(min_width=min_width), |
| 161 |
] |
| •162 |
p = progressbar2.ProgressBar(widgets=widgets, term_width=term_width) |
| 163 |
p.update(0) |
| 164 |
p.update() |
| 165 |
for widget in p._format_widgets(): |
| 166 |
if min_width and min_width > term_width: |
| 167 |
assert widget == '' |
| 168 |
else: |
| 169 |
assert widget != '' |
| 170 |
|
| 171 |
|
| 172 |
@pytest.mark.parametrize('max_width', [None, 1, 2, 80, 120]) |
| 173 |
@pytest.mark.parametrize('term_width', [1, 2, 80, 120]) |
| 174 |
def test_all_widgets_max_width(max_width, term_width) -> None: |
| 175 |
widgets = [ |
| •176 |
progressbar2.Timer(max_width=max_width), |
| 177 |
progressbar2.ETA(max_width=max_width), |
| 178 |
progressbar2.AdaptiveETA(max_width=max_width), |
| 179 |
progressbar2.AbsoluteETA(max_width=max_width), |
| 180 |
progressbar2.DataSize(max_width=max_width), |
| 181 |
progressbar2.FileTransferSpeed(max_width=max_width), |
| 182 |
progressbar2.AdaptiveTransferSpeed(max_width=max_width), |
| 183 |
progressbar2.AnimatedMarker(max_width=max_width), |
| 184 |
progressbar2.Counter(max_width=max_width), |
| 185 |
progressbar2.Percentage(max_width=max_width), |
| 186 |
progressbar2.FormatLabel('%(value)d', max_width=max_width), |
| 187 |
progressbar2.SimpleProgress(max_width=max_width), |
| 188 |
progressbar2.Bar(max_width=max_width), |
| 189 |
progressbar2.ReverseBar(max_width=max_width), |
| 190 |
progressbar2.BouncingBar(max_width=max_width), |
| 191 |
progressbar2.FormatCustomText( |
| 192 |
'Custom %(text)s', |
| 193 |
dict(text='text'), |
| 194 |
max_width=max_width, |
| 195 |
), |
| •196 |
progressbar2.DynamicMessage('custom', max_width=max_width), |
| 197 |
progressbar2.CurrentTime(max_width=max_width), |
| 198 |
] |
| •199 |
p = progressbar2.ProgressBar(widgets=widgets, term_width=term_width) |
| 200 |
p.update(0) |
| 201 |
p.update() |
| 202 |
for widget in p._format_widgets(): |
| 203 |
if max_width and max_width < term_width: |
| 204 |
assert widget == '' |
| 205 |
else: |
| 206 |
assert widget != '' |