Better solution for #21

This commit is contained in:
Mat Ryer 2017-07-18 10:00:26 +01:00
parent 879118f880
commit e71b1c1ebe
6 changed files with 96 additions and 19 deletions

View File

@ -120,10 +120,14 @@ func (m *Mocker) packageQualifier(pkg *types.Package) string {
if m.pkgName == pkg.Name() {
return ""
}
path := pkg.Path()
if pkg.Path() == "." {
return ""
wd, err := os.Getwd()
if err == nil {
path = stripGopath(wd)
}
}
m.imports[pkg.Path()] = true
m.imports[path] = true
return pkg.Name()
}

View File

@ -196,7 +196,7 @@ func TestVendoredPackages(t *testing.T) {
// TestDotImports tests for https://github.com/matryer/moq/issues/21.
func TestDotImports(t *testing.T) {
err := os.Chdir("testpackages/issue-21")
err := os.Chdir("testpackages/dotimport")
if err != nil {
t.Errorf("Chdir: %s", err)
}
@ -210,7 +210,7 @@ func TestDotImports(t *testing.T) {
t.Errorf("mock error: %s", err)
}
s := buf.String()
if strings.Contains(s, `"."`) {
if !strings.Contains(s, `"github.com/matryer/moq/pkg/moq/testpackages/dotimport"`) {
t.Error("contains invalid dot import")
}
}

View File

@ -0,0 +1,14 @@
// Package dotimport addresses issue 21.
package dotimport
//go:generate moq -out service_moq_test.go -pkg dotimport_test . Service
// Service is the interface which should be mocked by moq
type Service interface {
User(ID string) (User, error)
}
// User is just a struct for testing
type User struct {
Name string
}

View File

@ -0,0 +1,73 @@
// Code generated by moq; DO NOT EDIT
// github.com/matryer/moq
package dotimport_test
import (
"dotimport"
"sync"
)
var (
lockServiceMockUser sync.RWMutex
)
// ServiceMock is a mock implementation of Service.
//
// func TestSomethingThatUsesService(t *testing.T) {
//
// // make and configure a mocked Service
// mockedService := &ServiceMock{
// UserFunc: func(ID string) (dotimport.User, error) {
// panic("TODO: mock out the User method")
// },
// }
//
// // TODO: use mockedService in code that requires Service
// // and then make assertions.
//
// }
type ServiceMock struct {
// UserFunc mocks the User method.
UserFunc func(ID string) (dotimport.User, error)
// calls tracks calls to the methods.
calls struct {
// User holds details about calls to the User method.
User []struct {
// ID is the ID argument value.
ID string
}
}
}
// User calls UserFunc.
func (mock *ServiceMock) User(ID string) (dotimport.User, error) {
if mock.UserFunc == nil {
panic("moq: ServiceMock.UserFunc is nil but Service.User was just called")
}
callInfo := struct {
ID string
}{
ID: ID,
}
lockServiceMockUser.Lock()
mock.calls.User = append(mock.calls.User, callInfo)
lockServiceMockUser.Unlock()
return mock.UserFunc(ID)
}
// UserCalls gets all the calls that were made to User.
// Check the length with:
// len(mockedService.UserCalls())
func (mock *ServiceMock) UserCalls() []struct {
ID string
} {
var calls []struct {
ID string
}
lockServiceMockUser.RLock()
calls = mock.calls.User
lockServiceMockUser.RUnlock()
return calls
}

View File

@ -1,4 +1,4 @@
package moqtest_test
package dotimport_test
import "testing"

View File

@ -1,14 +0,0 @@
// Package moqtest is just a package to try getting started with moq without the overhead of a real package
package moqtest
//go:generate moq -out service_moq_test.go -pkg moqtest_test . Service
// Service is the interface which should be mocked by moq
type Service interface {
User(ID string) (User, error)
}
// User is just a struct for testing
type User struct {
Name string
}