Skip to content

Commit

Permalink
Add new expressions to allowed types (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgautier-dd authored Dec 24, 2024
1 parent 13d696b commit ede4f2c
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ packages:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/index_list_expr:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/issue845:
config:
<<: *inpackage_config
Expand Down
36 changes: 36 additions & 0 deletions pkg/fixtures/iface_new_type/iface_new_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package iface_new_type_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"github.com/vektra/mockery/v2/pkg"
)

func TestParsing(t *testing.T) {
parser := pkg.NewParser(nil)
ctx := context.Background()
require.NoError(t, parser.ParsePackages(ctx, []string{"github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type"}))
require.NoError(t, parser.Load(ctx))

for _, ifaceName := range []string{"Interface1", "Interface2", "Interface3"} {
iface, err := parser.Find(ifaceName)
require.NoError(t, err)
require.NotNil(t, iface)
}
}

func TestUsage(t *testing.T) {
interface1 := NewMockInterface1(t)
interface1.EXPECT().Method1().Return()
interface1.Method1()

interface2 := NewMockInterface2(t)
interface2.EXPECT().Method1().Return()
interface2.Method1()

interface3 := NewMockInterface3(t)
interface3.EXPECT().Method1().Return()
interface3.Method1()
}
12 changes: 12 additions & 0 deletions pkg/fixtures/iface_new_type/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package iface_new_type

import "github.com/vektra/mockery/v2/pkg/fixtures/iface_new_type/subpkg"

type Interface1 interface {
Method1()
}

type (
Interface2 Interface1
Interface3 subpkg.SubPkgInterface
)
64 changes: 64 additions & 0 deletions pkg/fixtures/iface_new_type/mock_interface_1_test.go

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

64 changes: 64 additions & 0 deletions pkg/fixtures/iface_new_type/mock_interface_2_test.go

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

64 changes: 64 additions & 0 deletions pkg/fixtures/iface_new_type/mock_interface_3_test.go

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

5 changes: 5 additions & 0 deletions pkg/fixtures/iface_new_type/subpkg/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package subpkg

type SubPkgInterface interface {
Method1()
}
2 changes: 1 addition & 1 deletion pkg/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (nv *NodeVisitor) Visit(node ast.Node) ast.Visitor {
break
}
nv.add(nv.ctx, n)
case *ast.InterfaceType, *ast.IndexExpr, *ast.IndexListExpr:
case *ast.InterfaceType, *ast.IndexExpr, *ast.IndexListExpr, *ast.SelectorExpr, *ast.Ident:
nv.add(nv.ctx, n)
default:
log.Debug().Msg("found node with unacceptable type for mocking. Rejecting.")
Expand Down

0 comments on commit ede4f2c

Please sign in to comment.