-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
73 lines (63 loc) · 2.71 KB
/
__init__.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
import unittest
import colorama
import statusbar
class TestProgressBar(unittest.TestCase):
"""Test of the progressbar part of a status bar."""
def test_chunk_widths(self):
pb = statusbar.ProgressBar()
pb.add_progress(1, '.')
pb.add_progress(1, '#')
breakpoints = pb._get_chunk_sizes(2)
self.assertListEqual(breakpoints, [1, 1])
pb = statusbar.ProgressBar()
pb.add_progress(1, '.')
pb.add_progress(2, '#')
breakpoints = pb._get_chunk_sizes(3)
self.assertListEqual(breakpoints, [1, 2])
# squeezing [1,2] into width 2 will give 1 to each since
# the first breakpoint is at 2/3 which is rounded up.
pb = statusbar.ProgressBar()
pb.add_progress(1, '.')
pb.add_progress(2, '#')
breakpoints = pb._get_chunk_sizes(2)
self.assertListEqual(breakpoints, [1, 1])
# squeezing [1,3] into width 2 will give (0,1).
# The first breakpoint is at 1/2 which *should* be rounded up
# but actually round(0.5) seems to be 0 in Python...
pb = statusbar.ProgressBar()
pb.add_progress(1, '.')
pb.add_progress(3, '#')
breakpoints = pb._get_chunk_sizes(2)
self.assertListEqual(breakpoints, [0, 2])
pb = statusbar.ProgressBar()
pb.add_progress(1, '.')
pb.add_progress(3, '#')
breakpoints = pb._get_chunk_sizes(4)
self.assertListEqual(breakpoints, [1, 3])
def test_progress_formatting(self):
pb = statusbar.ProgressBar()
pb.add_progress(1, '.')
pb.add_progress(1, '#')
progress = pb.format_progress(4)
self.assertEqual(progress, "[.#]")
pb = statusbar.ProgressBar()
pb.add_progress(1, '.')
pb.add_progress(2, '#')
progress = pb.format_progress(5)
self.assertEqual(progress, "[.##]")
# Adding a forground colour makes each segment ten characters
# longer; five characters are used for setting the color and another
# five for resetting it again. These are not shown, so the width
# doesn't take this into account.
pb = statusbar.ProgressBar()
pb.add_progress(1, '.', fg=colorama.Fore.GREEN)
pb.add_progress(2, '#', fg=colorama.Fore.RED)
progress = pb.format_progress(5)
self.assertEqual(progress[0], "[")
self.assertEqual(progress[1:6], colorama.Fore.GREEN)
self.assertEqual(progress[6], ".")
self.assertEqual(progress[7:12], colorama.Fore.RESET)
self.assertEqual(progress[12:17], colorama.Fore.RED)
self.assertEqual(progress[17:19], "##")
self.assertEqual(progress[19:24], colorama.Fore.RESET)
self.assertEqual(progress[24], "]")