Skip to content

Commit

Permalink
Make the parser accept index list expressions (#857)
Browse files Browse the repository at this point in the history
* Make the parser accept index list expressions
  • Loading branch information
volmedo authored Dec 4, 2024
1 parent 7d8df22 commit 0ba6fad
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ packages:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/example_project:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/index_list_expr:
config: *inpackage_config
github.com/vektra/mockery/v2/pkg/fixtures/issue845:
config:
<<: *inpackage_config
Expand Down
7 changes: 7 additions & 0 deletions pkg/fixtures/index_list_expr/index_list_expression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package index_list_expr

type GenericMultipleTypes[T1 any, T2 any, T3 any] interface {
Func(arg1 *T1, arg2 T2) T3
}

type IndexListExpr GenericMultipleTypes[int, string, bool]
35 changes: 35 additions & 0 deletions pkg/fixtures/index_list_expr/index_list_expression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package index_list_expr_test

import (
"context"
"testing"

"github.com/vektra/mockery/v2/pkg"

"github.com/stretchr/testify/require"
)

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/index_list_expr"}))
require.NoError(t, parser.Load(ctx))

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

func TestUsage(t *testing.T) {
gmt := NewMockGenericMultipleTypes[string, int, bool](t)
testString := "foo"
gmt.EXPECT().Func(&testString, 1).Return(false)
require.Equal(t, false, gmt.Func(&testString, 1))

ile := NewMockIndexListExpr(t)
testInt := 1
ile.EXPECT().Func(&testInt, "foo").Return(true)
require.Equal(t, true, ile.Func(&testInt, "foo"))
}
81 changes: 81 additions & 0 deletions pkg/fixtures/index_list_expr/mock_generic_multiple_types_test.go

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

79 changes: 79 additions & 0 deletions pkg/fixtures/index_list_expr/mock_index_list_expr_test.go

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

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:
case *ast.InterfaceType, *ast.IndexExpr, *ast.IndexListExpr:
nv.add(nv.ctx, n)
default:
log.Debug().Msg("found node with unacceptable type for mocking. Rejecting.")
Expand Down

0 comments on commit 0ba6fad

Please sign in to comment.