diff --git a/go.mod b/go.mod index a2ccae1..03fa590 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,11 @@ module go.uber.org/mock go 1.19 require ( - golang.org/x/mod v0.5.1 - golang.org/x/tools v0.1.8 + golang.org/x/mod v0.11.0 + golang.org/x/tools v0.2.0 ) require ( - github.com/yuin/goldmark v1.4.1 // indirect - golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + github.com/yuin/goldmark v1.4.13 // indirect + golang.org/x/sys v0.1.0 // indirect ) diff --git a/go.sum b/go.sum index 6255813..38c7646 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,8 @@ -github.com/yuin/goldmark v1.4.1 h1:/vn0k+RBvwlxEmP5E7SZMqNxPhfMVFEJiykr15/0XKM= -github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= -golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w= -golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU= +golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE= +golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= diff --git a/mockgen/generic_go118.go b/mockgen/generic_go118.go index 917a15e..3b8ba6b 100644 --- a/mockgen/generic_go118.go +++ b/mockgen/generic_go118.go @@ -11,6 +11,7 @@ package main import ( + "fmt" "go/ast" "strings" @@ -24,7 +25,7 @@ func getTypeSpecTypeParams(ts *ast.TypeSpec) []*ast.Field { return ts.TypeParams.List } -func (p *fileParser) parseGenericType(pkg string, typ ast.Expr, tps map[string]bool) (model.Type, error) { +func (p *fileParser) parseGenericType(pkg string, typ ast.Expr, tps map[string]model.Type) (model.Type, error) { switch v := typ.(type) { case *ast.IndexExpr: m, err := p.parseType(pkg, v.X, tps) @@ -86,3 +87,30 @@ func getIdentTypeParams(decl interface{}) string { sb.WriteString("]") return sb.String() } + +func (p *fileParser) parseGenericMethod(field *ast.Field, it *namedInterface, iface *model.Interface, pkg string, tps map[string]model.Type) ([]*model.Method, error) { + var indices []ast.Expr + var typ ast.Expr + switch v := field.Type.(type) { + case *ast.IndexExpr: + indices = []ast.Expr{v.Index} + typ = v.X + case *ast.IndexListExpr: + indices = v.Indices + typ = v.X + default: + return nil, fmt.Errorf("don't know how to mock method of type %T", field.Type) + } + + nf := &ast.Field{ + Doc: field.Comment, + Names: field.Names, + Type: typ, + Tag: field.Tag, + Comment: field.Comment, + } + + it.embeddedInstTypeParams = indices + + return p.parseMethod(nf, it, iface, pkg, tps) +} diff --git a/mockgen/generic_notgo118.go b/mockgen/generic_notgo118.go index b608438..b991b64 100644 --- a/mockgen/generic_notgo118.go +++ b/mockgen/generic_notgo118.go @@ -18,6 +18,7 @@ package main import ( + "fmt" "go/ast" "go.uber.org/mock/mockgen/model" @@ -27,10 +28,14 @@ func getTypeSpecTypeParams(ts *ast.TypeSpec) []*ast.Field { return nil } -func (p *fileParser) parseGenericType(pkg string, typ ast.Expr, tps map[string]bool) (model.Type, error) { +func (p *fileParser) parseGenericType(pkg string, typ ast.Expr, tps map[string]model.Type) (model.Type, error) { return nil, nil } func getIdentTypeParams(decl interface{}) string { return "" } + +func (p *fileParser) parseGenericMethod(field *ast.Field, it *namedInterface, iface *model.Interface, pkg string, tps map[string]model.Type) ([]*model.Method, error) { + return nil, fmt.Errorf("don't know how to mock method of type %T", field.Type) +} diff --git a/mockgen/internal/tests/generics/external.go b/mockgen/internal/tests/generics/external.go index 68b2d4b..a12c39f 100644 --- a/mockgen/internal/tests/generics/external.go +++ b/mockgen/internal/tests/generics/external.go @@ -1,13 +1,16 @@ package generics import ( + "context" + "io" + "go.uber.org/mock/mockgen/internal/tests/generics/other" "golang.org/x/exp/constraints" ) -//go:generate mockgen --source=external.go --destination=source/mock_external_test.go --package source +//go:generate mockgen --source=external.go --destination=source/mock_external_mock.go --package source -type ExternalConstraint[I constraints.Integer, F constraints.Float] interface { +type ExternalConstraint[I constraints.Integer, F any] interface { One(string) string Two(I) string Three(I) F @@ -18,4 +21,23 @@ type ExternalConstraint[I constraints.Integer, F constraints.Float] interface { Eight(F) other.Two[I, F] Nine(Iface[I]) Ten(*I) + Eleven() map[string]I + Twelve(ctx context.Context) <-chan []I + Thirteen(...I) *F +} + +type EmbeddingIface[T constraints.Integer, R constraints.Float] interface { + io.Reader + Generator[R] + Earth[Generator[T]] + other.Either[R, StructType, other.Five, Generator[T]] + ExternalConstraint[T, R] +} + +type Generator[T any] interface { + Generate() T +} + +type Group[T Generator[any]] interface { + Join(ctx context.Context) []T } diff --git a/mockgen/internal/tests/generics/generics.go b/mockgen/internal/tests/generics/generics.go index 647a6fc..5ee5692 100644 --- a/mockgen/internal/tests/generics/generics.go +++ b/mockgen/internal/tests/generics/generics.go @@ -1,8 +1,11 @@ package generics -import "go.uber.org/mock/mockgen/internal/tests/generics/other" +import ( + "go.uber.org/mock/mockgen/internal/tests/generics/other" + "golang.org/x/exp/constraints" +) -//go:generate mockgen --source=generics.go --destination=source/mock_generics_test.go --package source +//go:generate mockgen --source=generics.go --destination=source/mock_generics_mock.go --package source ////go:generate mockgen --destination=reflect/mock_test.go --package reflect . Bar,Bar2 type Bar[T any, R any] interface { @@ -38,3 +41,19 @@ type StructType struct{} type StructType2 struct{} type AliasType Baz[other.Three] + +type Universe[T constraints.Signed] interface { + MilkyWay[T] +} + +type MilkyWay[R constraints.Integer] interface { + SolarSystem[R] +} + +type SolarSystem[T constraints.Ordered] interface { + Earth[T] +} + +type Earth[R any] interface { + Water(R) []R +} diff --git a/mockgen/internal/tests/generics/other/other.go b/mockgen/internal/tests/generics/other/other.go index 9265422..6de7fe1 100644 --- a/mockgen/internal/tests/generics/other/other.go +++ b/mockgen/internal/tests/generics/other/other.go @@ -9,3 +9,10 @@ type Three struct{} type Four struct{} type Five interface{} + +type Either[T, R, K, V any] interface { + First() T + Second() R + Third() K + Fourth() V +} diff --git a/mockgen/internal/tests/generics/source/mock_external_mock.go b/mockgen/internal/tests/generics/source/mock_external_mock.go new file mode 100644 index 0000000..62f42d5 --- /dev/null +++ b/mockgen/internal/tests/generics/source/mock_external_mock.go @@ -0,0 +1,598 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: external.go + +// Package source is a generated GoMock package. +package source + +import ( + context "context" + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + generics "go.uber.org/mock/mockgen/internal/tests/generics" + other "go.uber.org/mock/mockgen/internal/tests/generics/other" + constraints "golang.org/x/exp/constraints" +) + +// MockExternalConstraint is a mock of ExternalConstraint interface. +type MockExternalConstraint[I constraints.Integer, F any] struct { + ctrl *gomock.Controller + recorder *MockExternalConstraintMockRecorder[I, F] +} + +// MockExternalConstraintMockRecorder is the mock recorder for MockExternalConstraint. +type MockExternalConstraintMockRecorder[I constraints.Integer, F any] struct { + mock *MockExternalConstraint[I, F] +} + +// NewMockExternalConstraint creates a new mock instance. +func NewMockExternalConstraint[I constraints.Integer, F any](ctrl *gomock.Controller) *MockExternalConstraint[I, F] { + mock := &MockExternalConstraint[I, F]{ctrl: ctrl} + mock.recorder = &MockExternalConstraintMockRecorder[I, F]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockExternalConstraint[I, F]) EXPECT() *MockExternalConstraintMockRecorder[I, F] { + return m.recorder +} + +// Eight mocks base method. +func (m *MockExternalConstraint[I, F]) Eight(arg0 F) other.Two[I, F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eight", arg0) + ret0, _ := ret[0].(other.Two[I, F]) + return ret0 +} + +// Eight indicates an expected call of Eight. +func (mr *MockExternalConstraintMockRecorder[I, F]) Eight(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eight", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Eight), arg0) +} + +// Eleven mocks base method. +func (m *MockExternalConstraint[I, F]) Eleven() map[string]I { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eleven") + ret0, _ := ret[0].(map[string]I) + return ret0 +} + +// Eleven indicates an expected call of Eleven. +func (mr *MockExternalConstraintMockRecorder[I, F]) Eleven() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eleven", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Eleven)) +} + +// Five mocks base method. +func (m *MockExternalConstraint[I, F]) Five(arg0 I) generics.Baz[F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Five", arg0) + ret0, _ := ret[0].(generics.Baz[F]) + return ret0 +} + +// Five indicates an expected call of Five. +func (mr *MockExternalConstraintMockRecorder[I, F]) Five(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Five", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Five), arg0) +} + +// Four mocks base method. +func (m *MockExternalConstraint[I, F]) Four(arg0 I) generics.Foo[I, F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Four", arg0) + ret0, _ := ret[0].(generics.Foo[I, F]) + return ret0 +} + +// Four indicates an expected call of Four. +func (mr *MockExternalConstraintMockRecorder[I, F]) Four(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Four", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Four), arg0) +} + +// Nine mocks base method. +func (m *MockExternalConstraint[I, F]) Nine(arg0 generics.Iface[I]) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Nine", arg0) +} + +// Nine indicates an expected call of Nine. +func (mr *MockExternalConstraintMockRecorder[I, F]) Nine(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Nine", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Nine), arg0) +} + +// One mocks base method. +func (m *MockExternalConstraint[I, F]) One(arg0 string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "One", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// One indicates an expected call of One. +func (mr *MockExternalConstraintMockRecorder[I, F]) One(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "One", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).One), arg0) +} + +// Seven mocks base method. +func (m *MockExternalConstraint[I, F]) Seven(arg0 I) other.One[I] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Seven", arg0) + ret0, _ := ret[0].(other.One[I]) + return ret0 +} + +// Seven indicates an expected call of Seven. +func (mr *MockExternalConstraintMockRecorder[I, F]) Seven(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Seven", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Seven), arg0) +} + +// Six mocks base method. +func (m *MockExternalConstraint[I, F]) Six(arg0 I) *generics.Baz[F] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Six", arg0) + ret0, _ := ret[0].(*generics.Baz[F]) + return ret0 +} + +// Six indicates an expected call of Six. +func (mr *MockExternalConstraintMockRecorder[I, F]) Six(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Six", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Six), arg0) +} + +// Ten mocks base method. +func (m *MockExternalConstraint[I, F]) Ten(arg0 *I) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Ten", arg0) +} + +// Ten indicates an expected call of Ten. +func (mr *MockExternalConstraintMockRecorder[I, F]) Ten(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ten", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Ten), arg0) +} + +// Thirteen mocks base method. +func (m *MockExternalConstraint[I, F]) Thirteen(arg0 ...I) *F { + m.ctrl.T.Helper() + varargs := []interface{}{} + for _, a := range arg0 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "Thirteen", varargs...) + ret0, _ := ret[0].(*F) + return ret0 +} + +// Thirteen indicates an expected call of Thirteen. +func (mr *MockExternalConstraintMockRecorder[I, F]) Thirteen(arg0 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Thirteen", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Thirteen), arg0...) +} + +// Three mocks base method. +func (m *MockExternalConstraint[I, F]) Three(arg0 I) F { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Three", arg0) + ret0, _ := ret[0].(F) + return ret0 +} + +// Three indicates an expected call of Three. +func (mr *MockExternalConstraintMockRecorder[I, F]) Three(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Three", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Three), arg0) +} + +// Twelve mocks base method. +func (m *MockExternalConstraint[I, F]) Twelve(ctx context.Context) <-chan []I { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Twelve", ctx) + ret0, _ := ret[0].(<-chan []I) + return ret0 +} + +// Twelve indicates an expected call of Twelve. +func (mr *MockExternalConstraintMockRecorder[I, F]) Twelve(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Twelve", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Twelve), ctx) +} + +// Two mocks base method. +func (m *MockExternalConstraint[I, F]) Two(arg0 I) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Two", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// Two indicates an expected call of Two. +func (mr *MockExternalConstraintMockRecorder[I, F]) Two(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Two", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Two), arg0) +} + +// MockEmbeddingIface is a mock of EmbeddingIface interface. +type MockEmbeddingIface[T constraints.Integer, R constraints.Float] struct { + ctrl *gomock.Controller + recorder *MockEmbeddingIfaceMockRecorder[T, R] +} + +// MockEmbeddingIfaceMockRecorder is the mock recorder for MockEmbeddingIface. +type MockEmbeddingIfaceMockRecorder[T constraints.Integer, R constraints.Float] struct { + mock *MockEmbeddingIface[T, R] +} + +// NewMockEmbeddingIface creates a new mock instance. +func NewMockEmbeddingIface[T constraints.Integer, R constraints.Float](ctrl *gomock.Controller) *MockEmbeddingIface[T, R] { + mock := &MockEmbeddingIface[T, R]{ctrl: ctrl} + mock.recorder = &MockEmbeddingIfaceMockRecorder[T, R]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockEmbeddingIface[T, R]) EXPECT() *MockEmbeddingIfaceMockRecorder[T, R] { + return m.recorder +} + +// Eight mocks base method. +func (m *MockEmbeddingIface[T, R]) Eight(arg0 R) other.Two[T, R] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eight", arg0) + ret0, _ := ret[0].(other.Two[T, R]) + return ret0 +} + +// Eight indicates an expected call of Eight. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Eight(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eight", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Eight), arg0) +} + +// Eleven mocks base method. +func (m *MockEmbeddingIface[T, R]) Eleven() map[string]T { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Eleven") + ret0, _ := ret[0].(map[string]T) + return ret0 +} + +// Eleven indicates an expected call of Eleven. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Eleven() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eleven", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Eleven)) +} + +// First mocks base method. +func (m *MockEmbeddingIface[T, R]) First() R { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "First") + ret0, _ := ret[0].(R) + return ret0 +} + +// First indicates an expected call of First. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) First() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "First", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).First)) +} + +// Five mocks base method. +func (m *MockEmbeddingIface[T, R]) Five(arg0 T) generics.Baz[R] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Five", arg0) + ret0, _ := ret[0].(generics.Baz[R]) + return ret0 +} + +// Five indicates an expected call of Five. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Five(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Five", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Five), arg0) +} + +// Four mocks base method. +func (m *MockEmbeddingIface[T, R]) Four(arg0 T) generics.Foo[T, R] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Four", arg0) + ret0, _ := ret[0].(generics.Foo[T, R]) + return ret0 +} + +// Four indicates an expected call of Four. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Four(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Four", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Four), arg0) +} + +// Fourth mocks base method. +func (m *MockEmbeddingIface[T, R]) Fourth() generics.Generator[T] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Fourth") + ret0, _ := ret[0].(generics.Generator[T]) + return ret0 +} + +// Fourth indicates an expected call of Fourth. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Fourth() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Fourth", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Fourth)) +} + +// Generate mocks base method. +func (m *MockEmbeddingIface[T, R]) Generate() R { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Generate") + ret0, _ := ret[0].(R) + return ret0 +} + +// Generate indicates an expected call of Generate. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Generate() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Generate", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Generate)) +} + +// Nine mocks base method. +func (m *MockEmbeddingIface[T, R]) Nine(arg0 generics.Iface[T]) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Nine", arg0) +} + +// Nine indicates an expected call of Nine. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Nine(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Nine", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Nine), arg0) +} + +// One mocks base method. +func (m *MockEmbeddingIface[T, R]) One(arg0 string) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "One", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// One indicates an expected call of One. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) One(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "One", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).One), arg0) +} + +// Read mocks base method. +func (m *MockEmbeddingIface[T, R]) Read(p []byte) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Read", p) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Read indicates an expected call of Read. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Read(p interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Read), p) +} + +// Second mocks base method. +func (m *MockEmbeddingIface[T, R]) Second() generics.StructType { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Second") + ret0, _ := ret[0].(generics.StructType) + return ret0 +} + +// Second indicates an expected call of Second. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Second() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Second", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Second)) +} + +// Seven mocks base method. +func (m *MockEmbeddingIface[T, R]) Seven(arg0 T) other.One[T] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Seven", arg0) + ret0, _ := ret[0].(other.One[T]) + return ret0 +} + +// Seven indicates an expected call of Seven. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Seven(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Seven", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Seven), arg0) +} + +// Six mocks base method. +func (m *MockEmbeddingIface[T, R]) Six(arg0 T) *generics.Baz[R] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Six", arg0) + ret0, _ := ret[0].(*generics.Baz[R]) + return ret0 +} + +// Six indicates an expected call of Six. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Six(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Six", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Six), arg0) +} + +// Ten mocks base method. +func (m *MockEmbeddingIface[T, R]) Ten(arg0 *T) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Ten", arg0) +} + +// Ten indicates an expected call of Ten. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Ten(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ten", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Ten), arg0) +} + +// Third mocks base method. +func (m *MockEmbeddingIface[T, R]) Third() other.Five { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Third") + ret0, _ := ret[0].(other.Five) + return ret0 +} + +// Third indicates an expected call of Third. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Third() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Third", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Third)) +} + +// Thirteen mocks base method. +func (m *MockEmbeddingIface[T, R]) Thirteen(arg0 ...T) *R { + m.ctrl.T.Helper() + varargs := []interface{}{} + for _, a := range arg0 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "Thirteen", varargs...) + ret0, _ := ret[0].(*R) + return ret0 +} + +// Thirteen indicates an expected call of Thirteen. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Thirteen(arg0 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Thirteen", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Thirteen), arg0...) +} + +// Three mocks base method. +func (m *MockEmbeddingIface[T, R]) Three(arg0 T) R { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Three", arg0) + ret0, _ := ret[0].(R) + return ret0 +} + +// Three indicates an expected call of Three. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Three(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Three", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Three), arg0) +} + +// Twelve mocks base method. +func (m *MockEmbeddingIface[T, R]) Twelve(ctx context.Context) <-chan []T { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Twelve", ctx) + ret0, _ := ret[0].(<-chan []T) + return ret0 +} + +// Twelve indicates an expected call of Twelve. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Twelve(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Twelve", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Twelve), ctx) +} + +// Two mocks base method. +func (m *MockEmbeddingIface[T, R]) Two(arg0 T) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Two", arg0) + ret0, _ := ret[0].(string) + return ret0 +} + +// Two indicates an expected call of Two. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Two(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Two", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Two), arg0) +} + +// Water mocks base method. +func (m *MockEmbeddingIface[T, R]) Water(arg0 generics.Generator[T]) []generics.Generator[T] { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Water", arg0) + ret0, _ := ret[0].([]generics.Generator[T]) + return ret0 +} + +// Water indicates an expected call of Water. +func (mr *MockEmbeddingIfaceMockRecorder[T, R]) Water(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Water", reflect.TypeOf((*MockEmbeddingIface[T, R])(nil).Water), arg0) +} + +// MockGenerator is a mock of Generator interface. +type MockGenerator[T any] struct { + ctrl *gomock.Controller + recorder *MockGeneratorMockRecorder[T] +} + +// MockGeneratorMockRecorder is the mock recorder for MockGenerator. +type MockGeneratorMockRecorder[T any] struct { + mock *MockGenerator[T] +} + +// NewMockGenerator creates a new mock instance. +func NewMockGenerator[T any](ctrl *gomock.Controller) *MockGenerator[T] { + mock := &MockGenerator[T]{ctrl: ctrl} + mock.recorder = &MockGeneratorMockRecorder[T]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockGenerator[T]) EXPECT() *MockGeneratorMockRecorder[T] { + return m.recorder +} + +// Generate mocks base method. +func (m *MockGenerator[T]) Generate() T { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Generate") + ret0, _ := ret[0].(T) + return ret0 +} + +// Generate indicates an expected call of Generate. +func (mr *MockGeneratorMockRecorder[T]) Generate() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Generate", reflect.TypeOf((*MockGenerator[T])(nil).Generate)) +} + +// MockGroup is a mock of Group interface. +type MockGroup[T generics.Generator[any]] struct { + ctrl *gomock.Controller + recorder *MockGroupMockRecorder[T] +} + +// MockGroupMockRecorder is the mock recorder for MockGroup. +type MockGroupMockRecorder[T generics.Generator[any]] struct { + mock *MockGroup[T] +} + +// NewMockGroup creates a new mock instance. +func NewMockGroup[T generics.Generator[any]](ctrl *gomock.Controller) *MockGroup[T] { + mock := &MockGroup[T]{ctrl: ctrl} + mock.recorder = &MockGroupMockRecorder[T]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockGroup[T]) EXPECT() *MockGroupMockRecorder[T] { + return m.recorder +} + +// Join mocks base method. +func (m *MockGroup[T]) Join(ctx context.Context) []T { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Join", ctx) + ret0, _ := ret[0].([]T) + return ret0 +} + +// Join indicates an expected call of Join. +func (mr *MockGroupMockRecorder[T]) Join(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Join", reflect.TypeOf((*MockGroup[T])(nil).Join), ctx) +} diff --git a/mockgen/internal/tests/generics/source/mock_external_test.go b/mockgen/internal/tests/generics/source/mock_external_test.go index 32a876b..c345164 100644 --- a/mockgen/internal/tests/generics/source/mock_external_test.go +++ b/mockgen/internal/tests/generics/source/mock_external_test.go @@ -1,173 +1,41 @@ -// Code generated by MockGen. DO NOT EDIT. -// Source: external.go - -// Package source is a generated GoMock package. package source import ( - reflect "reflect" + "context" + "testing" - gomock "go.uber.org/mock/gomock" - generics "go.uber.org/mock/mockgen/internal/tests/generics" - other "go.uber.org/mock/mockgen/internal/tests/generics/other" - constraints "golang.org/x/exp/constraints" + "go.uber.org/mock/gomock" + "go.uber.org/mock/mockgen/internal/tests/generics" ) -// MockExternalConstraint is a mock of ExternalConstraint interface. -type MockExternalConstraint[I constraints.Integer, F constraints.Float] struct { - ctrl *gomock.Controller - recorder *MockExternalConstraintMockRecorder[I, F] -} - -// MockExternalConstraintMockRecorder is the mock recorder for MockExternalConstraint. -type MockExternalConstraintMockRecorder[I constraints.Integer, F constraints.Float] struct { - mock *MockExternalConstraint[I, F] -} - -// NewMockExternalConstraint creates a new mock instance. -func NewMockExternalConstraint[I constraints.Integer, F constraints.Float](ctrl *gomock.Controller) *MockExternalConstraint[I, F] { - mock := &MockExternalConstraint[I, F]{ctrl: ctrl} - mock.recorder = &MockExternalConstraintMockRecorder[I, F]{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockExternalConstraint[I, F]) EXPECT() *MockExternalConstraintMockRecorder[I, F] { - return m.recorder -} - -// Eight mocks base method. -func (m *MockExternalConstraint[I, F]) Eight(arg0 F) other.Two[I, F] { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Eight", arg0) - ret0, _ := ret[0].(other.Two[I, F]) - return ret0 -} - -// Eight indicates an expected call of Eight. -func (mr *MockExternalConstraintMockRecorder[I, F]) Eight(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Eight", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Eight), arg0) -} - -// Five mocks base method. -func (m *MockExternalConstraint[I, F]) Five(arg0 I) generics.Baz[F] { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Five", arg0) - ret0, _ := ret[0].(generics.Baz[F]) - return ret0 -} - -// Five indicates an expected call of Five. -func (mr *MockExternalConstraintMockRecorder[I, F]) Five(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Five", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Five), arg0) -} - -// Four mocks base method. -func (m *MockExternalConstraint[I, F]) Four(arg0 I) generics.Foo[I, F] { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Four", arg0) - ret0, _ := ret[0].(generics.Foo[I, F]) - return ret0 -} - -// Four indicates an expected call of Four. -func (mr *MockExternalConstraintMockRecorder[I, F]) Four(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Four", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Four), arg0) -} - -// Nine mocks base method. -func (m *MockExternalConstraint[I, F]) Nine(arg0 generics.Iface[I]) { - m.ctrl.T.Helper() - m.ctrl.Call(m, "Nine", arg0) -} - -// Nine indicates an expected call of Nine. -func (mr *MockExternalConstraintMockRecorder[I, F]) Nine(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Nine", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Nine), arg0) -} - -// One mocks base method. -func (m *MockExternalConstraint[I, F]) One(arg0 string) string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "One", arg0) - ret0, _ := ret[0].(string) - return ret0 -} - -// One indicates an expected call of One. -func (mr *MockExternalConstraintMockRecorder[I, F]) One(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "One", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).One), arg0) -} - -// Seven mocks base method. -func (m *MockExternalConstraint[I, F]) Seven(arg0 I) other.One[I] { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Seven", arg0) - ret0, _ := ret[0].(other.One[I]) - return ret0 -} - -// Seven indicates an expected call of Seven. -func (mr *MockExternalConstraintMockRecorder[I, F]) Seven(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Seven", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Seven), arg0) -} - -// Six mocks base method. -func (m *MockExternalConstraint[I, F]) Six(arg0 I) *generics.Baz[F] { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Six", arg0) - ret0, _ := ret[0].(*generics.Baz[F]) - return ret0 -} - -// Six indicates an expected call of Six. -func (mr *MockExternalConstraintMockRecorder[I, F]) Six(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Six", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Six), arg0) -} +func TestMockEmbeddingIface_One(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() -// Ten mocks base method. -func (m *MockExternalConstraint[I, F]) Ten(arg0 *I) { - m.ctrl.T.Helper() - m.ctrl.Call(m, "Ten", arg0) + m := NewMockEmbeddingIface[int, float64](ctrl) + m.EXPECT().One("foo").Return("bar") + if v := m.One("foo"); v != "bar" { + t.Errorf("One() = %v, want %v", v, "bar") + } } -// Ten indicates an expected call of Ten. -func (mr *MockExternalConstraintMockRecorder[I, F]) Ten(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ten", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Ten), arg0) -} +func TestMockUniverse_Water(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() -// Three mocks base method. -func (m *MockExternalConstraint[I, F]) Three(arg0 I) F { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Three", arg0) - ret0, _ := ret[0].(F) - return ret0 + m := NewMockUniverse[int](ctrl) + m.EXPECT().Water(1024) + m.Water(1024) } -// Three indicates an expected call of Three. -func (mr *MockExternalConstraintMockRecorder[I, F]) Three(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Three", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Three), arg0) -} - -// Two mocks base method. -func (m *MockExternalConstraint[I, F]) Two(arg0 I) string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Two", arg0) - ret0, _ := ret[0].(string) - return ret0 -} +func TestNewMockGroup_Join(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() -// Two indicates an expected call of Two. -func (mr *MockExternalConstraintMockRecorder[I, F]) Two(arg0 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Two", reflect.TypeOf((*MockExternalConstraint[I, F])(nil).Two), arg0) + m := NewMockGroup[generics.Generator[any]](ctrl) + ctx := context.TODO() + m.EXPECT().Join(ctx).Return(nil) + if v := m.Join(ctx); v != nil { + t.Errorf("Join() = %v, want %v", v, nil) + } } diff --git a/mockgen/internal/tests/generics/source/mock_generics_test.go b/mockgen/internal/tests/generics/source/mock_generics_mock.go similarity index 69% rename from mockgen/internal/tests/generics/source/mock_generics_test.go rename to mockgen/internal/tests/generics/source/mock_generics_mock.go index ad34340..db27915 100644 --- a/mockgen/internal/tests/generics/source/mock_generics_test.go +++ b/mockgen/internal/tests/generics/source/mock_generics_mock.go @@ -10,6 +10,7 @@ import ( gomock "go.uber.org/mock/gomock" generics "go.uber.org/mock/mockgen/internal/tests/generics" other "go.uber.org/mock/mockgen/internal/tests/generics/other" + constraints "golang.org/x/exp/constraints" ) // MockBar is a mock of Bar interface. @@ -327,3 +328,151 @@ func NewMockIface[T any](ctrl *gomock.Controller) *MockIface[T] { func (m *MockIface[T]) EXPECT() *MockIfaceMockRecorder[T] { return m.recorder } + +// MockUniverse is a mock of Universe interface. +type MockUniverse[T constraints.Signed] struct { + ctrl *gomock.Controller + recorder *MockUniverseMockRecorder[T] +} + +// MockUniverseMockRecorder is the mock recorder for MockUniverse. +type MockUniverseMockRecorder[T constraints.Signed] struct { + mock *MockUniverse[T] +} + +// NewMockUniverse creates a new mock instance. +func NewMockUniverse[T constraints.Signed](ctrl *gomock.Controller) *MockUniverse[T] { + mock := &MockUniverse[T]{ctrl: ctrl} + mock.recorder = &MockUniverseMockRecorder[T]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUniverse[T]) EXPECT() *MockUniverseMockRecorder[T] { + return m.recorder +} + +// Water mocks base method. +func (m *MockUniverse[T]) Water(arg0 T) []T { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Water", arg0) + ret0, _ := ret[0].([]T) + return ret0 +} + +// Water indicates an expected call of Water. +func (mr *MockUniverseMockRecorder[T]) Water(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Water", reflect.TypeOf((*MockUniverse[T])(nil).Water), arg0) +} + +// MockMilkyWay is a mock of MilkyWay interface. +type MockMilkyWay[R constraints.Integer] struct { + ctrl *gomock.Controller + recorder *MockMilkyWayMockRecorder[R] +} + +// MockMilkyWayMockRecorder is the mock recorder for MockMilkyWay. +type MockMilkyWayMockRecorder[R constraints.Integer] struct { + mock *MockMilkyWay[R] +} + +// NewMockMilkyWay creates a new mock instance. +func NewMockMilkyWay[R constraints.Integer](ctrl *gomock.Controller) *MockMilkyWay[R] { + mock := &MockMilkyWay[R]{ctrl: ctrl} + mock.recorder = &MockMilkyWayMockRecorder[R]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMilkyWay[R]) EXPECT() *MockMilkyWayMockRecorder[R] { + return m.recorder +} + +// Water mocks base method. +func (m *MockMilkyWay[R]) Water(arg0 R) []R { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Water", arg0) + ret0, _ := ret[0].([]R) + return ret0 +} + +// Water indicates an expected call of Water. +func (mr *MockMilkyWayMockRecorder[R]) Water(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Water", reflect.TypeOf((*MockMilkyWay[R])(nil).Water), arg0) +} + +// MockSolarSystem is a mock of SolarSystem interface. +type MockSolarSystem[T constraints.Ordered] struct { + ctrl *gomock.Controller + recorder *MockSolarSystemMockRecorder[T] +} + +// MockSolarSystemMockRecorder is the mock recorder for MockSolarSystem. +type MockSolarSystemMockRecorder[T constraints.Ordered] struct { + mock *MockSolarSystem[T] +} + +// NewMockSolarSystem creates a new mock instance. +func NewMockSolarSystem[T constraints.Ordered](ctrl *gomock.Controller) *MockSolarSystem[T] { + mock := &MockSolarSystem[T]{ctrl: ctrl} + mock.recorder = &MockSolarSystemMockRecorder[T]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockSolarSystem[T]) EXPECT() *MockSolarSystemMockRecorder[T] { + return m.recorder +} + +// Water mocks base method. +func (m *MockSolarSystem[T]) Water(arg0 T) []T { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Water", arg0) + ret0, _ := ret[0].([]T) + return ret0 +} + +// Water indicates an expected call of Water. +func (mr *MockSolarSystemMockRecorder[T]) Water(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Water", reflect.TypeOf((*MockSolarSystem[T])(nil).Water), arg0) +} + +// MockEarth is a mock of Earth interface. +type MockEarth[R any] struct { + ctrl *gomock.Controller + recorder *MockEarthMockRecorder[R] +} + +// MockEarthMockRecorder is the mock recorder for MockEarth. +type MockEarthMockRecorder[R any] struct { + mock *MockEarth[R] +} + +// NewMockEarth creates a new mock instance. +func NewMockEarth[R any](ctrl *gomock.Controller) *MockEarth[R] { + mock := &MockEarth[R]{ctrl: ctrl} + mock.recorder = &MockEarthMockRecorder[R]{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockEarth[R]) EXPECT() *MockEarthMockRecorder[R] { + return m.recorder +} + +// Water mocks base method. +func (m *MockEarth[R]) Water(arg0 R) []R { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Water", arg0) + ret0, _ := ret[0].([]R) + return ret0 +} + +// Water indicates an expected call of Water. +func (mr *MockEarthMockRecorder[R]) Water(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Water", reflect.TypeOf((*MockEarth[R])(nil).Water), arg0) +} diff --git a/mockgen/parse.go b/mockgen/parse.go index aab9066..a397922 100644 --- a/mockgen/parse.go +++ b/mockgen/parse.go @@ -274,20 +274,77 @@ func (p *fileParser) parsePackage(path string) (*fileParser, error) { return newP, nil } +func (p *fileParser) constructInstParams(pkg string, params []*ast.Field, instParams []model.Type, embeddedInstParams []ast.Expr, tps map[string]model.Type) ([]model.Type, error) { + pm := make(map[string]int) + var i int + for _, v := range params { + for _, n := range v.Names { + pm[n.Name] = i + instParams = append(instParams, model.PredeclaredType(n.Name)) + i++ + } + } + + var runtimeInstParams []model.Type + for _, instParam := range embeddedInstParams { + switch t := instParam.(type) { + case *ast.Ident: + if idx, ok := pm[t.Name]; ok { + runtimeInstParams = append(runtimeInstParams, instParams[idx]) + continue + } + } + modelType, err := p.parseType(pkg, instParam, tps) + if err != nil { + return nil, err + } + runtimeInstParams = append(runtimeInstParams, modelType) + } + + return runtimeInstParams, nil +} + +func (p *fileParser) constructTps(it *namedInterface) (tps map[string]model.Type) { + tps = make(map[string]model.Type) + n := 0 + for _, tp := range it.typeParams { + for _, tm := range tp.Names { + tps[tm.Name] = nil + if len(it.instTypes) != 0 { + tps[tm.Name] = it.instTypes[n] + n++ + } + } + } + return tps +} + +// parseInterface loads interface specified by pkg and name, parses it and returns +// a new model with the parsed. func (p *fileParser) parseInterface(name, pkg string, it *namedInterface) (*model.Interface, error) { iface := &model.Interface{Name: name} - tps := make(map[string]bool) - + tps := p.constructTps(it) tp, err := p.parseFieldList(pkg, it.typeParams, tps) if err != nil { return nil, fmt.Errorf("unable to parse interface type parameters: %v", name) } + iface.TypeParams = tp - for _, v := range tp { - tps[v.Name] = true + for _, field := range it.it.Methods.List { + var methods []*model.Method + if methods, err = p.parseMethod(field, it, iface, pkg, tps); err != nil { + return nil, err + } + for _, m := range methods { + iface.AddMethod(m) + } } + return iface, nil +} - for _, field := range it.it.Methods.List { +func (p *fileParser) parseMethod(field *ast.Field, it *namedInterface, iface *model.Interface, pkg string, tps map[string]model.Type) ([]*model.Method, error) { + // {} for git diff + { switch v := field.Type.(type) { case *ast.FuncType: if nn := len(field.Names); nn != 1 { @@ -301,7 +358,7 @@ func (p *fileParser) parseInterface(name, pkg string, it *namedInterface) (*mode if err != nil { return nil, err } - iface.AddMethod(m) + return []*model.Method{m}, nil case *ast.Ident: // Embedded interface in this package. embeddedIfaceType := p.auxInterfaces.Get(pkg, v.String()) @@ -312,10 +369,15 @@ func (p *fileParser) parseInterface(name, pkg string, it *namedInterface) (*mode var embeddedIface *model.Interface if embeddedIfaceType != nil { var err error + embeddedIfaceType.instTypes, err = p.constructInstParams(pkg, it.typeParams, it.instTypes, it.embeddedInstTypeParams, tps) + if err != nil { + return nil, err + } embeddedIface, err = p.parseInterface(v.String(), pkg, embeddedIfaceType) if err != nil { return nil, err } + } else { // This is built-in error interface. if v.String() == model.ErrorInterface.Name { @@ -330,16 +392,17 @@ func (p *fileParser) parseInterface(name, pkg string, it *namedInterface) (*mode return nil, p.errorf(v.Pos(), "unknown embedded interface %s.%s", pkg, v.String()) } + embeddedIfaceType.instTypes, err = p.constructInstParams(pkg, it.typeParams, it.instTypes, it.embeddedInstTypeParams, tps) + if err != nil { + return nil, err + } embeddedIface, err = ip.parseInterface(v.String(), pkg, embeddedIfaceType) if err != nil { return nil, err } } } - // Copy the methods. - for _, m := range embeddedIface.Methods { - iface.AddMethod(m) - } + return embeddedIface.Methods, nil case *ast.SelectorExpr: // Embedded interface in another package. filePkg, sel := v.X.(*ast.Ident).String(), v.Sel.String() @@ -352,6 +415,10 @@ func (p *fileParser) parseInterface(name, pkg string, it *namedInterface) (*mode var err error embeddedIfaceType := p.auxInterfaces.Get(filePkg, sel) if embeddedIfaceType != nil { + embeddedIfaceType.instTypes, err = p.constructInstParams(pkg, it.typeParams, it.instTypes, it.embeddedInstTypeParams, tps) + if err != nil { + return nil, err + } embeddedIface, err = p.parseInterface(sel, filePkg, embeddedIfaceType) if err != nil { return nil, err @@ -373,24 +440,25 @@ func (p *fileParser) parseInterface(name, pkg string, it *namedInterface) (*mode if embeddedIfaceType = parser.importedInterfaces.Get(path, sel); embeddedIfaceType == nil { return nil, p.errorf(v.Pos(), "unknown embedded interface %s.%s", path, sel) } + + embeddedIfaceType.instTypes, err = p.constructInstParams(pkg, it.typeParams, it.instTypes, it.embeddedInstTypeParams, tps) + if err != nil { + return nil, err + } embeddedIface, err = parser.parseInterface(sel, path, embeddedIfaceType) if err != nil { return nil, err } } - // Copy the methods. // TODO: apply shadowing rules. - for _, m := range embeddedIface.Methods { - iface.AddMethod(m) - } + return embeddedIface.Methods, nil default: - return nil, fmt.Errorf("don't know how to mock method of type %T", field.Type) + return p.parseGenericMethod(field, it, iface, pkg, tps) } } - return iface, nil } -func (p *fileParser) parseFunc(pkg string, f *ast.FuncType, tps map[string]bool) (inParam []*model.Parameter, variadic *model.Parameter, outParam []*model.Parameter, err error) { +func (p *fileParser) parseFunc(pkg string, f *ast.FuncType, tps map[string]model.Type) (inParam []*model.Parameter, variadic *model.Parameter, outParam []*model.Parameter, err error) { if f.Params != nil { regParams := f.Params.List if isVariadic(f) { @@ -417,7 +485,7 @@ func (p *fileParser) parseFunc(pkg string, f *ast.FuncType, tps map[string]bool) return } -func (p *fileParser) parseFieldList(pkg string, fields []*ast.Field, tps map[string]bool) ([]*model.Parameter, error) { +func (p *fileParser) parseFieldList(pkg string, fields []*ast.Field, tps map[string]model.Type) ([]*model.Parameter, error) { nf := 0 for _, f := range fields { nn := len(f.Names) @@ -451,7 +519,7 @@ func (p *fileParser) parseFieldList(pkg string, fields []*ast.Field, tps map[str return ps, nil } -func (p *fileParser) parseType(pkg string, typ ast.Expr, tps map[string]bool) (model.Type, error) { +func (p *fileParser) parseType(pkg string, typ ast.Expr, tps map[string]model.Type) (model.Type, error) { switch v := typ.(type) { case *ast.ArrayType: ln := -1 @@ -493,7 +561,8 @@ func (p *fileParser) parseType(pkg string, typ ast.Expr, tps map[string]bool) (m } return &model.FuncType{In: in, Out: out, Variadic: variadic}, nil case *ast.Ident: - if v.IsExported() && !tps[v.Name] { + it, ok := tps[v.Name] + if v.IsExported() && !ok { // `pkg` may be an aliased imported pkg // if so, patch the import w/ the fully qualified import maybeImportedPkg, ok := p.imports[pkg] @@ -503,7 +572,9 @@ func (p *fileParser) parseType(pkg string, typ ast.Expr, tps map[string]bool) (m // assume type in this package return &model.NamedType{Package: pkg, Type: v.Name}, nil } - + if ok && it != nil { + return it, nil + } // assume predeclared type return model.PredeclaredType(v.Name), nil case *ast.InterfaceType: @@ -657,9 +728,11 @@ func importsOfFile(file *ast.File) (normalImports map[string]importedPackage, do } type namedInterface struct { - name *ast.Ident - it *ast.InterfaceType - typeParams []*ast.Field + name *ast.Ident + it *ast.InterfaceType + typeParams []*ast.Field + embeddedInstTypeParams []ast.Expr + instTypes []model.Type } // Create an iterator over all interfaces in file. @@ -681,7 +754,7 @@ func iterInterfaces(file *ast.File) <-chan *namedInterface { continue } - ch <- &namedInterface{ts.Name, it, getTypeSpecTypeParams(ts)} + ch <- &namedInterface{name: ts.Name, it: it, typeParams: getTypeSpecTypeParams(ts)} } } close(ch)