Debian Codegraph @

tests/test_widgets.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 from __future__ import annotations
2
3 import time
4
5 import pytest
6
7 import progressbar
8
9 max_values: list[None | type[progressbar.base.UnknownLength] | int] = [
10 None,
11 10,
12 progressbar.UnknownLength,
13 ]
14
15
16 def test_create_wrapper() -> None:
17 with pytest.raises(AssertionError):
18 progressbar.widgets.create_wrapper('ab')
19
20 with pytest.raises(RuntimeError):
21 progressbar.widgets.create_wrapper(123)
22
23
24 def test_widgets_small_values() -> None:
25 widgets = [
26 'Test: ',
27 progressbar.Percentage(),
28 ' ',
29 progressbar.Bar(marker=progressbar.RotatingMarker()),
30 ' ',
31 progressbar.ETA(),
32 ' ',
33 progressbar.AbsoluteETA(),
34 ' ',
35 progressbar.FileTransferSpeed(),
36 ]
37 p = progressbar.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 progressbar.Percentage(),
50 ' ',
51 progressbar.Bar(marker=progressbar.RotatingMarker()),
52 ' ',
53 progressbar.ETA(),
54 ' ',
55 progressbar.AbsoluteETA(),
56 ' ',
57 progressbar.FileTransferSpeed(),
58 ]
59 p = progressbar.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 progressbar.FormatLabel(f'%({mapping})r')
69 for mapping in progressbar.FormatLabel.mapping
70 ]
71 p = progressbar.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 progressbar.Timer(),
80 progressbar.ETA(),
81 progressbar.AdaptiveETA(),
82 progressbar.AbsoluteETA(),
83 progressbar.DataSize(),
84 progressbar.FileTransferSpeed(),
85 progressbar.AdaptiveTransferSpeed(),
86 progressbar.AnimatedMarker(),
87 progressbar.Counter(),
88 progressbar.Percentage(),
89 progressbar.FormatLabel('%(value)d'),
90 progressbar.SimpleProgress(),
91 progressbar.Bar(),
92 progressbar.ReverseBar(),
93 progressbar.BouncingBar(),
94 progressbar.CurrentTime(),
95 progressbar.CurrentTime(microseconds=False),
96 progressbar.CurrentTime(microseconds=True),
97 ]
98 p = progressbar.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 progressbar.Timer(),
109 progressbar.ETA(),
110 progressbar.AdaptiveETA(),
111 progressbar.AbsoluteETA(),
112 progressbar.DataSize(),
113 progressbar.FileTransferSpeed(),
114 progressbar.AdaptiveTransferSpeed(),
115 progressbar.AnimatedMarker(),
116 progressbar.Counter(),
117 progressbar.Percentage(),
118 progressbar.FormatLabel('%(value)d/%(max_value)d'),
119 progressbar.SimpleProgress(),
120 progressbar.Bar(fill=lambda progress, data, width: '#'),
121 progressbar.ReverseBar(),
122 progressbar.BouncingBar(),
123 progressbar.FormatCustomText('Custom %(text)s', dict(text='text')),
124 ]
125 p = progressbar.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 progressbar.Timer(min_width=min_width),
140 progressbar.ETA(min_width=min_width),
141 progressbar.AdaptiveETA(min_width=min_width),
142 progressbar.AbsoluteETA(min_width=min_width),
143 progressbar.DataSize(min_width=min_width),
144 progressbar.FileTransferSpeed(min_width=min_width),
145 progressbar.AdaptiveTransferSpeed(min_width=min_width),
146 progressbar.AnimatedMarker(min_width=min_width),
147 progressbar.Counter(min_width=min_width),
148 progressbar.Percentage(min_width=min_width),
149 progressbar.FormatLabel('%(value)d', min_width=min_width),
150 progressbar.SimpleProgress(min_width=min_width),
151 progressbar.Bar(min_width=min_width),
152 progressbar.ReverseBar(min_width=min_width),
153 progressbar.BouncingBar(min_width=min_width),
154 progressbar.FormatCustomText(
155 'Custom %(text)s',
156 dict(text='text'),
157 min_width=min_width,
158 ),
159 progressbar.DynamicMessage('custom', min_width=min_width),
160 progressbar.CurrentTime(min_width=min_width),
161 ]
162 p = progressbar.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 progressbar.Timer(max_width=max_width),
177 progressbar.ETA(max_width=max_width),
178 progressbar.AdaptiveETA(max_width=max_width),
179 progressbar.AbsoluteETA(max_width=max_width),
180 progressbar.DataSize(max_width=max_width),
181 progressbar.FileTransferSpeed(max_width=max_width),
182 progressbar.AdaptiveTransferSpeed(max_width=max_width),
183 progressbar.AnimatedMarker(max_width=max_width),
184 progressbar.Counter(max_width=max_width),
185 progressbar.Percentage(max_width=max_width),
186 progressbar.FormatLabel('%(value)d', max_width=max_width),
187 progressbar.SimpleProgress(max_width=max_width),
188 progressbar.Bar(max_width=max_width),
189 progressbar.ReverseBar(max_width=max_width),
190 progressbar.BouncingBar(max_width=max_width),
191 progressbar.FormatCustomText(
192 'Custom %(text)s',
193 dict(text='text'),
194 max_width=max_width,
195 ),
196 progressbar.DynamicMessage('custom', max_width=max_width),
197 progressbar.CurrentTime(max_width=max_width),
198 ]
199 p = progressbar.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 != ''