@@ -43,6 +43,7 @@ const (
43
43
var (
44
44
source = flag .String ("source" , "" , "(source mode) Input Go source file; enables source mode." )
45
45
destination = flag .String ("destination" , "" , "Output file; defaults to stdout." )
46
+ mockNames = flag .String ("mockNames" , "" , "Comma-separated interfaceName=mockName pairs of explicit mock names to use. Mock names default to 'Mock'+ interfaceName suffix." )
46
47
packageOut = flag .String ("package" , "" , "Package of the generated code; defaults to the package of the input with a 'mock_' prefix." )
47
48
selfPackage = flag .String ("self_package" , "" , "If set, the package this mock will be part of." )
48
49
writePkgComment = flag .Bool ("write_package_comment" , true , "Writes package documentation comment (godoc) if true." )
@@ -98,13 +99,28 @@ func main() {
98
99
g .srcPackage = flag .Arg (0 )
99
100
g .srcInterfaces = flag .Arg (1 )
100
101
}
102
+
103
+ if * mockNames != "" {
104
+ g .mockNames = parseMockNames (* mockNames )
105
+ }
101
106
if err := g .Generate (pkg , packageName ); err != nil {
102
107
log .Fatalf ("Failed generating mock: %v" , err )
103
108
}
104
109
if _ , err := dst .Write (g .Output ()); err != nil {
105
110
log .Fatalf ("Failed writing to destination: %v" , err )
106
111
}
107
112
}
113
+ func parseMockNames (names string ) map [string ]string {
114
+ mocksMap := make (map [string ]string )
115
+ for _ , kv := range strings .Split (names , "," ) {
116
+ parts := strings .SplitN (kv , "=" , 2 )
117
+ if len (parts ) != 2 || parts [1 ] == "" {
118
+ log .Fatalf ("bad mock names spec: %v" , kv )
119
+ }
120
+ mocksMap [parts [0 ]] = parts [1 ]
121
+ }
122
+ return mocksMap
123
+ }
108
124
109
125
func usage () {
110
126
io .WriteString (os .Stderr , usageText )
@@ -129,11 +145,11 @@ Example:
129
145
`
130
146
131
147
type generator struct {
132
- buf bytes.Buffer
133
- indent string
134
-
135
- filename string // may be empty
136
- srcPackage , srcInterfaces string // may be empty
148
+ buf bytes.Buffer
149
+ indent string
150
+ mockNames map [ string ] string //may be empty
151
+ filename string // may be empty
152
+ srcPackage , srcInterfaces string // may be empty
137
153
138
154
packageMap map [string ]string // map from import path to package name
139
155
}
@@ -255,12 +271,16 @@ func (g *generator) Generate(pkg *model.Package, pkgName string) error {
255
271
}
256
272
257
273
// The name of the mock type to use for the given interface identifier.
258
- func mockName (typeName string ) string {
274
+ func (g * generator ) mockName (typeName string ) string {
275
+ if mockName , ok := g .mockNames [typeName ]; ok {
276
+ return mockName
277
+ }
278
+
259
279
return "Mock" + typeName
260
280
}
261
281
262
282
func (g * generator ) GenerateMockInterface (intf * model.Interface ) error {
263
- mockType := mockName (intf .Name )
283
+ mockType := g . mockName (intf .Name )
264
284
265
285
g .p ("" )
266
286
g .p ("// %v is a mock of %v interface" , mockType , intf .Name )
0 commit comments