-
Notifications
You must be signed in to change notification settings - Fork 6
/
progress_bar_test.go
165 lines (156 loc) · 4.61 KB
/
progress_bar_test.go
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
package progress_bar
import (
"bytes"
"errors"
"fmt"
"testing"
"time"
)
func TestDefaultProgressBar(t *testing.T) {
//create output
output := new(bytes.Buffer)
//create default progress bar
pb := DefaultProgressBar(50)
pb.Output = output
//start
expectedOutput := "[..................................................][%0.0][0.0/50.0][Elapsed: 0.0s Remaining: +Infs]"
expectedByteCount := len(expectedOutput) //100
err := start(pb, output, expectedByteCount, expectedOutput)
if err != nil {
t.Error(err.Error())
}
//update
expectedOutput = "\u001b[2K\u001b[100D[#########################.........................][%50.0][25.0/50.0][Elapsed: 1.0s Remaining: 1.0s]"
expectedByteCount = len(expectedOutput) //111
err = update(pb, 25, output, expectedByteCount, expectedOutput)
if err != nil {
t.Error(err.Error())
}
//stop
expectedOutput = "\u001b[2K\u001b[101D[##################################################][%100.0][50.0/50.0][Elapsed: 2.0s Remaining: 0.0s]\n" //101: 111 - 10(escape codes)
expectedByteCount = len(expectedOutput)
err = update(pb, 50, output, expectedByteCount, expectedOutput)
if err != nil {
t.Error(err.Error())
}
}
func TestNewProgressBar(t *testing.T) {
//create output
output := new(bytes.Buffer)
//create custom parameters
schema := fmt.Sprintf("(%s)(%s)(%s of %s)(E: %ss R: %ss)", svBar, svPercent, svCurrentValue, svTotalValue, svElapsedDuration, svRemainingDuration)
filledCharacter := "="
blankCharacter := "-"
var length float64 = 60
var value float64 = 80
pb := NewProgressBar(output, schema, filledCharacter, blankCharacter, length, value)
//start
expectedOutput := "(------------------------------------------------------------)(%0.0)(0.0 of 80.0)(E: 0.0s R: +Infs)"
expectedByteCount := len(expectedOutput) //99
err := start(pb, output, expectedByteCount, expectedOutput)
if err != nil {
t.Error(err.Error())
}
//update
expectedOutput = "\u001b[2K\u001b[99D(==============================------------------------------)(%50.0)(40.0 of 80.0)(E: 1.0s R: 1.0s)"
expectedByteCount = len(expectedOutput) //109
err = update(pb, 40, output, expectedByteCount, expectedOutput)
if err != nil {
t.Error(err.Error())
}
//stop
expectedOutput = "\u001b[2K\u001b[100D(============================================================)(%100.0)(80.0 of 80.0)(E: 2.0s R: 0.0s)\n" //100: 109 - 9(escape codes)
expectedByteCount = len(expectedOutput)
err = update(pb, 80, output, expectedByteCount, expectedOutput)
if err != nil {
t.Error(err.Error())
}
}
func TestStartError(t *testing.T) {
//create output
output := new(bytes.Buffer)
//create default progress bar
pb := DefaultProgressBar(100)
pb.Output = output
//start
err := pb.Start()
if err != nil {
t.Error("starting progress bar failed")
}
//start again
err = pb.Start()
if err == nil {
t.Error("catching \"progress bar is already started\" error failed")
}
}
func TestStopError(t *testing.T) {
//create output
output := new(bytes.Buffer)
//create default progress bar
pb := DefaultProgressBar(100)
pb.Output = output
//stop again
err := pb.Stop()
if err == nil {
t.Error("catching \"progress bar is not started\" error failed")
}
}
func TestUpdateErrors(t *testing.T) {
//create output
output := new(bytes.Buffer)
//create default progress bar
pb := DefaultProgressBar(100)
pb.Output = output
//update
err := pb.Update(50)
if err == nil {
t.Error("catching \"progress bar is noty started\" error failed")
}
//start
err = pb.Start()
if err != nil {
t.Error("starting progress bar failed")
}
//update with a value which is greater then total value
err = pb.Update(101)
if err == nil {
t.Error("catching \"value is greater then total value")
}
}
func start(pb *ProgressBar, output *bytes.Buffer, expectedByteCount int, expectedOutput string) error {
err := pb.Start()
if err != nil {
return err
}
bs := make([]byte, expectedByteCount)
n, err := output.Read(bs)
if err != nil {
return errors.New("reading output failed")
}
if n != expectedByteCount {
return errors.New("byte count doesn't match")
}
if string(bs) != expectedOutput {
return errors.New("output doesn't match")
}
return nil
}
func update(pb *ProgressBar, value float64, output *bytes.Buffer, expectedByteCount int, expectedOutput string) error {
time.Sleep(1 * time.Second)
err := pb.Update(value)
if err != nil {
return err
}
bs := make([]byte, expectedByteCount)
n, err := output.Read(bs)
if err != nil {
return errors.New("reading output failed")
}
if n != expectedByteCount {
return errors.New("byte count doesn't match")
}
if string(bs) != expectedOutput {
return errors.New("output doesn't match")
}
return nil
}