From 37723c8bd422ddc8899c425fba354906188e350b Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Wed, 21 Sep 2016 21:39:26 +0100 Subject: [PATCH] fixed #3 - correct type names --- main.go | 12 ++++----- package/moq/moq.go | 59 ++++++++++++++++++++++++++++++----------- package/moq/moq_test.go | 54 +++++++++++++++++++++++++++++++++++-- 3 files changed, 101 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index b8a46dd..6326e9e 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,9 @@ package main -import ( - "github.com/matryer/moq/package/moq" -) - func main() { - - m := moq.New() - + // var ( + // ) + // out := os.Stdout + // m := moq.New(".") + // m.Mock(out, os.Args...) } diff --git a/package/moq/moq.go b/package/moq/moq.go index f1e217f..5bf4e66 100644 --- a/package/moq/moq.go +++ b/package/moq/moq.go @@ -1,6 +1,7 @@ package moq import ( + "errors" "fmt" "go/ast" "go/importer" @@ -15,14 +16,15 @@ import ( // Mocker can generate mock structs. type Mocker struct { - src string - tmpl *template.Template - fset *token.FileSet - pkgs map[string]*ast.Package + src string + tmpl *template.Template + fset *token.FileSet + pkgs map[string]*ast.Package + pkgName string } // New makes a new Mocker for the specified package directory. -func New(src string) (*Mocker, error) { +func New(src, packageName string) (*Mocker, error) { fset := token.NewFileSet() noTestFiles := func(i os.FileInfo) bool { return !strings.HasSuffix(i.Name(), "_test.go") @@ -31,15 +33,28 @@ func New(src string) (*Mocker, error) { if err != nil { return nil, err } + if len(packageName) == 0 { + for pkgName := range pkgs { + if strings.Contains(pkgName, "_test") { + continue + } + packageName = pkgName + break + } + } + if len(packageName) == 0 { + return nil, errors.New("failed to determine package name") + } tmpl, err := template.New("moq").Parse(moqTemplate) if err != nil { return nil, err } return &Mocker{ - src: src, - tmpl: tmpl, - fset: fset, - pkgs: pkgs, + src: src, + tmpl: tmpl, + fset: fset, + pkgs: pkgs, + pkgName: packageName, }, nil } @@ -74,20 +89,33 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error { Name: meth.Name(), } obj.Methods = append(obj.Methods, method) - method.Params = extractArgs(sig.Params(), "in%d") - method.Returns = extractArgs(sig.Results(), "out%d") + method.Params = m.extractArgs(sig.Params(), "in%d") + method.Returns = m.extractArgs(sig.Results(), "out%d") } objs = append(objs, obj) } } - err := m.tmpl.Execute(w, struct{ Objs []*obj }{Objs: objs}) + err := m.tmpl.Execute(w, struct { + PackageName string + Objs []*obj + }{ + PackageName: m.pkgName, + Objs: objs, + }) if err != nil { return err } return nil } -func extractArgs(list *types.Tuple, nameFormat string) []*param { +func (m *Mocker) packageQualifier(pkg *types.Package) string { + if m.pkgName == pkg.Name() { + return "" + } + return pkg.Name() +} + +func (m *Mocker) extractArgs(list *types.Tuple, nameFormat string) []*param { var params []*param for ii := 0; ii < list.Len(); ii++ { p := list.At(ii) @@ -95,9 +123,10 @@ func extractArgs(list *types.Tuple, nameFormat string) []*param { if name == "" { name = fmt.Sprintf(nameFormat, ii+1) } + typename := types.TypeString(p.Type(), m.packageQualifier) param := ¶m{ Name: name, - Type: p.Type().String(), + Type: typename, } params = append(params, param) } @@ -155,7 +184,7 @@ func (p param) TypeString() string { } var moqTemplate = ` -package todo +package {{.PackageName}} // AUTOGENERATED BY MOQ // github.com/matryer/moq diff --git a/package/moq/moq_test.go b/package/moq/moq_test.go index fb244d7..7249cf7 100644 --- a/package/moq/moq_test.go +++ b/package/moq/moq_test.go @@ -3,12 +3,13 @@ package moq import ( "bytes" "log" + "strings" "testing" ) func TestMoq(t *testing.T) { - m, err := New("../../example") + m, err := New("../../example", "") if err != nil { t.Errorf("moq.New: %s", err) } @@ -17,6 +18,55 @@ func TestMoq(t *testing.T) { if err != nil { t.Errorf("m.Mock: %s", err) } - log.Println(buf.String()) + s := buf.String() + + log.Println(s) + + // assertions of things that should be mentioned + var strs = []string{ + "package example", + "type PersonStoreMock struct", + "CreateFunc func(ctx context.Context, person *Person, confirm bool) error", + "GetFunc func(ctx context.Context, id string) (*Person, error)", + "func (mock *PersonStoreMock) Create(ctx context.Context, person *Person, confirm bool) error", + "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*Person, error)", + } + for _, str := range strs { + if !strings.Contains(s, str) { + t.Errorf("expected but missing: \"%s\"", str) + } + } + +} + +func TestMoqExplicitPackage(t *testing.T) { + + m, err := New("../../example", "different") + if err != nil { + t.Errorf("moq.New: %s", err) + } + var buf bytes.Buffer + err = m.Mock(&buf, "PersonStore") + if err != nil { + t.Errorf("m.Mock: %s", err) + } + s := buf.String() + + log.Println(s) + + // assertions of things that should be mentioned + var strs = []string{ + "package different", + "type PersonStoreMock struct", + "CreateFunc func(ctx context.Context, person *example.Person, confirm bool) error", + "GetFunc func(ctx context.Context, id string) (*example.Person, error)", + "func (mock *PersonStoreMock) Create(ctx context.Context, person *example.Person, confirm bool) error", + "func (mock *PersonStoreMock) Get(ctx context.Context, id string) (*example.Person, error)", + } + for _, str := range strs { + if !strings.Contains(s, str) { + t.Errorf("expected but missing: \"%s\"", str) + } + } }