Skip to content

Commit

Permalink
test: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jun 24, 2022
1 parent 1ec2d10 commit d4f0d83
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
12 changes: 5 additions & 7 deletions mocktail.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const commentTagPattern = "// mocktail:"

// PackageDesc represent a package.
type PackageDesc struct {
PkgName string
PkgPath string
Pkg *types.Package
Imports map[string]struct{}
Interfaces []InterfaceDesc
}
Expand Down Expand Up @@ -136,9 +135,8 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) {
continue
}

if packageDesc.PkgName == "" {
packageDesc.PkgName = lookup.Pkg().Name()
packageDesc.PkgPath = lookup.Pkg().Path()
if packageDesc.Pkg == nil {
packageDesc.Pkg = lookup.Pkg()
}

interfaceDesc := InterfaceDesc{Name: interfaceName}
Expand All @@ -150,7 +148,7 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) {

interfaceDesc.Methods = append(interfaceDesc.Methods, method)

for _, imp := range getMethodImports(method, packageDesc.PkgPath) {
for _, imp := range getMethodImports(method, packageDesc.Pkg.Name()) {
packageDesc.Imports[imp] = struct{}{}
}
}
Expand Down Expand Up @@ -252,7 +250,7 @@ func generate(model map[string]PackageDesc) error {
signature := method.Type().(*types.Signature)

syrup := Syrup{
PkgPath: pkgDesc.PkgPath,
PkgPath: pkgDesc.Pkg.Path(),
InterfaceName: interfaceDesc.Name,
Method: method,
Signature: signature,
Expand Down
27 changes: 21 additions & 6 deletions mocktail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (
"github.com/stretchr/testify/require"
)

const testRoot = "./testdata/src"

func TestMocktail(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip(runtime.GOOS)
}

testRoot := "./testdata/src"

dir, err := os.ReadDir(testRoot)
require.NoError(t, err)
dir, errR := os.ReadDir(testRoot)
require.NoError(t, errR)

for _, entry := range dir {
if !entry.IsDir() {
Expand All @@ -38,7 +38,7 @@ func TestMocktail(t *testing.T) {
require.NoError(t, err)
}

require.NoError(t, filepath.WalkDir(testRoot, func(path string, d fs.DirEntry, errW error) error {
errW := filepath.WalkDir(testRoot, func(path string, d fs.DirEntry, errW error) error {
if errW != nil {
return errW
}
Expand All @@ -61,5 +61,20 @@ func TestMocktail(t *testing.T) {
}

return nil
}))
})
require.NoError(t, errW)

for _, entry := range dir {
if !entry.IsDir() {
continue
}

cmd := exec.Command("go", "test", "-v")
cmd.Dir = filepath.Join(testRoot, entry.Name())

output, err := cmd.CombinedOutput()
t.Log(string(output))

require.NoError(t, err)
}
}
2 changes: 1 addition & 1 deletion syrup.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ func writeImports(writer io.Writer, descPkg PackageDesc) error {
}

data := map[string]interface{}{
"Name": descPkg.PkgName,
"Name": descPkg.Pkg.Name(),
"Imports": quickGoImports(descPkg),
}
return tmpl.Execute(writer, data)
Expand Down
6 changes: 4 additions & 2 deletions testdata/src/a/b/b.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package b

import "a"
import (
"a/c"
)

type Carrot interface {
Bar(string) *Potato
Bur(string) *a.Coconut
Bur(string) *c.Cherry
}

type Potato struct {
Expand Down
5 changes: 5 additions & 0 deletions testdata/src/a/c/c.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package c

type Cherry struct {
Name string
}
11 changes: 6 additions & 5 deletions testdata/src/a/mock_gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions testdata/src/a/mock_gen_test.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package a

import (
"a/b"
"a/c"
"bytes"
"context"
"testing"
Expand Down Expand Up @@ -3019,14 +3020,14 @@ func (_c *carrotBarCall) OnBurRaw(aParam interface{}) *carrotBurCall {
return _c.Parent.OnBurRaw(aParam)
}

func (_m *carrotMock) Bur(aParam string) *Coconut {
func (_m *carrotMock) Bur(aParam string) *c.Cherry {
_ret := _m.Called(aParam)

if _rf, ok := _ret.Get(0).(func(string) *Coconut); ok {
if _rf, ok := _ret.Get(0).(func(string) *c.Cherry); ok {
return _rf(aParam)
}

_ra0, _ := _ret.Get(0).(*Coconut)
_ra0, _ := _ret.Get(0).(*c.Cherry)

return _ra0
}
Expand Down Expand Up @@ -3084,12 +3085,12 @@ func (_c *carrotBurCall) Maybe() *carrotBurCall {
return _c
}

func (_c *carrotBurCall) TypedReturns(a *Coconut) *carrotBurCall {
func (_c *carrotBurCall) TypedReturns(a *c.Cherry) *carrotBurCall {
_c.Call = _c.Return(a)
return _c
}

func (_c *carrotBurCall) ReturnsFn(fn func(string) *Coconut) *carrotBurCall {
func (_c *carrotBurCall) ReturnsFn(fn func(string) *c.Cherry) *carrotBurCall {
_c.Call = _c.Return(fn)
return _c
}
Expand Down

0 comments on commit d4f0d83

Please sign in to comment.