from __future__ import annotations import time import pytest import progressbar2 max_values: list[None | type[progressbar2.base.UnknownLength] | int] = [ None, 10, progressbar2.UnknownLength, ] def test_create_wrapper() -> None: with pytest.raises(AssertionError): progressbar2.widgets.create_wrapper('ab') with pytest.raises(RuntimeError): progressbar2.widgets.create_wrapper(123) def test_widgets_small_values() -> None: widgets = [ 'Test: ', progressbar2.Percentage(), ' ', progressbar2.Bar(marker=progressbar2.RotatingMarker()), ' ', progressbar2.ETA(), ' ', progressbar2.AbsoluteETA(), ' ', progressbar2.FileTransferSpeed(), ] p = progressbar2.ProgressBar(widgets=widgets, max_value=10).start() p.update(0) for i in range(10): time.sleep(1) p.update(i + 1) p.finish() @pytest.mark.parametrize('max_value', [10**6, 10**8]) def test_widgets_large_values(max_value) -> None: widgets = [ 'Test: ', progressbar2.Percentage(), ' ', progressbar2.Bar(marker=progressbar2.RotatingMarker()), ' ', progressbar2.ETA(), ' ', progressbar2.AbsoluteETA(), ' ', progressbar2.FileTransferSpeed(), ] p = progressbar2.ProgressBar(widgets=widgets, max_value=max_value).start() for i in range(0, 10**6, 10**4): time.sleep(1) p.update(i + 1) p.finish() def test_format_widget() -> None: widgets = [ progressbar2.FormatLabel(f'%({mapping})r') for mapping in progressbar2.FormatLabel.mapping ] p = progressbar2.ProgressBar(widgets=widgets) for _ in p(range(10)): time.sleep(1) @pytest.mark.parametrize('max_value', [None, 10]) def test_all_widgets_small_values(max_value) -> None: widgets = [ progressbar2.Timer(), progressbar2.ETA(), progressbar2.AdaptiveETA(), progressbar2.AbsoluteETA(), progressbar2.DataSize(), progressbar2.FileTransferSpeed(), progressbar2.AdaptiveTransferSpeed(), progressbar2.AnimatedMarker(), progressbar2.Counter(), progressbar2.Percentage(), progressbar2.FormatLabel('%(value)d'), progressbar2.SimpleProgress(), progressbar2.Bar(), progressbar2.ReverseBar(), progressbar2.BouncingBar(), progressbar2.CurrentTime(), progressbar2.CurrentTime(microseconds=False), progressbar2.CurrentTime(microseconds=True), ] p = progressbar2.ProgressBar(widgets=widgets, max_value=max_value) for i in range(10): time.sleep(1) p.update(i + 1) p.finish() @pytest.mark.parametrize('max_value', [10**6, 10**7]) def test_all_widgets_large_values(max_value) -> None: widgets = [ progressbar2.Timer(), progressbar2.ETA(), progressbar2.AdaptiveETA(), progressbar2.AbsoluteETA(), progressbar2.DataSize(), progressbar2.FileTransferSpeed(), progressbar2.AdaptiveTransferSpeed(), progressbar2.AnimatedMarker(), progressbar2.Counter(), progressbar2.Percentage(), progressbar2.FormatLabel('%(value)d/%(max_value)d'), progressbar2.SimpleProgress(), progressbar2.Bar(fill=lambda progress, data, width: '#'), progressbar2.ReverseBar(), progressbar2.BouncingBar(), progressbar2.FormatCustomText('Custom %(text)s', dict(text='text')), ] p = progressbar2.ProgressBar(widgets=widgets, max_value=max_value) p.update() time.sleep(1) p.update() for i in range(0, 10**6, 10**4): time.sleep(1) p.update(i) @pytest.mark.parametrize('min_width', [None, 1, 2, 80, 120]) @pytest.mark.parametrize('term_width', [1, 2, 80, 120]) def test_all_widgets_min_width(min_width, term_width) -> None: widgets = [ progressbar2.Timer(min_width=min_width), progressbar2.ETA(min_width=min_width), progressbar2.AdaptiveETA(min_width=min_width), progressbar2.AbsoluteETA(min_width=min_width), progressbar2.DataSize(min_width=min_width), progressbar2.FileTransferSpeed(min_width=min_width), progressbar2.AdaptiveTransferSpeed(min_width=min_width), progressbar2.AnimatedMarker(min_width=min_width), progressbar2.Counter(min_width=min_width), progressbar2.Percentage(min_width=min_width), progressbar2.FormatLabel('%(value)d', min_width=min_width), progressbar2.SimpleProgress(min_width=min_width), progressbar2.Bar(min_width=min_width), progressbar2.ReverseBar(min_width=min_width), progressbar2.BouncingBar(min_width=min_width), progressbar2.FormatCustomText( 'Custom %(text)s', dict(text='text'), min_width=min_width, ), progressbar2.DynamicMessage('custom', min_width=min_width), progressbar2.CurrentTime(min_width=min_width), ] p = progressbar2.ProgressBar(widgets=widgets, term_width=term_width) p.update(0) p.update() for widget in p._format_widgets(): if min_width and min_width > term_width: assert widget == '' else: assert widget != '' @pytest.mark.parametrize('max_width', [None, 1, 2, 80, 120]) @pytest.mark.parametrize('term_width', [1, 2, 80, 120]) def test_all_widgets_max_width(max_width, term_width) -> None: widgets = [ progressbar2.Timer(max_width=max_width), progressbar2.ETA(max_width=max_width), progressbar2.AdaptiveETA(max_width=max_width), progressbar2.AbsoluteETA(max_width=max_width), progressbar2.DataSize(max_width=max_width), progressbar2.FileTransferSpeed(max_width=max_width), progressbar2.AdaptiveTransferSpeed(max_width=max_width), progressbar2.AnimatedMarker(max_width=max_width), progressbar2.Counter(max_width=max_width), progressbar2.Percentage(max_width=max_width), progressbar2.FormatLabel('%(value)d', max_width=max_width), progressbar2.SimpleProgress(max_width=max_width), progressbar2.Bar(max_width=max_width), progressbar2.ReverseBar(max_width=max_width), progressbar2.BouncingBar(max_width=max_width), progressbar2.FormatCustomText( 'Custom %(text)s', dict(text='text'), max_width=max_width, ), progressbar2.DynamicMessage('custom', max_width=max_width), progressbar2.CurrentTime(max_width=max_width), ] p = progressbar2.ProgressBar(widgets=widgets, term_width=term_width) p.update(0) p.update() for widget in p._format_widgets(): if max_width and max_width < term_width: assert widget == '' else: assert widget != ''