/
tests.py
259 lines (217 loc) · 9.51 KB
/
tests.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import unittest
from pygressbar import RED, COL_RESET, BLUE, MAGENTA, GREEN # Colors
from pygressbar import (SimpleProgressBar,
CustomProgressBar,
SimplePercentProgressBar,
SimpleAnimatedProgressBar,
SimpleColorBar)
class TestPygressBar(unittest.TestCase):
def format_string(self, total, left_limit, right_limit, fill_char,
empty_char, head):
return "{0}{1}{2}{3}{4}".format(left_limit,
fill_char * (total // 2 - len(head)),
head,
empty_char * (total // 2),
right_limit)
def test_empty_bar(self):
"""Tests the default empty progress bar"""
total = 20
bar = SimpleProgressBar()
self.assertEqual("[{0}]".format(' ' * total,), bar.progress_bar)
#print("Result for 0%: " + bar.progress_bar)
def test_50_percent_bar(self):
"""Tests the default 50 percent progress bar"""
total = 20
bar = SimpleProgressBar()
bar.increase(50)
self.assertEqual("[{0}>{1}]".format('=' * (total // 2 - 1),
' ' * (total // 2)),
bar.progress_bar)
#print("Result for 50%: " + bar.progress_bar)
def test_100_percent_bar(self):
"""Tests the default 100 percent progress bar"""
total = 20
bar = SimpleProgressBar()
bar.increase(100)
self.assertEqual("[{0}>{1}]".format('=' * (total - 1), ''),
bar.progress_bar)
#print("Result for 100%: " + bar.progress_bar)
def test_complete_query_percent_bar(self):
"""Tests the query of complete or not complete"""
bar = SimpleProgressBar()
bar.increase(100)
self.assertTrue(bar.completed())
bar = SimpleProgressBar()
bar.increase(99)
self.assertFalse(bar.completed())
def test_incr_10_percent_bar(self):
"""Tests the default bar one by one"""
bar = SimpleProgressBar()
incr_factor = 1
total = 20
scale = 100
while not bar.completed():
total_progress = bar.progress + incr_factor
bar.increase(incr_factor)
filled = total * total_progress // scale
empty = total - filled
# Custoize The head
char = '>' if filled > 0 else ''
test_bar = "[{0}{1}{2}]".format('=' * (filled - 1),
char,
' ' * empty)
self.assertEqual(test_bar, bar.progress_bar)
#print("Result for {0}%: {1}".format(total_progress,
# bar.progress_bar))
def test_decrease_bar(self):
"""Tests the decrement of the bar"""
total = 20
bar = SimpleProgressBar()
bar.increase(50)
self.assertEqual("[{0}>{1}]".format('=' * (total // 2 - 1),
' ' * (total // 2)),
bar.progress_bar)
bar.decrease(10)
self.assertEqual("[{0}>{1}]".format('=' * (total // 2 - 3),
' ' * (total // 2 + 2)),
bar.progress_bar)
def test_custom_bar(self):
"""Test a custom progress bar"""
total = 100
fill_char = '#'
empty_char = '.'
head = '|'
left_limit = '('
right_limit = ')'
scale_start = 0
scale_end = 100
bar = CustomProgressBar(length=total,
left_limit=left_limit,
right_limit=right_limit,
head_repr=head,
empty_repr=empty_char,
filled_repr=fill_char,
start=0,
scale_start=scale_start,
scale_end=scale_end)
bar.increase((scale_end - scale_start) // 2)
test_str = self.format_string(total, left_limit, right_limit, fill_char,
empty_char, head)
self.assertEqual(test_str, bar.progress_bar)
#print("Result for custom 50%: " + bar.progress_bar)
def test_custom_bar_without_head(self):
"""Test a custom progress bar without head"""
total = 50
fill_char = '#'
empty_char = ' '
head = None
left_limit = '['
right_limit = ']'
scale_start = 0
scale_end = 100
bar = CustomProgressBar(length=total,
left_limit=left_limit,
right_limit=right_limit,
head_repr=head,
empty_repr=empty_char,
filled_repr=fill_char,
start=0,
scale_start=scale_start,
scale_end=scale_end)
head = fill_char if not head else head
bar.increase((scale_end - scale_start) // 2)
test_str = self.format_string(total, left_limit, right_limit, fill_char,
empty_char, head)
self.assertEqual(test_str, bar.progress_bar)
#print("Result for custom 50%: " + bar.progress_bar)
def test_custom_bar_with_triple_head(self):
"""Test a custom progress bar with a triple head"""
total = 100
fill_char = '='
empty_char = ' '
head = '>>>'
left_limit = '['
right_limit = ']'
scale_start = 0
scale_end = 100
bar = CustomProgressBar(length=total,
left_limit=left_limit,
right_limit=right_limit,
head_repr=head,
empty_repr=empty_char,
filled_repr=fill_char,
start=0,
scale_start=scale_start,
scale_end=scale_end)
head = fill_char if not head else head
bar.increase((scale_end - scale_start) // 2)
test_str = self.format_string(total, left_limit, right_limit, fill_char,
empty_char, head)
self.assertEqual(test_str, bar.progress_bar)
#print("Result for custom 50%: " + bar.progress_bar)
def test_custom_bar_with_scale(self):
"""Test a custom progress bar with a custom scale"""
total = 50
fill_char = '#'
empty_char = ' '
head = '>'
left_limit = '['
right_limit = ']'
scale_start = 415
scale_end = 431
bar = CustomProgressBar(length=total,
left_limit=left_limit,
right_limit=right_limit,
head_repr=head,
empty_repr=empty_char,
filled_repr=fill_char,
start=0,
scale_start=scale_start,
scale_end=scale_end)
head = fill_char if not head else head
bar.increase((scale_end - scale_start) // 2)
test_str = self.format_string(total, left_limit, right_limit, fill_char,
empty_char, head)
self.assertEqual(test_str, bar.progress_bar)
#print("Result for custom 50%: " + bar.progress_bar)
def test_simple_bar_with_percent(self):
"""Tests the simple percent animation bar"""
total = 20
bar = SimplePercentProgressBar()
bar.increase(50)
self.assertEqual("[{0}>{1}]({2}%)".format('=' * (total // 2 - 1),
' ' * (total // 2),
100 * (total // 2) // total),
bar.progress_bar)
#print("Result for 100%: " + bar.progress_bar)
def test_animated_head_bar(self):
"""Tests the animated head bar"""
total = 20
bar = SimpleAnimatedProgressBar(speed=1990)
times = 10
bar.increase(100)
self.assertEqual("[{0}|]".format('=' * (total - 1)), bar.progress_bar)
for i in range(0, times):
bar.progress_bar
self.assertEqual("[{0}/]".format('=' * (total - 1)), bar.progress_bar)
for i in range(0, times):
bar.progress_bar
self.assertEqual("[{0}-]".format('=' * (total - 1)), bar.progress_bar)
for i in range(0, times):
bar.progress_bar
self.assertEqual("[{0}\\]".format('=' * (total - 1)), bar.progress_bar)
def test_50_percent_color_bar(self):
"""Tests the color 50 percent bar"""
total = 20
bar = SimpleColorBar()
bar.increase(50)
format = GREEN + "[" + COL_RESET +\
BLUE + "{0}" + COL_RESET +\
RED + ">" + COL_RESET +\
MAGENTA + "{1}" + COL_RESET +\
GREEN + "]" + COL_RESET
self.assertEqual(format.format('=' * (total // 2 - 1),
'.' * (total // 2)),
bar.progress_bar)
if __name__ == '__main__':
unittest.main()