-
Notifications
You must be signed in to change notification settings - Fork 1
/
interfacegen_test.go
172 lines (137 loc) · 2.7 KB
/
interfacegen_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
166
167
168
169
170
171
172
package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
const (
test1Input = `package main
func main() {
}
// Hello, world!
type Foo struct {}
// Name prints Foo's name
func (f *Foo) Name() string {
return "foo"
}
`
test1Output = `package interfaces
// Hello, world!
type Foo interface {
// Name prints Foo's name
Name() string
}
`
test2Input = `package interfaces
//interfacegen:skip
type Foo struct {}
func (f *Foo) Name() string {
return "foo"
}
type Bar struct {}
func (b *Bar) Age() int {
return 3
}
`
test2Output = `package interfaces
type Bar interface {
Age() int
}
`
test3Input = `package main
func main() {
}
// Hello, world!
type Foo struct {}
// Name prints Foo's name
func (f *Foo) Name() string {
return "foo"
}
`
test3Output = `package interfaces
type Foo interface {
Name() string
}
`
test4Input = `package interfaces
type Foo int
func (f Foo) Val() int {
return f
}
// interfacegen:skip
func (f *Foo) Incr() {
*f++
}
`
test4Output = `package interfaces
type Foo interface {
Val() int
}
`
goMod = `module foo
go 1.16`
)
func TestInterfacegen(t *testing.T) {
tests := map[string]struct {
input string
output string
app *application
}{
"happy path": {
input: test1Input,
output: test1Output,
},
"type skip": {
input: test2Input,
output: test2Output,
},
"no comments": {
input: test3Input,
output: test3Output,
app: &application{
IncludeDocs: false,
},
},
"method skip": {
input: test4Input,
output: test4Output,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
require := require.New(t)
inputDir, err := os.MkdirTemp("", "")
require.NoError(err)
defer func() {
os.RemoveAll(inputDir)
}()
t.Logf("Created %s to store temporary input", inputDir)
require.NoError(os.Chdir(inputDir))
require.NoError(os.WriteFile(filepath.Join(inputDir, "main.go"), []byte(test.input), 0644))
require.NoError(os.WriteFile(filepath.Join(inputDir, "go.mod"), []byte(goMod), 0644))
outputFile, err := os.CreateTemp("", "")
require.NoError(err)
require.NoError(outputFile.Close())
t.Logf("Created %s to store temporary output", outputFile.Name())
var app application
if test.app == nil {
app = application{
IncludeDocs: true,
IncludeAllPackages: true,
}
} else {
app = *test.app
}
app.SrcPackage = "./"
app.DstPackage = "interfaces"
app.Output = outputFile.Name()
require.NoError(app.Run(context.Background()))
data, err := os.ReadFile(outputFile.Name())
require.NoError(err)
require.Equal(test.output, string(data))
require.NoError(os.Remove(outputFile.Name()))
})
}
}