From 4508aec1fbd252ca28bd2e219f305b8d67648082 Mon Sep 17 00:00:00 2001 From: Marvin Preuss Date: Thu, 26 Jan 2023 09:34:19 +0000 Subject: [PATCH] Adds failing test for generic interfaces with imported structs See https://github.com/matryer/moq/issues/177 --- pkg/moq/moq_test.go | 11 ++ .../genericsthirdparty/genericsthirdparty.go | 14 ++ .../genericsthirdparty_moq.golden.go | 131 ++++++++++++++++++ .../thirdparty/thirdparty.go | 5 + 4 files changed, 161 insertions(+) create mode 100644 pkg/moq/testpackages/genericsthirdparty/genericsthirdparty.go create mode 100644 pkg/moq/testpackages/genericsthirdparty/genericsthirdparty_moq.golden.go create mode 100644 pkg/moq/testpackages/genericsthirdparty/thirdparty/thirdparty.go diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 758e92c..e334f83 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -395,6 +395,17 @@ func TestMockGolden(t *testing.T) { interfaces: []string{"Transient"}, goldenFile: filepath.Join("testpackages/transientimport", "transient_moq.golden.go"), }, + // Tests if moq imports the package of a third party generic object. + // See https://github.com/matryer/moq/issues/177 + { + name: "GenericsThirdParty", + cfg: Config{SrcDir: "testpackages/genericsthirdparty"}, + interfaces: []string{"GenericStore"}, + goldenFile: filepath.Join( + "testpackages/genericsthirdparty", + "genericsthirdparty_moq.golden.go", + ), + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/moq/testpackages/genericsthirdparty/genericsthirdparty.go b/pkg/moq/testpackages/genericsthirdparty/genericsthirdparty.go new file mode 100644 index 0000000..1d9d6fa --- /dev/null +++ b/pkg/moq/testpackages/genericsthirdparty/genericsthirdparty.go @@ -0,0 +1,14 @@ +package genericsthirdparty + +import ( + "context" + + "github.com/matryer/moq/pkg/moq/testpackages/genericsthirdparty/thirdparty" +) + +type Item[T any] struct{} + +type GenericStore interface { + Get(ctx context.Context, item Item[thirdparty.Item]) + Create(ctx context.Context, item Item[thirdparty.Item]) +} diff --git a/pkg/moq/testpackages/genericsthirdparty/genericsthirdparty_moq.golden.go b/pkg/moq/testpackages/genericsthirdparty/genericsthirdparty_moq.golden.go new file mode 100644 index 0000000..549deb0 --- /dev/null +++ b/pkg/moq/testpackages/genericsthirdparty/genericsthirdparty_moq.golden.go @@ -0,0 +1,131 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package genericsthirdparty + +import ( + "context" + "sync" +) + +// Ensure, that GenericStoreMock does implement GenericStore. +// If this is not the case, regenerate this file with moq. +var _ GenericStore = &GenericStoreMock{} + +// GenericStoreMock is a mock implementation of GenericStore. +// +// func TestSomethingThatUsesGenericStore(t *testing.T) { +// +// // make and configure a mocked GenericStore +// mockedGenericStore := &GenericStoreMock{ +// CreateFunc: func(ctx context.Context, item Item[Item]) { +// panic("mock out the Create method") +// }, +// GetFunc: func(ctx context.Context, item Item[Item]) { +// panic("mock out the Get method") +// }, +// } +// +// // use mockedGenericStore in code that requires GenericStore +// // and then make assertions. +// +// } +type GenericStoreMock struct { + // CreateFunc mocks the Create method. + CreateFunc func(ctx context.Context, item Item[Item]) + + // GetFunc mocks the Get method. + GetFunc func(ctx context.Context, item Item[Item]) + + // calls tracks calls to the methods. + calls struct { + // Create holds details about calls to the Create method. + Create []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // Item is the item argument value. + Item Item[Item] + } + // Get holds details about calls to the Get method. + Get []struct { + // Ctx is the ctx argument value. + Ctx context.Context + // Item is the item argument value. + Item Item[Item] + } + } + lockCreate sync.RWMutex + lockGet sync.RWMutex +} + +// Create calls CreateFunc. +func (mock *GenericStoreMock) Create(ctx context.Context, item Item[Item]) { + if mock.CreateFunc == nil { + panic("GenericStoreMock.CreateFunc: method is nil but GenericStore.Create was just called") + } + callInfo := struct { + Ctx context.Context + Item Item[Item] + }{ + Ctx: ctx, + Item: item, + } + mock.lockCreate.Lock() + mock.calls.Create = append(mock.calls.Create, callInfo) + mock.lockCreate.Unlock() + mock.CreateFunc(ctx, item) +} + +// CreateCalls gets all the calls that were made to Create. +// Check the length with: +// +// len(mockedGenericStore.CreateCalls()) +func (mock *GenericStoreMock) CreateCalls() []struct { + Ctx context.Context + Item Item[Item] +} { + var calls []struct { + Ctx context.Context + Item Item[Item] + } + mock.lockCreate.RLock() + calls = mock.calls.Create + mock.lockCreate.RUnlock() + return calls +} + +// Get calls GetFunc. +func (mock *GenericStoreMock) Get(ctx context.Context, item Item[Item]) { + if mock.GetFunc == nil { + panic("GenericStoreMock.GetFunc: method is nil but GenericStore.Get was just called") + } + callInfo := struct { + Ctx context.Context + Item Item[Item] + }{ + Ctx: ctx, + Item: item, + } + mock.lockGet.Lock() + mock.calls.Get = append(mock.calls.Get, callInfo) + mock.lockGet.Unlock() + mock.GetFunc(ctx, item) +} + +// GetCalls gets all the calls that were made to Get. +// Check the length with: +// +// len(mockedGenericStore.GetCalls()) +func (mock *GenericStoreMock) GetCalls() []struct { + Ctx context.Context + Item Item[Item] +} { + var calls []struct { + Ctx context.Context + Item Item[Item] + } + mock.lockGet.RLock() + calls = mock.calls.Get + mock.lockGet.RUnlock() + return calls +} diff --git a/pkg/moq/testpackages/genericsthirdparty/thirdparty/thirdparty.go b/pkg/moq/testpackages/genericsthirdparty/thirdparty/thirdparty.go new file mode 100644 index 0000000..1fafc60 --- /dev/null +++ b/pkg/moq/testpackages/genericsthirdparty/thirdparty/thirdparty.go @@ -0,0 +1,5 @@ +package thirdparty + +type Item struct { + ID int +}