-
Notifications
You must be signed in to change notification settings - Fork 1
/
progressbar_test.go
133 lines (116 loc) · 2.78 KB
/
progressbar_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
package p0d
import (
"github.com/acarl005/stripansi"
"strings"
"testing"
"time"
)
func TestProgressBarMarkRamp(t *testing.T) {
layout := "2006-01-02T15:04:05.000Z"
p := P0d{
Config: Config{
Exec: Exec{
DurationSeconds: 30},
},
}
p.Time.Start, _ = time.Parse(layout, "2022-01-01T00:00:00.000Z")
pb := ProgressBar{
curSecs: 0,
maxSecs: 30,
size: 20,
chunkProps: make([]ChunkProps, 20),
}
pn := p.Time.Start.Add(3 * time.Second)
pb.markRamp(pn, &p)
if pb.chunkProps[pb.chunkPropIndexFor(pn, &p)].isRamp == false {
t.Error("ramp not set")
}
}
func TestProgressBarMarkError(t *testing.T) {
layout := "2006-01-02T15:04:05.000Z"
p := P0d{
Config: Config{
Exec: Exec{
DurationSeconds: 30},
},
}
p.Time.Start, _ = time.Parse(layout, "2022-01-01T00:00:00.000Z")
pb := ProgressBar{
curSecs: 0,
maxSecs: 30,
size: 20,
chunkProps: make([]ChunkProps, 20),
}
pn := p.Time.Start.Add(3 * time.Second)
pb.markError(pn, &p)
if pb.chunkProps[pb.chunkPropIndexFor(pn, &p)].hasErrors == false {
t.Error("error not set")
}
}
func TestProgressBarChunkIndex(t *testing.T) {
layout := "2006-01-02T15:04:05.000Z"
p := P0d{
Config: Config{
Exec: Exec{
DurationSeconds: 300,
RampSeconds: 60,
},
},
}
p.Time.Start, _ = time.Parse(layout, "2022-01-01T00:00:00.000Z")
pb := ProgressBar{
curSecs: 0,
maxSecs: 300,
size: 30,
chunkProps: make([]ChunkProps, 30),
}
type chunkTest struct {
deltaSecond float64
wantIndex int
}
tests := []chunkTest{
{deltaSecond: 0.0, wantIndex: 0},
{deltaSecond: 0.01, wantIndex: 0},
{deltaSecond: 1.0, wantIndex: 0},
{deltaSecond: 9.9, wantIndex: 0},
{deltaSecond: 10, wantIndex: 1},
{deltaSecond: 59.9, wantIndex: 5},
{deltaSecond: 60, wantIndex: 6},
{deltaSecond: 240, wantIndex: 24},
{deltaSecond: 290, wantIndex: 29},
{deltaSecond: 300, wantIndex: 29},
}
for _, tc := range tests {
pns := p.Time.Start.Add(time.Duration(tc.deltaSecond * float64(time.Second)))
got := pb.chunkPropIndexFor(pns, &p)
if got != tc.wantIndex {
t.Errorf("bad chunk index for deltaSeconds %v, want %v got %v", tc.deltaSecond, tc.wantIndex, got)
}
}
}
func TestProgressBarRenderLength(t *testing.T) {
layout := "2006-01-02T15:04:05.000Z"
want := 30
p := P0d{
Config: Config{
Exec: Exec{
DurationSeconds: 300,
RampSeconds: 60,
},
},
}
p.Time.Start, _ = time.Parse(layout, "2022-01-01T00:00:00.000Z")
pb := ProgressBar{
curSecs: 0,
maxSecs: 300,
size: want,
chunkProps: make([]ChunkProps, want),
}
bar := stripansi.Strip(pb.render(p.Time.Start, &p))
b := strings.Index(bar, "[") + 1
e := strings.Index(bar, "]")
got := len(bar[b:e])
if got != want {
t.Errorf("bar length incorrect, want %v, but got %v", want, got)
}
}