Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 3fabe93

Browse files
authored
Merge pull request #1 from mariusstaicu/custom-mock-names
Custom mock names
2 parents c7d0ee7 + 87106cf commit 3fabe93

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ It supports the following flags:
7474

7575
* `-build_flags`: (reflect mode only) Flags passed verbatim to `go build`.
7676

77+
* `-mockNames`: A list of custom names for generated mocks. This is specified
78+
as a comma-separated list of elements of the form
79+
`Repository=MockSensorRepository,Endpoint=MockSensorEndpoint`, where
80+
`Repository` is the interface name and `MockSensorRepository` is the desired
81+
mock name (mock factory method and mock recorder will be named after the mock).
82+
If one of the interfaces has no custom name specified, then default naming
83+
convention will be used.
84+
7785
For an example of the use of `mockgen`, see the `sample/` directory. In simple
7886
cases, you will need only the `-source` flag.
7987

mockgen/mockgen.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343
var (
4444
source = flag.String("source", "", "(source mode) Input Go source file; enables source mode.")
4545
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.")
4647
packageOut = flag.String("package", "", "Package of the generated code; defaults to the package of the input with a 'mock_' prefix.")
4748
selfPackage = flag.String("self_package", "", "If set, the package this mock will be part of.")
4849
writePkgComment = flag.Bool("write_package_comment", true, "Writes package documentation comment (godoc) if true.")
@@ -98,13 +99,28 @@ func main() {
9899
g.srcPackage = flag.Arg(0)
99100
g.srcInterfaces = flag.Arg(1)
100101
}
102+
103+
if *mockNames != "" {
104+
g.mockNames = parseMockNames(*mockNames)
105+
}
101106
if err := g.Generate(pkg, packageName); err != nil {
102107
log.Fatalf("Failed generating mock: %v", err)
103108
}
104109
if _, err := dst.Write(g.Output()); err != nil {
105110
log.Fatalf("Failed writing to destination: %v", err)
106111
}
107112
}
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+
}
108124

109125
func usage() {
110126
io.WriteString(os.Stderr, usageText)
@@ -129,11 +145,11 @@ Example:
129145
`
130146

131147
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
137153

138154
packageMap map[string]string // map from import path to package name
139155
}
@@ -255,12 +271,16 @@ func (g *generator) Generate(pkg *model.Package, pkgName string) error {
255271
}
256272

257273
// 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+
259279
return "Mock" + typeName
260280
}
261281

262282
func (g *generator) GenerateMockInterface(intf *model.Interface) error {
263-
mockType := mockName(intf.Name)
283+
mockType := g.mockName(intf.Name)
264284

265285
g.p("")
266286
g.p("// %v is a mock of %v interface", mockType, intf.Name)

0 commit comments

Comments
 (0)