Merge pull request #26 from fvosberg/fix-emptyinterfaces
[PATCH] Omit sync package while mocking empty interface
This commit is contained in:
commit
3dc8438cf7
@ -71,6 +71,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
|||||||
PackageName: m.pkgName,
|
PackageName: m.pkgName,
|
||||||
Imports: moqImports,
|
Imports: moqImports,
|
||||||
}
|
}
|
||||||
|
mocksMethods := false
|
||||||
for _, pkg := range m.pkgs {
|
for _, pkg := range m.pkgs {
|
||||||
i := 0
|
i := 0
|
||||||
files := make([]*ast.File, len(pkg.Files))
|
files := make([]*ast.File, len(pkg.Files))
|
||||||
@ -96,6 +97,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
|||||||
InterfaceName: n,
|
InterfaceName: n,
|
||||||
}
|
}
|
||||||
for i := 0; i < iiface.NumMethods(); i++ {
|
for i := 0; i < iiface.NumMethods(); i++ {
|
||||||
|
mocksMethods = true
|
||||||
meth := iiface.Method(i)
|
meth := iiface.Method(i)
|
||||||
sig := meth.Type().(*types.Signature)
|
sig := meth.Type().(*types.Signature)
|
||||||
method := &method{
|
method := &method{
|
||||||
@ -108,6 +110,9 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
|||||||
doc.Objects = append(doc.Objects, obj)
|
doc.Objects = append(doc.Objects, obj)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if mocksMethods {
|
||||||
|
doc.Imports = append(doc.Imports, "sync")
|
||||||
|
}
|
||||||
for pkgToImport := range m.imports {
|
for pkgToImport := range m.imports {
|
||||||
doc.Imports = append(doc.Imports, pkgToImport)
|
doc.Imports = append(doc.Imports, pkgToImport)
|
||||||
}
|
}
|
||||||
|
@ -224,3 +224,19 @@ func TestDotImports(t *testing.T) {
|
|||||||
t.Error("contains invalid dot import")
|
t.Error("contains invalid dot import")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEmptyInterface(t *testing.T) {
|
||||||
|
m, err := New("testpackages/emptyinterface", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("moq.New: %s", err)
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err = m.Mock(&buf, "Empty")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("mock error: %s", err)
|
||||||
|
}
|
||||||
|
s := buf.String()
|
||||||
|
if strings.Contains(s, `"sync"`) {
|
||||||
|
t.Error("contains sync import, although this package isn't used")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package moq
|
package moq
|
||||||
|
|
||||||
// moqImports are the imports all moq files get.
|
// moqImports are the imports all moq files get.
|
||||||
var moqImports = []string{"sync"}
|
var moqImports = []string{}
|
||||||
|
|
||||||
// moqTemplate is the template for mocked code.
|
// moqTemplate is the template for mocked code.
|
||||||
var moqTemplate = `// Code generated by moq; DO NOT EDIT
|
var moqTemplate = `// Code generated by moq; DO NOT EDIT
|
||||||
@ -35,7 +35,7 @@ var (
|
|||||||
//
|
//
|
||||||
// // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}}
|
// // TODO: use mocked{{.InterfaceName}} in code that requires {{.InterfaceName}}
|
||||||
// // and then make assertions.
|
// // and then make assertions.
|
||||||
//
|
//
|
||||||
// }
|
// }
|
||||||
type {{.InterfaceName}}Mock struct {
|
type {{.InterfaceName}}Mock struct {
|
||||||
{{- range .Methods }}
|
{{- range .Methods }}
|
||||||
@ -43,7 +43,7 @@ type {{.InterfaceName}}Mock struct {
|
|||||||
{{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}}
|
{{.Name}}Func func({{ .Arglist }}) {{.ReturnArglist}}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
// calls tracks calls to the methods.
|
// calls tracks calls to the methods.
|
||||||
calls struct {
|
calls struct {
|
||||||
{{- range .Methods }}
|
{{- range .Methods }}
|
||||||
// {{ .Name }} holds details about calls to the {{.Name}} method.
|
// {{ .Name }} holds details about calls to the {{.Name}} method.
|
||||||
{{ .Name }} []struct {
|
{{ .Name }} []struct {
|
||||||
|
4
pkg/moq/testpackages/emptyinterface/empty.go
Normal file
4
pkg/moq/testpackages/emptyinterface/empty.go
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package emptyinterface
|
||||||
|
|
||||||
|
// Empty is an empty interface
|
||||||
|
type Empty interface{}
|
Loading…
Reference in New Issue
Block a user