diff --git a/.goreleaser.yml b/.goreleaser.yml index ea1ca7e..5466578 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -15,7 +15,7 @@ builds: - windows ldflags: - - -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.buildDate={{.Date}} + - -X main.version={{.Tag}} -X main.commit={{.FullCommit}} -X main.buildDate={{.Date}} env_files: github_token: .gh_token diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ae3b587..0000000 --- a/.travis.yml +++ /dev/null @@ -1,20 +0,0 @@ -language: go - -go: - - 1.x - -install: - - go get github.com/mattn/goveralls - - if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi - - export BINDIR=./bin - - export PATH=`pwd`/bin:$PATH - - export GO111MODULE=on - -script: - - cd $TRAVIS_BUILD_DIR && make all - - cd tests/ - - go test -c -covermode=count -coverpkg=github.com/gojuno/minimock/tests - - ./tests.test -test.coverprofile coverage.cov - -after_script: - - $HOME/gopath/bin/goveralls -service=travis-ci -coverprofile=coverage.cov diff --git a/Makefile b/Makefile index c076730..9f24d68 100644 --- a/Makefile +++ b/Makefile @@ -7,14 +7,16 @@ all: install test lint generate: go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests go run ./cmd/minimock/minimock.go -i ./tests.Formatter -o ./tests/formatter_mock.go + go run ./cmd/minimock/minimock.go -i ./tests.genericInout -o ./tests/generic_inout.go + go run ./cmd/minimock/minimock.go -i ./tests.genericOut -o ./tests/generic_out.go + go run ./cmd/minimock/minimock.go -i ./tests.genericIn -o ./tests/generic_in.go + go run ./cmd/minimock/minimock.go -i ./tests.genericSpecific -o ./tests/generic_specific.go + go run ./cmd/minimock/minimock.go -i ./tests.genericSimpleUnion -o ./tests/generic_simple_union.go + go run ./cmd/minimock/minimock.go -i ./tests.genericComplexUnion -o ./tests/generic_complex_union.go + go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go + go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic_inline_union.go + go run ./cmd/minimock/minimock.go -i ./tests.contextAccepter -o ./tests/context_accepter_mock.go go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests/package_name_specified_test.go -p tests_test - go run ./cmd/minimock/minimock.go -i ./tests.genericInout -o ./tests/generic/generic_inout.go - go run ./cmd/minimock/minimock.go -i ./tests.genericOut -o ./tests/generic/generic_out.go - go run ./cmd/minimock/minimock.go -i ./tests.genericIn -o ./tests/generic/generic_in.go - go run ./cmd/minimock/minimock.go -i ./tests.genericSpecific -o ./tests/generic/generic_specific.go - go run ./cmd/minimock/minimock.go -i ./tests.genericSimpleUnion -o ./tests/generic/generic_simple_union.go - go run ./cmd/minimock/minimock.go -i ./tests.genericComplexUnion -o ./tests/generic/generic_complex_union.go - go run ./cmd/minimock/minimock.go -i ./tests.genericInlineUnion -o ./tests/generic/generic_inline_union.go ./bin: mkdir ./bin @@ -35,8 +37,9 @@ install: ./bin/minimock: go build ./cmd/minimock -o ./bin/minimock +.PHONY: test: - go test $(go list ./... | grep -v /snapshots) + go test -race ./... -v release: ./bin/goreleaser ./bin/goreleaser release diff --git a/README.md b/README.md index 6bddfe7..b0dab14 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ ![logo](https://rawgit.com/gojuno/minimock/master/logo.svg) [![GoDoc](https://godoc.org/github.com/gojuno/minimock?status.svg)](http://godoc.org/github.com/gojuno/minimock) -[![Build Status](https://travis-ci.org/gojuno/minimock.svg?branch=master)](https://travis-ci.org/gojuno/minimock) [![Go Report Card](https://goreportcard.com/badge/github.com/gojuno/minimock)](https://goreportcard.com/report/github.com/gojuno/minimock) -[![Coverage Status](https://coveralls.io/repos/github/gojuno/minimock/badge.svg?branch=master)](https://coveralls.io/github/gojuno/minimock?branch=master) [![Release](https://img.shields.io/github/release/gojuno/minimock.svg)](https://github.com/gojuno/minimock/releases/latest) [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go#testing) @@ -20,7 +18,7 @@ The main features of minimock are: * It can generate several mocks in one run. * It generates code that passes [gometalinter](https://github.com/alecthomas/gometalinter) checks. * It puts //go:generate instruction into the generated code, so all you need to do when the source interface is updated is to run the `go generate ./...` command from within the project's directory. -* It provides Finish and Wait helpers to check if all mocked methods have been called during the test and keeps your test code clean and up to date. +* It makes sure that all mocked methods have been called during the test and keeps your test code clean and up to date. * It provides When and Then helpers to set up several expectations and results for any method. * It generates concurrent-safe mocks and mock invocation counters that you can use to manage mock behavior depending on the number of calls. * It can be used with the [GoUnit](https://github.com/hexdigest/gounit) tool which generates table-driven tests that make use of minimock. @@ -30,7 +28,7 @@ The main features of minimock are: If you use go modules please download the [latest binary](https://github.com/gojuno/minimock/releases/latest) or install minimock from source: ``` -go install github.com/gojuno/minimock/v3/cmd/minimock +go install github.com/gojuno/minimock/v3/cmd/minimock@latest ``` If you don't use go modules please find the latest v2.x binary [here](https://github.com/gojuno/minimock/releases) @@ -129,8 +127,8 @@ readCloserMock := NewReadCloserMock(mc).ReadMock.Inspect(func(p []byte){ ```go mc := minimock.NewController(t) formatterMock := NewFormatterMock(mc) -formatterMock.When("Hello %s!", "world").Then("Hello world!") -formatterMock.When("Hi %s!", "there").Then("Hi there!") +formatterMock.FormatMock.When("Hello %s!", "world").Then("Hello world!") +formatterMock.FormatMock.When("Hi %s!", "there").Then("Hi there!") ``` alternatively you can use the one-liner: @@ -156,15 +154,38 @@ formatterMock.FormatMock.Set(func(string, ...interface{}) string { }) ``` +### Mocking context +Sometimes context gets modified by the time the mocked method is being called. +However, in most cases you don't really care about the exact value of the context argument. +In such cases you can use special `minimock.AnyContext` variable, here are a couple of examples: + +```go +mc := minimock.NewController(t) +senderMock := NewSenderMock(mc). + SendMock. + When(minimock.AnyContext, "message1").Then(nil). + When(minimock.AnyContext, "message2").Then(errors.New("invalid message")) +``` + +or using Expect: + +```go +mc := minimock.NewController(t) +senderMock := NewSenderMock(mc). + SendMock.Expect(minimock.AnyContext, "message").Return(nil) +``` + ### Make sure that your mocks are being used Often we write tons of mocks to test our code but sometimes the tested code stops using mocked dependencies. -You can easily identify this problem by using mc.Finish or mc.Wait helpers. +You can easily identify this problem by using `minimock.NewController` instead of just `*testing.T`. +Alternatively you can use `mc.Wait` helper if your're testing concurrent code. These helpers ensure that all your mocks and expectations have been used at least once during the test run. ```go func TestSomething(t *testing.T) { + // it will mark this example test as failed because there are no calls + // to formatterMock.Format() and readCloserMock.Read() below mc := minimock.NewController(t) - defer mc.Finish() //it will mark this example test as failed because there are no calls to formatterMock.Format() and readCloserMock.Read() below formatterMock := NewFormatterMock(mc) formatterMock.FormatMock.Return("minimock") diff --git a/cmd/minimock/minimock.go b/cmd/minimock/minimock.go index 7fda0be..8ea17a0 100644 --- a/cmd/minimock/minimock.go +++ b/cmd/minimock/minimock.go @@ -155,6 +155,8 @@ func processPackage(opts generator.Options, interfaces []types.InterfaceSpecific return errors.Wrapf(err, "failed to generate mock for %s", iface.InterfaceName) } + opts.HeaderVars["OutputFile"] = filepath.Base(opts.OutputFile) + opts.Vars["MockName"] = fmt.Sprintf("%sMock", opts.InterfaceName) if mockName != "" { opts.Vars["MockName"] = mockName @@ -180,6 +182,10 @@ func processPackage(opts generator.Options, interfaces []types.InterfaceSpecific } if err := generate(opts); err != nil { + if strings.Contains(err.Error(), "interface has no methods") { + continue + } + return err } diff --git a/equal.go b/equal.go index 2c43b4c..0e9010b 100644 --- a/equal.go +++ b/equal.go @@ -1,7 +1,9 @@ package minimock import ( + "context" "reflect" + "unsafe" "github.com/davecgh/go-spew/spew" "github.com/pmezard/go-difflib/difflib" @@ -13,12 +15,48 @@ var dumpConf = spew.ConfigState{ SortKeys: true, } +type anyContext struct { + context.Context +} + +var AnyContext = anyContext{} + // Equal returns true if a equals b func Equal(a, b interface{}) bool { if a == nil && b == nil { return a == b } + if reflect.TypeOf(a).Kind() == reflect.Struct { + aV := reflect.ValueOf(a) + bV := reflect.ValueOf(b) + + ap := reflect.New(aV.Type()).Elem() + bp := reflect.New(bV.Type()).Elem() + + ap.Set(aV) + bp.Set(bV) + + // for every field in a + for i := 0; i < reflect.TypeOf(a).NumField(); i++ { + aFieldValue := unexported(ap.Field(i)) + bFieldValue := unexported(bp.Field(i)) + + _, ok := aFieldValue.(anyContext) + if ok { + if ctx, ok := bFieldValue.(context.Context); ok && ctx != nil { + continue + } + } + + if !reflect.DeepEqual(aFieldValue, bFieldValue) { + return false + } + } + + return true + } + return reflect.DeepEqual(a, b) } @@ -61,3 +99,7 @@ func Diff(e, a interface{}) string { return "\n\nDiff:\n" + diff } + +func unexported(field reflect.Value) interface{} { + return reflect.NewAt(field.Type(), unsafe.Pointer(field.UnsafeAddr())).Elem().Interface() +} diff --git a/go.mod b/go.mod index 7c01771..bb54703 100644 --- a/go.mod +++ b/go.mod @@ -1,23 +1,22 @@ module github.com/gojuno/minimock/v3 require ( - github.com/bradleyjkemp/cupaloy v2.3.0+incompatible github.com/davecgh/go-spew v1.1.1 - github.com/hexdigest/gowrap v1.1.8 + github.com/hexdigest/gowrap v1.3.2 github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 - github.com/stretchr/testify v1.7.0 - golang.org/x/tools v0.1.3 + github.com/stretchr/testify v1.7.1 + golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a google.golang.org/protobuf v1.30.0 ) require ( github.com/kr/text v0.2.0 // indirect - golang.org/x/mod v0.4.2 // indirect + golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect golang.org/x/sys v0.1.0 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gopkg.in/yaml.v3 v3.0.0 // indirect ) -go 1.18 +go 1.19 diff --git a/go.sum b/go.sum index 9f89634..aad4cab 100644 --- a/go.sum +++ b/go.sum @@ -1,132 +1,41 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= -github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y= -github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk= -github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= -github.com/gojuno/minimock/v3 v3.0.4/go.mod h1:HqeqnwV8mAABn3pO5hqF+RE7gjA0jsN8cbbSogoGrzI= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/hexdigest/gowrap v1.1.7/go.mod h1:Z+nBFUDLa01iaNM+/jzoOA1JJ7sm51rnYFauKFUB5fs= -github.com/hexdigest/gowrap v1.1.8 h1:xGTnuMvHou3sa+PSHphOCxPJTJyqNRvGl21t/p3eLes= -github.com/hexdigest/gowrap v1.1.8/go.mod h1:H/JiFmQMp//tedlV8qt2xBdGzmne6bpbaSuiHmygnMw= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= +github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= +github.com/hexdigest/gowrap v1.3.2 h1:ZDhDFhrbAHYRdt9ZnULKZyggC/3+W9EpfX6R8DjlggY= +github.com/hexdigest/gowrap v1.3.2/go.mod h1:g8N2jI4n9AKrf843erksNTrt4sdkG+TGVfhWe8dWrJQ= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= -github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= -github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= -github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/twitchtv/twirp v5.8.0+incompatible/go.mod h1:RRJoFSAmTEh2weEqWtpPE3vFK5YBhA6bqp2l1kfCC5A= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= 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/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190614205625-5aca471b1d59/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.3 h1:L69ShwSZEyCsLKoAxDKeMvLDZkumEe8gXUZAjab0tX8= -golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a h1:ofrrl6c6NG5/IOSx/R1cyiQxxjqlur0h/TvbUhkH0II= +golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -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= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f h1:GGU+dLjvlC3qDwqYgL6UgRmHXhOOgns0bZu2Ty5mm6U= +golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/types/interface.go b/internal/types/interface.go index 8abfe59..94fd2ec 100644 --- a/internal/types/interface.go +++ b/internal/types/interface.go @@ -2,7 +2,6 @@ package types import ( "bytes" - "fmt" "go/ast" "go/printer" "go/token" @@ -55,7 +54,6 @@ func FindAllInterfaces(p *ast.Package, pattern string) []InterfaceSpecification }) } - fmt.Printf("interfaceSpecifications: %+v\n", interfaceSpecifications) return interfaceSpecifications } diff --git a/mock_controller.go b/mock_controller.go index e432a25..cd2c961 100644 --- a/mock_controller.go +++ b/mock_controller.go @@ -5,7 +5,7 @@ import ( "time" ) -//Mocker describes common interface for all mocks generated by minimock +// Mocker describes common interface for all mocks generated by minimock type Mocker interface { MinimockFinish() MinimockWait(time.Duration) @@ -18,19 +18,20 @@ type Tester interface { Error(...interface{}) Errorf(format string, args ...interface{}) FailNow() + Cleanup(f func()) } -//MockController can be passed to mocks generated by minimock +// MockController can be passed to mocks generated by minimock type MockController interface { Tester RegisterMocker(Mocker) } -//Controller implements MockController interface and has to be used in your tests: -//mockController := minimock.NewController(t) -//defer mockController.Finish() -//stringerMock := NewStringerMock(mockController) +// Controller implements MockController interface and has to be used in your tests: +// mockController := minimock.NewController(t) +// defer mockController.Finish() +// stringerMock := NewStringerMock(mockController) type Controller struct { Tester sync.Mutex @@ -41,19 +42,22 @@ type Controller struct { // Check if Controller supports MockController interface var _ MockController = &Controller{} -//NewController returns an instance of Controller +// NewController returns an instance of Controller func NewController(t Tester) *Controller { - return &Controller{Tester: newSafeTester(t)} + c := Controller{Tester: newSafeTester(t)} + t.Cleanup(c.Finish) + + return &c } -//RegisterMocker puts mocker to the list of controller mockers +// RegisterMocker puts mocker to the list of controller mockers func (c *Controller) RegisterMocker(m Mocker) { c.Lock() c.mockers = append(c.mockers, m) c.Unlock() } -//Finish calls to MinimockFinish method for all registered mockers +// Finish calls to MinimockFinish method for all registered mockers func (c *Controller) Finish() { c.Lock() for _, m := range c.mockers { @@ -62,7 +66,7 @@ func (c *Controller) Finish() { c.Unlock() } -//Wait calls to MinimockWait method for all registered mockers +// Wait calls to MinimockWait method for all registered mockers func (c *Controller) Wait(d time.Duration) { wg := sync.WaitGroup{} wg.Add(len(c.mockers)) diff --git a/snapshot_test.go b/snapshot_test.go deleted file mode 100644 index da274c2..0000000 --- a/snapshot_test.go +++ /dev/null @@ -1,159 +0,0 @@ -package minimock_test - -import ( - "bytes" - "context" - "os" - "os/exec" - "testing" - "time" - - "github.com/bradleyjkemp/cupaloy" -) - -func mustRunMinimockWithArgs( - t *testing.T, - args ...string, -) error { - t.Helper() - - var outBuffer bytes.Buffer - - timeoutContext, cancel := context.WithTimeout(context.Background(), time.Second*10) - defer cancel() - - args = append([]string{"run", "cmd/minimock/minimock.go"}, args...) - cmd := exec.CommandContext(timeoutContext, "go", args...) - cmd.Stdout = &outBuffer - cmd.Stderr = &outBuffer - - t.Log(cmd.String()) - - err := cmd.Run() - if err != nil { - t.Log(outBuffer.String()) - } - - return err -} - -func mustReadFile( - t *testing.T, - filename string, -) []byte { - t.Helper() - - contents, err := os.ReadFile(filename) - if err != nil { - t.Errorf("failed to read the file: %v", err) - t.Fail() - } - - return contents -} - -func TestSnapshot(t *testing.T) { - snapshotter := cupaloy.New( - cupaloy.SnapshotSubdirectory("snapshots"), - cupaloy.SnapshotFileExtension(".go"), - ) - - type testCase struct { - name string - args []string - expectedOutputFile string - wantErr bool - } - - testCases := []testCase{ - { - name: "package reference", - args: []string{"-o", "./tests", "-i", "github.com/gojuno/minimock/v3.Tester"}, - expectedOutputFile: "./tests/tester_mock_test.go", - }, - { - name: "relative reference", - args: []string{"-o", "./tests/formatter_mock.go", "-i", "./tests.Formatter"}, - expectedOutputFile: "./tests/formatter_mock.go", - }, - { - name: "generics with any used as param and return type", - args: []string{"-o", "./tests/generic_inout.go", "-i", "./tests.genericInout"}, - expectedOutputFile: "./tests/generic_inout.go", - }, - { - name: "generics with any used as return type", - args: []string{"-o", "./tests/generic_out.go", "-i", "./tests.genericOut"}, - expectedOutputFile: "./tests/generic_out.go", - }, - { - name: "generics with any used as param type", - args: []string{"-o", "./tests/generic_in.go", "-i", "./tests.genericIn"}, - expectedOutputFile: "./tests/generic_in.go", - }, - { - name: "generics with specific type used as a generic constraint", - args: []string{"-o", "./tests/generic_specific.go", "-i", "./tests.genericSpecific"}, - expectedOutputFile: "./tests/generic_specific.go", - }, - { - name: "generics with simple union used as a generic constraint", - args: []string{"-o", "./tests/generic_simple_union.go", "-i", "./tests.genericSimpleUnion"}, - expectedOutputFile: "./tests/generic_simple_union.go", - }, - { - name: "generics with complex union used as a generic constraint", - args: []string{"-o", "./tests/generic_complex_union.go", "-i", "./tests.genericComplexUnion"}, - expectedOutputFile: "./tests/generic_complex_union.go", - }, - { - name: "generics with complex inline union used as a generic constraint", - args: []string{"-o", "./tests/generic_inline_union.go", "-i", "./tests.genericInlineUnion"}, - expectedOutputFile: "./tests/generic_inline_union.go", - }, - { - name: "generics with complex inline union with many types", - args: []string{"-o", "./tests/generic_inline_with_many_options.go", "-i", "./tests.genericInlineUnionWithManyTypes"}, - expectedOutputFile: "./tests/generic_inline_with_many_options.go", - }, - { - name: "package name specified", - args: []string{"-o", "./tests/package_name_specified_test.go", "-i", "github.com/gojuno/minimock/v3.Tester", "-p", "tests_test"}, - expectedOutputFile: "./tests/package_name_specified_test.go", - }, - { - name: "error too many package names", - args: []string{"-o", "./tests/package_name_specified_test.go", "-i", "github.com/gojuno/minimock/v3.Tester", "-p", "tests_test, tests"}, - wantErr: true, - }, - { - name: "error interface wildcard and package specified", - args: []string{"-o", "./tests/package_name_specified_test.go", "-i", "./tests.*", "-p", "tests_test"}, - wantErr: true, - }, - { - name: "2 generic arguments with different types", - args: []string{"-o", "./tests/generic_multiple_args_with_different_types.go", "-i", "./tests.genericMultipleTypes"}, - expectedOutputFile: "./tests/generic_multiple_args_with_different_types.go", - }, - } - - for _, testCase := range testCases { - testCase := testCase - - t.Run(testCase.name, func(t *testing.T) { - t.Parallel() - - err := mustRunMinimockWithArgs(t, testCase.args...) - if err != nil && testCase.wantErr { - return - } - - if testCase.wantErr != (err != nil) { - t.FailNow() - } - - snapshotter.SnapshotT(t, mustReadFile(t, testCase.expectedOutputFile)) - }) - } -} diff --git a/snapshots/TestSnapshot-2_generic_arguments_with_different_types.go b/snapshots/TestSnapshot-2_generic_arguments_with_different_types.go deleted file mode 100644 index 7f00ecf..0000000 --- a/snapshots/TestSnapshot-2_generic_arguments_with_different_types.go +++ /dev/null @@ -1,256 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericMultipleTypes -o ./tests/generic_multiple_args_with_different_types.go -n GenericMultipleTypesMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" - "google.golang.org/protobuf/proto" -) - -// GenericMultipleTypesMock implements genericMultipleTypes -type GenericMultipleTypesMock[T proto.Message, K any] struct { - t minimock.Tester - - funcName func(t1 T, k1 K) - inspectFuncName func(t1 T, k1 K) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericMultipleTypesMockName[T, K] -} - -// NewGenericMultipleTypesMock returns a mock for genericMultipleTypes -func NewGenericMultipleTypesMock[T proto.Message, K any](t minimock.Tester) *GenericMultipleTypesMock[T, K] { - m := &GenericMultipleTypesMock[T, K]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericMultipleTypesMockName[T, K]{mock: m} - m.NameMock.callArgs = []*GenericMultipleTypesMockNameParams[T, K]{} - - return m -} - -type mGenericMultipleTypesMockName[T proto.Message, K any] struct { - mock *GenericMultipleTypesMock[T, K] - defaultExpectation *GenericMultipleTypesMockNameExpectation[T, K] - expectations []*GenericMultipleTypesMockNameExpectation[T, K] - - callArgs []*GenericMultipleTypesMockNameParams[T, K] - mutex sync.RWMutex -} - -// GenericMultipleTypesMockNameExpectation specifies expectation struct of the genericMultipleTypes.Name -type GenericMultipleTypesMockNameExpectation[T proto.Message, K any] struct { - mock *GenericMultipleTypesMock[T, K] - params *GenericMultipleTypesMockNameParams[T, K] - - Counter uint64 -} - -// GenericMultipleTypesMockNameParams contains parameters of the genericMultipleTypes.Name -type GenericMultipleTypesMockNameParams[T proto.Message, K any] struct { - t1 T - k1 K -} - -// Expect sets up expected params for genericMultipleTypes.Name -func (mmName *mGenericMultipleTypesMockName[T, K]) Expect(t1 T, k1 K) *mGenericMultipleTypesMockName[T, K] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericMultipleTypesMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericMultipleTypesMockNameExpectation[T, K]{} - } - - mmName.defaultExpectation.params = &GenericMultipleTypesMockNameParams[T, K]{t1, k1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericMultipleTypes.Name -func (mmName *mGenericMultipleTypesMockName[T, K]) Inspect(f func(t1 T, k1 K)) *mGenericMultipleTypesMockName[T, K] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericMultipleTypesMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericMultipleTypes.Name -func (mmName *mGenericMultipleTypesMockName[T, K]) Return() *GenericMultipleTypesMock[T, K] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericMultipleTypesMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericMultipleTypesMockNameExpectation[T, K]{mock: mmName.mock} - } - - return mmName.mock -} - -// Set uses given function f to mock the genericMultipleTypes.Name method -func (mmName *mGenericMultipleTypesMockName[T, K]) Set(f func(t1 T, k1 K)) *GenericMultipleTypesMock[T, K] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericMultipleTypes.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericMultipleTypes.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericMultipleTypes -func (mmName *GenericMultipleTypesMock[T, K]) Name(t1 T, k1 K) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1, k1) - } - - mm_params := &GenericMultipleTypesMockNameParams[T, K]{t1, k1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericMultipleTypesMockNameParams[T, K]{t1, k1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmName.funcName != nil { - mmName.funcName(t1, k1) - return - } - mmName.t.Fatalf("Unexpected call to GenericMultipleTypesMock.Name. %v %v", t1, k1) - -} - -// NameAfterCounter returns a count of finished GenericMultipleTypesMock.Name invocations -func (mmName *GenericMultipleTypesMock[T, K]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericMultipleTypesMock.Name invocations -func (mmName *GenericMultipleTypesMock[T, K]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericMultipleTypesMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericMultipleTypesMockName[T, K]) Calls() []*GenericMultipleTypesMockNameParams[T, K] { - mmName.mutex.RLock() - - argCopy := make([]*GenericMultipleTypesMockNameParams[T, K], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericMultipleTypesMock[T, K]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericMultipleTypesMock[T, K]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericMultipleTypesMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericMultipleTypesMock.Name") - } else { - m.t.Errorf("Expected call to GenericMultipleTypesMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericMultipleTypesMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericMultipleTypesMock[T, K]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericMultipleTypesMock[T, K]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericMultipleTypesMock[T, K]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} diff --git a/snapshots/TestSnapshot-generics_with_any_used_as_param_and_return_type.go b/snapshots/TestSnapshot-generics_with_any_used_as_param_and_return_type.go deleted file mode 100644 index 936eb31..0000000 --- a/snapshots/TestSnapshot-generics_with_any_used_as_param_and_return_type.go +++ /dev/null @@ -1,283 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInout -o ./tests/generic_inout.go -n GenericInoutMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// GenericInoutMock implements genericInout -type GenericInoutMock[T any] struct { - t minimock.Tester - - funcName func(t1 T) (t2 T) - inspectFuncName func(t1 T) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericInoutMockName[T] -} - -// NewGenericInoutMock returns a mock for genericInout -func NewGenericInoutMock[T any](t minimock.Tester) *GenericInoutMock[T] { - m := &GenericInoutMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericInoutMockName[T]{mock: m} - m.NameMock.callArgs = []*GenericInoutMockNameParams[T]{} - - return m -} - -type mGenericInoutMockName[T any] struct { - mock *GenericInoutMock[T] - defaultExpectation *GenericInoutMockNameExpectation[T] - expectations []*GenericInoutMockNameExpectation[T] - - callArgs []*GenericInoutMockNameParams[T] - mutex sync.RWMutex -} - -// GenericInoutMockNameExpectation specifies expectation struct of the genericInout.Name -type GenericInoutMockNameExpectation[T any] struct { - mock *GenericInoutMock[T] - params *GenericInoutMockNameParams[T] - results *GenericInoutMockNameResults[T] - Counter uint64 -} - -// GenericInoutMockNameParams contains parameters of the genericInout.Name -type GenericInoutMockNameParams[T any] struct { - t1 T -} - -// GenericInoutMockNameResults contains results of the genericInout.Name -type GenericInoutMockNameResults[T any] struct { - t2 T -} - -// Expect sets up expected params for genericInout.Name -func (mmName *mGenericInoutMockName[T]) Expect(t1 T) *mGenericInoutMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{} - } - - mmName.defaultExpectation.params = &GenericInoutMockNameParams[T]{t1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericInout.Name -func (mmName *mGenericInoutMockName[T]) Inspect(f func(t1 T)) *mGenericInoutMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericInoutMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericInout.Name -func (mmName *mGenericInoutMockName[T]) Return(t2 T) *GenericInoutMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{mock: mmName.mock} - } - mmName.defaultExpectation.results = &GenericInoutMockNameResults[T]{t2} - return mmName.mock -} - -// Set uses given function f to mock the genericInout.Name method -func (mmName *mGenericInoutMockName[T]) Set(f func(t1 T) (t2 T)) *GenericInoutMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericInout.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericInout.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// When sets expectation for the genericInout.Name which will trigger the result defined by the following -// Then helper -func (mmName *mGenericInoutMockName[T]) When(t1 T) *GenericInoutMockNameExpectation[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInoutMock.Name mock is already set by Set") - } - - expectation := &GenericInoutMockNameExpectation[T]{ - mock: mmName.mock, - params: &GenericInoutMockNameParams[T]{t1}, - } - mmName.expectations = append(mmName.expectations, expectation) - return expectation -} - -// Then sets up genericInout.Name return parameters for the expectation previously defined by the When method -func (e *GenericInoutMockNameExpectation[T]) Then(t2 T) *GenericInoutMock[T] { - e.results = &GenericInoutMockNameResults[T]{t2} - return e.mock -} - -// Name implements genericInout -func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1) - } - - mm_params := &GenericInoutMockNameParams[T]{t1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return e.results.t2 - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericInoutMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericInoutMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - mm_results := mmName.NameMock.defaultExpectation.results - if mm_results == nil { - mmName.t.Fatal("No results are set for the GenericInoutMock.Name") - } - return (*mm_results).t2 - } - if mmName.funcName != nil { - return mmName.funcName(t1) - } - mmName.t.Fatalf("Unexpected call to GenericInoutMock.Name. %v", t1) - return -} - -// NameAfterCounter returns a count of finished GenericInoutMock.Name invocations -func (mmName *GenericInoutMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericInoutMock.Name invocations -func (mmName *GenericInoutMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericInoutMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericInoutMockName[T]) Calls() []*GenericInoutMockNameParams[T] { - mmName.mutex.RLock() - - argCopy := make([]*GenericInoutMockNameParams[T], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericInoutMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericInoutMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericInoutMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInoutMock.Name") - } else { - m.t.Errorf("Expected call to GenericInoutMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericInoutMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericInoutMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericInoutMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericInoutMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-generics_with_any_used_as_param_type.go b/snapshots/TestSnapshot-generics_with_any_used_as_param_type.go deleted file mode 100644 index 6a76ee7..0000000 --- a/snapshots/TestSnapshot-generics_with_any_used_as_param_type.go +++ /dev/null @@ -1,255 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericIn -o ./tests/generic_in.go -n GenericInMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// GenericInMock implements genericIn -type GenericInMock[T any] struct { - t minimock.Tester - - funcName func(t1 T) - inspectFuncName func(t1 T) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericInMockName[T] -} - -// NewGenericInMock returns a mock for genericIn -func NewGenericInMock[T any](t minimock.Tester) *GenericInMock[T] { - m := &GenericInMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericInMockName[T]{mock: m} - m.NameMock.callArgs = []*GenericInMockNameParams[T]{} - - return m -} - -type mGenericInMockName[T any] struct { - mock *GenericInMock[T] - defaultExpectation *GenericInMockNameExpectation[T] - expectations []*GenericInMockNameExpectation[T] - - callArgs []*GenericInMockNameParams[T] - mutex sync.RWMutex -} - -// GenericInMockNameExpectation specifies expectation struct of the genericIn.Name -type GenericInMockNameExpectation[T any] struct { - mock *GenericInMock[T] - params *GenericInMockNameParams[T] - - Counter uint64 -} - -// GenericInMockNameParams contains parameters of the genericIn.Name -type GenericInMockNameParams[T any] struct { - t1 T -} - -// Expect sets up expected params for genericIn.Name -func (mmName *mGenericInMockName[T]) Expect(t1 T) *mGenericInMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInMockNameExpectation[T]{} - } - - mmName.defaultExpectation.params = &GenericInMockNameParams[T]{t1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericIn.Name -func (mmName *mGenericInMockName[T]) Inspect(f func(t1 T)) *mGenericInMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericInMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericIn.Name -func (mmName *mGenericInMockName[T]) Return() *GenericInMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInMockNameExpectation[T]{mock: mmName.mock} - } - - return mmName.mock -} - -// Set uses given function f to mock the genericIn.Name method -func (mmName *mGenericInMockName[T]) Set(f func(t1 T)) *GenericInMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericIn.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericIn.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericIn -func (mmName *GenericInMock[T]) Name(t1 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1) - } - - mm_params := &GenericInMockNameParams[T]{t1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericInMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericInMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmName.funcName != nil { - mmName.funcName(t1) - return - } - mmName.t.Fatalf("Unexpected call to GenericInMock.Name. %v", t1) - -} - -// NameAfterCounter returns a count of finished GenericInMock.Name invocations -func (mmName *GenericInMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericInMock.Name invocations -func (mmName *GenericInMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericInMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericInMockName[T]) Calls() []*GenericInMockNameParams[T] { - mmName.mutex.RLock() - - argCopy := make([]*GenericInMockNameParams[T], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericInMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericInMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericInMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInMock.Name") - } else { - m.t.Errorf("Expected call to GenericInMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericInMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericInMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericInMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericInMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-generics_with_any_used_as_return_type.go b/snapshots/TestSnapshot-generics_with_any_used_as_return_type.go deleted file mode 100644 index 987bfa3..0000000 --- a/snapshots/TestSnapshot-generics_with_any_used_as_return_type.go +++ /dev/null @@ -1,209 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericOut -o ./tests/generic_out.go -n GenericOutMock -p tests - -import ( - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// GenericOutMock implements genericOut -type GenericOutMock[T any] struct { - t minimock.Tester - - funcName func() (t1 T) - inspectFuncName func() - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericOutMockName[T] -} - -// NewGenericOutMock returns a mock for genericOut -func NewGenericOutMock[T any](t minimock.Tester) *GenericOutMock[T] { - m := &GenericOutMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericOutMockName[T]{mock: m} - - return m -} - -type mGenericOutMockName[T any] struct { - mock *GenericOutMock[T] - defaultExpectation *GenericOutMockNameExpectation[T] - expectations []*GenericOutMockNameExpectation[T] -} - -// GenericOutMockNameExpectation specifies expectation struct of the genericOut.Name -type GenericOutMockNameExpectation[T any] struct { - mock *GenericOutMock[T] - - results *GenericOutMockNameResults[T] - Counter uint64 -} - -// GenericOutMockNameResults contains results of the genericOut.Name -type GenericOutMockNameResults[T any] struct { - t1 T -} - -// Expect sets up expected params for genericOut.Name -func (mmName *mGenericOutMockName[T]) Expect() *mGenericOutMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericOutMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericOutMockNameExpectation[T]{} - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericOut.Name -func (mmName *mGenericOutMockName[T]) Inspect(f func()) *mGenericOutMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericOutMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericOut.Name -func (mmName *mGenericOutMockName[T]) Return(t1 T) *GenericOutMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericOutMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericOutMockNameExpectation[T]{mock: mmName.mock} - } - mmName.defaultExpectation.results = &GenericOutMockNameResults[T]{t1} - return mmName.mock -} - -// Set uses given function f to mock the genericOut.Name method -func (mmName *mGenericOutMockName[T]) Set(f func() (t1 T)) *GenericOutMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericOut.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericOut.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericOut -func (mmName *GenericOutMock[T]) Name() (t1 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName() - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - - mm_results := mmName.NameMock.defaultExpectation.results - if mm_results == nil { - mmName.t.Fatal("No results are set for the GenericOutMock.Name") - } - return (*mm_results).t1 - } - if mmName.funcName != nil { - return mmName.funcName() - } - mmName.t.Fatalf("Unexpected call to GenericOutMock.Name.") - return -} - -// NameAfterCounter returns a count of finished GenericOutMock.Name invocations -func (mmName *GenericOutMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericOutMock.Name invocations -func (mmName *GenericOutMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericOutMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericOutMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Error("Expected call to GenericOutMock.Name") - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericOutMock.Name") - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericOutMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericOutMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericOutMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericOutMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-generics_with_complex_inline_union_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_complex_inline_union_used_as_a_generic_constraint.go deleted file mode 100644 index fd2d00c..0000000 --- a/snapshots/TestSnapshot-generics_with_complex_inline_union_used_as_a_generic_constraint.go +++ /dev/null @@ -1,255 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnion -o ./tests/generic_inline_union.go -n GenericInlineUnionMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// GenericInlineUnionMock implements genericInlineUnion -type GenericInlineUnionMock[T int | float64] struct { - t minimock.Tester - - funcName func(t1 T) - inspectFuncName func(t1 T) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericInlineUnionMockName[T] -} - -// NewGenericInlineUnionMock returns a mock for genericInlineUnion -func NewGenericInlineUnionMock[T int | float64](t minimock.Tester) *GenericInlineUnionMock[T] { - m := &GenericInlineUnionMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericInlineUnionMockName[T]{mock: m} - m.NameMock.callArgs = []*GenericInlineUnionMockNameParams[T]{} - - return m -} - -type mGenericInlineUnionMockName[T int | float64] struct { - mock *GenericInlineUnionMock[T] - defaultExpectation *GenericInlineUnionMockNameExpectation[T] - expectations []*GenericInlineUnionMockNameExpectation[T] - - callArgs []*GenericInlineUnionMockNameParams[T] - mutex sync.RWMutex -} - -// GenericInlineUnionMockNameExpectation specifies expectation struct of the genericInlineUnion.Name -type GenericInlineUnionMockNameExpectation[T int | float64] struct { - mock *GenericInlineUnionMock[T] - params *GenericInlineUnionMockNameParams[T] - - Counter uint64 -} - -// GenericInlineUnionMockNameParams contains parameters of the genericInlineUnion.Name -type GenericInlineUnionMockNameParams[T int | float64] struct { - t1 T -} - -// Expect sets up expected params for genericInlineUnion.Name -func (mmName *mGenericInlineUnionMockName[T]) Expect(t1 T) *mGenericInlineUnionMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInlineUnionMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInlineUnionMockNameExpectation[T]{} - } - - mmName.defaultExpectation.params = &GenericInlineUnionMockNameParams[T]{t1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericInlineUnion.Name -func (mmName *mGenericInlineUnionMockName[T]) Inspect(f func(t1 T)) *mGenericInlineUnionMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericInlineUnionMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericInlineUnion.Name -func (mmName *mGenericInlineUnionMockName[T]) Return() *GenericInlineUnionMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInlineUnionMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInlineUnionMockNameExpectation[T]{mock: mmName.mock} - } - - return mmName.mock -} - -// Set uses given function f to mock the genericInlineUnion.Name method -func (mmName *mGenericInlineUnionMockName[T]) Set(f func(t1 T)) *GenericInlineUnionMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericInlineUnion.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericInlineUnion.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericInlineUnion -func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1) - } - - mm_params := &GenericInlineUnionMockNameParams[T]{t1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericInlineUnionMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericInlineUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmName.funcName != nil { - mmName.funcName(t1) - return - } - mmName.t.Fatalf("Unexpected call to GenericInlineUnionMock.Name. %v", t1) - -} - -// NameAfterCounter returns a count of finished GenericInlineUnionMock.Name invocations -func (mmName *GenericInlineUnionMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericInlineUnionMock.Name invocations -func (mmName *GenericInlineUnionMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericInlineUnionMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericInlineUnionMockName[T]) Calls() []*GenericInlineUnionMockNameParams[T] { - mmName.mutex.RLock() - - argCopy := make([]*GenericInlineUnionMockNameParams[T], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericInlineUnionMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericInlineUnionMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericInlineUnionMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInlineUnionMock.Name") - } else { - m.t.Errorf("Expected call to GenericInlineUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericInlineUnionMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericInlineUnionMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericInlineUnionMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericInlineUnionMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-generics_with_complex_inline_union_with_many_types.go b/snapshots/TestSnapshot-generics_with_complex_inline_union_with_many_types.go deleted file mode 100644 index 55482bb..0000000 --- a/snapshots/TestSnapshot-generics_with_complex_inline_union_with_many_types.go +++ /dev/null @@ -1,255 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnionWithManyTypes -o ./tests/generic_inline_with_many_options.go -n GenericInlineUnionWithManyTypesMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// GenericInlineUnionWithManyTypesMock implements genericInlineUnionWithManyTypes -type GenericInlineUnionWithManyTypesMock[T int | float64 | string] struct { - t minimock.Tester - - funcName func(t1 T) - inspectFuncName func(t1 T) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericInlineUnionWithManyTypesMockName[T] -} - -// NewGenericInlineUnionWithManyTypesMock returns a mock for genericInlineUnionWithManyTypes -func NewGenericInlineUnionWithManyTypesMock[T int | float64 | string](t minimock.Tester) *GenericInlineUnionWithManyTypesMock[T] { - m := &GenericInlineUnionWithManyTypesMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericInlineUnionWithManyTypesMockName[T]{mock: m} - m.NameMock.callArgs = []*GenericInlineUnionWithManyTypesMockNameParams[T]{} - - return m -} - -type mGenericInlineUnionWithManyTypesMockName[T int | float64 | string] struct { - mock *GenericInlineUnionWithManyTypesMock[T] - defaultExpectation *GenericInlineUnionWithManyTypesMockNameExpectation[T] - expectations []*GenericInlineUnionWithManyTypesMockNameExpectation[T] - - callArgs []*GenericInlineUnionWithManyTypesMockNameParams[T] - mutex sync.RWMutex -} - -// GenericInlineUnionWithManyTypesMockNameExpectation specifies expectation struct of the genericInlineUnionWithManyTypes.Name -type GenericInlineUnionWithManyTypesMockNameExpectation[T int | float64 | string] struct { - mock *GenericInlineUnionWithManyTypesMock[T] - params *GenericInlineUnionWithManyTypesMockNameParams[T] - - Counter uint64 -} - -// GenericInlineUnionWithManyTypesMockNameParams contains parameters of the genericInlineUnionWithManyTypes.Name -type GenericInlineUnionWithManyTypesMockNameParams[T int | float64 | string] struct { - t1 T -} - -// Expect sets up expected params for genericInlineUnionWithManyTypes.Name -func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Expect(t1 T) *mGenericInlineUnionWithManyTypesMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{} - } - - mmName.defaultExpectation.params = &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericInlineUnionWithManyTypes.Name -func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Inspect(f func(t1 T)) *mGenericInlineUnionWithManyTypesMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericInlineUnionWithManyTypesMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericInlineUnionWithManyTypes.Name -func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Return() *GenericInlineUnionWithManyTypesMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericInlineUnionWithManyTypesMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{mock: mmName.mock} - } - - return mmName.mock -} - -// Set uses given function f to mock the genericInlineUnionWithManyTypes.Name method -func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Set(f func(t1 T)) *GenericInlineUnionWithManyTypesMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericInlineUnionWithManyTypes.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericInlineUnionWithManyTypes.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericInlineUnionWithManyTypes -func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1) - } - - mm_params := &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericInlineUnionWithManyTypesMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmName.funcName != nil { - mmName.funcName(t1) - return - } - mmName.t.Fatalf("Unexpected call to GenericInlineUnionWithManyTypesMock.Name. %v", t1) - -} - -// NameAfterCounter returns a count of finished GenericInlineUnionWithManyTypesMock.Name invocations -func (mmName *GenericInlineUnionWithManyTypesMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericInlineUnionWithManyTypesMock.Name invocations -func (mmName *GenericInlineUnionWithManyTypesMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericInlineUnionWithManyTypesMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Calls() []*GenericInlineUnionWithManyTypesMockNameParams[T] { - mmName.mutex.RLock() - - argCopy := make([]*GenericInlineUnionWithManyTypesMockNameParams[T], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") - } else { - m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericInlineUnionWithManyTypesMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-generics_with_complex_union_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_complex_union_used_as_a_generic_constraint.go deleted file mode 100644 index 6355afe..0000000 --- a/snapshots/TestSnapshot-generics_with_complex_union_used_as_a_generic_constraint.go +++ /dev/null @@ -1,255 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericComplexUnion -o ./tests/generic_complex_union.go -n GenericComplexUnionMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// GenericComplexUnionMock implements genericComplexUnion -type GenericComplexUnionMock[T complexUnion] struct { - t minimock.Tester - - funcName func(t1 T) - inspectFuncName func(t1 T) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericComplexUnionMockName[T] -} - -// NewGenericComplexUnionMock returns a mock for genericComplexUnion -func NewGenericComplexUnionMock[T complexUnion](t minimock.Tester) *GenericComplexUnionMock[T] { - m := &GenericComplexUnionMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericComplexUnionMockName[T]{mock: m} - m.NameMock.callArgs = []*GenericComplexUnionMockNameParams[T]{} - - return m -} - -type mGenericComplexUnionMockName[T complexUnion] struct { - mock *GenericComplexUnionMock[T] - defaultExpectation *GenericComplexUnionMockNameExpectation[T] - expectations []*GenericComplexUnionMockNameExpectation[T] - - callArgs []*GenericComplexUnionMockNameParams[T] - mutex sync.RWMutex -} - -// GenericComplexUnionMockNameExpectation specifies expectation struct of the genericComplexUnion.Name -type GenericComplexUnionMockNameExpectation[T complexUnion] struct { - mock *GenericComplexUnionMock[T] - params *GenericComplexUnionMockNameParams[T] - - Counter uint64 -} - -// GenericComplexUnionMockNameParams contains parameters of the genericComplexUnion.Name -type GenericComplexUnionMockNameParams[T complexUnion] struct { - t1 T -} - -// Expect sets up expected params for genericComplexUnion.Name -func (mmName *mGenericComplexUnionMockName[T]) Expect(t1 T) *mGenericComplexUnionMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericComplexUnionMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericComplexUnionMockNameExpectation[T]{} - } - - mmName.defaultExpectation.params = &GenericComplexUnionMockNameParams[T]{t1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericComplexUnion.Name -func (mmName *mGenericComplexUnionMockName[T]) Inspect(f func(t1 T)) *mGenericComplexUnionMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericComplexUnionMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericComplexUnion.Name -func (mmName *mGenericComplexUnionMockName[T]) Return() *GenericComplexUnionMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericComplexUnionMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericComplexUnionMockNameExpectation[T]{mock: mmName.mock} - } - - return mmName.mock -} - -// Set uses given function f to mock the genericComplexUnion.Name method -func (mmName *mGenericComplexUnionMockName[T]) Set(f func(t1 T)) *GenericComplexUnionMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericComplexUnion.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericComplexUnion.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericComplexUnion -func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1) - } - - mm_params := &GenericComplexUnionMockNameParams[T]{t1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericComplexUnionMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericComplexUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmName.funcName != nil { - mmName.funcName(t1) - return - } - mmName.t.Fatalf("Unexpected call to GenericComplexUnionMock.Name. %v", t1) - -} - -// NameAfterCounter returns a count of finished GenericComplexUnionMock.Name invocations -func (mmName *GenericComplexUnionMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericComplexUnionMock.Name invocations -func (mmName *GenericComplexUnionMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericComplexUnionMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericComplexUnionMockName[T]) Calls() []*GenericComplexUnionMockNameParams[T] { - mmName.mutex.RLock() - - argCopy := make([]*GenericComplexUnionMockNameParams[T], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericComplexUnionMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericComplexUnionMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericComplexUnionMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericComplexUnionMock.Name") - } else { - m.t.Errorf("Expected call to GenericComplexUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericComplexUnionMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericComplexUnionMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericComplexUnionMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericComplexUnionMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-generics_with_simple_union_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_simple_union_used_as_a_generic_constraint.go deleted file mode 100644 index 866ac77..0000000 --- a/snapshots/TestSnapshot-generics_with_simple_union_used_as_a_generic_constraint.go +++ /dev/null @@ -1,255 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSimpleUnion -o ./tests/generic_simple_union.go -n GenericSimpleUnionMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// GenericSimpleUnionMock implements genericSimpleUnion -type GenericSimpleUnionMock[T simpleUnion] struct { - t minimock.Tester - - funcName func(t1 T) - inspectFuncName func(t1 T) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericSimpleUnionMockName[T] -} - -// NewGenericSimpleUnionMock returns a mock for genericSimpleUnion -func NewGenericSimpleUnionMock[T simpleUnion](t minimock.Tester) *GenericSimpleUnionMock[T] { - m := &GenericSimpleUnionMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericSimpleUnionMockName[T]{mock: m} - m.NameMock.callArgs = []*GenericSimpleUnionMockNameParams[T]{} - - return m -} - -type mGenericSimpleUnionMockName[T simpleUnion] struct { - mock *GenericSimpleUnionMock[T] - defaultExpectation *GenericSimpleUnionMockNameExpectation[T] - expectations []*GenericSimpleUnionMockNameExpectation[T] - - callArgs []*GenericSimpleUnionMockNameParams[T] - mutex sync.RWMutex -} - -// GenericSimpleUnionMockNameExpectation specifies expectation struct of the genericSimpleUnion.Name -type GenericSimpleUnionMockNameExpectation[T simpleUnion] struct { - mock *GenericSimpleUnionMock[T] - params *GenericSimpleUnionMockNameParams[T] - - Counter uint64 -} - -// GenericSimpleUnionMockNameParams contains parameters of the genericSimpleUnion.Name -type GenericSimpleUnionMockNameParams[T simpleUnion] struct { - t1 T -} - -// Expect sets up expected params for genericSimpleUnion.Name -func (mmName *mGenericSimpleUnionMockName[T]) Expect(t1 T) *mGenericSimpleUnionMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericSimpleUnionMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericSimpleUnionMockNameExpectation[T]{} - } - - mmName.defaultExpectation.params = &GenericSimpleUnionMockNameParams[T]{t1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericSimpleUnion.Name -func (mmName *mGenericSimpleUnionMockName[T]) Inspect(f func(t1 T)) *mGenericSimpleUnionMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericSimpleUnionMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericSimpleUnion.Name -func (mmName *mGenericSimpleUnionMockName[T]) Return() *GenericSimpleUnionMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericSimpleUnionMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericSimpleUnionMockNameExpectation[T]{mock: mmName.mock} - } - - return mmName.mock -} - -// Set uses given function f to mock the genericSimpleUnion.Name method -func (mmName *mGenericSimpleUnionMockName[T]) Set(f func(t1 T)) *GenericSimpleUnionMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericSimpleUnion.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericSimpleUnion.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericSimpleUnion -func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1) - } - - mm_params := &GenericSimpleUnionMockNameParams[T]{t1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericSimpleUnionMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericSimpleUnionMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmName.funcName != nil { - mmName.funcName(t1) - return - } - mmName.t.Fatalf("Unexpected call to GenericSimpleUnionMock.Name. %v", t1) - -} - -// NameAfterCounter returns a count of finished GenericSimpleUnionMock.Name invocations -func (mmName *GenericSimpleUnionMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericSimpleUnionMock.Name invocations -func (mmName *GenericSimpleUnionMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericSimpleUnionMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericSimpleUnionMockName[T]) Calls() []*GenericSimpleUnionMockNameParams[T] { - mmName.mutex.RLock() - - argCopy := make([]*GenericSimpleUnionMockNameParams[T], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericSimpleUnionMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericSimpleUnionMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericSimpleUnionMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericSimpleUnionMock.Name") - } else { - m.t.Errorf("Expected call to GenericSimpleUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericSimpleUnionMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericSimpleUnionMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericSimpleUnionMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericSimpleUnionMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-generics_with_specific_type_used_as_a_generic_constraint.go b/snapshots/TestSnapshot-generics_with_specific_type_used_as_a_generic_constraint.go deleted file mode 100644 index b24a5ae..0000000 --- a/snapshots/TestSnapshot-generics_with_specific_type_used_as_a_generic_constraint.go +++ /dev/null @@ -1,256 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSpecific -o ./tests/generic_specific.go -n GenericSpecificMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" - "google.golang.org/protobuf/proto" -) - -// GenericSpecificMock implements genericSpecific -type GenericSpecificMock[T proto.Message] struct { - t minimock.Tester - - funcName func(t1 T) - inspectFuncName func(t1 T) - afterNameCounter uint64 - beforeNameCounter uint64 - NameMock mGenericSpecificMockName[T] -} - -// NewGenericSpecificMock returns a mock for genericSpecific -func NewGenericSpecificMock[T proto.Message](t minimock.Tester) *GenericSpecificMock[T] { - m := &GenericSpecificMock[T]{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.NameMock = mGenericSpecificMockName[T]{mock: m} - m.NameMock.callArgs = []*GenericSpecificMockNameParams[T]{} - - return m -} - -type mGenericSpecificMockName[T proto.Message] struct { - mock *GenericSpecificMock[T] - defaultExpectation *GenericSpecificMockNameExpectation[T] - expectations []*GenericSpecificMockNameExpectation[T] - - callArgs []*GenericSpecificMockNameParams[T] - mutex sync.RWMutex -} - -// GenericSpecificMockNameExpectation specifies expectation struct of the genericSpecific.Name -type GenericSpecificMockNameExpectation[T proto.Message] struct { - mock *GenericSpecificMock[T] - params *GenericSpecificMockNameParams[T] - - Counter uint64 -} - -// GenericSpecificMockNameParams contains parameters of the genericSpecific.Name -type GenericSpecificMockNameParams[T proto.Message] struct { - t1 T -} - -// Expect sets up expected params for genericSpecific.Name -func (mmName *mGenericSpecificMockName[T]) Expect(t1 T) *mGenericSpecificMockName[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericSpecificMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericSpecificMockNameExpectation[T]{} - } - - mmName.defaultExpectation.params = &GenericSpecificMockNameParams[T]{t1} - for _, e := range mmName.expectations { - if minimock.Equal(e.params, mmName.defaultExpectation.params) { - mmName.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmName.defaultExpectation.params) - } - } - - return mmName -} - -// Inspect accepts an inspector function that has same arguments as the genericSpecific.Name -func (mmName *mGenericSpecificMockName[T]) Inspect(f func(t1 T)) *mGenericSpecificMockName[T] { - if mmName.mock.inspectFuncName != nil { - mmName.mock.t.Fatalf("Inspect function is already set for GenericSpecificMock.Name") - } - - mmName.mock.inspectFuncName = f - - return mmName -} - -// Return sets up results that will be returned by genericSpecific.Name -func (mmName *mGenericSpecificMockName[T]) Return() *GenericSpecificMock[T] { - if mmName.mock.funcName != nil { - mmName.mock.t.Fatalf("GenericSpecificMock.Name mock is already set by Set") - } - - if mmName.defaultExpectation == nil { - mmName.defaultExpectation = &GenericSpecificMockNameExpectation[T]{mock: mmName.mock} - } - - return mmName.mock -} - -// Set uses given function f to mock the genericSpecific.Name method -func (mmName *mGenericSpecificMockName[T]) Set(f func(t1 T)) *GenericSpecificMock[T] { - if mmName.defaultExpectation != nil { - mmName.mock.t.Fatalf("Default expectation is already set for the genericSpecific.Name method") - } - - if len(mmName.expectations) > 0 { - mmName.mock.t.Fatalf("Some expectations are already set for the genericSpecific.Name method") - } - - mmName.mock.funcName = f - return mmName.mock -} - -// Name implements genericSpecific -func (mmName *GenericSpecificMock[T]) Name(t1 T) { - mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) - defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) - - if mmName.inspectFuncName != nil { - mmName.inspectFuncName(t1) - } - - mm_params := &GenericSpecificMockNameParams[T]{t1} - - // Record call args - mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) - mmName.NameMock.mutex.Unlock() - - for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmName.NameMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmName.NameMock.defaultExpectation.Counter, 1) - mm_want := mmName.NameMock.defaultExpectation.params - mm_got := GenericSpecificMockNameParams[T]{t1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmName.t.Errorf("GenericSpecificMock.Name got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmName.funcName != nil { - mmName.funcName(t1) - return - } - mmName.t.Fatalf("Unexpected call to GenericSpecificMock.Name. %v", t1) - -} - -// NameAfterCounter returns a count of finished GenericSpecificMock.Name invocations -func (mmName *GenericSpecificMock[T]) NameAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.afterNameCounter) -} - -// NameBeforeCounter returns a count of GenericSpecificMock.Name invocations -func (mmName *GenericSpecificMock[T]) NameBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmName.beforeNameCounter) -} - -// Calls returns a list of arguments used in each call to GenericSpecificMock.Name. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmName *mGenericSpecificMockName[T]) Calls() []*GenericSpecificMockNameParams[T] { - mmName.mutex.RLock() - - argCopy := make([]*GenericSpecificMockNameParams[T], len(mmName.callArgs)) - copy(argCopy, mmName.callArgs) - - mmName.mutex.RUnlock() - - return argCopy -} - -// MinimockNameDone returns true if the count of the Name invocations corresponds -// the number of defined expectations -func (m *GenericSpecificMock[T]) MinimockNameDone() bool { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - return false - } - return true -} - -// MinimockNameInspect logs each unmet expectation -func (m *GenericSpecificMock[T]) MinimockNameInspect() { - for _, e := range m.NameMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to GenericSpecificMock.Name with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.NameMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericSpecificMock.Name") - } else { - m.t.Errorf("Expected call to GenericSpecificMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcName != nil && mm_atomic.LoadUint64(&m.afterNameCounter) < 1 { - m.t.Error("Expected call to GenericSpecificMock.Name") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *GenericSpecificMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *GenericSpecificMock[T]) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *GenericSpecificMock[T]) minimockDone() bool { - done := true - return done && - m.MinimockNameDone() -} - diff --git a/snapshots/TestSnapshot-package_name_specified.go b/snapshots/TestSnapshot-package_name_specified.go deleted file mode 100644 index d81cd83..0000000 --- a/snapshots/TestSnapshot-package_name_specified.go +++ /dev/null @@ -1,1000 +0,0 @@ -package tests_test - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3.Tester -o ./tests/package_name_specified_test.go -n TesterMock -p tests_test - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// TesterMock implements minimock.Tester -type TesterMock struct { - t minimock.Tester - - funcError func(p1 ...interface{}) - inspectFuncError func(p1 ...interface{}) - afterErrorCounter uint64 - beforeErrorCounter uint64 - ErrorMock mTesterMockError - - funcErrorf func(format string, args ...interface{}) - inspectFuncErrorf func(format string, args ...interface{}) - afterErrorfCounter uint64 - beforeErrorfCounter uint64 - ErrorfMock mTesterMockErrorf - - funcFailNow func() - inspectFuncFailNow func() - afterFailNowCounter uint64 - beforeFailNowCounter uint64 - FailNowMock mTesterMockFailNow - - funcFatal func(args ...interface{}) - inspectFuncFatal func(args ...interface{}) - afterFatalCounter uint64 - beforeFatalCounter uint64 - FatalMock mTesterMockFatal - - funcFatalf func(format string, args ...interface{}) - inspectFuncFatalf func(format string, args ...interface{}) - afterFatalfCounter uint64 - beforeFatalfCounter uint64 - FatalfMock mTesterMockFatalf -} - -// NewTesterMock returns a mock for minimock.Tester -func NewTesterMock(t minimock.Tester) *TesterMock { - m := &TesterMock{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.ErrorMock = mTesterMockError{mock: m} - m.ErrorMock.callArgs = []*TesterMockErrorParams{} - - m.ErrorfMock = mTesterMockErrorf{mock: m} - m.ErrorfMock.callArgs = []*TesterMockErrorfParams{} - - m.FailNowMock = mTesterMockFailNow{mock: m} - - m.FatalMock = mTesterMockFatal{mock: m} - m.FatalMock.callArgs = []*TesterMockFatalParams{} - - m.FatalfMock = mTesterMockFatalf{mock: m} - m.FatalfMock.callArgs = []*TesterMockFatalfParams{} - - return m -} - -type mTesterMockError struct { - mock *TesterMock - defaultExpectation *TesterMockErrorExpectation - expectations []*TesterMockErrorExpectation - - callArgs []*TesterMockErrorParams - mutex sync.RWMutex -} - -// TesterMockErrorExpectation specifies expectation struct of the Tester.Error -type TesterMockErrorExpectation struct { - mock *TesterMock - params *TesterMockErrorParams - - Counter uint64 -} - -// TesterMockErrorParams contains parameters of the Tester.Error -type TesterMockErrorParams struct { - p1 []interface{} -} - -// Expect sets up expected params for Tester.Error -func (mmError *mTesterMockError) Expect(p1 ...interface{}) *mTesterMockError { - if mmError.mock.funcError != nil { - mmError.mock.t.Fatalf("TesterMock.Error mock is already set by Set") - } - - if mmError.defaultExpectation == nil { - mmError.defaultExpectation = &TesterMockErrorExpectation{} - } - - mmError.defaultExpectation.params = &TesterMockErrorParams{p1} - for _, e := range mmError.expectations { - if minimock.Equal(e.params, mmError.defaultExpectation.params) { - mmError.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmError.defaultExpectation.params) - } - } - - return mmError -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Error -func (mmError *mTesterMockError) Inspect(f func(p1 ...interface{})) *mTesterMockError { - if mmError.mock.inspectFuncError != nil { - mmError.mock.t.Fatalf("Inspect function is already set for TesterMock.Error") - } - - mmError.mock.inspectFuncError = f - - return mmError -} - -// Return sets up results that will be returned by Tester.Error -func (mmError *mTesterMockError) Return() *TesterMock { - if mmError.mock.funcError != nil { - mmError.mock.t.Fatalf("TesterMock.Error mock is already set by Set") - } - - if mmError.defaultExpectation == nil { - mmError.defaultExpectation = &TesterMockErrorExpectation{mock: mmError.mock} - } - - return mmError.mock -} - -// Set uses given function f to mock the Tester.Error method -func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { - if mmError.defaultExpectation != nil { - mmError.mock.t.Fatalf("Default expectation is already set for the Tester.Error method") - } - - if len(mmError.expectations) > 0 { - mmError.mock.t.Fatalf("Some expectations are already set for the Tester.Error method") - } - - mmError.mock.funcError = f - return mmError.mock -} - -// Error implements minimock.Tester -func (mmError *TesterMock) Error(p1 ...interface{}) { - mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) - defer mm_atomic.AddUint64(&mmError.afterErrorCounter, 1) - - if mmError.inspectFuncError != nil { - mmError.inspectFuncError(p1...) - } - - mm_params := &TesterMockErrorParams{p1} - - // Record call args - mmError.ErrorMock.mutex.Lock() - mmError.ErrorMock.callArgs = append(mmError.ErrorMock.callArgs, mm_params) - mmError.ErrorMock.mutex.Unlock() - - for _, e := range mmError.ErrorMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmError.ErrorMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmError.ErrorMock.defaultExpectation.Counter, 1) - mm_want := mmError.ErrorMock.defaultExpectation.params - mm_got := TesterMockErrorParams{p1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmError.t.Errorf("TesterMock.Error got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmError.funcError != nil { - mmError.funcError(p1...) - return - } - mmError.t.Fatalf("Unexpected call to TesterMock.Error. %v", p1) - -} - -// ErrorAfterCounter returns a count of finished TesterMock.Error invocations -func (mmError *TesterMock) ErrorAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmError.afterErrorCounter) -} - -// ErrorBeforeCounter returns a count of TesterMock.Error invocations -func (mmError *TesterMock) ErrorBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmError.beforeErrorCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Error. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmError *mTesterMockError) Calls() []*TesterMockErrorParams { - mmError.mutex.RLock() - - argCopy := make([]*TesterMockErrorParams, len(mmError.callArgs)) - copy(argCopy, mmError.callArgs) - - mmError.mutex.RUnlock() - - return argCopy -} - -// MinimockErrorDone returns true if the count of the Error invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockErrorDone() bool { - for _, e := range m.ErrorMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - return true -} - -// MinimockErrorInspect logs each unmet expectation -func (m *TesterMock) MinimockErrorInspect() { - for _, e := range m.ErrorMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - if m.ErrorMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Error") - } else { - m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *m.ErrorMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - m.t.Error("Expected call to TesterMock.Error") - } -} - -type mTesterMockErrorf struct { - mock *TesterMock - defaultExpectation *TesterMockErrorfExpectation - expectations []*TesterMockErrorfExpectation - - callArgs []*TesterMockErrorfParams - mutex sync.RWMutex -} - -// TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf -type TesterMockErrorfExpectation struct { - mock *TesterMock - params *TesterMockErrorfParams - - Counter uint64 -} - -// TesterMockErrorfParams contains parameters of the Tester.Errorf -type TesterMockErrorfParams struct { - format string - args []interface{} -} - -// Expect sets up expected params for Tester.Errorf -func (mmErrorf *mTesterMockErrorf) Expect(format string, args ...interface{}) *mTesterMockErrorf { - if mmErrorf.mock.funcErrorf != nil { - mmErrorf.mock.t.Fatalf("TesterMock.Errorf mock is already set by Set") - } - - if mmErrorf.defaultExpectation == nil { - mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{} - } - - mmErrorf.defaultExpectation.params = &TesterMockErrorfParams{format, args} - for _, e := range mmErrorf.expectations { - if minimock.Equal(e.params, mmErrorf.defaultExpectation.params) { - mmErrorf.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmErrorf.defaultExpectation.params) - } - } - - return mmErrorf -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Errorf -func (mmErrorf *mTesterMockErrorf) Inspect(f func(format string, args ...interface{})) *mTesterMockErrorf { - if mmErrorf.mock.inspectFuncErrorf != nil { - mmErrorf.mock.t.Fatalf("Inspect function is already set for TesterMock.Errorf") - } - - mmErrorf.mock.inspectFuncErrorf = f - - return mmErrorf -} - -// Return sets up results that will be returned by Tester.Errorf -func (mmErrorf *mTesterMockErrorf) Return() *TesterMock { - if mmErrorf.mock.funcErrorf != nil { - mmErrorf.mock.t.Fatalf("TesterMock.Errorf mock is already set by Set") - } - - if mmErrorf.defaultExpectation == nil { - mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{mock: mmErrorf.mock} - } - - return mmErrorf.mock -} - -// Set uses given function f to mock the Tester.Errorf method -func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{})) *TesterMock { - if mmErrorf.defaultExpectation != nil { - mmErrorf.mock.t.Fatalf("Default expectation is already set for the Tester.Errorf method") - } - - if len(mmErrorf.expectations) > 0 { - mmErrorf.mock.t.Fatalf("Some expectations are already set for the Tester.Errorf method") - } - - mmErrorf.mock.funcErrorf = f - return mmErrorf.mock -} - -// Errorf implements minimock.Tester -func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { - mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) - defer mm_atomic.AddUint64(&mmErrorf.afterErrorfCounter, 1) - - if mmErrorf.inspectFuncErrorf != nil { - mmErrorf.inspectFuncErrorf(format, args...) - } - - mm_params := &TesterMockErrorfParams{format, args} - - // Record call args - mmErrorf.ErrorfMock.mutex.Lock() - mmErrorf.ErrorfMock.callArgs = append(mmErrorf.ErrorfMock.callArgs, mm_params) - mmErrorf.ErrorfMock.mutex.Unlock() - - for _, e := range mmErrorf.ErrorfMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmErrorf.ErrorfMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmErrorf.ErrorfMock.defaultExpectation.Counter, 1) - mm_want := mmErrorf.ErrorfMock.defaultExpectation.params - mm_got := TesterMockErrorfParams{format, args} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmErrorf.funcErrorf != nil { - mmErrorf.funcErrorf(format, args...) - return - } - mmErrorf.t.Fatalf("Unexpected call to TesterMock.Errorf. %v %v", format, args) - -} - -// ErrorfAfterCounter returns a count of finished TesterMock.Errorf invocations -func (mmErrorf *TesterMock) ErrorfAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmErrorf.afterErrorfCounter) -} - -// ErrorfBeforeCounter returns a count of TesterMock.Errorf invocations -func (mmErrorf *TesterMock) ErrorfBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmErrorf.beforeErrorfCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Errorf. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmErrorf *mTesterMockErrorf) Calls() []*TesterMockErrorfParams { - mmErrorf.mutex.RLock() - - argCopy := make([]*TesterMockErrorfParams, len(mmErrorf.callArgs)) - copy(argCopy, mmErrorf.callArgs) - - mmErrorf.mutex.RUnlock() - - return argCopy -} - -// MinimockErrorfDone returns true if the count of the Errorf invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockErrorfDone() bool { - for _, e := range m.ErrorfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - return true -} - -// MinimockErrorfInspect logs each unmet expectation -func (m *TesterMock) MinimockErrorfInspect() { - for _, e := range m.ErrorfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - if m.ErrorfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Errorf") - } else { - m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *m.ErrorfMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - m.t.Error("Expected call to TesterMock.Errorf") - } -} - -type mTesterMockFailNow struct { - mock *TesterMock - defaultExpectation *TesterMockFailNowExpectation - expectations []*TesterMockFailNowExpectation -} - -// TesterMockFailNowExpectation specifies expectation struct of the Tester.FailNow -type TesterMockFailNowExpectation struct { - mock *TesterMock - - Counter uint64 -} - -// Expect sets up expected params for Tester.FailNow -func (mmFailNow *mTesterMockFailNow) Expect() *mTesterMockFailNow { - if mmFailNow.mock.funcFailNow != nil { - mmFailNow.mock.t.Fatalf("TesterMock.FailNow mock is already set by Set") - } - - if mmFailNow.defaultExpectation == nil { - mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{} - } - - return mmFailNow -} - -// Inspect accepts an inspector function that has same arguments as the Tester.FailNow -func (mmFailNow *mTesterMockFailNow) Inspect(f func()) *mTesterMockFailNow { - if mmFailNow.mock.inspectFuncFailNow != nil { - mmFailNow.mock.t.Fatalf("Inspect function is already set for TesterMock.FailNow") - } - - mmFailNow.mock.inspectFuncFailNow = f - - return mmFailNow -} - -// Return sets up results that will be returned by Tester.FailNow -func (mmFailNow *mTesterMockFailNow) Return() *TesterMock { - if mmFailNow.mock.funcFailNow != nil { - mmFailNow.mock.t.Fatalf("TesterMock.FailNow mock is already set by Set") - } - - if mmFailNow.defaultExpectation == nil { - mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{mock: mmFailNow.mock} - } - - return mmFailNow.mock -} - -// Set uses given function f to mock the Tester.FailNow method -func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { - if mmFailNow.defaultExpectation != nil { - mmFailNow.mock.t.Fatalf("Default expectation is already set for the Tester.FailNow method") - } - - if len(mmFailNow.expectations) > 0 { - mmFailNow.mock.t.Fatalf("Some expectations are already set for the Tester.FailNow method") - } - - mmFailNow.mock.funcFailNow = f - return mmFailNow.mock -} - -// FailNow implements minimock.Tester -func (mmFailNow *TesterMock) FailNow() { - mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) - defer mm_atomic.AddUint64(&mmFailNow.afterFailNowCounter, 1) - - if mmFailNow.inspectFuncFailNow != nil { - mmFailNow.inspectFuncFailNow() - } - - if mmFailNow.FailNowMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmFailNow.FailNowMock.defaultExpectation.Counter, 1) - - return - - } - if mmFailNow.funcFailNow != nil { - mmFailNow.funcFailNow() - return - } - mmFailNow.t.Fatalf("Unexpected call to TesterMock.FailNow.") - -} - -// FailNowAfterCounter returns a count of finished TesterMock.FailNow invocations -func (mmFailNow *TesterMock) FailNowAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmFailNow.afterFailNowCounter) -} - -// FailNowBeforeCounter returns a count of TesterMock.FailNow invocations -func (mmFailNow *TesterMock) FailNowBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmFailNow.beforeFailNowCounter) -} - -// MinimockFailNowDone returns true if the count of the FailNow invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockFailNowDone() bool { - for _, e := range m.FailNowMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - return true -} - -// MinimockFailNowInspect logs each unmet expectation -func (m *TesterMock) MinimockFailNowInspect() { - for _, e := range m.FailNowMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Error("Expected call to TesterMock.FailNow") - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - m.t.Error("Expected call to TesterMock.FailNow") - } - // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - m.t.Error("Expected call to TesterMock.FailNow") - } -} - -type mTesterMockFatal struct { - mock *TesterMock - defaultExpectation *TesterMockFatalExpectation - expectations []*TesterMockFatalExpectation - - callArgs []*TesterMockFatalParams - mutex sync.RWMutex -} - -// TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal -type TesterMockFatalExpectation struct { - mock *TesterMock - params *TesterMockFatalParams - - Counter uint64 -} - -// TesterMockFatalParams contains parameters of the Tester.Fatal -type TesterMockFatalParams struct { - args []interface{} -} - -// Expect sets up expected params for Tester.Fatal -func (mmFatal *mTesterMockFatal) Expect(args ...interface{}) *mTesterMockFatal { - if mmFatal.mock.funcFatal != nil { - mmFatal.mock.t.Fatalf("TesterMock.Fatal mock is already set by Set") - } - - if mmFatal.defaultExpectation == nil { - mmFatal.defaultExpectation = &TesterMockFatalExpectation{} - } - - mmFatal.defaultExpectation.params = &TesterMockFatalParams{args} - for _, e := range mmFatal.expectations { - if minimock.Equal(e.params, mmFatal.defaultExpectation.params) { - mmFatal.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFatal.defaultExpectation.params) - } - } - - return mmFatal -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Fatal -func (mmFatal *mTesterMockFatal) Inspect(f func(args ...interface{})) *mTesterMockFatal { - if mmFatal.mock.inspectFuncFatal != nil { - mmFatal.mock.t.Fatalf("Inspect function is already set for TesterMock.Fatal") - } - - mmFatal.mock.inspectFuncFatal = f - - return mmFatal -} - -// Return sets up results that will be returned by Tester.Fatal -func (mmFatal *mTesterMockFatal) Return() *TesterMock { - if mmFatal.mock.funcFatal != nil { - mmFatal.mock.t.Fatalf("TesterMock.Fatal mock is already set by Set") - } - - if mmFatal.defaultExpectation == nil { - mmFatal.defaultExpectation = &TesterMockFatalExpectation{mock: mmFatal.mock} - } - - return mmFatal.mock -} - -// Set uses given function f to mock the Tester.Fatal method -func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { - if mmFatal.defaultExpectation != nil { - mmFatal.mock.t.Fatalf("Default expectation is already set for the Tester.Fatal method") - } - - if len(mmFatal.expectations) > 0 { - mmFatal.mock.t.Fatalf("Some expectations are already set for the Tester.Fatal method") - } - - mmFatal.mock.funcFatal = f - return mmFatal.mock -} - -// Fatal implements minimock.Tester -func (mmFatal *TesterMock) Fatal(args ...interface{}) { - mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) - defer mm_atomic.AddUint64(&mmFatal.afterFatalCounter, 1) - - if mmFatal.inspectFuncFatal != nil { - mmFatal.inspectFuncFatal(args...) - } - - mm_params := &TesterMockFatalParams{args} - - // Record call args - mmFatal.FatalMock.mutex.Lock() - mmFatal.FatalMock.callArgs = append(mmFatal.FatalMock.callArgs, mm_params) - mmFatal.FatalMock.mutex.Unlock() - - for _, e := range mmFatal.FatalMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmFatal.FatalMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmFatal.FatalMock.defaultExpectation.Counter, 1) - mm_want := mmFatal.FatalMock.defaultExpectation.params - mm_got := TesterMockFatalParams{args} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmFatal.funcFatal != nil { - mmFatal.funcFatal(args...) - return - } - mmFatal.t.Fatalf("Unexpected call to TesterMock.Fatal. %v", args) - -} - -// FatalAfterCounter returns a count of finished TesterMock.Fatal invocations -func (mmFatal *TesterMock) FatalAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatal.afterFatalCounter) -} - -// FatalBeforeCounter returns a count of TesterMock.Fatal invocations -func (mmFatal *TesterMock) FatalBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatal.beforeFatalCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Fatal. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmFatal *mTesterMockFatal) Calls() []*TesterMockFatalParams { - mmFatal.mutex.RLock() - - argCopy := make([]*TesterMockFatalParams, len(mmFatal.callArgs)) - copy(argCopy, mmFatal.callArgs) - - mmFatal.mutex.RUnlock() - - return argCopy -} - -// MinimockFatalDone returns true if the count of the Fatal invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockFatalDone() bool { - for _, e := range m.FatalMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - return true -} - -// MinimockFatalInspect logs each unmet expectation -func (m *TesterMock) MinimockFatalInspect() { - for _, e := range m.FatalMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - if m.FatalMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatal") - } else { - m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *m.FatalMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - m.t.Error("Expected call to TesterMock.Fatal") - } -} - -type mTesterMockFatalf struct { - mock *TesterMock - defaultExpectation *TesterMockFatalfExpectation - expectations []*TesterMockFatalfExpectation - - callArgs []*TesterMockFatalfParams - mutex sync.RWMutex -} - -// TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf -type TesterMockFatalfExpectation struct { - mock *TesterMock - params *TesterMockFatalfParams - - Counter uint64 -} - -// TesterMockFatalfParams contains parameters of the Tester.Fatalf -type TesterMockFatalfParams struct { - format string - args []interface{} -} - -// Expect sets up expected params for Tester.Fatalf -func (mmFatalf *mTesterMockFatalf) Expect(format string, args ...interface{}) *mTesterMockFatalf { - if mmFatalf.mock.funcFatalf != nil { - mmFatalf.mock.t.Fatalf("TesterMock.Fatalf mock is already set by Set") - } - - if mmFatalf.defaultExpectation == nil { - mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{} - } - - mmFatalf.defaultExpectation.params = &TesterMockFatalfParams{format, args} - for _, e := range mmFatalf.expectations { - if minimock.Equal(e.params, mmFatalf.defaultExpectation.params) { - mmFatalf.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFatalf.defaultExpectation.params) - } - } - - return mmFatalf -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Fatalf -func (mmFatalf *mTesterMockFatalf) Inspect(f func(format string, args ...interface{})) *mTesterMockFatalf { - if mmFatalf.mock.inspectFuncFatalf != nil { - mmFatalf.mock.t.Fatalf("Inspect function is already set for TesterMock.Fatalf") - } - - mmFatalf.mock.inspectFuncFatalf = f - - return mmFatalf -} - -// Return sets up results that will be returned by Tester.Fatalf -func (mmFatalf *mTesterMockFatalf) Return() *TesterMock { - if mmFatalf.mock.funcFatalf != nil { - mmFatalf.mock.t.Fatalf("TesterMock.Fatalf mock is already set by Set") - } - - if mmFatalf.defaultExpectation == nil { - mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{mock: mmFatalf.mock} - } - - return mmFatalf.mock -} - -// Set uses given function f to mock the Tester.Fatalf method -func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{})) *TesterMock { - if mmFatalf.defaultExpectation != nil { - mmFatalf.mock.t.Fatalf("Default expectation is already set for the Tester.Fatalf method") - } - - if len(mmFatalf.expectations) > 0 { - mmFatalf.mock.t.Fatalf("Some expectations are already set for the Tester.Fatalf method") - } - - mmFatalf.mock.funcFatalf = f - return mmFatalf.mock -} - -// Fatalf implements minimock.Tester -func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { - mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) - defer mm_atomic.AddUint64(&mmFatalf.afterFatalfCounter, 1) - - if mmFatalf.inspectFuncFatalf != nil { - mmFatalf.inspectFuncFatalf(format, args...) - } - - mm_params := &TesterMockFatalfParams{format, args} - - // Record call args - mmFatalf.FatalfMock.mutex.Lock() - mmFatalf.FatalfMock.callArgs = append(mmFatalf.FatalfMock.callArgs, mm_params) - mmFatalf.FatalfMock.mutex.Unlock() - - for _, e := range mmFatalf.FatalfMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmFatalf.FatalfMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmFatalf.FatalfMock.defaultExpectation.Counter, 1) - mm_want := mmFatalf.FatalfMock.defaultExpectation.params - mm_got := TesterMockFatalfParams{format, args} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmFatalf.funcFatalf != nil { - mmFatalf.funcFatalf(format, args...) - return - } - mmFatalf.t.Fatalf("Unexpected call to TesterMock.Fatalf. %v %v", format, args) - -} - -// FatalfAfterCounter returns a count of finished TesterMock.Fatalf invocations -func (mmFatalf *TesterMock) FatalfAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatalf.afterFatalfCounter) -} - -// FatalfBeforeCounter returns a count of TesterMock.Fatalf invocations -func (mmFatalf *TesterMock) FatalfBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatalf.beforeFatalfCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Fatalf. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmFatalf *mTesterMockFatalf) Calls() []*TesterMockFatalfParams { - mmFatalf.mutex.RLock() - - argCopy := make([]*TesterMockFatalfParams, len(mmFatalf.callArgs)) - copy(argCopy, mmFatalf.callArgs) - - mmFatalf.mutex.RUnlock() - - return argCopy -} - -// MinimockFatalfDone returns true if the count of the Fatalf invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockFatalfDone() bool { - for _, e := range m.FatalfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - return true -} - -// MinimockFatalfInspect logs each unmet expectation -func (m *TesterMock) MinimockFatalfInspect() { - for _, e := range m.FatalfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - if m.FatalfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatalf") - } else { - m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *m.FatalfMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - m.t.Error("Expected call to TesterMock.Fatalf") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *TesterMock) MinimockFinish() { - if !m.minimockDone() { - m.MinimockErrorInspect() - - m.MinimockErrorfInspect() - - m.MinimockFailNowInspect() - - m.MinimockFatalInspect() - - m.MinimockFatalfInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *TesterMock) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *TesterMock) minimockDone() bool { - done := true - return done && - m.MinimockErrorDone() && - m.MinimockErrorfDone() && - m.MinimockFailNowDone() && - m.MinimockFatalDone() && - m.MinimockFatalfDone() -} - diff --git a/snapshots/TestSnapshot-package_reference.go b/snapshots/TestSnapshot-package_reference.go deleted file mode 100644 index c9abb62..0000000 --- a/snapshots/TestSnapshot-package_reference.go +++ /dev/null @@ -1,1000 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3.Tester -o ./tests/tester_mock_test.go -n TesterMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// TesterMock implements minimock.Tester -type TesterMock struct { - t minimock.Tester - - funcError func(p1 ...interface{}) - inspectFuncError func(p1 ...interface{}) - afterErrorCounter uint64 - beforeErrorCounter uint64 - ErrorMock mTesterMockError - - funcErrorf func(format string, args ...interface{}) - inspectFuncErrorf func(format string, args ...interface{}) - afterErrorfCounter uint64 - beforeErrorfCounter uint64 - ErrorfMock mTesterMockErrorf - - funcFailNow func() - inspectFuncFailNow func() - afterFailNowCounter uint64 - beforeFailNowCounter uint64 - FailNowMock mTesterMockFailNow - - funcFatal func(args ...interface{}) - inspectFuncFatal func(args ...interface{}) - afterFatalCounter uint64 - beforeFatalCounter uint64 - FatalMock mTesterMockFatal - - funcFatalf func(format string, args ...interface{}) - inspectFuncFatalf func(format string, args ...interface{}) - afterFatalfCounter uint64 - beforeFatalfCounter uint64 - FatalfMock mTesterMockFatalf -} - -// NewTesterMock returns a mock for minimock.Tester -func NewTesterMock(t minimock.Tester) *TesterMock { - m := &TesterMock{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.ErrorMock = mTesterMockError{mock: m} - m.ErrorMock.callArgs = []*TesterMockErrorParams{} - - m.ErrorfMock = mTesterMockErrorf{mock: m} - m.ErrorfMock.callArgs = []*TesterMockErrorfParams{} - - m.FailNowMock = mTesterMockFailNow{mock: m} - - m.FatalMock = mTesterMockFatal{mock: m} - m.FatalMock.callArgs = []*TesterMockFatalParams{} - - m.FatalfMock = mTesterMockFatalf{mock: m} - m.FatalfMock.callArgs = []*TesterMockFatalfParams{} - - return m -} - -type mTesterMockError struct { - mock *TesterMock - defaultExpectation *TesterMockErrorExpectation - expectations []*TesterMockErrorExpectation - - callArgs []*TesterMockErrorParams - mutex sync.RWMutex -} - -// TesterMockErrorExpectation specifies expectation struct of the Tester.Error -type TesterMockErrorExpectation struct { - mock *TesterMock - params *TesterMockErrorParams - - Counter uint64 -} - -// TesterMockErrorParams contains parameters of the Tester.Error -type TesterMockErrorParams struct { - p1 []interface{} -} - -// Expect sets up expected params for Tester.Error -func (mmError *mTesterMockError) Expect(p1 ...interface{}) *mTesterMockError { - if mmError.mock.funcError != nil { - mmError.mock.t.Fatalf("TesterMock.Error mock is already set by Set") - } - - if mmError.defaultExpectation == nil { - mmError.defaultExpectation = &TesterMockErrorExpectation{} - } - - mmError.defaultExpectation.params = &TesterMockErrorParams{p1} - for _, e := range mmError.expectations { - if minimock.Equal(e.params, mmError.defaultExpectation.params) { - mmError.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmError.defaultExpectation.params) - } - } - - return mmError -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Error -func (mmError *mTesterMockError) Inspect(f func(p1 ...interface{})) *mTesterMockError { - if mmError.mock.inspectFuncError != nil { - mmError.mock.t.Fatalf("Inspect function is already set for TesterMock.Error") - } - - mmError.mock.inspectFuncError = f - - return mmError -} - -// Return sets up results that will be returned by Tester.Error -func (mmError *mTesterMockError) Return() *TesterMock { - if mmError.mock.funcError != nil { - mmError.mock.t.Fatalf("TesterMock.Error mock is already set by Set") - } - - if mmError.defaultExpectation == nil { - mmError.defaultExpectation = &TesterMockErrorExpectation{mock: mmError.mock} - } - - return mmError.mock -} - -// Set uses given function f to mock the Tester.Error method -func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { - if mmError.defaultExpectation != nil { - mmError.mock.t.Fatalf("Default expectation is already set for the Tester.Error method") - } - - if len(mmError.expectations) > 0 { - mmError.mock.t.Fatalf("Some expectations are already set for the Tester.Error method") - } - - mmError.mock.funcError = f - return mmError.mock -} - -// Error implements minimock.Tester -func (mmError *TesterMock) Error(p1 ...interface{}) { - mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) - defer mm_atomic.AddUint64(&mmError.afterErrorCounter, 1) - - if mmError.inspectFuncError != nil { - mmError.inspectFuncError(p1...) - } - - mm_params := &TesterMockErrorParams{p1} - - // Record call args - mmError.ErrorMock.mutex.Lock() - mmError.ErrorMock.callArgs = append(mmError.ErrorMock.callArgs, mm_params) - mmError.ErrorMock.mutex.Unlock() - - for _, e := range mmError.ErrorMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmError.ErrorMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmError.ErrorMock.defaultExpectation.Counter, 1) - mm_want := mmError.ErrorMock.defaultExpectation.params - mm_got := TesterMockErrorParams{p1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmError.t.Errorf("TesterMock.Error got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmError.funcError != nil { - mmError.funcError(p1...) - return - } - mmError.t.Fatalf("Unexpected call to TesterMock.Error. %v", p1) - -} - -// ErrorAfterCounter returns a count of finished TesterMock.Error invocations -func (mmError *TesterMock) ErrorAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmError.afterErrorCounter) -} - -// ErrorBeforeCounter returns a count of TesterMock.Error invocations -func (mmError *TesterMock) ErrorBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmError.beforeErrorCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Error. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmError *mTesterMockError) Calls() []*TesterMockErrorParams { - mmError.mutex.RLock() - - argCopy := make([]*TesterMockErrorParams, len(mmError.callArgs)) - copy(argCopy, mmError.callArgs) - - mmError.mutex.RUnlock() - - return argCopy -} - -// MinimockErrorDone returns true if the count of the Error invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockErrorDone() bool { - for _, e := range m.ErrorMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - return true -} - -// MinimockErrorInspect logs each unmet expectation -func (m *TesterMock) MinimockErrorInspect() { - for _, e := range m.ErrorMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - if m.ErrorMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Error") - } else { - m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *m.ErrorMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - m.t.Error("Expected call to TesterMock.Error") - } -} - -type mTesterMockErrorf struct { - mock *TesterMock - defaultExpectation *TesterMockErrorfExpectation - expectations []*TesterMockErrorfExpectation - - callArgs []*TesterMockErrorfParams - mutex sync.RWMutex -} - -// TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf -type TesterMockErrorfExpectation struct { - mock *TesterMock - params *TesterMockErrorfParams - - Counter uint64 -} - -// TesterMockErrorfParams contains parameters of the Tester.Errorf -type TesterMockErrorfParams struct { - format string - args []interface{} -} - -// Expect sets up expected params for Tester.Errorf -func (mmErrorf *mTesterMockErrorf) Expect(format string, args ...interface{}) *mTesterMockErrorf { - if mmErrorf.mock.funcErrorf != nil { - mmErrorf.mock.t.Fatalf("TesterMock.Errorf mock is already set by Set") - } - - if mmErrorf.defaultExpectation == nil { - mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{} - } - - mmErrorf.defaultExpectation.params = &TesterMockErrorfParams{format, args} - for _, e := range mmErrorf.expectations { - if minimock.Equal(e.params, mmErrorf.defaultExpectation.params) { - mmErrorf.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmErrorf.defaultExpectation.params) - } - } - - return mmErrorf -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Errorf -func (mmErrorf *mTesterMockErrorf) Inspect(f func(format string, args ...interface{})) *mTesterMockErrorf { - if mmErrorf.mock.inspectFuncErrorf != nil { - mmErrorf.mock.t.Fatalf("Inspect function is already set for TesterMock.Errorf") - } - - mmErrorf.mock.inspectFuncErrorf = f - - return mmErrorf -} - -// Return sets up results that will be returned by Tester.Errorf -func (mmErrorf *mTesterMockErrorf) Return() *TesterMock { - if mmErrorf.mock.funcErrorf != nil { - mmErrorf.mock.t.Fatalf("TesterMock.Errorf mock is already set by Set") - } - - if mmErrorf.defaultExpectation == nil { - mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{mock: mmErrorf.mock} - } - - return mmErrorf.mock -} - -// Set uses given function f to mock the Tester.Errorf method -func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{})) *TesterMock { - if mmErrorf.defaultExpectation != nil { - mmErrorf.mock.t.Fatalf("Default expectation is already set for the Tester.Errorf method") - } - - if len(mmErrorf.expectations) > 0 { - mmErrorf.mock.t.Fatalf("Some expectations are already set for the Tester.Errorf method") - } - - mmErrorf.mock.funcErrorf = f - return mmErrorf.mock -} - -// Errorf implements minimock.Tester -func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { - mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) - defer mm_atomic.AddUint64(&mmErrorf.afterErrorfCounter, 1) - - if mmErrorf.inspectFuncErrorf != nil { - mmErrorf.inspectFuncErrorf(format, args...) - } - - mm_params := &TesterMockErrorfParams{format, args} - - // Record call args - mmErrorf.ErrorfMock.mutex.Lock() - mmErrorf.ErrorfMock.callArgs = append(mmErrorf.ErrorfMock.callArgs, mm_params) - mmErrorf.ErrorfMock.mutex.Unlock() - - for _, e := range mmErrorf.ErrorfMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmErrorf.ErrorfMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmErrorf.ErrorfMock.defaultExpectation.Counter, 1) - mm_want := mmErrorf.ErrorfMock.defaultExpectation.params - mm_got := TesterMockErrorfParams{format, args} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmErrorf.funcErrorf != nil { - mmErrorf.funcErrorf(format, args...) - return - } - mmErrorf.t.Fatalf("Unexpected call to TesterMock.Errorf. %v %v", format, args) - -} - -// ErrorfAfterCounter returns a count of finished TesterMock.Errorf invocations -func (mmErrorf *TesterMock) ErrorfAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmErrorf.afterErrorfCounter) -} - -// ErrorfBeforeCounter returns a count of TesterMock.Errorf invocations -func (mmErrorf *TesterMock) ErrorfBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmErrorf.beforeErrorfCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Errorf. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmErrorf *mTesterMockErrorf) Calls() []*TesterMockErrorfParams { - mmErrorf.mutex.RLock() - - argCopy := make([]*TesterMockErrorfParams, len(mmErrorf.callArgs)) - copy(argCopy, mmErrorf.callArgs) - - mmErrorf.mutex.RUnlock() - - return argCopy -} - -// MinimockErrorfDone returns true if the count of the Errorf invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockErrorfDone() bool { - for _, e := range m.ErrorfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - return true -} - -// MinimockErrorfInspect logs each unmet expectation -func (m *TesterMock) MinimockErrorfInspect() { - for _, e := range m.ErrorfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - if m.ErrorfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Errorf") - } else { - m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *m.ErrorfMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - m.t.Error("Expected call to TesterMock.Errorf") - } -} - -type mTesterMockFailNow struct { - mock *TesterMock - defaultExpectation *TesterMockFailNowExpectation - expectations []*TesterMockFailNowExpectation -} - -// TesterMockFailNowExpectation specifies expectation struct of the Tester.FailNow -type TesterMockFailNowExpectation struct { - mock *TesterMock - - Counter uint64 -} - -// Expect sets up expected params for Tester.FailNow -func (mmFailNow *mTesterMockFailNow) Expect() *mTesterMockFailNow { - if mmFailNow.mock.funcFailNow != nil { - mmFailNow.mock.t.Fatalf("TesterMock.FailNow mock is already set by Set") - } - - if mmFailNow.defaultExpectation == nil { - mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{} - } - - return mmFailNow -} - -// Inspect accepts an inspector function that has same arguments as the Tester.FailNow -func (mmFailNow *mTesterMockFailNow) Inspect(f func()) *mTesterMockFailNow { - if mmFailNow.mock.inspectFuncFailNow != nil { - mmFailNow.mock.t.Fatalf("Inspect function is already set for TesterMock.FailNow") - } - - mmFailNow.mock.inspectFuncFailNow = f - - return mmFailNow -} - -// Return sets up results that will be returned by Tester.FailNow -func (mmFailNow *mTesterMockFailNow) Return() *TesterMock { - if mmFailNow.mock.funcFailNow != nil { - mmFailNow.mock.t.Fatalf("TesterMock.FailNow mock is already set by Set") - } - - if mmFailNow.defaultExpectation == nil { - mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{mock: mmFailNow.mock} - } - - return mmFailNow.mock -} - -// Set uses given function f to mock the Tester.FailNow method -func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { - if mmFailNow.defaultExpectation != nil { - mmFailNow.mock.t.Fatalf("Default expectation is already set for the Tester.FailNow method") - } - - if len(mmFailNow.expectations) > 0 { - mmFailNow.mock.t.Fatalf("Some expectations are already set for the Tester.FailNow method") - } - - mmFailNow.mock.funcFailNow = f - return mmFailNow.mock -} - -// FailNow implements minimock.Tester -func (mmFailNow *TesterMock) FailNow() { - mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) - defer mm_atomic.AddUint64(&mmFailNow.afterFailNowCounter, 1) - - if mmFailNow.inspectFuncFailNow != nil { - mmFailNow.inspectFuncFailNow() - } - - if mmFailNow.FailNowMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmFailNow.FailNowMock.defaultExpectation.Counter, 1) - - return - - } - if mmFailNow.funcFailNow != nil { - mmFailNow.funcFailNow() - return - } - mmFailNow.t.Fatalf("Unexpected call to TesterMock.FailNow.") - -} - -// FailNowAfterCounter returns a count of finished TesterMock.FailNow invocations -func (mmFailNow *TesterMock) FailNowAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmFailNow.afterFailNowCounter) -} - -// FailNowBeforeCounter returns a count of TesterMock.FailNow invocations -func (mmFailNow *TesterMock) FailNowBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmFailNow.beforeFailNowCounter) -} - -// MinimockFailNowDone returns true if the count of the FailNow invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockFailNowDone() bool { - for _, e := range m.FailNowMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - return false - } - return true -} - -// MinimockFailNowInspect logs each unmet expectation -func (m *TesterMock) MinimockFailNowInspect() { - for _, e := range m.FailNowMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Error("Expected call to TesterMock.FailNow") - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FailNowMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - m.t.Error("Expected call to TesterMock.FailNow") - } - // if func was set then invocations count should be greater than zero - if m.funcFailNow != nil && mm_atomic.LoadUint64(&m.afterFailNowCounter) < 1 { - m.t.Error("Expected call to TesterMock.FailNow") - } -} - -type mTesterMockFatal struct { - mock *TesterMock - defaultExpectation *TesterMockFatalExpectation - expectations []*TesterMockFatalExpectation - - callArgs []*TesterMockFatalParams - mutex sync.RWMutex -} - -// TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal -type TesterMockFatalExpectation struct { - mock *TesterMock - params *TesterMockFatalParams - - Counter uint64 -} - -// TesterMockFatalParams contains parameters of the Tester.Fatal -type TesterMockFatalParams struct { - args []interface{} -} - -// Expect sets up expected params for Tester.Fatal -func (mmFatal *mTesterMockFatal) Expect(args ...interface{}) *mTesterMockFatal { - if mmFatal.mock.funcFatal != nil { - mmFatal.mock.t.Fatalf("TesterMock.Fatal mock is already set by Set") - } - - if mmFatal.defaultExpectation == nil { - mmFatal.defaultExpectation = &TesterMockFatalExpectation{} - } - - mmFatal.defaultExpectation.params = &TesterMockFatalParams{args} - for _, e := range mmFatal.expectations { - if minimock.Equal(e.params, mmFatal.defaultExpectation.params) { - mmFatal.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFatal.defaultExpectation.params) - } - } - - return mmFatal -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Fatal -func (mmFatal *mTesterMockFatal) Inspect(f func(args ...interface{})) *mTesterMockFatal { - if mmFatal.mock.inspectFuncFatal != nil { - mmFatal.mock.t.Fatalf("Inspect function is already set for TesterMock.Fatal") - } - - mmFatal.mock.inspectFuncFatal = f - - return mmFatal -} - -// Return sets up results that will be returned by Tester.Fatal -func (mmFatal *mTesterMockFatal) Return() *TesterMock { - if mmFatal.mock.funcFatal != nil { - mmFatal.mock.t.Fatalf("TesterMock.Fatal mock is already set by Set") - } - - if mmFatal.defaultExpectation == nil { - mmFatal.defaultExpectation = &TesterMockFatalExpectation{mock: mmFatal.mock} - } - - return mmFatal.mock -} - -// Set uses given function f to mock the Tester.Fatal method -func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { - if mmFatal.defaultExpectation != nil { - mmFatal.mock.t.Fatalf("Default expectation is already set for the Tester.Fatal method") - } - - if len(mmFatal.expectations) > 0 { - mmFatal.mock.t.Fatalf("Some expectations are already set for the Tester.Fatal method") - } - - mmFatal.mock.funcFatal = f - return mmFatal.mock -} - -// Fatal implements minimock.Tester -func (mmFatal *TesterMock) Fatal(args ...interface{}) { - mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) - defer mm_atomic.AddUint64(&mmFatal.afterFatalCounter, 1) - - if mmFatal.inspectFuncFatal != nil { - mmFatal.inspectFuncFatal(args...) - } - - mm_params := &TesterMockFatalParams{args} - - // Record call args - mmFatal.FatalMock.mutex.Lock() - mmFatal.FatalMock.callArgs = append(mmFatal.FatalMock.callArgs, mm_params) - mmFatal.FatalMock.mutex.Unlock() - - for _, e := range mmFatal.FatalMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmFatal.FatalMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmFatal.FatalMock.defaultExpectation.Counter, 1) - mm_want := mmFatal.FatalMock.defaultExpectation.params - mm_got := TesterMockFatalParams{args} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmFatal.funcFatal != nil { - mmFatal.funcFatal(args...) - return - } - mmFatal.t.Fatalf("Unexpected call to TesterMock.Fatal. %v", args) - -} - -// FatalAfterCounter returns a count of finished TesterMock.Fatal invocations -func (mmFatal *TesterMock) FatalAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatal.afterFatalCounter) -} - -// FatalBeforeCounter returns a count of TesterMock.Fatal invocations -func (mmFatal *TesterMock) FatalBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatal.beforeFatalCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Fatal. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmFatal *mTesterMockFatal) Calls() []*TesterMockFatalParams { - mmFatal.mutex.RLock() - - argCopy := make([]*TesterMockFatalParams, len(mmFatal.callArgs)) - copy(argCopy, mmFatal.callArgs) - - mmFatal.mutex.RUnlock() - - return argCopy -} - -// MinimockFatalDone returns true if the count of the Fatal invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockFatalDone() bool { - for _, e := range m.FatalMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - return false - } - return true -} - -// MinimockFatalInspect logs each unmet expectation -func (m *TesterMock) MinimockFatalInspect() { - for _, e := range m.FatalMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - if m.FatalMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatal") - } else { - m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *m.FatalMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcFatal != nil && mm_atomic.LoadUint64(&m.afterFatalCounter) < 1 { - m.t.Error("Expected call to TesterMock.Fatal") - } -} - -type mTesterMockFatalf struct { - mock *TesterMock - defaultExpectation *TesterMockFatalfExpectation - expectations []*TesterMockFatalfExpectation - - callArgs []*TesterMockFatalfParams - mutex sync.RWMutex -} - -// TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf -type TesterMockFatalfExpectation struct { - mock *TesterMock - params *TesterMockFatalfParams - - Counter uint64 -} - -// TesterMockFatalfParams contains parameters of the Tester.Fatalf -type TesterMockFatalfParams struct { - format string - args []interface{} -} - -// Expect sets up expected params for Tester.Fatalf -func (mmFatalf *mTesterMockFatalf) Expect(format string, args ...interface{}) *mTesterMockFatalf { - if mmFatalf.mock.funcFatalf != nil { - mmFatalf.mock.t.Fatalf("TesterMock.Fatalf mock is already set by Set") - } - - if mmFatalf.defaultExpectation == nil { - mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{} - } - - mmFatalf.defaultExpectation.params = &TesterMockFatalfParams{format, args} - for _, e := range mmFatalf.expectations { - if minimock.Equal(e.params, mmFatalf.defaultExpectation.params) { - mmFatalf.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFatalf.defaultExpectation.params) - } - } - - return mmFatalf -} - -// Inspect accepts an inspector function that has same arguments as the Tester.Fatalf -func (mmFatalf *mTesterMockFatalf) Inspect(f func(format string, args ...interface{})) *mTesterMockFatalf { - if mmFatalf.mock.inspectFuncFatalf != nil { - mmFatalf.mock.t.Fatalf("Inspect function is already set for TesterMock.Fatalf") - } - - mmFatalf.mock.inspectFuncFatalf = f - - return mmFatalf -} - -// Return sets up results that will be returned by Tester.Fatalf -func (mmFatalf *mTesterMockFatalf) Return() *TesterMock { - if mmFatalf.mock.funcFatalf != nil { - mmFatalf.mock.t.Fatalf("TesterMock.Fatalf mock is already set by Set") - } - - if mmFatalf.defaultExpectation == nil { - mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{mock: mmFatalf.mock} - } - - return mmFatalf.mock -} - -// Set uses given function f to mock the Tester.Fatalf method -func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{})) *TesterMock { - if mmFatalf.defaultExpectation != nil { - mmFatalf.mock.t.Fatalf("Default expectation is already set for the Tester.Fatalf method") - } - - if len(mmFatalf.expectations) > 0 { - mmFatalf.mock.t.Fatalf("Some expectations are already set for the Tester.Fatalf method") - } - - mmFatalf.mock.funcFatalf = f - return mmFatalf.mock -} - -// Fatalf implements minimock.Tester -func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { - mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) - defer mm_atomic.AddUint64(&mmFatalf.afterFatalfCounter, 1) - - if mmFatalf.inspectFuncFatalf != nil { - mmFatalf.inspectFuncFatalf(format, args...) - } - - mm_params := &TesterMockFatalfParams{format, args} - - // Record call args - mmFatalf.FatalfMock.mutex.Lock() - mmFatalf.FatalfMock.callArgs = append(mmFatalf.FatalfMock.callArgs, mm_params) - mmFatalf.FatalfMock.mutex.Unlock() - - for _, e := range mmFatalf.FatalfMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return - } - } - - if mmFatalf.FatalfMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmFatalf.FatalfMock.defaultExpectation.Counter, 1) - mm_want := mmFatalf.FatalfMock.defaultExpectation.params - mm_got := TesterMockFatalfParams{format, args} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - return - - } - if mmFatalf.funcFatalf != nil { - mmFatalf.funcFatalf(format, args...) - return - } - mmFatalf.t.Fatalf("Unexpected call to TesterMock.Fatalf. %v %v", format, args) - -} - -// FatalfAfterCounter returns a count of finished TesterMock.Fatalf invocations -func (mmFatalf *TesterMock) FatalfAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatalf.afterFatalfCounter) -} - -// FatalfBeforeCounter returns a count of TesterMock.Fatalf invocations -func (mmFatalf *TesterMock) FatalfBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmFatalf.beforeFatalfCounter) -} - -// Calls returns a list of arguments used in each call to TesterMock.Fatalf. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmFatalf *mTesterMockFatalf) Calls() []*TesterMockFatalfParams { - mmFatalf.mutex.RLock() - - argCopy := make([]*TesterMockFatalfParams, len(mmFatalf.callArgs)) - copy(argCopy, mmFatalf.callArgs) - - mmFatalf.mutex.RUnlock() - - return argCopy -} - -// MinimockFatalfDone returns true if the count of the Fatalf invocations corresponds -// the number of defined expectations -func (m *TesterMock) MinimockFatalfDone() bool { - for _, e := range m.FatalfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - return false - } - return true -} - -// MinimockFatalfInspect logs each unmet expectation -func (m *TesterMock) MinimockFatalfInspect() { - for _, e := range m.FatalfMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FatalfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - if m.FatalfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatalf") - } else { - m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *m.FatalfMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcFatalf != nil && mm_atomic.LoadUint64(&m.afterFatalfCounter) < 1 { - m.t.Error("Expected call to TesterMock.Fatalf") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *TesterMock) MinimockFinish() { - if !m.minimockDone() { - m.MinimockErrorInspect() - - m.MinimockErrorfInspect() - - m.MinimockFailNowInspect() - - m.MinimockFatalInspect() - - m.MinimockFatalfInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *TesterMock) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *TesterMock) minimockDone() bool { - done := true - return done && - m.MinimockErrorDone() && - m.MinimockErrorfDone() && - m.MinimockFailNowDone() && - m.MinimockFatalDone() && - m.MinimockFatalfDone() -} - diff --git a/snapshots/TestSnapshot-relative_reference.go b/snapshots/TestSnapshot-relative_reference.go deleted file mode 100644 index 4878b08..0000000 --- a/snapshots/TestSnapshot-relative_reference.go +++ /dev/null @@ -1,284 +0,0 @@ -package tests - -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. - -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.Formatter -o ./tests/formatter_mock.go -n FormatterMock -p tests - -import ( - "sync" - mm_atomic "sync/atomic" - mm_time "time" - - "github.com/gojuno/minimock/v3" -) - -// FormatterMock implements Formatter -type FormatterMock struct { - t minimock.Tester - - funcFormat func(s1 string, p1 ...interface{}) (s2 string) - inspectFuncFormat func(s1 string, p1 ...interface{}) - afterFormatCounter uint64 - beforeFormatCounter uint64 - FormatMock mFormatterMockFormat -} - -// NewFormatterMock returns a mock for Formatter -func NewFormatterMock(t minimock.Tester) *FormatterMock { - m := &FormatterMock{t: t} - if controller, ok := t.(minimock.MockController); ok { - controller.RegisterMocker(m) - } - - m.FormatMock = mFormatterMockFormat{mock: m} - m.FormatMock.callArgs = []*FormatterMockFormatParams{} - - return m -} - -type mFormatterMockFormat struct { - mock *FormatterMock - defaultExpectation *FormatterMockFormatExpectation - expectations []*FormatterMockFormatExpectation - - callArgs []*FormatterMockFormatParams - mutex sync.RWMutex -} - -// FormatterMockFormatExpectation specifies expectation struct of the Formatter.Format -type FormatterMockFormatExpectation struct { - mock *FormatterMock - params *FormatterMockFormatParams - results *FormatterMockFormatResults - Counter uint64 -} - -// FormatterMockFormatParams contains parameters of the Formatter.Format -type FormatterMockFormatParams struct { - s1 string - p1 []interface{} -} - -// FormatterMockFormatResults contains results of the Formatter.Format -type FormatterMockFormatResults struct { - s2 string -} - -// Expect sets up expected params for Formatter.Format -func (mmFormat *mFormatterMockFormat) Expect(s1 string, p1 ...interface{}) *mFormatterMockFormat { - if mmFormat.mock.funcFormat != nil { - mmFormat.mock.t.Fatalf("FormatterMock.Format mock is already set by Set") - } - - if mmFormat.defaultExpectation == nil { - mmFormat.defaultExpectation = &FormatterMockFormatExpectation{} - } - - mmFormat.defaultExpectation.params = &FormatterMockFormatParams{s1, p1} - for _, e := range mmFormat.expectations { - if minimock.Equal(e.params, mmFormat.defaultExpectation.params) { - mmFormat.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmFormat.defaultExpectation.params) - } - } - - return mmFormat -} - -// Inspect accepts an inspector function that has same arguments as the Formatter.Format -func (mmFormat *mFormatterMockFormat) Inspect(f func(s1 string, p1 ...interface{})) *mFormatterMockFormat { - if mmFormat.mock.inspectFuncFormat != nil { - mmFormat.mock.t.Fatalf("Inspect function is already set for FormatterMock.Format") - } - - mmFormat.mock.inspectFuncFormat = f - - return mmFormat -} - -// Return sets up results that will be returned by Formatter.Format -func (mmFormat *mFormatterMockFormat) Return(s2 string) *FormatterMock { - if mmFormat.mock.funcFormat != nil { - mmFormat.mock.t.Fatalf("FormatterMock.Format mock is already set by Set") - } - - if mmFormat.defaultExpectation == nil { - mmFormat.defaultExpectation = &FormatterMockFormatExpectation{mock: mmFormat.mock} - } - mmFormat.defaultExpectation.results = &FormatterMockFormatResults{s2} - return mmFormat.mock -} - -// Set uses given function f to mock the Formatter.Format method -func (mmFormat *mFormatterMockFormat) Set(f func(s1 string, p1 ...interface{}) (s2 string)) *FormatterMock { - if mmFormat.defaultExpectation != nil { - mmFormat.mock.t.Fatalf("Default expectation is already set for the Formatter.Format method") - } - - if len(mmFormat.expectations) > 0 { - mmFormat.mock.t.Fatalf("Some expectations are already set for the Formatter.Format method") - } - - mmFormat.mock.funcFormat = f - return mmFormat.mock -} - -// When sets expectation for the Formatter.Format which will trigger the result defined by the following -// Then helper -func (mmFormat *mFormatterMockFormat) When(s1 string, p1 ...interface{}) *FormatterMockFormatExpectation { - if mmFormat.mock.funcFormat != nil { - mmFormat.mock.t.Fatalf("FormatterMock.Format mock is already set by Set") - } - - expectation := &FormatterMockFormatExpectation{ - mock: mmFormat.mock, - params: &FormatterMockFormatParams{s1, p1}, - } - mmFormat.expectations = append(mmFormat.expectations, expectation) - return expectation -} - -// Then sets up Formatter.Format return parameters for the expectation previously defined by the When method -func (e *FormatterMockFormatExpectation) Then(s2 string) *FormatterMock { - e.results = &FormatterMockFormatResults{s2} - return e.mock -} - -// Format implements Formatter -func (mmFormat *FormatterMock) Format(s1 string, p1 ...interface{}) (s2 string) { - mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) - defer mm_atomic.AddUint64(&mmFormat.afterFormatCounter, 1) - - if mmFormat.inspectFuncFormat != nil { - mmFormat.inspectFuncFormat(s1, p1...) - } - - mm_params := &FormatterMockFormatParams{s1, p1} - - // Record call args - mmFormat.FormatMock.mutex.Lock() - mmFormat.FormatMock.callArgs = append(mmFormat.FormatMock.callArgs, mm_params) - mmFormat.FormatMock.mutex.Unlock() - - for _, e := range mmFormat.FormatMock.expectations { - if minimock.Equal(e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - return e.results.s2 - } - } - - if mmFormat.FormatMock.defaultExpectation != nil { - mm_atomic.AddUint64(&mmFormat.FormatMock.defaultExpectation.Counter, 1) - mm_want := mmFormat.FormatMock.defaultExpectation.params - mm_got := FormatterMockFormatParams{s1, p1} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFormat.t.Errorf("FormatterMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - - mm_results := mmFormat.FormatMock.defaultExpectation.results - if mm_results == nil { - mmFormat.t.Fatal("No results are set for the FormatterMock.Format") - } - return (*mm_results).s2 - } - if mmFormat.funcFormat != nil { - return mmFormat.funcFormat(s1, p1...) - } - mmFormat.t.Fatalf("Unexpected call to FormatterMock.Format. %v %v", s1, p1) - return -} - -// FormatAfterCounter returns a count of finished FormatterMock.Format invocations -func (mmFormat *FormatterMock) FormatAfterCounter() uint64 { - return mm_atomic.LoadUint64(&mmFormat.afterFormatCounter) -} - -// FormatBeforeCounter returns a count of FormatterMock.Format invocations -func (mmFormat *FormatterMock) FormatBeforeCounter() uint64 { - return mm_atomic.LoadUint64(&mmFormat.beforeFormatCounter) -} - -// Calls returns a list of arguments used in each call to FormatterMock.Format. -// The list is in the same order as the calls were made (i.e. recent calls have a higher index) -func (mmFormat *mFormatterMockFormat) Calls() []*FormatterMockFormatParams { - mmFormat.mutex.RLock() - - argCopy := make([]*FormatterMockFormatParams, len(mmFormat.callArgs)) - copy(argCopy, mmFormat.callArgs) - - mmFormat.mutex.RUnlock() - - return argCopy -} - -// MinimockFormatDone returns true if the count of the Format invocations corresponds -// the number of defined expectations -func (m *FormatterMock) MinimockFormatDone() bool { - for _, e := range m.FormatMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FormatMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcFormat != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { - return false - } - return true -} - -// MinimockFormatInspect logs each unmet expectation -func (m *FormatterMock) MinimockFormatInspect() { - for _, e := range m.FormatMock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to FormatterMock.Format with params: %#v", *e.params) - } - } - - // if default expectation was set then invocations count should be greater than zero - if m.FormatMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { - if m.FormatMock.defaultExpectation.params == nil { - m.t.Error("Expected call to FormatterMock.Format") - } else { - m.t.Errorf("Expected call to FormatterMock.Format with params: %#v", *m.FormatMock.defaultExpectation.params) - } - } - // if func was set then invocations count should be greater than zero - if m.funcFormat != nil && mm_atomic.LoadUint64(&m.afterFormatCounter) < 1 { - m.t.Error("Expected call to FormatterMock.Format") - } -} - -// MinimockFinish checks that all mocked methods have been called the expected number of times -func (m *FormatterMock) MinimockFinish() { - if !m.minimockDone() { - m.MinimockFormatInspect() - m.t.FailNow() - } -} - -// MinimockWait waits for all mocked methods to be called the expected number of times -func (m *FormatterMock) MinimockWait(timeout mm_time.Duration) { - timeoutCh := mm_time.After(timeout) - for { - if m.minimockDone() { - return - } - select { - case <-timeoutCh: - m.MinimockFinish() - return - case <-mm_time.After(10 * mm_time.Millisecond): - } - } -} - -func (m *FormatterMock) minimockDone() bool { - done := true - return done && - m.MinimockFormatDone() -} - diff --git a/template.go b/template.go index ad184c6..13c0947 100644 --- a/template.go +++ b/template.go @@ -12,7 +12,7 @@ const ( // Code generated by http://github.com/gojuno/minimock ({{$.Options.HeaderVars.Version}}). DO NOT EDIT. {{if $.Options.HeaderVars.GenerateInstruction}} - //go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} + //go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} {{end}} import ( @@ -32,6 +32,8 @@ const ( // {{$mock}} implements {{$.Interface.Type}} type {{$mock}}{{(params)}} struct { t minimock.Tester + finishOnce sync.Once + {{ range $method := $.Interface.Methods }} func{{$method.Name}} func{{ $method.Signature }} inspectFunc{{$method.Name}} func({{ $method.Params}}) @@ -44,6 +46,7 @@ const ( // New{{$mock}} returns a mock for {{$.Interface.Type}} func New{{$mock}}{{(params)}}(t minimock.Tester) *{{$mock}}{{(paramsRef)}} { m := &{{$mock}}{{(paramsRef)}}{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -51,6 +54,9 @@ const ( m.{{$method.Name}}Mock = m{{$mock}}{{$method.Name}}{{(paramsRef)}}{mock: m} {{ if $method.HasParams }} m.{{$method.Name}}Mock.callArgs = []*{{$mock}}{{$method.Name}}Params{{(paramsRef)}}{} {{ end }} {{ end }} + + t.Cleanup(m.MinimockFinish) + return m } @@ -177,15 +183,15 @@ const ( } {{if $method.HasParams}} - mm_params := &{{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{$method.ParamsNames}} } + mm_params := {{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{$method.ParamsNames}} } // Record call args {{$m}}.{{$method.Name}}Mock.mutex.Lock() - {{$m}}.{{$method.Name}}Mock.callArgs = append({{$m}}.{{$method.Name}}Mock.callArgs, mm_params) + {{$m}}.{{$method.Name}}Mock.callArgs = append({{$m}}.{{$method.Name}}Mock.callArgs, &mm_params) {{$m}}.{{$method.Name}}Mock.mutex.Unlock() for _, e := range {{$m}}.{{$method.Name}}Mock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) {{$method.ReturnStruct "e.results" -}} } @@ -296,12 +302,14 @@ const ( // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *{{$mock}}{{(paramsRef)}}) MinimockFinish() { - if !m.minimockDone() { - {{- range $method := $.Interface.Methods }} - m.Minimock{{$method.Name}}Inspect() - {{ end -}} - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + {{- range $method := $.Interface.Methods }} + m.Minimock{{$method.Name}}Inspect() + {{ end -}} + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/context_accepter_mock.go b/tests/context_accepter_mock.go new file mode 100644 index 0000000..3b43bee --- /dev/null +++ b/tests/context_accepter_mock.go @@ -0,0 +1,490 @@ +package tests + +// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. + +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.contextAccepter -o context_accepter_mock.go -n ContextAccepterMock -p tests + +import ( + "context" + "sync" + mm_atomic "sync/atomic" + mm_time "time" + + "github.com/gojuno/minimock/v3" +) + +// ContextAccepterMock implements contextAccepter +type ContextAccepterMock struct { + t minimock.Tester + finishOnce sync.Once + + funcAcceptContext func(ctx context.Context) + inspectFuncAcceptContext func(ctx context.Context) + afterAcceptContextCounter uint64 + beforeAcceptContextCounter uint64 + AcceptContextMock mContextAccepterMockAcceptContext + + funcAcceptContextWithOtherArgs func(ctx context.Context, i1 int) (i2 int, err error) + inspectFuncAcceptContextWithOtherArgs func(ctx context.Context, i1 int) + afterAcceptContextWithOtherArgsCounter uint64 + beforeAcceptContextWithOtherArgsCounter uint64 + AcceptContextWithOtherArgsMock mContextAccepterMockAcceptContextWithOtherArgs +} + +// NewContextAccepterMock returns a mock for contextAccepter +func NewContextAccepterMock(t minimock.Tester) *ContextAccepterMock { + m := &ContextAccepterMock{t: t} + + if controller, ok := t.(minimock.MockController); ok { + controller.RegisterMocker(m) + } + + m.AcceptContextMock = mContextAccepterMockAcceptContext{mock: m} + m.AcceptContextMock.callArgs = []*ContextAccepterMockAcceptContextParams{} + + m.AcceptContextWithOtherArgsMock = mContextAccepterMockAcceptContextWithOtherArgs{mock: m} + m.AcceptContextWithOtherArgsMock.callArgs = []*ContextAccepterMockAcceptContextWithOtherArgsParams{} + + t.Cleanup(m.MinimockFinish) + + return m +} + +type mContextAccepterMockAcceptContext struct { + mock *ContextAccepterMock + defaultExpectation *ContextAccepterMockAcceptContextExpectation + expectations []*ContextAccepterMockAcceptContextExpectation + + callArgs []*ContextAccepterMockAcceptContextParams + mutex sync.RWMutex +} + +// ContextAccepterMockAcceptContextExpectation specifies expectation struct of the contextAccepter.AcceptContext +type ContextAccepterMockAcceptContextExpectation struct { + mock *ContextAccepterMock + params *ContextAccepterMockAcceptContextParams + + Counter uint64 +} + +// ContextAccepterMockAcceptContextParams contains parameters of the contextAccepter.AcceptContext +type ContextAccepterMockAcceptContextParams struct { + ctx context.Context +} + +// Expect sets up expected params for contextAccepter.AcceptContext +func (mmAcceptContext *mContextAccepterMockAcceptContext) Expect(ctx context.Context) *mContextAccepterMockAcceptContext { + if mmAcceptContext.mock.funcAcceptContext != nil { + mmAcceptContext.mock.t.Fatalf("ContextAccepterMock.AcceptContext mock is already set by Set") + } + + if mmAcceptContext.defaultExpectation == nil { + mmAcceptContext.defaultExpectation = &ContextAccepterMockAcceptContextExpectation{} + } + + mmAcceptContext.defaultExpectation.params = &ContextAccepterMockAcceptContextParams{ctx} + for _, e := range mmAcceptContext.expectations { + if minimock.Equal(e.params, mmAcceptContext.defaultExpectation.params) { + mmAcceptContext.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmAcceptContext.defaultExpectation.params) + } + } + + return mmAcceptContext +} + +// Inspect accepts an inspector function that has same arguments as the contextAccepter.AcceptContext +func (mmAcceptContext *mContextAccepterMockAcceptContext) Inspect(f func(ctx context.Context)) *mContextAccepterMockAcceptContext { + if mmAcceptContext.mock.inspectFuncAcceptContext != nil { + mmAcceptContext.mock.t.Fatalf("Inspect function is already set for ContextAccepterMock.AcceptContext") + } + + mmAcceptContext.mock.inspectFuncAcceptContext = f + + return mmAcceptContext +} + +// Return sets up results that will be returned by contextAccepter.AcceptContext +func (mmAcceptContext *mContextAccepterMockAcceptContext) Return() *ContextAccepterMock { + if mmAcceptContext.mock.funcAcceptContext != nil { + mmAcceptContext.mock.t.Fatalf("ContextAccepterMock.AcceptContext mock is already set by Set") + } + + if mmAcceptContext.defaultExpectation == nil { + mmAcceptContext.defaultExpectation = &ContextAccepterMockAcceptContextExpectation{mock: mmAcceptContext.mock} + } + + return mmAcceptContext.mock +} + +// Set uses given function f to mock the contextAccepter.AcceptContext method +func (mmAcceptContext *mContextAccepterMockAcceptContext) Set(f func(ctx context.Context)) *ContextAccepterMock { + if mmAcceptContext.defaultExpectation != nil { + mmAcceptContext.mock.t.Fatalf("Default expectation is already set for the contextAccepter.AcceptContext method") + } + + if len(mmAcceptContext.expectations) > 0 { + mmAcceptContext.mock.t.Fatalf("Some expectations are already set for the contextAccepter.AcceptContext method") + } + + mmAcceptContext.mock.funcAcceptContext = f + return mmAcceptContext.mock +} + +// AcceptContext implements contextAccepter +func (mmAcceptContext *ContextAccepterMock) AcceptContext(ctx context.Context) { + mm_atomic.AddUint64(&mmAcceptContext.beforeAcceptContextCounter, 1) + defer mm_atomic.AddUint64(&mmAcceptContext.afterAcceptContextCounter, 1) + + if mmAcceptContext.inspectFuncAcceptContext != nil { + mmAcceptContext.inspectFuncAcceptContext(ctx) + } + + mm_params := ContextAccepterMockAcceptContextParams{ctx} + + // Record call args + mmAcceptContext.AcceptContextMock.mutex.Lock() + mmAcceptContext.AcceptContextMock.callArgs = append(mmAcceptContext.AcceptContextMock.callArgs, &mm_params) + mmAcceptContext.AcceptContextMock.mutex.Unlock() + + for _, e := range mmAcceptContext.AcceptContextMock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmAcceptContext.AcceptContextMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmAcceptContext.AcceptContextMock.defaultExpectation.Counter, 1) + mm_want := mmAcceptContext.AcceptContextMock.defaultExpectation.params + mm_got := ContextAccepterMockAcceptContextParams{ctx} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmAcceptContext.t.Errorf("ContextAccepterMock.AcceptContext got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmAcceptContext.funcAcceptContext != nil { + mmAcceptContext.funcAcceptContext(ctx) + return + } + mmAcceptContext.t.Fatalf("Unexpected call to ContextAccepterMock.AcceptContext. %v", ctx) + +} + +// AcceptContextAfterCounter returns a count of finished ContextAccepterMock.AcceptContext invocations +func (mmAcceptContext *ContextAccepterMock) AcceptContextAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmAcceptContext.afterAcceptContextCounter) +} + +// AcceptContextBeforeCounter returns a count of ContextAccepterMock.AcceptContext invocations +func (mmAcceptContext *ContextAccepterMock) AcceptContextBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmAcceptContext.beforeAcceptContextCounter) +} + +// Calls returns a list of arguments used in each call to ContextAccepterMock.AcceptContext. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmAcceptContext *mContextAccepterMockAcceptContext) Calls() []*ContextAccepterMockAcceptContextParams { + mmAcceptContext.mutex.RLock() + + argCopy := make([]*ContextAccepterMockAcceptContextParams, len(mmAcceptContext.callArgs)) + copy(argCopy, mmAcceptContext.callArgs) + + mmAcceptContext.mutex.RUnlock() + + return argCopy +} + +// MinimockAcceptContextDone returns true if the count of the AcceptContext invocations corresponds +// the number of defined expectations +func (m *ContextAccepterMock) MinimockAcceptContextDone() bool { + for _, e := range m.AcceptContextMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.AcceptContextMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcAcceptContext != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { + return false + } + return true +} + +// MinimockAcceptContextInspect logs each unmet expectation +func (m *ContextAccepterMock) MinimockAcceptContextInspect() { + for _, e := range m.AcceptContextMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContext with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.AcceptContextMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { + if m.AcceptContextMock.defaultExpectation.params == nil { + m.t.Error("Expected call to ContextAccepterMock.AcceptContext") + } else { + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContext with params: %#v", *m.AcceptContextMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcAcceptContext != nil && mm_atomic.LoadUint64(&m.afterAcceptContextCounter) < 1 { + m.t.Error("Expected call to ContextAccepterMock.AcceptContext") + } +} + +type mContextAccepterMockAcceptContextWithOtherArgs struct { + mock *ContextAccepterMock + defaultExpectation *ContextAccepterMockAcceptContextWithOtherArgsExpectation + expectations []*ContextAccepterMockAcceptContextWithOtherArgsExpectation + + callArgs []*ContextAccepterMockAcceptContextWithOtherArgsParams + mutex sync.RWMutex +} + +// ContextAccepterMockAcceptContextWithOtherArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithOtherArgs +type ContextAccepterMockAcceptContextWithOtherArgsExpectation struct { + mock *ContextAccepterMock + params *ContextAccepterMockAcceptContextWithOtherArgsParams + results *ContextAccepterMockAcceptContextWithOtherArgsResults + Counter uint64 +} + +// ContextAccepterMockAcceptContextWithOtherArgsParams contains parameters of the contextAccepter.AcceptContextWithOtherArgs +type ContextAccepterMockAcceptContextWithOtherArgsParams struct { + ctx context.Context + i1 int +} + +// ContextAccepterMockAcceptContextWithOtherArgsResults contains results of the contextAccepter.AcceptContextWithOtherArgs +type ContextAccepterMockAcceptContextWithOtherArgsResults struct { + i2 int + err error +} + +// Expect sets up expected params for contextAccepter.AcceptContextWithOtherArgs +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) Expect(ctx context.Context, i1 int) *mContextAccepterMockAcceptContextWithOtherArgs { + if mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgs != nil { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithOtherArgs mock is already set by Set") + } + + if mmAcceptContextWithOtherArgs.defaultExpectation == nil { + mmAcceptContextWithOtherArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithOtherArgsExpectation{} + } + + mmAcceptContextWithOtherArgs.defaultExpectation.params = &ContextAccepterMockAcceptContextWithOtherArgsParams{ctx, i1} + for _, e := range mmAcceptContextWithOtherArgs.expectations { + if minimock.Equal(e.params, mmAcceptContextWithOtherArgs.defaultExpectation.params) { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmAcceptContextWithOtherArgs.defaultExpectation.params) + } + } + + return mmAcceptContextWithOtherArgs +} + +// Inspect accepts an inspector function that has same arguments as the contextAccepter.AcceptContextWithOtherArgs +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) Inspect(f func(ctx context.Context, i1 int)) *mContextAccepterMockAcceptContextWithOtherArgs { + if mmAcceptContextWithOtherArgs.mock.inspectFuncAcceptContextWithOtherArgs != nil { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("Inspect function is already set for ContextAccepterMock.AcceptContextWithOtherArgs") + } + + mmAcceptContextWithOtherArgs.mock.inspectFuncAcceptContextWithOtherArgs = f + + return mmAcceptContextWithOtherArgs +} + +// Return sets up results that will be returned by contextAccepter.AcceptContextWithOtherArgs +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) Return(i2 int, err error) *ContextAccepterMock { + if mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgs != nil { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithOtherArgs mock is already set by Set") + } + + if mmAcceptContextWithOtherArgs.defaultExpectation == nil { + mmAcceptContextWithOtherArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithOtherArgsExpectation{mock: mmAcceptContextWithOtherArgs.mock} + } + mmAcceptContextWithOtherArgs.defaultExpectation.results = &ContextAccepterMockAcceptContextWithOtherArgsResults{i2, err} + return mmAcceptContextWithOtherArgs.mock +} + +// Set uses given function f to mock the contextAccepter.AcceptContextWithOtherArgs method +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) Set(f func(ctx context.Context, i1 int) (i2 int, err error)) *ContextAccepterMock { + if mmAcceptContextWithOtherArgs.defaultExpectation != nil { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("Default expectation is already set for the contextAccepter.AcceptContextWithOtherArgs method") + } + + if len(mmAcceptContextWithOtherArgs.expectations) > 0 { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("Some expectations are already set for the contextAccepter.AcceptContextWithOtherArgs method") + } + + mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgs = f + return mmAcceptContextWithOtherArgs.mock +} + +// When sets expectation for the contextAccepter.AcceptContextWithOtherArgs which will trigger the result defined by the following +// Then helper +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) When(ctx context.Context, i1 int) *ContextAccepterMockAcceptContextWithOtherArgsExpectation { + if mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgs != nil { + mmAcceptContextWithOtherArgs.mock.t.Fatalf("ContextAccepterMock.AcceptContextWithOtherArgs mock is already set by Set") + } + + expectation := &ContextAccepterMockAcceptContextWithOtherArgsExpectation{ + mock: mmAcceptContextWithOtherArgs.mock, + params: &ContextAccepterMockAcceptContextWithOtherArgsParams{ctx, i1}, + } + mmAcceptContextWithOtherArgs.expectations = append(mmAcceptContextWithOtherArgs.expectations, expectation) + return expectation +} + +// Then sets up contextAccepter.AcceptContextWithOtherArgs return parameters for the expectation previously defined by the When method +func (e *ContextAccepterMockAcceptContextWithOtherArgsExpectation) Then(i2 int, err error) *ContextAccepterMock { + e.results = &ContextAccepterMockAcceptContextWithOtherArgsResults{i2, err} + return e.mock +} + +// AcceptContextWithOtherArgs implements contextAccepter +func (mmAcceptContextWithOtherArgs *ContextAccepterMock) AcceptContextWithOtherArgs(ctx context.Context, i1 int) (i2 int, err error) { + mm_atomic.AddUint64(&mmAcceptContextWithOtherArgs.beforeAcceptContextWithOtherArgsCounter, 1) + defer mm_atomic.AddUint64(&mmAcceptContextWithOtherArgs.afterAcceptContextWithOtherArgsCounter, 1) + + if mmAcceptContextWithOtherArgs.inspectFuncAcceptContextWithOtherArgs != nil { + mmAcceptContextWithOtherArgs.inspectFuncAcceptContextWithOtherArgs(ctx, i1) + } + + mm_params := ContextAccepterMockAcceptContextWithOtherArgsParams{ctx, i1} + + // Record call args + mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.mutex.Lock() + mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.callArgs = append(mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.callArgs, &mm_params) + mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.mutex.Unlock() + + for _, e := range mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return e.results.i2, e.results.err + } + } + + if mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation.Counter, 1) + mm_want := mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation.params + mm_got := ContextAccepterMockAcceptContextWithOtherArgsParams{ctx, i1} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmAcceptContextWithOtherArgs.t.Errorf("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + mm_results := mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation.results + if mm_results == nil { + mmAcceptContextWithOtherArgs.t.Fatal("No results are set for the ContextAccepterMock.AcceptContextWithOtherArgs") + } + return (*mm_results).i2, (*mm_results).err + } + if mmAcceptContextWithOtherArgs.funcAcceptContextWithOtherArgs != nil { + return mmAcceptContextWithOtherArgs.funcAcceptContextWithOtherArgs(ctx, i1) + } + mmAcceptContextWithOtherArgs.t.Fatalf("Unexpected call to ContextAccepterMock.AcceptContextWithOtherArgs. %v %v", ctx, i1) + return +} + +// AcceptContextWithOtherArgsAfterCounter returns a count of finished ContextAccepterMock.AcceptContextWithOtherArgs invocations +func (mmAcceptContextWithOtherArgs *ContextAccepterMock) AcceptContextWithOtherArgsAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.afterAcceptContextWithOtherArgsCounter) +} + +// AcceptContextWithOtherArgsBeforeCounter returns a count of ContextAccepterMock.AcceptContextWithOtherArgs invocations +func (mmAcceptContextWithOtherArgs *ContextAccepterMock) AcceptContextWithOtherArgsBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmAcceptContextWithOtherArgs.beforeAcceptContextWithOtherArgsCounter) +} + +// Calls returns a list of arguments used in each call to ContextAccepterMock.AcceptContextWithOtherArgs. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherArgs) Calls() []*ContextAccepterMockAcceptContextWithOtherArgsParams { + mmAcceptContextWithOtherArgs.mutex.RLock() + + argCopy := make([]*ContextAccepterMockAcceptContextWithOtherArgsParams, len(mmAcceptContextWithOtherArgs.callArgs)) + copy(argCopy, mmAcceptContextWithOtherArgs.callArgs) + + mmAcceptContextWithOtherArgs.mutex.RUnlock() + + return argCopy +} + +// MinimockAcceptContextWithOtherArgsDone returns true if the count of the AcceptContextWithOtherArgs invocations corresponds +// the number of defined expectations +func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsDone() bool { + for _, e := range m.AcceptContextWithOtherArgsMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.AcceptContextWithOtherArgsMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcAcceptContextWithOtherArgs != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { + return false + } + return true +} + +// MinimockAcceptContextWithOtherArgsInspect logs each unmet expectation +func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsInspect() { + for _, e := range m.AcceptContextWithOtherArgsMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.AcceptContextWithOtherArgsMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { + if m.AcceptContextWithOtherArgsMock.defaultExpectation.params == nil { + m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs") + } else { + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs with params: %#v", *m.AcceptContextWithOtherArgsMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcAcceptContextWithOtherArgs != nil && mm_atomic.LoadUint64(&m.afterAcceptContextWithOtherArgsCounter) < 1 { + m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs") + } +} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *ContextAccepterMock) MinimockFinish() { + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockAcceptContextInspect() + + m.MinimockAcceptContextWithOtherArgsInspect() + m.t.FailNow() + } + }) +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *ContextAccepterMock) MinimockWait(timeout mm_time.Duration) { + timeoutCh := mm_time.After(timeout) + for { + if m.minimockDone() { + return + } + select { + case <-timeoutCh: + m.MinimockFinish() + return + case <-mm_time.After(10 * mm_time.Millisecond): + } + } +} + +func (m *ContextAccepterMock) minimockDone() bool { + done := true + return done && + m.MinimockAcceptContextDone() && + m.MinimockAcceptContextWithOtherArgsDone() +} diff --git a/tests/context_accepter_mock_test.go b/tests/context_accepter_mock_test.go new file mode 100644 index 0000000..e4a6f71 --- /dev/null +++ b/tests/context_accepter_mock_test.go @@ -0,0 +1,49 @@ +package tests + +import ( + "context" + "testing" + + "github.com/gojuno/minimock/v3" + "github.com/stretchr/testify/assert" +) + +func TestContextAccepterMock_AnyContext(t *testing.T) { + tester := NewTesterMock(t) + + var mockCalled bool + tester.ErrorfMock.Set(func(s string, args ...interface{}) { + assert.Equal(t, "ContextAccepterMock.AcceptContext got unexpected parameters, want: %#v, got: %#v%s\n", s) + + mockCalled = true + }).CleanupMock.Return() + + mock := NewContextAccepterMock(tester). + AcceptContextMock.Expect(context.Background()).Return() + + mock.AcceptContext(context.TODO()) + + assert.True(t, mockCalled) +} + +func TestContextAccepterMock_TodoContextMatchesAnycontext(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Return() + + mock := NewContextAccepterMock(tester). + AcceptContextMock.Expect(minimock.AnyContext).Return() + + mock.AcceptContext(context.TODO()) +} + +func TestContextAccepterMock_WhenThenMatchAnycontext(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Return() + + mock := NewContextAccepterMock(tester). + AcceptContextWithOtherArgsMock.When(minimock.AnyContext, 1).Then(42, nil) + + result, err := mock.AcceptContextWithOtherArgs(context.TODO(), 1) + assert.NoError(t, err) + assert.Equal(t, 42, result) +} diff --git a/tests/formatter_mock.go b/tests/formatter_mock.go index 2e18d1f..67c2b5c 100644 --- a/tests/formatter_mock.go +++ b/tests/formatter_mock.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.Formatter -o ./tests/formatter_mock.go -n FormatterMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.Formatter -o formatter_mock.go -n FormatterMock -p tests import ( "sync" @@ -14,7 +14,8 @@ import ( // FormatterMock implements Formatter type FormatterMock struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcFormat func(s1 string, p1 ...interface{}) (s2 string) inspectFuncFormat func(s1 string, p1 ...interface{}) @@ -26,6 +27,7 @@ type FormatterMock struct { // NewFormatterMock returns a mock for Formatter func NewFormatterMock(t minimock.Tester) *FormatterMock { m := &FormatterMock{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -33,6 +35,8 @@ func NewFormatterMock(t minimock.Tester) *FormatterMock { m.FormatMock = mFormatterMockFormat{mock: m} m.FormatMock.callArgs = []*FormatterMockFormatParams{} + t.Cleanup(m.MinimockFinish) + return m } @@ -152,15 +156,15 @@ func (mmFormat *FormatterMock) Format(s1 string, p1 ...interface{}) (s2 string) mmFormat.inspectFuncFormat(s1, p1...) } - mm_params := &FormatterMockFormatParams{s1, p1} + mm_params := FormatterMockFormatParams{s1, p1} // Record call args mmFormat.FormatMock.mutex.Lock() - mmFormat.FormatMock.callArgs = append(mmFormat.FormatMock.callArgs, mm_params) + mmFormat.FormatMock.callArgs = append(mmFormat.FormatMock.callArgs, &mm_params) mmFormat.FormatMock.mutex.Unlock() for _, e := range mmFormat.FormatMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return e.results.s2 } @@ -254,10 +258,12 @@ func (m *FormatterMock) MinimockFormatInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *FormatterMock) MinimockFinish() { - if !m.minimockDone() { - m.MinimockFormatInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockFormatInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/formatter_mock_test.go b/tests/formatter_mock_test.go index 495e430..1a5fac1 100644 --- a/tests/formatter_mock_test.go +++ b/tests/formatter_mock_test.go @@ -11,10 +11,26 @@ import ( ) func TestFormatterMock_ImplementsStringer(t *testing.T) { - v := NewFormatterMock(NewTesterMock(t)) + tester := NewTesterMock(t) + tester.CleanupMock.Return() + + // this is not required since tester registers MinimockFinish as a cleanup function + // but we want to make sure that existing tests that use MinimockFinish don't break + defer tester.MinimockFinish() + + v := NewFormatterMock(tester) assert.True(t, reflect.TypeOf(v).Implements(reflect.TypeOf((*Formatter)(nil)).Elem())) } +func TestFormatterMock_CleanupIsCalled(t *testing.T) { + tester := NewTesterMock(t) + tester.CleanupMock.Set(t.Cleanup) + tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return() + tester.FailNowMock.Return() + + NewFormatterMock(tester).FormatMock.Return("") +} + func TestFormatterMock_UnmockedCallFailsTest(t *testing.T) { var mockCalled bool tester := NewTesterMock(t) @@ -23,9 +39,7 @@ func TestFormatterMock_UnmockedCallFailsTest(t *testing.T) { assert.Equal(t, "this call fails because Format method isn't mocked", args[0]) mockCalled = true - }) - - defer tester.MinimockFinish() + }).CleanupMock.Return() formatterMock := NewFormatterMock(tester) dummyFormatter{formatterMock}.Format("this call fails because Format method isn't mocked") @@ -33,20 +47,19 @@ func TestFormatterMock_UnmockedCallFailsTest(t *testing.T) { } func TestFormatterMock_MockedCallSucceeds(t *testing.T) { - tester := NewTesterMock(t) + tester := NewTesterMock(t).CleanupMock.Return() formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Set(func(format string, args ...interface{}) string { return "mock is successfully called" }) - defer tester.MinimockFinish() df := dummyFormatter{formatterMock} assert.Equal(t, "mock is successfully called", df.Format("")) } func TestFormatterMock_Wait(t *testing.T) { - tester := NewTesterMock(t) + tester := NewTesterMock(t).CleanupMock.Return() formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Set(func(format string, args ...interface{}) string { @@ -62,7 +75,7 @@ func TestFormatterMock_Wait(t *testing.T) { } func TestFormatterMock_Expect(t *testing.T) { - tester := NewTesterMock(t) + tester := NewTesterMock(t).CleanupMock.Return() formatterMock := NewFormatterMock(tester).FormatMock.Expect("Hello", "world", "!").Return("") @@ -75,8 +88,7 @@ func TestFormatterMock_Expect(t *testing.T) { func TestFormatterMock_ExpectDifferentArguments(t *testing.T) { assert.Panics(t, func() { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.ErrorfMock.Set(func(s string, args ...interface{}) { assert.Equal(t, "FormatterMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", s) @@ -94,8 +106,7 @@ func TestFormatterMock_ExpectDifferentArguments(t *testing.T) { } func TestFormatterMock_ExpectAfterSet(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.FatalfMock.Expect("FormatterMock.Format mock is already set by Set").Return() @@ -106,8 +117,7 @@ func TestFormatterMock_ExpectAfterSet(t *testing.T) { } func TestFormatterMock_ExpectAfterWhen(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.FatalfMock.Expect("Expectation set by When has same params: %#v", FormatterMockFormatParams{s1: "Should not work", p1: nil}).Return() @@ -120,7 +130,7 @@ func TestFormatterMock_ExpectAfterWhen(t *testing.T) { } func TestFormatterMock_Return(t *testing.T) { - tester := NewTesterMock(t) + tester := NewTesterMock(t).CleanupMock.Return() formatterMock := NewFormatterMock(tester).FormatMock.Return("Hello world!") df := dummyFormatter{formatterMock} @@ -128,8 +138,7 @@ func TestFormatterMock_Return(t *testing.T) { } func TestFormatterMock_ReturnAfterSet(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.FatalfMock.Expect("FormatterMock.Format mock is already set by Set").Return() @@ -142,8 +151,7 @@ func TestFormatterMock_ReturnAfterSet(t *testing.T) { func TestFormatterMock_ReturnWithoutExpectForFixedArgsMethod(t *testing.T) { // Test for issue https://github.com/gojuno/minimock/issues/31 - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.ErrorMock.Expect("Expected call to FormatterMock.Format") tester.FailNowMock.Expect() @@ -154,7 +162,7 @@ func TestFormatterMock_ReturnWithoutExpectForFixedArgsMethod(t *testing.T) { } func TestFormatterMock_Set(t *testing.T) { - tester := NewTesterMock(t) + tester := NewTesterMock(t).CleanupMock.Return() formatterMock := NewFormatterMock(tester).FormatMock.Set(func(string, ...interface{}) string { return "set" @@ -165,8 +173,7 @@ func TestFormatterMock_Set(t *testing.T) { } func TestFormatterMock_SetAfterExpect(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.FatalfMock.Expect("Default expectation is already set for the Formatter.Format method").Return() @@ -177,8 +184,7 @@ func TestFormatterMock_SetAfterExpect(t *testing.T) { } func TestFormatterMock_SetAfterWhen(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.FatalfMock.Expect("Some expectations are already set for the Formatter.Format method").Return() @@ -200,8 +206,7 @@ func TestFormatterMockFormat_WhenThen(t *testing.T) { } func TestFormatterMockFormat_WhenAfterSet(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.FatalfMock.Expect("FormatterMock.Format mock is already set by Set").Return() @@ -212,19 +217,19 @@ func TestFormatterMockFormat_WhenAfterSet(t *testing.T) { } func TestFormatterMock_MinimockFormatDone(t *testing.T) { - formatterMock := NewFormatterMock(t) + tester := NewTesterMock(t). + CleanupMock.Return() - formatterMock.FormatMock.expectations = []*FormatterMockFormatExpectation{{}} - assert.False(t, formatterMock.MinimockFormatDone()) + formatterMock := NewFormatterMock(tester) - formatterMock = NewFormatterMock(t) - formatterMock.FormatMock.defaultExpectation = &FormatterMockFormatExpectation{} + formatterMock.FormatMock.expectations = []*FormatterMockFormatExpectation{{ + params: &FormatterMockFormatParams{}, + }} assert.False(t, formatterMock.MinimockFormatDone()) } func TestFormatterMock_MinimockFinish(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return() tester.FailNowMock.Expect().Return() @@ -236,8 +241,7 @@ func TestFormatterMock_MinimockFinish(t *testing.T) { } func TestFormatterMock_MinimockFinish_WithNoMetExpectations(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.ErrorfMock.Set(func(m string, args ...interface{}) { assert.Equal(t, m, "Expected call to FormatterMock.Format with params: %#v") @@ -252,8 +256,7 @@ func TestFormatterMock_MinimockFinish_WithNoMetExpectations(t *testing.T) { } func TestFormatterMock_MinimockWait(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return() tester.FailNowMock.Expect().Return() @@ -266,8 +269,7 @@ func TestFormatterMock_MinimockWait(t *testing.T) { // Verifies that Calls() doesn't return nil if no calls were made func TestFormatterMock_CallsNotNil(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() formatterMock := NewFormatterMock(tester) calls := formatterMock.FormatMock.Calls() @@ -278,8 +280,7 @@ func TestFormatterMock_CallsNotNil(t *testing.T) { // Verifies that Calls() returns the correct call args in the expected order func TestFormatterMock_Calls(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() // Arguments used for each mock call expected := []*FormatterMockFormatParams{ @@ -301,8 +302,7 @@ func TestFormatterMock_Calls(t *testing.T) { // Verifies that Calls() returns a new shallow copy of the params list each time func TestFormatterMock_CallsReturnsCopy(t *testing.T) { - tester := NewTesterMock(t) - defer tester.MinimockFinish() + tester := NewTesterMock(t).CleanupMock.Return() expected := []*FormatterMockFormatParams{ {"a1", []interface{}{"a1"}}, @@ -338,6 +338,10 @@ func (dmc *dummyMockController) RegisterMocker(m minimock.Mocker) { dmc.registerCounter++ } +func (dmc *dummyMockController) Cleanup(f func()) { + f() +} + func TestFormatterMock_RegistersMocker(t *testing.T) { mockController := &dummyMockController{} diff --git a/tests/formatter_with_custom_name_mock.go b/tests/formatter_with_custom_name_mock.go index 473b2b5..bc6bc60 100644 --- a/tests/formatter_with_custom_name_mock.go +++ b/tests/formatter_with_custom_name_mock.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.Formatter -o ./tests/formatter_with_custom_name_mock.go -n CustomFormatterNameMock +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.Formatter -o formatter_with_custom_name_mock.go -n CustomFormatterNameMock import ( "sync" @@ -108,7 +108,7 @@ func (mmFormat *mCustomFormatterNameMockFormat) Return(s2 string) *CustomFormatt return mmFormat.mock } -//Set uses given function f to mock the Formatter.Format method +// Set uses given function f to mock the Formatter.Format method func (mmFormat *mCustomFormatterNameMockFormat) Set(f func(s1 string, p1 ...interface{}) (s2 string)) *CustomFormatterNameMock { if mmFormat.defaultExpectation != nil { mmFormat.mock.t.Fatalf("Default expectation is already set for the Formatter.Format method") @@ -152,15 +152,15 @@ func (mmFormat *CustomFormatterNameMock) Format(s1 string, p1 ...interface{}) (s mmFormat.inspectFuncFormat(s1, p1...) } - mm_params := &CustomFormatterNameMockFormatParams{s1, p1} + mm_params := CustomFormatterNameMockFormatParams{s1, p1} // Record call args mmFormat.FormatMock.mutex.Lock() - mmFormat.FormatMock.callArgs = append(mmFormat.FormatMock.callArgs, mm_params) + mmFormat.FormatMock.callArgs = append(mmFormat.FormatMock.callArgs, &mm_params) mmFormat.FormatMock.mutex.Unlock() for _, e := range mmFormat.FormatMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return e.results.s2 } diff --git a/tests/generic_complex_union.go b/tests/generic_complex_union.go index 8b0254e..1084ed7 100644 --- a/tests/generic_complex_union.go +++ b/tests/generic_complex_union.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericComplexUnion -o ./tests/generic_complex_union.go -n GenericComplexUnionMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericComplexUnion -o generic_complex_union.go -n GenericComplexUnionMock -p tests import ( "sync" @@ -14,7 +14,8 @@ import ( // GenericComplexUnionMock implements genericComplexUnion type GenericComplexUnionMock[T complexUnion] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T) inspectFuncName func(t1 T) @@ -26,6 +27,7 @@ type GenericComplexUnionMock[T complexUnion] struct { // NewGenericComplexUnionMock returns a mock for genericComplexUnion func NewGenericComplexUnionMock[T complexUnion](t minimock.Tester) *GenericComplexUnionMock[T] { m := &GenericComplexUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -33,6 +35,8 @@ func NewGenericComplexUnionMock[T complexUnion](t minimock.Tester) *GenericCompl m.NameMock = mGenericComplexUnionMockName[T]{mock: m} m.NameMock.callArgs = []*GenericComplexUnionMockNameParams[T]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -125,15 +129,15 @@ func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { mmName.inspectFuncName(t1) } - mm_params := &GenericComplexUnionMockNameParams[T]{t1} + mm_params := GenericComplexUnionMockNameParams[T]{t1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -225,10 +229,12 @@ func (m *GenericComplexUnionMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericComplexUnionMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/generic_in.go b/tests/generic_in.go index d84cb02..11180c9 100644 --- a/tests/generic_in.go +++ b/tests/generic_in.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericIn -o ./tests/generic_in.go -n GenericInMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericIn -o generic_in.go -n GenericInMock -p tests import ( "sync" @@ -14,7 +14,8 @@ import ( // GenericInMock implements genericIn type GenericInMock[T any] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T) inspectFuncName func(t1 T) @@ -26,6 +27,7 @@ type GenericInMock[T any] struct { // NewGenericInMock returns a mock for genericIn func NewGenericInMock[T any](t minimock.Tester) *GenericInMock[T] { m := &GenericInMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -33,6 +35,8 @@ func NewGenericInMock[T any](t minimock.Tester) *GenericInMock[T] { m.NameMock = mGenericInMockName[T]{mock: m} m.NameMock.callArgs = []*GenericInMockNameParams[T]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -125,15 +129,15 @@ func (mmName *GenericInMock[T]) Name(t1 T) { mmName.inspectFuncName(t1) } - mm_params := &GenericInMockNameParams[T]{t1} + mm_params := GenericInMockNameParams[T]{t1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -225,10 +229,12 @@ func (m *GenericInMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericInMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/generic_inline_union.go b/tests/generic_inline_union.go index fd65d95..e32ee18 100644 --- a/tests/generic_inline_union.go +++ b/tests/generic_inline_union.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnion -o ./tests/generic_inline_union.go -n GenericInlineUnionMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnion -o generic_inline_union.go -n GenericInlineUnionMock -p tests import ( "sync" @@ -14,7 +14,8 @@ import ( // GenericInlineUnionMock implements genericInlineUnion type GenericInlineUnionMock[T int | float64] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T) inspectFuncName func(t1 T) @@ -26,6 +27,7 @@ type GenericInlineUnionMock[T int | float64] struct { // NewGenericInlineUnionMock returns a mock for genericInlineUnion func NewGenericInlineUnionMock[T int | float64](t minimock.Tester) *GenericInlineUnionMock[T] { m := &GenericInlineUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -33,6 +35,8 @@ func NewGenericInlineUnionMock[T int | float64](t minimock.Tester) *GenericInlin m.NameMock = mGenericInlineUnionMockName[T]{mock: m} m.NameMock.callArgs = []*GenericInlineUnionMockNameParams[T]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -125,15 +129,15 @@ func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { mmName.inspectFuncName(t1) } - mm_params := &GenericInlineUnionMockNameParams[T]{t1} + mm_params := GenericInlineUnionMockNameParams[T]{t1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -225,10 +229,12 @@ func (m *GenericInlineUnionMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericInlineUnionMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/generic_inline_with_many_options.go b/tests/generic_inline_with_many_options.go index e4130ab..4256ce3 100644 --- a/tests/generic_inline_with_many_options.go +++ b/tests/generic_inline_with_many_options.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnionWithManyTypes -o ./tests/generic_inline_with_many_options.go -n GenericInlineUnionWithManyTypesMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInlineUnionWithManyTypes -o generic_inline_with_many_options.go -n GenericInlineUnionWithManyTypesMock import ( "sync" @@ -125,15 +125,15 @@ func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { mmName.inspectFuncName(t1) } - mm_params := &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + mm_params := GenericInlineUnionWithManyTypesMockNameParams[T]{t1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } diff --git a/tests/generic_inout.go b/tests/generic_inout.go index 7d580fa..5b306a4 100644 --- a/tests/generic_inout.go +++ b/tests/generic_inout.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInout -o ./tests/generic_inout.go -n GenericInoutMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericInout -o generic_inout.go -n GenericInoutMock -p tests import ( "sync" @@ -14,7 +14,8 @@ import ( // GenericInoutMock implements genericInout type GenericInoutMock[T any] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T) (t2 T) inspectFuncName func(t1 T) @@ -26,6 +27,7 @@ type GenericInoutMock[T any] struct { // NewGenericInoutMock returns a mock for genericInout func NewGenericInoutMock[T any](t minimock.Tester) *GenericInoutMock[T] { m := &GenericInoutMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -33,6 +35,8 @@ func NewGenericInoutMock[T any](t minimock.Tester) *GenericInoutMock[T] { m.NameMock = mGenericInoutMockName[T]{mock: m} m.NameMock.callArgs = []*GenericInoutMockNameParams[T]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -151,15 +155,15 @@ func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { mmName.inspectFuncName(t1) } - mm_params := &GenericInoutMockNameParams[T]{t1} + mm_params := GenericInoutMockNameParams[T]{t1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return e.results.t2 } @@ -253,10 +257,12 @@ func (m *GenericInoutMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericInoutMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/generic_multiple_args_with_different_types.go b/tests/generic_multiple_args_with_different_types.go index 7f00ecf..ba49123 100644 --- a/tests/generic_multiple_args_with_different_types.go +++ b/tests/generic_multiple_args_with_different_types.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericMultipleTypes -o ./tests/generic_multiple_args_with_different_types.go -n GenericMultipleTypesMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericMultipleTypes -o generic_multiple_args_with_different_types.go -n GenericMultipleTypesMock import ( "sync" @@ -127,15 +127,15 @@ func (mmName *GenericMultipleTypesMock[T, K]) Name(t1 T, k1 K) { mmName.inspectFuncName(t1, k1) } - mm_params := &GenericMultipleTypesMockNameParams[T, K]{t1, k1} + mm_params := GenericMultipleTypesMockNameParams[T, K]{t1, k1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } diff --git a/tests/generic_out.go b/tests/generic_out.go index 7a2eba7..25ba517 100644 --- a/tests/generic_out.go +++ b/tests/generic_out.go @@ -2,9 +2,10 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericOut -o ./tests/generic_out.go -n GenericOutMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericOut -o generic_out.go -n GenericOutMock -p tests import ( + "sync" mm_atomic "sync/atomic" mm_time "time" @@ -13,7 +14,8 @@ import ( // GenericOutMock implements genericOut type GenericOutMock[T any] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func() (t1 T) inspectFuncName func() @@ -25,12 +27,15 @@ type GenericOutMock[T any] struct { // NewGenericOutMock returns a mock for genericOut func NewGenericOutMock[T any](t minimock.Tester) *GenericOutMock[T] { m := &GenericOutMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } m.NameMock = mGenericOutMockName[T]{mock: m} + t.Cleanup(m.MinimockFinish) + return m } @@ -179,10 +184,12 @@ func (m *GenericOutMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericOutMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/generic_simple_union.go b/tests/generic_simple_union.go index eb0fe07..674cfc7 100644 --- a/tests/generic_simple_union.go +++ b/tests/generic_simple_union.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSimpleUnion -o ./tests/generic_simple_union.go -n GenericSimpleUnionMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSimpleUnion -o generic_simple_union.go -n GenericSimpleUnionMock -p tests import ( "sync" @@ -14,7 +14,8 @@ import ( // GenericSimpleUnionMock implements genericSimpleUnion type GenericSimpleUnionMock[T simpleUnion] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T) inspectFuncName func(t1 T) @@ -26,6 +27,7 @@ type GenericSimpleUnionMock[T simpleUnion] struct { // NewGenericSimpleUnionMock returns a mock for genericSimpleUnion func NewGenericSimpleUnionMock[T simpleUnion](t minimock.Tester) *GenericSimpleUnionMock[T] { m := &GenericSimpleUnionMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -33,6 +35,8 @@ func NewGenericSimpleUnionMock[T simpleUnion](t minimock.Tester) *GenericSimpleU m.NameMock = mGenericSimpleUnionMockName[T]{mock: m} m.NameMock.callArgs = []*GenericSimpleUnionMockNameParams[T]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -125,15 +129,15 @@ func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { mmName.inspectFuncName(t1) } - mm_params := &GenericSimpleUnionMockNameParams[T]{t1} + mm_params := GenericSimpleUnionMockNameParams[T]{t1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -225,10 +229,12 @@ func (m *GenericSimpleUnionMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericSimpleUnionMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/generic_specific.go b/tests/generic_specific.go index 39cfbc8..43d051b 100644 --- a/tests/generic_specific.go +++ b/tests/generic_specific.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSpecific -o ./tests/generic_specific.go -n GenericSpecificMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3/tests.genericSpecific -o generic_specific.go -n GenericSpecificMock -p tests import ( "sync" @@ -15,7 +15,8 @@ import ( // GenericSpecificMock implements genericSpecific type GenericSpecificMock[T proto.Message] struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once funcName func(t1 T) inspectFuncName func(t1 T) @@ -27,6 +28,7 @@ type GenericSpecificMock[T proto.Message] struct { // NewGenericSpecificMock returns a mock for genericSpecific func NewGenericSpecificMock[T proto.Message](t minimock.Tester) *GenericSpecificMock[T] { m := &GenericSpecificMock[T]{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } @@ -34,6 +36,8 @@ func NewGenericSpecificMock[T proto.Message](t minimock.Tester) *GenericSpecific m.NameMock = mGenericSpecificMockName[T]{mock: m} m.NameMock.callArgs = []*GenericSpecificMockNameParams[T]{} + t.Cleanup(m.MinimockFinish) + return m } @@ -126,15 +130,15 @@ func (mmName *GenericSpecificMock[T]) Name(t1 T) { mmName.inspectFuncName(t1) } - mm_params := &GenericSpecificMockNameParams[T]{t1} + mm_params := GenericSpecificMockNameParams[T]{t1} // Record call args mmName.NameMock.mutex.Lock() - mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, mm_params) + mmName.NameMock.callArgs = append(mmName.NameMock.callArgs, &mm_params) mmName.NameMock.mutex.Unlock() for _, e := range mmName.NameMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -226,10 +230,12 @@ func (m *GenericSpecificMock[T]) MinimockNameInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *GenericSpecificMock[T]) MinimockFinish() { - if !m.minimockDone() { - m.MinimockNameInspect() - m.t.FailNow() - } + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockNameInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times diff --git a/tests/package_name_specified_test.go b/tests/package_name_specified_test.go index 8e0f36d..b1f224e 100644 --- a/tests/package_name_specified_test.go +++ b/tests/package_name_specified_test.go @@ -2,7 +2,7 @@ package tests_test // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3.Tester -o ./tests/package_name_specified_test.go -n TesterMock -p tests_test +//go:generate minimock -i github.com/gojuno/minimock/v3.Tester -o package_name_specified_test.go -n TesterMock -p tests_test import ( "sync" @@ -14,7 +14,14 @@ import ( // TesterMock implements minimock.Tester type TesterMock struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once + + funcCleanup func(f func()) + inspectFuncCleanup func(f func()) + afterCleanupCounter uint64 + beforeCleanupCounter uint64 + CleanupMock mTesterMockCleanup funcError func(p1 ...interface{}) inspectFuncError func(p1 ...interface{}) @@ -50,10 +57,14 @@ type TesterMock struct { // NewTesterMock returns a mock for minimock.Tester func NewTesterMock(t minimock.Tester) *TesterMock { m := &TesterMock{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } + m.CleanupMock = mTesterMockCleanup{mock: m} + m.CleanupMock.callArgs = []*TesterMockCleanupParams{} + m.ErrorMock = mTesterMockError{mock: m} m.ErrorMock.callArgs = []*TesterMockErrorParams{} @@ -68,9 +79,198 @@ func NewTesterMock(t minimock.Tester) *TesterMock { m.FatalfMock = mTesterMockFatalf{mock: m} m.FatalfMock.callArgs = []*TesterMockFatalfParams{} + t.Cleanup(m.MinimockFinish) + return m } +type mTesterMockCleanup struct { + mock *TesterMock + defaultExpectation *TesterMockCleanupExpectation + expectations []*TesterMockCleanupExpectation + + callArgs []*TesterMockCleanupParams + mutex sync.RWMutex +} + +// TesterMockCleanupExpectation specifies expectation struct of the Tester.Cleanup +type TesterMockCleanupExpectation struct { + mock *TesterMock + params *TesterMockCleanupParams + + Counter uint64 +} + +// TesterMockCleanupParams contains parameters of the Tester.Cleanup +type TesterMockCleanupParams struct { + f func() +} + +// Expect sets up expected params for Tester.Cleanup +func (mmCleanup *mTesterMockCleanup) Expect(f func()) *mTesterMockCleanup { + if mmCleanup.mock.funcCleanup != nil { + mmCleanup.mock.t.Fatalf("TesterMock.Cleanup mock is already set by Set") + } + + if mmCleanup.defaultExpectation == nil { + mmCleanup.defaultExpectation = &TesterMockCleanupExpectation{} + } + + mmCleanup.defaultExpectation.params = &TesterMockCleanupParams{f} + for _, e := range mmCleanup.expectations { + if minimock.Equal(e.params, mmCleanup.defaultExpectation.params) { + mmCleanup.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmCleanup.defaultExpectation.params) + } + } + + return mmCleanup +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Cleanup +func (mmCleanup *mTesterMockCleanup) Inspect(f func(f func())) *mTesterMockCleanup { + if mmCleanup.mock.inspectFuncCleanup != nil { + mmCleanup.mock.t.Fatalf("Inspect function is already set for TesterMock.Cleanup") + } + + mmCleanup.mock.inspectFuncCleanup = f + + return mmCleanup +} + +// Return sets up results that will be returned by Tester.Cleanup +func (mmCleanup *mTesterMockCleanup) Return() *TesterMock { + if mmCleanup.mock.funcCleanup != nil { + mmCleanup.mock.t.Fatalf("TesterMock.Cleanup mock is already set by Set") + } + + if mmCleanup.defaultExpectation == nil { + mmCleanup.defaultExpectation = &TesterMockCleanupExpectation{mock: mmCleanup.mock} + } + + return mmCleanup.mock +} + +// Set uses given function f to mock the Tester.Cleanup method +func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { + if mmCleanup.defaultExpectation != nil { + mmCleanup.mock.t.Fatalf("Default expectation is already set for the Tester.Cleanup method") + } + + if len(mmCleanup.expectations) > 0 { + mmCleanup.mock.t.Fatalf("Some expectations are already set for the Tester.Cleanup method") + } + + mmCleanup.mock.funcCleanup = f + return mmCleanup.mock +} + +// Cleanup implements minimock.Tester +func (mmCleanup *TesterMock) Cleanup(f func()) { + mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) + defer mm_atomic.AddUint64(&mmCleanup.afterCleanupCounter, 1) + + if mmCleanup.inspectFuncCleanup != nil { + mmCleanup.inspectFuncCleanup(f) + } + + mm_params := TesterMockCleanupParams{f} + + // Record call args + mmCleanup.CleanupMock.mutex.Lock() + mmCleanup.CleanupMock.callArgs = append(mmCleanup.CleanupMock.callArgs, &mm_params) + mmCleanup.CleanupMock.mutex.Unlock() + + for _, e := range mmCleanup.CleanupMock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmCleanup.CleanupMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmCleanup.CleanupMock.defaultExpectation.Counter, 1) + mm_want := mmCleanup.CleanupMock.defaultExpectation.params + mm_got := TesterMockCleanupParams{f} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmCleanup.funcCleanup != nil { + mmCleanup.funcCleanup(f) + return + } + mmCleanup.t.Fatalf("Unexpected call to TesterMock.Cleanup. %v", f) + +} + +// CleanupAfterCounter returns a count of finished TesterMock.Cleanup invocations +func (mmCleanup *TesterMock) CleanupAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmCleanup.afterCleanupCounter) +} + +// CleanupBeforeCounter returns a count of TesterMock.Cleanup invocations +func (mmCleanup *TesterMock) CleanupBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmCleanup.beforeCleanupCounter) +} + +// Calls returns a list of arguments used in each call to TesterMock.Cleanup. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmCleanup *mTesterMockCleanup) Calls() []*TesterMockCleanupParams { + mmCleanup.mutex.RLock() + + argCopy := make([]*TesterMockCleanupParams, len(mmCleanup.callArgs)) + copy(argCopy, mmCleanup.callArgs) + + mmCleanup.mutex.RUnlock() + + return argCopy +} + +// MinimockCleanupDone returns true if the count of the Cleanup invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockCleanupDone() bool { + for _, e := range m.CleanupMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + return false + } + return true +} + +// MinimockCleanupInspect logs each unmet expectation +func (m *TesterMock) MinimockCleanupInspect() { + for _, e := range m.CleanupMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TesterMock.Cleanup with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + if m.CleanupMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TesterMock.Cleanup") + } else { + m.t.Errorf("Expected call to TesterMock.Cleanup with params: %#v", *m.CleanupMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + m.t.Error("Expected call to TesterMock.Cleanup") + } +} + type mTesterMockError struct { mock *TesterMock defaultExpectation *TesterMockErrorExpectation @@ -160,15 +360,15 @@ func (mmError *TesterMock) Error(p1 ...interface{}) { mmError.inspectFuncError(p1...) } - mm_params := &TesterMockErrorParams{p1} + mm_params := TesterMockErrorParams{p1} // Record call args mmError.ErrorMock.mutex.Lock() - mmError.ErrorMock.callArgs = append(mmError.ErrorMock.callArgs, mm_params) + mmError.ErrorMock.callArgs = append(mmError.ErrorMock.callArgs, &mm_params) mmError.ErrorMock.mutex.Unlock() for _, e := range mmError.ErrorMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -348,15 +548,15 @@ func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mmErrorf.inspectFuncErrorf(format, args...) } - mm_params := &TesterMockErrorfParams{format, args} + mm_params := TesterMockErrorfParams{format, args} // Record call args mmErrorf.ErrorfMock.mutex.Lock() - mmErrorf.ErrorfMock.callArgs = append(mmErrorf.ErrorfMock.callArgs, mm_params) + mmErrorf.ErrorfMock.callArgs = append(mmErrorf.ErrorfMock.callArgs, &mm_params) mmErrorf.ErrorfMock.mutex.Unlock() for _, e := range mmErrorf.ErrorfMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -670,15 +870,15 @@ func (mmFatal *TesterMock) Fatal(args ...interface{}) { mmFatal.inspectFuncFatal(args...) } - mm_params := &TesterMockFatalParams{args} + mm_params := TesterMockFatalParams{args} // Record call args mmFatal.FatalMock.mutex.Lock() - mmFatal.FatalMock.callArgs = append(mmFatal.FatalMock.callArgs, mm_params) + mmFatal.FatalMock.callArgs = append(mmFatal.FatalMock.callArgs, &mm_params) mmFatal.FatalMock.mutex.Unlock() for _, e := range mmFatal.FatalMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -858,15 +1058,15 @@ func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mmFatalf.inspectFuncFatalf(format, args...) } - mm_params := &TesterMockFatalfParams{format, args} + mm_params := TesterMockFatalfParams{format, args} // Record call args mmFatalf.FatalfMock.mutex.Lock() - mmFatalf.FatalfMock.callArgs = append(mmFatalf.FatalfMock.callArgs, mm_params) + mmFatalf.FatalfMock.callArgs = append(mmFatalf.FatalfMock.callArgs, &mm_params) mmFatalf.FatalfMock.mutex.Unlock() for _, e := range mmFatalf.FatalfMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -958,18 +1158,22 @@ func (m *TesterMock) MinimockFatalfInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *TesterMock) MinimockFinish() { - if !m.minimockDone() { - m.MinimockErrorInspect() + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockCleanupInspect() - m.MinimockErrorfInspect() + m.MinimockErrorInspect() - m.MinimockFailNowInspect() + m.MinimockErrorfInspect() - m.MinimockFatalInspect() + m.MinimockFailNowInspect() - m.MinimockFatalfInspect() - m.t.FailNow() - } + m.MinimockFatalInspect() + + m.MinimockFatalfInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times @@ -991,6 +1195,7 @@ func (m *TesterMock) MinimockWait(timeout mm_time.Duration) { func (m *TesterMock) minimockDone() bool { done := true return done && + m.MinimockCleanupDone() && m.MinimockErrorDone() && m.MinimockErrorfDone() && m.MinimockFailNowDone() && diff --git a/tests/tester_mock_test.go b/tests/tester_mock_test.go index 8800bef..1dcf0b6 100644 --- a/tests/tester_mock_test.go +++ b/tests/tester_mock_test.go @@ -2,7 +2,7 @@ package tests // Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. -//go:generate minimock -i github.com/gojuno/minimock/v3.Tester -o ./tests/tester_mock_test.go -n TesterMock -p tests +//go:generate minimock -i github.com/gojuno/minimock/v3.Tester -o tester_mock_test.go -n TesterMock -p tests import ( "sync" @@ -14,7 +14,14 @@ import ( // TesterMock implements minimock.Tester type TesterMock struct { - t minimock.Tester + t minimock.Tester + finishOnce sync.Once + + funcCleanup func(f func()) + inspectFuncCleanup func(f func()) + afterCleanupCounter uint64 + beforeCleanupCounter uint64 + CleanupMock mTesterMockCleanup funcError func(p1 ...interface{}) inspectFuncError func(p1 ...interface{}) @@ -50,10 +57,14 @@ type TesterMock struct { // NewTesterMock returns a mock for minimock.Tester func NewTesterMock(t minimock.Tester) *TesterMock { m := &TesterMock{t: t} + if controller, ok := t.(minimock.MockController); ok { controller.RegisterMocker(m) } + m.CleanupMock = mTesterMockCleanup{mock: m} + m.CleanupMock.callArgs = []*TesterMockCleanupParams{} + m.ErrorMock = mTesterMockError{mock: m} m.ErrorMock.callArgs = []*TesterMockErrorParams{} @@ -68,9 +79,198 @@ func NewTesterMock(t minimock.Tester) *TesterMock { m.FatalfMock = mTesterMockFatalf{mock: m} m.FatalfMock.callArgs = []*TesterMockFatalfParams{} + t.Cleanup(m.MinimockFinish) + return m } +type mTesterMockCleanup struct { + mock *TesterMock + defaultExpectation *TesterMockCleanupExpectation + expectations []*TesterMockCleanupExpectation + + callArgs []*TesterMockCleanupParams + mutex sync.RWMutex +} + +// TesterMockCleanupExpectation specifies expectation struct of the Tester.Cleanup +type TesterMockCleanupExpectation struct { + mock *TesterMock + params *TesterMockCleanupParams + + Counter uint64 +} + +// TesterMockCleanupParams contains parameters of the Tester.Cleanup +type TesterMockCleanupParams struct { + f func() +} + +// Expect sets up expected params for Tester.Cleanup +func (mmCleanup *mTesterMockCleanup) Expect(f func()) *mTesterMockCleanup { + if mmCleanup.mock.funcCleanup != nil { + mmCleanup.mock.t.Fatalf("TesterMock.Cleanup mock is already set by Set") + } + + if mmCleanup.defaultExpectation == nil { + mmCleanup.defaultExpectation = &TesterMockCleanupExpectation{} + } + + mmCleanup.defaultExpectation.params = &TesterMockCleanupParams{f} + for _, e := range mmCleanup.expectations { + if minimock.Equal(e.params, mmCleanup.defaultExpectation.params) { + mmCleanup.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmCleanup.defaultExpectation.params) + } + } + + return mmCleanup +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Cleanup +func (mmCleanup *mTesterMockCleanup) Inspect(f func(f func())) *mTesterMockCleanup { + if mmCleanup.mock.inspectFuncCleanup != nil { + mmCleanup.mock.t.Fatalf("Inspect function is already set for TesterMock.Cleanup") + } + + mmCleanup.mock.inspectFuncCleanup = f + + return mmCleanup +} + +// Return sets up results that will be returned by Tester.Cleanup +func (mmCleanup *mTesterMockCleanup) Return() *TesterMock { + if mmCleanup.mock.funcCleanup != nil { + mmCleanup.mock.t.Fatalf("TesterMock.Cleanup mock is already set by Set") + } + + if mmCleanup.defaultExpectation == nil { + mmCleanup.defaultExpectation = &TesterMockCleanupExpectation{mock: mmCleanup.mock} + } + + return mmCleanup.mock +} + +// Set uses given function f to mock the Tester.Cleanup method +func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { + if mmCleanup.defaultExpectation != nil { + mmCleanup.mock.t.Fatalf("Default expectation is already set for the Tester.Cleanup method") + } + + if len(mmCleanup.expectations) > 0 { + mmCleanup.mock.t.Fatalf("Some expectations are already set for the Tester.Cleanup method") + } + + mmCleanup.mock.funcCleanup = f + return mmCleanup.mock +} + +// Cleanup implements minimock.Tester +func (mmCleanup *TesterMock) Cleanup(f func()) { + mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) + defer mm_atomic.AddUint64(&mmCleanup.afterCleanupCounter, 1) + + if mmCleanup.inspectFuncCleanup != nil { + mmCleanup.inspectFuncCleanup(f) + } + + mm_params := TesterMockCleanupParams{f} + + // Record call args + mmCleanup.CleanupMock.mutex.Lock() + mmCleanup.CleanupMock.callArgs = append(mmCleanup.CleanupMock.callArgs, &mm_params) + mmCleanup.CleanupMock.mutex.Unlock() + + for _, e := range mmCleanup.CleanupMock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + return + } + } + + if mmCleanup.CleanupMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmCleanup.CleanupMock.defaultExpectation.Counter, 1) + mm_want := mmCleanup.CleanupMock.defaultExpectation.params + mm_got := TesterMockCleanupParams{f} + if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + + return + + } + if mmCleanup.funcCleanup != nil { + mmCleanup.funcCleanup(f) + return + } + mmCleanup.t.Fatalf("Unexpected call to TesterMock.Cleanup. %v", f) + +} + +// CleanupAfterCounter returns a count of finished TesterMock.Cleanup invocations +func (mmCleanup *TesterMock) CleanupAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmCleanup.afterCleanupCounter) +} + +// CleanupBeforeCounter returns a count of TesterMock.Cleanup invocations +func (mmCleanup *TesterMock) CleanupBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmCleanup.beforeCleanupCounter) +} + +// Calls returns a list of arguments used in each call to TesterMock.Cleanup. +// The list is in the same order as the calls were made (i.e. recent calls have a higher index) +func (mmCleanup *mTesterMockCleanup) Calls() []*TesterMockCleanupParams { + mmCleanup.mutex.RLock() + + argCopy := make([]*TesterMockCleanupParams, len(mmCleanup.callArgs)) + copy(argCopy, mmCleanup.callArgs) + + mmCleanup.mutex.RUnlock() + + return argCopy +} + +// MinimockCleanupDone returns true if the count of the Cleanup invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockCleanupDone() bool { + for _, e := range m.CleanupMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + return false + } + // if func was set then invocations count should be greater than zero + if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + return false + } + return true +} + +// MinimockCleanupInspect logs each unmet expectation +func (m *TesterMock) MinimockCleanupInspect() { + for _, e := range m.CleanupMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Errorf("Expected call to TesterMock.Cleanup with params: %#v", *e.params) + } + } + + // if default expectation was set then invocations count should be greater than zero + if m.CleanupMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + if m.CleanupMock.defaultExpectation.params == nil { + m.t.Error("Expected call to TesterMock.Cleanup") + } else { + m.t.Errorf("Expected call to TesterMock.Cleanup with params: %#v", *m.CleanupMock.defaultExpectation.params) + } + } + // if func was set then invocations count should be greater than zero + if m.funcCleanup != nil && mm_atomic.LoadUint64(&m.afterCleanupCounter) < 1 { + m.t.Error("Expected call to TesterMock.Cleanup") + } +} + type mTesterMockError struct { mock *TesterMock defaultExpectation *TesterMockErrorExpectation @@ -160,15 +360,15 @@ func (mmError *TesterMock) Error(p1 ...interface{}) { mmError.inspectFuncError(p1...) } - mm_params := &TesterMockErrorParams{p1} + mm_params := TesterMockErrorParams{p1} // Record call args mmError.ErrorMock.mutex.Lock() - mmError.ErrorMock.callArgs = append(mmError.ErrorMock.callArgs, mm_params) + mmError.ErrorMock.callArgs = append(mmError.ErrorMock.callArgs, &mm_params) mmError.ErrorMock.mutex.Unlock() for _, e := range mmError.ErrorMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -348,15 +548,15 @@ func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mmErrorf.inspectFuncErrorf(format, args...) } - mm_params := &TesterMockErrorfParams{format, args} + mm_params := TesterMockErrorfParams{format, args} // Record call args mmErrorf.ErrorfMock.mutex.Lock() - mmErrorf.ErrorfMock.callArgs = append(mmErrorf.ErrorfMock.callArgs, mm_params) + mmErrorf.ErrorfMock.callArgs = append(mmErrorf.ErrorfMock.callArgs, &mm_params) mmErrorf.ErrorfMock.mutex.Unlock() for _, e := range mmErrorf.ErrorfMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -670,15 +870,15 @@ func (mmFatal *TesterMock) Fatal(args ...interface{}) { mmFatal.inspectFuncFatal(args...) } - mm_params := &TesterMockFatalParams{args} + mm_params := TesterMockFatalParams{args} // Record call args mmFatal.FatalMock.mutex.Lock() - mmFatal.FatalMock.callArgs = append(mmFatal.FatalMock.callArgs, mm_params) + mmFatal.FatalMock.callArgs = append(mmFatal.FatalMock.callArgs, &mm_params) mmFatal.FatalMock.mutex.Unlock() for _, e := range mmFatal.FatalMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -858,15 +1058,15 @@ func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mmFatalf.inspectFuncFatalf(format, args...) } - mm_params := &TesterMockFatalfParams{format, args} + mm_params := TesterMockFatalfParams{format, args} // Record call args mmFatalf.FatalfMock.mutex.Lock() - mmFatalf.FatalfMock.callArgs = append(mmFatalf.FatalfMock.callArgs, mm_params) + mmFatalf.FatalfMock.callArgs = append(mmFatalf.FatalfMock.callArgs, &mm_params) mmFatalf.FatalfMock.mutex.Unlock() for _, e := range mmFatalf.FatalfMock.expectations { - if minimock.Equal(e.params, mm_params) { + if minimock.Equal(*e.params, mm_params) { mm_atomic.AddUint64(&e.Counter, 1) return } @@ -958,18 +1158,22 @@ func (m *TesterMock) MinimockFatalfInspect() { // MinimockFinish checks that all mocked methods have been called the expected number of times func (m *TesterMock) MinimockFinish() { - if !m.minimockDone() { - m.MinimockErrorInspect() + m.finishOnce.Do(func() { + if !m.minimockDone() { + m.MinimockCleanupInspect() - m.MinimockErrorfInspect() + m.MinimockErrorInspect() - m.MinimockFailNowInspect() + m.MinimockErrorfInspect() - m.MinimockFatalInspect() + m.MinimockFailNowInspect() - m.MinimockFatalfInspect() - m.t.FailNow() - } + m.MinimockFatalInspect() + + m.MinimockFatalfInspect() + m.t.FailNow() + } + }) } // MinimockWait waits for all mocked methods to be called the expected number of times @@ -991,6 +1195,7 @@ func (m *TesterMock) MinimockWait(timeout mm_time.Duration) { func (m *TesterMock) minimockDone() bool { done := true return done && + m.MinimockCleanupDone() && m.MinimockErrorDone() && m.MinimockErrorfDone() && m.MinimockFailNowDone() && diff --git a/tests/types.go b/tests/types.go index f499542..35e2eb3 100644 --- a/tests/types.go +++ b/tests/types.go @@ -1,7 +1,11 @@ // Package tests contains tests for minimock tool and demonstrates minimock features package tests -import "google.golang.org/protobuf/proto" +import ( + "context" + + "google.golang.org/protobuf/proto" +) type ( //Formatter interface is used to test code generated by minimock @@ -63,4 +67,9 @@ type ( genericMultipleTypes[T proto.Message, K any] interface { Name(T, K) } + + contextAccepter interface { + AcceptContext(context.Context) + AcceptContextWithOtherArgs(context.Context, int) (int, error) + } )