-
Notifications
You must be signed in to change notification settings - Fork 3
/
writer_test.go
107 lines (90 loc) · 1.98 KB
/
writer_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
package progress
import (
"errors"
"io"
"strings"
"testing"
)
type writeResult struct {
n int
}
type writerState struct {
bytes int64
}
type writeStep struct {
result writeResult
expected writerState
}
func TestWriter(t *testing.T) {
cases := []struct {
name string
size int64
steps []writeStep
}{
{
name: "go case",
size: -1,
steps: []writeStep{
{
result: writeResult{n: 0},
expected: writerState{bytes: 0},
},
},
},
{
name: "multi step",
size: -1,
steps: []writeStep{
{
result: writeResult{n: 5},
expected: writerState{bytes: 5},
},
{
result: writeResult{n: 5},
expected: writerState{bytes: 10},
},
{
result: writeResult{n: 3},
expected: writerState{bytes: 13},
},
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
writer := NewWriter()
for i, s := range c.steps {
p := []byte(strings.Repeat(".", s.result.n))
actualN, actualErr := writer.Write(p)
if writer.Size() != c.size {
t.Fatalf("step %d: unexpected size: %d", i, writer.Size())
}
if s.result.n != actualN {
t.Fatalf("step %d: mismatched N: '%+v'!='%+v'", i, writer.Current(), actualN)
}
if !errors.Is(actualErr, nil) {
t.Fatalf("step %d: mismatched err: '%+v'!='%+v'", i, actualErr, nil)
}
if !errors.Is(writer.Error(), nil) {
t.Fatalf("step %d: mismatched cumulative err: '%+v'!='%+v'", i, writer.Error(), nil)
}
if writer.Current() != s.expected.bytes {
t.Fatalf("step %d: mismatched byte count: '%+v'!='%+v'", i, writer.Current(), s.expected.bytes)
}
}
})
}
}
func TestInitialConditions(t *testing.T) {
w := NewSizedWriter(42)
if w.Error() != nil {
t.Errorf("Expected nil error but was %+v", w.Error())
}
if w.Size() != 42 {
t.Errorf("Expect %d size but was %d", 42, w.Size())
}
w.SetComplete()
if !errors.Is(w.Error(), io.EOF) {
t.Errorf("SetCompleted should set EOF, but got %+v", w.Error())
}
}