-
Notifications
You must be signed in to change notification settings - Fork 1
/
changelog_test.go
113 lines (95 loc) · 2.25 KB
/
changelog_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
package main
import (
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"github.com/jianghushinian/blog-go-example/test/testable/reader/mocks"
)
func TestGetChangeLog(t *testing.T) {
expected := ChangeLogSpec{
Version: "v0.1.1",
ChangeLog: `
# Changelog
All notable changes to this project will be documented in this file.
`,
}
f, err := os.CreateTemp("", "TEST_CHANGELOG")
assert.NoError(t, err)
defer func() {
_ = f.Close()
_ = os.RemoveAll(f.Name())
}()
data := `
# Changelog
All notable changes to this project will be documented in this file.
`
_, err = f.WriteString(data)
assert.NoError(t, err)
_, _ = f.Seek(0, 0)
actual, err := GetChangeLog(f)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
type fakeReader struct {
data string
offset int
}
func NewFakeReader(input string) io.Reader {
return &fakeReader{
data: input,
offset: 0,
}
}
func (r *fakeReader) Read(p []byte) (int, error) {
if r.offset >= len(r.data) {
return 0, io.EOF // 表示数据已读取完毕
}
n := copy(p, r.data[r.offset:]) // 将数据从字符串复制到 p 中
r.offset += n
return n, nil
}
func TestGetChangeLogByIOReader(t *testing.T) {
expected := ChangeLogSpec{
Version: "v0.1.1",
ChangeLog: `
# Changelog
All notable changes to this project will be documented in this file.
`,
}
data := `
# Changelog
All notable changes to this project will be documented in this file.
`
reader := NewFakeReader(data)
actual, err := GetChangeLogByIOReader(reader)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
func TestGetChangeLogByIOReader_mock(t *testing.T) {
expected := ChangeLogSpec{
Version: "v0.1.1",
ChangeLog: `
# Changelog
All notable changes to this project will be documented in this file.
`,
}
data := `
# Changelog
All notable changes to this project will be documented in this file.
`
ctrl := gomock.NewController(t)
// reader := mocks.NewMockReaderWrapper(ctrl)
reader := mocks.NewMockIReader(ctrl)
reader.EXPECT().Read(gomock.Any()).DoAndReturn(func(p []byte) (int, error) {
copy(p, data)
return len(data), io.EOF
})
actual, err := GetChangeLogByIOReader(reader)
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
func init() {
version = "v0.1.1"
}