fixed #3 - correct type names
This commit is contained in:
parent
86b99f5859
commit
37723c8bd4
12
main.go
12
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...)
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user