From 8bd60ed62cea56aa97c4a47d2806e0f07ea046b2 Mon Sep 17 00:00:00 2001 From: Arkadiusz Osowski Date: Wed, 21 Aug 2024 17:17:43 +0200 Subject: [PATCH] add test case --- pkg/moq/moq_test.go | 6 ++ pkg/moq/testpackages/rangenum/rangenum.go | 13 ++++ .../rangenum/rangenum_moq.golden.go | 67 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 pkg/moq/testpackages/rangenum/rangenum.go create mode 100644 pkg/moq/testpackages/rangenum/rangenum_moq.golden.go diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index 71c62dd..5d6230d 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -406,6 +406,12 @@ func TestMockGolden(t *testing.T) { interfaces: []string{"ResetStore"}, goldenFile: filepath.Join("testpackages/withresets", "withresets_moq.golden.go"), }, + { + name: "RangeNumber", + cfg: Config{SrcDir: "testpackages/rangenum"}, + interfaces: []string{"Magician"}, + goldenFile: filepath.Join("testpackages/rangenum", "rangenum_moq.golden.go"), + }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { diff --git a/pkg/moq/testpackages/rangenum/rangenum.go b/pkg/moq/testpackages/rangenum/rangenum.go new file mode 100644 index 0000000..d6e9fb2 --- /dev/null +++ b/pkg/moq/testpackages/rangenum/rangenum.go @@ -0,0 +1,13 @@ +package rangenum + +import "fmt" + +func DoMagic() { + for range 10 { + fmt.Println("abrakadabra") + } +} + +type Magician interface { + DoMagic() +} diff --git a/pkg/moq/testpackages/rangenum/rangenum_moq.golden.go b/pkg/moq/testpackages/rangenum/rangenum_moq.golden.go new file mode 100644 index 0000000..5fb191e --- /dev/null +++ b/pkg/moq/testpackages/rangenum/rangenum_moq.golden.go @@ -0,0 +1,67 @@ +// Code generated by moq; DO NOT EDIT. +// github.com/matryer/moq + +package rangenum + +import ( + "sync" +) + +// Ensure, that MagicianMock does implement Magician. +// If this is not the case, regenerate this file with moq. +var _ Magician = &MagicianMock{} + +// MagicianMock is a mock implementation of Magician. +// +// func TestSomethingThatUsesMagician(t *testing.T) { +// +// // make and configure a mocked Magician +// mockedMagician := &MagicianMock{ +// DoMagicFunc: func() { +// panic("mock out the DoMagic method") +// }, +// } +// +// // use mockedMagician in code that requires Magician +// // and then make assertions. +// +// } +type MagicianMock struct { + // DoMagicFunc mocks the DoMagic method. + DoMagicFunc func() + + // calls tracks calls to the methods. + calls struct { + // DoMagic holds details about calls to the DoMagic method. + DoMagic []struct { + } + } + lockDoMagic sync.RWMutex +} + +// DoMagic calls DoMagicFunc. +func (mock *MagicianMock) DoMagic() { + if mock.DoMagicFunc == nil { + panic("MagicianMock.DoMagicFunc: method is nil but Magician.DoMagic was just called") + } + callInfo := struct { + }{} + mock.lockDoMagic.Lock() + mock.calls.DoMagic = append(mock.calls.DoMagic, callInfo) + mock.lockDoMagic.Unlock() + mock.DoMagicFunc() +} + +// DoMagicCalls gets all the calls that were made to DoMagic. +// Check the length with: +// +// len(mockedMagician.DoMagicCalls()) +func (mock *MagicianMock) DoMagicCalls() []struct { +} { + var calls []struct { + } + mock.lockDoMagic.RLock() + calls = mock.calls.DoMagic + mock.lockDoMagic.RUnlock() + return calls +}