diff --git a/caller.go b/caller.go new file mode 100644 index 0000000..41e0be3 --- /dev/null +++ b/caller.go @@ -0,0 +1,13 @@ +package minimock + +import ( + "fmt" + "runtime" +) + +func CallerInfo(skip int) string { + if _, file, line, ok := runtime.Caller(skip + 1); ok { + return fmt.Sprintf("%s:%d", file, line) + } + return "unknown file" +} diff --git a/go.mod b/go.mod index 762caa9..fc26f18 100644 --- a/go.mod +++ b/go.mod @@ -2,18 +2,19 @@ module github.com/gojuno/minimock/v3 require ( github.com/davecgh/go-spew v1.1.1 - github.com/hexdigest/gowrap v1.3.10 + github.com/hexdigest/gowrap v1.4.0 github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.0 github.com/stretchr/testify v1.8.4 - golang.org/x/tools v0.1.11-0.20220316014157-77aa08bb151a + golang.org/x/tools v0.1.12 google.golang.org/protobuf v1.30.0 ) require ( github.com/kr/text v0.2.0 // indirect - golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect + golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/sys v0.5.0 // indirect + golang.org/x/text v0.3.7 // 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.1 // indirect diff --git a/go.sum b/go.sum index 9610285..f1ad37c 100644 --- a/go.sum +++ b/go.sum @@ -2,11 +2,10 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 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/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/hexdigest/gowrap v1.3.10 h1:e8NwAdtnwpRJ4Ks76+GctDNCFIkHR2TCFCWshug/zIE= -github.com/hexdigest/gowrap v1.3.10/go.mod h1:5KTYxPjK1RRfD+9L4Oo9gjP3XNAs4rkoVK2E7eAEFyM= +github.com/hexdigest/gowrap v1.4.0 h1:Zjhnl1omZXJs40S1K0gaWQjvO9ADy3u1sGqLkYJNcP8= +github.com/hexdigest/gowrap v1.4.0/go.mod h1:uOPX6MbEZnYtf5i5/+rS0Aj8NC3P/V594uaoaiMMbRg= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= @@ -22,12 +21,14 @@ github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XF github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -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/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -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/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 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= diff --git a/internal/templates/body.tmpl b/internal/templates/body.tmpl new file mode 100644 index 0000000..39d1851 --- /dev/null +++ b/internal/templates/body.tmpl @@ -0,0 +1,407 @@ +{{ $mock := (title (index $.Vars "MockName")) }} + +// {{$mock}} implements {{$.Interface.Type}} +type {{$mock}}{{(params)}} struct { + t minimock.Tester + finishOnce sync.Once + + {{ range $method := $.Interface.Methods }} + func{{$method.Name}} func{{ $method.Signature }} + func{{$method.Name}}Origin string + inspectFunc{{$method.Name}} func({{ $method.Params}}) + after{{$method.Name}}Counter uint64 + before{{$method.Name}}Counter uint64 + {{$method.Name}}Mock m{{$mock}}{{$method.Name}}{{(paramsRef)}} + {{ end }} +} + +// 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) + } + {{ range $method := $.Interface.Methods }} + 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 +} + +{{ range $method := $.Interface.Methods }} + {{ $m := (printf "mm%s" $method.Name) }} + + type m{{$mock}}{{$method.Name}}{{(params)}} struct { + optional bool + mock *{{$mock}}{{(paramsRef)}} + defaultExpectation *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}} + expectations []*{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}} + {{ if $method.HasParams }} + callArgs []*{{$mock}}{{$method.Name}}Params{{(paramsRef)}} + mutex sync.RWMutex + {{ end }} + + expectedInvocations uint64 + expectedInvocationsOrigin string + } + + // {{$mock}}{{$method.Name}}Expectation specifies expectation struct of the {{$.Interface.Name}}.{{$method.Name}} + type {{$mock}}{{$method.Name}}Expectation{{(params)}} struct { + mock *{{$mock}}{{(paramsRef)}} + {{ if $method.HasParams }} params *{{$mock}}{{$method.Name}}Params{{(paramsRef)}} {{end}} + {{ if $method.HasParams }} paramPtrs *{{$mock}}{{$method.Name}}ParamPtrs{{(paramsRef)}} {{end}} + {{ if $method.HasParams }} expectationOrigins {{$mock}}{{$method.Name}}ExpectationOrigins {{end}} + {{ if $method.HasResults }} results *{{$mock}}{{$method.Name}}Results{{(paramsRef)}} {{end}} + returnOrigin string + Counter uint64 + } + + {{if $method.HasParams }} + // {{$mock}}{{$method.Name}}Params contains parameters of the {{$.Interface.Name}}.{{$method.Name}} + type {{$mock}}{{$method.Name}}Params{{(params)}} {{$method.ParamsStruct}} + {{end}} + + {{if $method.HasParams }} + // {{$mock}}{{$method.Name}}ParamPtrs contains pointers to parameters of the {{$.Interface.Name}}.{{$method.Name}} + type {{$mock}}{{$method.Name}}ParamPtrs{{(params)}} {{$method.ParamPtrsStruct}} + {{end}} + + {{if $method.HasResults }} + // {{$mock}}{{$method.Name}}Results contains results of the {{$.Interface.Name}}.{{$method.Name}} + type {{$mock}}{{$method.Name}}Results{{(params)}} {{$method.ResultsStruct}} + {{end}} + + {{if $method.HasParams }} + // {{$mock}}{{$method.Name}}Origins contains origins of expectations of the {{$.Interface.Name}}.{{$method.Name}} + type {{$mock}}{{$method.Name}}ExpectationOrigins struct { + origin string + {{- range $param := $method.Params }} + origin{{ $param.Name | title }} string + {{- end}} + } + {{end}} + + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning + // the test will fail minimock's automatic final call check if the mocked method was not called at least once. + // Optional() makes method check to work in '0 or more' mode. + // It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to + // catch the problems when the expected method call is totally skipped during test run. + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Optional() *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { + {{$m}}.optional = true + return {{$m}} + } + + // Expect sets up expected params for {{$.Interface.Name}}.{{$method.Name}} + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Expect({{$method.Params}}) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { + if {{$m}}.mock.func{{$method.Name}} != nil { + {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") + } + + if {{$m}}.defaultExpectation == nil { + {{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{} + } + + {{if $method.HasParams }} + if {{$m}}.defaultExpectation.paramPtrs != nil { + {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by ExpectParams functions") + } + + {{$m}}.defaultExpectation.params = &{{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{ $method.ParamsNames }} } + {{$m}}.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) + for _, e := range {{$m}}.expectations { + if minimock.Equal(e.params, {{$m}}.defaultExpectation.params) { + {{$m}}.mock.t.Fatalf("Expectation set by When has same params: %#v", *{{$m}}.defaultExpectation.params) + } + } + {{end}} + return {{$m}} + } + + {{ range $idx, $param := $method.Params }} + // Expect{{ $param.Name | title }}Param{{$idx | inc}} sets up expected param {{ $param.Name }} for {{$.Interface.Name}}.{{$method.Name}} + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Expect{{ $param.Name | title }}Param{{$idx | inc}}({{$param.Name}} {{$param.Type}}) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { + if {{$m}}.mock.func{{$method.Name}} != nil { + {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") + } + + if {{$m}}.defaultExpectation == nil { + {{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{} + } + + if {{$m}}.defaultExpectation.params != nil { + {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Expect") + } + + if {{$m}}.defaultExpectation.paramPtrs == nil { + {{$m}}.defaultExpectation.paramPtrs = &{{$mock}}{{$method.Name}}ParamPtrs{{(paramsRef)}}{} + } + {{$m}}.defaultExpectation.paramPtrs.{{$param.Name}} = &{{$param.Name}} + {{$m}}.defaultExpectation.expectationOrigins.origin{{ $param.Name | title }} = minimock.CallerInfo(1) + + return {{$m}} + } + {{ end }} + // Inspect accepts an inspector function that has same arguments as the {{$.Interface.Name}}.{{$method.Name}} + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Inspect(f func({{$method.Params}})) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { + if {{$m}}.mock.inspectFunc{{$method.Name}} != nil { + {{$m}}.mock.t.Fatalf("Inspect function is already set for {{$mock}}.{{$method.Name}}") + } + + {{$m}}.mock.inspectFunc{{$method.Name}} = f + + return {{$m}} + } + + // Return sets up results that will be returned by {{$.Interface.Name}}.{{$method.Name}} + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Return({{$method.Results}}) *{{$mock}}{{(paramsRef)}} { + if {{$m}}.mock.func{{$method.Name}} != nil { + {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") + } + + if {{$m}}.defaultExpectation == nil { + {{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{mock: {{$m}}.mock} + } + {{if $method.HasResults }} {{$m}}.defaultExpectation.results = &{{$mock}}{{$method.Name}}Results{{(paramsRef)}}{ {{ $method.ResultsNames }} } {{end}} + {{$m}}.defaultExpectation.returnOrigin = minimock.CallerInfo(1) + return {{$m}}.mock + } + + // Set uses given function f to mock the {{$.Interface.Name}}.{{$method.Name}} method + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Set(f func{{$method.Signature}}) *{{$mock}}{{(paramsRef)}}{ + if {{$m}}.defaultExpectation != nil { + {{$m}}.mock.t.Fatalf("Default expectation is already set for the {{$.Interface.Name}}.{{$method.Name}} method") + } + + if len({{$m}}.expectations) > 0 { + {{$m}}.mock.t.Fatalf("Some expectations are already set for the {{$.Interface.Name}}.{{$method.Name}} method") + } + + {{$m}}.mock.func{{$method.Name}} = f + {{$m}}.mock.func{{$method.Name}}Origin = minimock.CallerInfo(1) + return {{$m}}.mock + } + + {{if (and $method.HasParams $method.HasResults)}} + // When sets expectation for the {{$.Interface.Name}}.{{$method.Name}} which will trigger the result defined by the following + // Then helper + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) When({{$method.Params}}) *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}} { + if {{$m}}.mock.func{{$method.Name}} != nil { + {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") + } + + expectation := &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{ + mock: {{$m}}.mock, + params: &{{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{ $method.ParamsNames }} }, + expectationOrigins: {{$mock}}{{$method.Name}}ExpectationOrigins{origin: minimock.CallerInfo(1)}, + } + {{$m}}.expectations = append({{$m}}.expectations, expectation) + return expectation + } + + // Then sets up {{$.Interface.Name}}.{{$method.Name}} return parameters for the expectation previously defined by the When method + func (e *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}) Then({{$method.Results}}) *{{$mock}}{{(paramsRef)}} { + e.results = &{{$mock}}{{$method.Name}}Results{{(paramsRef)}}{ {{ $method.ResultsNames }} } + return e.mock + } + {{end}} + + // Times sets number of times {{$.Interface.Name}}.{{$method.Name}} should be invoked + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Times(n uint64) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { + if n == 0 { + {{$m}}.mock.t.Fatalf("Times of {{$mock}}.{{$method.Name}} mock can not be zero") + } + mm_atomic.StoreUint64(&{{$m}}.expectedInvocations, n) + {{$m}}.expectedInvocationsOrigin = minimock.CallerInfo(1) + return {{$m}} + } + + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) invocationsDone() bool { + if len({{$m}}.expectations) == 0 && {{$m}}.defaultExpectation == nil && {{$m}}.mock.func{{$method.Name}} == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&{{$m}}.mock.after{{$method.Name}}Counter) + expectedInvocations := mm_atomic.LoadUint64(&{{$m}}.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) + } + + // {{$method.Name}} implements {{$.Interface.Type}} + func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Declaration}} { + mm_atomic.AddUint64(&{{$m}}.before{{$method.Name}}Counter, 1) + defer mm_atomic.AddUint64(&{{$m}}.after{{$method.Name}}Counter, 1) + + {{$m}}.t.Helper() + + if {{$m}}.inspectFunc{{$method.Name}} != nil { + {{$m}}.inspectFunc{{$method.Name}}({{$method.Params.Pass}}) + } + + {{if $method.HasParams}} + 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.mutex.Unlock() + + for _, e := range {{$m}}.{{$method.Name}}Mock.expectations { + if minimock.Equal(*e.params, mm_params) { + mm_atomic.AddUint64(&e.Counter, 1) + {{$method.ReturnStruct "e.results" -}} + } + } + {{end}} + + if {{$m}}.{{$method.Name}}Mock.defaultExpectation != nil { + mm_atomic.AddUint64(&{{$m}}.{{$method.Name}}Mock.defaultExpectation.Counter, 1) + {{- if $method.HasParams }} + mm_want := {{$m}}.{{$method.Name}}Mock.defaultExpectation.params + mm_want_ptrs := {{$m}}.{{$method.Name}}Mock.defaultExpectation.paramPtrs + + mm_got := {{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{$method.ParamsNames}} } + + if mm_want_ptrs != nil { + {{ range $idx, $param := $method.Params }} + if mm_want_ptrs.{{$param.Name}} != nil && !minimock.Equal(*mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}}) { + {{$m}}.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameter {{$param.Name}}, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + {{$m}}.{{$method.Name}}Mock.defaultExpectation.expectationOrigins.origin{{$param.Name | title}}, *mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}}, minimock.Diff(*mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}})) + } + {{ end }} + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + {{$m}}.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + {{$m}}.{{$method.Name}}Mock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + } + {{ end }} + {{if $method.HasResults }} + mm_results := {{$m}}.{{$method.Name}}Mock.defaultExpectation.results + if mm_results == nil { + {{$m}}.t.Fatal("No results are set for the {{$mock}}.{{$method.Name}}") + } + {{$method.ReturnStruct "(*mm_results)" -}} + {{else}} + return + {{ end }} + } + if {{$m}}.func{{$method.Name}} != nil { + {{$method.Pass (printf "%s.func" $m)}} + } + {{$m}}.t.Fatalf("Unexpected call to {{$mock}}.{{$method.Name}}.{{range $method.Params}} %v{{end}}", {{ $method.ParamsNames }} ) + {{if $method.HasResults}}return{{end}} + } + + // {{$method.Name}}AfterCounter returns a count of finished {{$mock}}.{{$method.Name}} invocations + func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Name}}AfterCounter() uint64 { + return mm_atomic.LoadUint64(&{{$m}}.after{{$method.Name}}Counter) + } + + // {{$method.Name}}BeforeCounter returns a count of {{$mock}}.{{$method.Name}} invocations + func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Name}}BeforeCounter() uint64 { + return mm_atomic.LoadUint64(&{{$m}}.before{{$method.Name}}Counter) + } + + {{ if $method.HasParams }} + // Calls returns a list of arguments used in each call to {{$mock}}.{{$method.Name}}. + // The list is in the same order as the calls were made (i.e. recent calls have a higher index) + func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Calls() []*{{$mock}}{{$method.Name}}Params{{(paramsRef)}} { + {{$m}}.mutex.RLock() + + argCopy := make([]*{{$mock}}{{$method.Name}}Params{{(paramsRef)}}, len({{$m}}.callArgs)) + copy(argCopy, {{$m}}.callArgs) + + {{$m}}.mutex.RUnlock() + + return argCopy + } + {{ end }} + + // Minimock{{$method.Name}}Done returns true if the count of the {{$method.Name}} invocations corresponds + // the number of defined expectations + func (m *{{$mock}}{{(paramsRef)}}) Minimock{{$method.Name}}Done() bool { + if m.{{$method.Name}}Mock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + + for _, e := range m.{{$method.Name}}Mock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + return m.{{$method.Name}}Mock.invocationsDone() + } + + // Minimock{{$method.Name}}Inspect logs each unmet expectation + func (m *{{$mock}}{{(paramsRef)}}) Minimock{{$method.Name}}Inspect() { + for _, e := range m.{{$method.Name}}Mock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + {{- if $method.HasParams}} + m.t.Errorf("Expected call to {{$mock}}.{{$method.Name}} at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) + {{else}} + m.t.Error("Expected call to {{$mock}}.{{$method.Name}}") + {{end -}} + } + } + + after{{$method.Name}}Counter := mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter) + // if default expectation was set then invocations count should be greater than zero + if m.{{$method.Name}}Mock.defaultExpectation != nil && after{{$method.Name}}Counter < 1 { + {{- if $method.HasParams}} + if m.{{$method.Name}}Mock.defaultExpectation.params == nil { + m.t.Errorf("Expected call to {{$mock}}.{{$method.Name}} at\n%s", m.{{$method.Name}}Mock.defaultExpectation.returnOrigin) + } else { + m.t.Errorf("Expected call to {{$mock}}.{{$method.Name}} at\n%s with params: %#v", m.{{$method.Name}}Mock.defaultExpectation.expectationOrigins.origin, *m.{{$method.Name}}Mock.defaultExpectation.params) + } + {{else}} + m.t.Errorf("Expected call to {{$mock}}.{{$method.Name}} at\n%s", m.{{$method.Name}}Mock.defaultExpectation.returnOrigin) + {{end -}} + } + // if func was set then invocations count should be greater than zero + if m.func{{$method.Name}} != nil && after{{$method.Name}}Counter < 1 { + m.t.Errorf("Expected call to {{$mock}}.{{$method.Name}} at\n%s", m.func{{$method.Name}}Origin) + } + + if !m.{{$method.Name}}Mock.invocationsDone() && after{{$method.Name}}Counter > 0 { + m.t.Errorf("Expected %d calls to {{$mock}}.{{$method.Name}} at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.{{$method.Name}}Mock.expectedInvocations), m.{{$method.Name}}Mock.expectedInvocationsOrigin, after{{$method.Name}}Counter) + } + } +{{end}} + +// MinimockFinish checks that all mocked methods have been called the expected number of times +func (m *{{$mock}}{{(paramsRef)}}) MinimockFinish() { + m.finishOnce.Do(func() { + if !m.minimockDone() { + {{- range $method := $.Interface.Methods }} + m.Minimock{{$method.Name}}Inspect() + {{ end -}} + } + }) +} + +// MinimockWait waits for all mocked methods to be called the expected number of times +func (m *{{$mock}}{{(paramsRef)}}) 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 *{{$mock}}{{(paramsRef)}}) minimockDone() bool { + done := true + return done {{ range $method := $.Interface.Methods }}&& + m.Minimock{{$method.Name}}Done(){{end -}} +} \ No newline at end of file diff --git a/internal/templates/header.tmpl b/internal/templates/header.tmpl new file mode 100644 index 0000000..4f265a8 --- /dev/null +++ b/internal/templates/header.tmpl @@ -0,0 +1,20 @@ +{{ $packageName := $.Package.Name }} +{{if $.Options.HeaderVars.PackageName }} + {{ $packageName = $.Options.HeaderVars.PackageName }} +{{end}} + +// Code generated by http://github.com/gojuno/minimock ({{$.Options.HeaderVars.Version}}). DO NOT EDIT. + +package {{$packageName}} + +{{if $.Options.HeaderVars.GenerateInstruction}} +//go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} +{{end}} + +import ( + {{range $import := $.Options.Imports}}{{- if not (in $import "\"time\"" "\"sync/atomic\"" "\"github.com/gojuno/minimock/v3\"")}} + {{$import}}{{end}}{{end}} + mm_atomic "sync/atomic" + mm_time "time" + "github.com/gojuno/minimock/v3" +) \ No newline at end of file diff --git a/mock_controller.go b/mock_controller.go index 60d8d4d..7bce5b1 100644 --- a/mock_controller.go +++ b/mock_controller.go @@ -19,6 +19,7 @@ type Tester interface { Errorf(format string, args ...interface{}) FailNow() Cleanup(f func()) + Helper() } // MockController can be passed to mocks generated by minimock diff --git a/mock_controller_test.go b/mock_controller_test.go index e59d487..f22a2f5 100644 --- a/mock_controller_test.go +++ b/mock_controller_test.go @@ -85,3 +85,7 @@ type unsafeTester struct { func (u *unsafeTester) Fatal(...interface{}) { u.finished = true } + +func (u *unsafeTester) Helper() { + +} diff --git a/safe_tester.go b/safe_tester.go index 8c84a40..2ebe2e9 100644 --- a/safe_tester.go +++ b/safe_tester.go @@ -15,6 +15,7 @@ func newSafeTester(t Tester) *safeTester { func (st *safeTester) Error(args ...interface{}) { st.m.Lock() defer st.m.Unlock() + st.Tester.Helper() st.Tester.Error(args...) } @@ -23,6 +24,7 @@ func (st *safeTester) Error(args ...interface{}) { func (st *safeTester) Errorf(format string, args ...interface{}) { st.m.Lock() defer st.m.Unlock() + st.Tester.Helper() st.Tester.Errorf(format, args...) } @@ -31,6 +33,7 @@ func (st *safeTester) Errorf(format string, args ...interface{}) { func (st *safeTester) Fatal(args ...interface{}) { st.m.Lock() defer st.m.Unlock() + st.Tester.Helper() st.Tester.Fatal(args...) } @@ -39,6 +42,7 @@ func (st *safeTester) Fatal(args ...interface{}) { func (st *safeTester) Fatalf(format string, args ...interface{}) { st.m.Lock() defer st.m.Unlock() + st.Tester.Helper() st.Tester.Fatalf(format, args...) } diff --git a/template.go b/template.go index 375700a..6730bb9 100644 --- a/template.go +++ b/template.go @@ -1,415 +1,13 @@ package minimock -const ( - // HeaderTemplate is used to generate package clause and go:generate instruction - HeaderTemplate = ` - {{ $packageName := $.Package.Name }} - {{if $.Options.HeaderVars.PackageName }} - {{ $packageName = $.Options.HeaderVars.PackageName }} - {{end}} +import _ "embed" - // Code generated by http://github.com/gojuno/minimock ({{$.Options.HeaderVars.Version}}). DO NOT EDIT. +// HeaderTemplate is used to generate package clause and go:generate instruction +// +//go:embed internal/templates/header.tmpl +var HeaderTemplate string - package {{$packageName}} - - {{if $.Options.HeaderVars.GenerateInstruction}} - //go:generate minimock -i {{$.SourcePackage.PkgPath}}.{{$.Options.InterfaceName}} -o {{$.Options.HeaderVars.OutputFile}} -n {{(title (index $.Vars "MockName"))}} -p {{ $packageName }} - {{end}} - - import ( - {{range $import := $.Options.Imports}}{{- if not (in $import "\"time\"" "\"sync/atomic\"" "\"github.com/gojuno/minimock/v3\"")}} - {{$import}}{{end}}{{end}} - {{$.Options.SourcePackageAlias}} "{{$.SourcePackage.PkgPath}}" - mm_atomic "sync/atomic" - mm_time "time" - "github.com/gojuno/minimock/v3" - ) - ` - - // BodyTemplate is used to generate mock body - BodyTemplate = ` - {{ $mock := (title (index $.Vars "MockName")) }} - - // {{$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}}) - after{{$method.Name}}Counter uint64 - before{{$method.Name}}Counter uint64 - {{$method.Name}}Mock m{{$mock}}{{$method.Name}}{{(paramsRef)}} - {{ end }} - } - - // 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) - } - {{ range $method := $.Interface.Methods }} - 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 - } - - {{ range $method := $.Interface.Methods }} - {{ $m := (printf "mm%s" $method.Name) }} - - type m{{$mock}}{{$method.Name}}{{(params)}} struct { - optional bool - mock *{{$mock}}{{(paramsRef)}} - defaultExpectation *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}} - expectations []*{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}} - {{ if $method.HasParams }} - callArgs []*{{$mock}}{{$method.Name}}Params{{(paramsRef)}} - mutex sync.RWMutex - {{ end }} - - expectedInvocations uint64 - } - - // {{$mock}}{{$method.Name}}Expectation specifies expectation struct of the {{$.Interface.Name}}.{{$method.Name}} - type {{$mock}}{{$method.Name}}Expectation{{(params)}} struct { - mock *{{$mock}}{{(paramsRef)}} - {{ if $method.HasParams }} params *{{$mock}}{{$method.Name}}Params{{(paramsRef)}} {{end}} - {{ if $method.HasParams }} paramPtrs *{{$mock}}{{$method.Name}}ParamPtrs{{(paramsRef)}} {{end}} - {{ if $method.HasResults }} results *{{$mock}}{{$method.Name}}Results{{(paramsRef)}} {{end}} - Counter uint64 - } - - {{if $method.HasParams }} - // {{$mock}}{{$method.Name}}Params contains parameters of the {{$.Interface.Name}}.{{$method.Name}} - type {{$mock}}{{$method.Name}}Params{{(params)}} {{$method.ParamsStruct}} - {{end}} - - {{if $method.HasParams }} - // {{$mock}}{{$method.Name}}ParamPtrs contains pointers to parameters of the {{$.Interface.Name}}.{{$method.Name}} - type {{$mock}}{{$method.Name}}ParamPtrs{{(params)}} {{$method.ParamPtrsStruct}} - {{end}} - - {{if $method.HasResults }} - // {{$mock}}{{$method.Name}}Results contains results of the {{$.Interface.Name}}.{{$method.Name}} - type {{$mock}}{{$method.Name}}Results{{(params)}} {{$method.ResultsStruct}} - {{end}} - - // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning - // the test will fail minimock's automatic final call check if the mocked method was not called at least once. - // Optional() makes method check to work in '0 or more' mode. - // It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to - // catch the problems when the expected method call is totally skipped during test run. - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Optional() *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { - {{$m}}.optional = true - return {{$m}} - } - - // Expect sets up expected params for {{$.Interface.Name}}.{{$method.Name}} - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Expect({{$method.Params}}) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { - if {{$m}}.mock.func{{$method.Name}} != nil { - {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") - } - - if {{$m}}.defaultExpectation == nil { - {{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{} - } - - {{if $method.HasParams }} - if {{$m}}.defaultExpectation.paramPtrs != nil { - {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by ExpectParams functions") - } - - {{$m}}.defaultExpectation.params = &{{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{ $method.ParamsNames }} } - for _, e := range {{$m}}.expectations { - if minimock.Equal(e.params, {{$m}}.defaultExpectation.params) { - {{$m}}.mock.t.Fatalf("Expectation set by When has same params: %#v", *{{$m}}.defaultExpectation.params) - } - } - {{end}} - return {{$m}} - } - - {{ range $idx, $param := $method.Params }} - // Expect{{ $param.Name | title }}Param{{$idx | inc}} sets up expected param {{ $param.Name }} for {{$.Interface.Name}}.{{$method.Name}} - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Expect{{ $param.Name | title }}Param{{$idx | inc}}({{$param.Name}} {{$param.Type}}) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { - if {{$m}}.mock.func{{$method.Name}} != nil { - {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") - } - - if {{$m}}.defaultExpectation == nil { - {{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{} - } - - if {{$m}}.defaultExpectation.params != nil { - {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Expect") - } - - if {{$m}}.defaultExpectation.paramPtrs == nil { - {{$m}}.defaultExpectation.paramPtrs = &{{$mock}}{{$method.Name}}ParamPtrs{{(paramsRef)}}{} - } - {{$m}}.defaultExpectation.paramPtrs.{{$param.Name}} = &{{$param.Name}} - - return {{$m}} - } - {{ end }} - // Inspect accepts an inspector function that has same arguments as the {{$.Interface.Name}}.{{$method.Name}} - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Inspect(f func({{$method.Params}})) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { - if {{$m}}.mock.inspectFunc{{$method.Name}} != nil { - {{$m}}.mock.t.Fatalf("Inspect function is already set for {{$mock}}.{{$method.Name}}") - } - - {{$m}}.mock.inspectFunc{{$method.Name}} = f - - return {{$m}} - } - - // Return sets up results that will be returned by {{$.Interface.Name}}.{{$method.Name}} - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Return({{$method.Results}}) *{{$mock}}{{(paramsRef)}} { - if {{$m}}.mock.func{{$method.Name}} != nil { - {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") - } - - if {{$m}}.defaultExpectation == nil { - {{$m}}.defaultExpectation = &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{mock: {{$m}}.mock} - } - {{if $method.HasResults }} {{$m}}.defaultExpectation.results = &{{$mock}}{{$method.Name}}Results{{(paramsRef)}}{ {{ $method.ResultsNames }} } {{end}} - return {{$m}}.mock - } - - // Set uses given function f to mock the {{$.Interface.Name}}.{{$method.Name}} method - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Set(f func{{$method.Signature}}) *{{$mock}}{{(paramsRef)}}{ - if {{$m}}.defaultExpectation != nil { - {{$m}}.mock.t.Fatalf("Default expectation is already set for the {{$.Interface.Name}}.{{$method.Name}} method") - } - - if len({{$m}}.expectations) > 0 { - {{$m}}.mock.t.Fatalf("Some expectations are already set for the {{$.Interface.Name}}.{{$method.Name}} method") - } - - {{$m}}.mock.func{{$method.Name}}= f - return {{$m}}.mock - } - - {{if (and $method.HasParams $method.HasResults)}} - // When sets expectation for the {{$.Interface.Name}}.{{$method.Name}} which will trigger the result defined by the following - // Then helper - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) When({{$method.Params}}) *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}} { - if {{$m}}.mock.func{{$method.Name}} != nil { - {{$m}}.mock.t.Fatalf("{{$mock}}.{{$method.Name}} mock is already set by Set") - } - - expectation := &{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}{ - mock: {{$m}}.mock, - params: &{{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{ $method.ParamsNames }} }, - } - {{$m}}.expectations = append({{$m}}.expectations, expectation) - return expectation - } - - // Then sets up {{$.Interface.Name}}.{{$method.Name}} return parameters for the expectation previously defined by the When method - func (e *{{$mock}}{{$method.Name}}Expectation{{(paramsRef)}}) Then({{$method.Results}}) *{{$mock}}{{(paramsRef)}} { - e.results = &{{$mock}}{{$method.Name}}Results{{(paramsRef)}}{ {{ $method.ResultsNames }} } - return e.mock - } - {{end}} - - // Times sets number of times {{$.Interface.Name}}.{{$method.Name}} should be invoked - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Times(n uint64) *m{{$mock}}{{$method.Name}}{{(paramsRef)}} { - if n == 0 { - {{$m}}.mock.t.Fatalf("Times of {{$mock}}.{{$method.Name}} mock can not be zero") - } - mm_atomic.StoreUint64(&{{$m}}.expectedInvocations, n) - return {{$m}} - } - - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) invocationsDone() bool { - if len({{$m}}.expectations) == 0 && {{$m}}.defaultExpectation == nil && {{$m}}.mock.func{{$method.Name}} == nil { - return true - } - - totalInvocations := mm_atomic.LoadUint64(&{{$m}}.mock.after{{$method.Name}}Counter) - expectedInvocations := mm_atomic.LoadUint64(&{{$m}}.expectedInvocations) - - return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) - } - - // {{$method.Name}} implements {{$.Interface.Type}} - func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Declaration}} { - mm_atomic.AddUint64(&{{$m}}.before{{$method.Name}}Counter, 1) - defer mm_atomic.AddUint64(&{{$m}}.after{{$method.Name}}Counter, 1) - - if {{$m}}.inspectFunc{{$method.Name}} != nil { - {{$m}}.inspectFunc{{$method.Name}}({{$method.Params.Pass}}) - } - - {{if $method.HasParams}} - 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.mutex.Unlock() - - for _, e := range {{$m}}.{{$method.Name}}Mock.expectations { - if minimock.Equal(*e.params, mm_params) { - mm_atomic.AddUint64(&e.Counter, 1) - {{$method.ReturnStruct "e.results" -}} - } - } - {{end}} - - if {{$m}}.{{$method.Name}}Mock.defaultExpectation != nil { - mm_atomic.AddUint64(&{{$m}}.{{$method.Name}}Mock.defaultExpectation.Counter, 1) - {{- if $method.HasParams }} - mm_want := {{$m}}.{{$method.Name}}Mock.defaultExpectation.params - mm_want_ptrs := {{$m}}.{{$method.Name}}Mock.defaultExpectation.paramPtrs - - mm_got := {{$mock}}{{$method.Name}}Params{{(paramsRef)}}{ {{$method.ParamsNames}} } - - if mm_want_ptrs != nil { - {{ range $idx, $param := $method.Params }} - if mm_want_ptrs.{{$param.Name}} != nil && !minimock.Equal(*mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}}) { - {{$m}}.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameter {{$param.Name}}, want: %#v, got: %#v%s\n", *mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}}, minimock.Diff(*mm_want_ptrs.{{$param.Name}}, mm_got.{{$param.Name}})) - } - {{ end }} - } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - {{$m}}.t.Errorf("{{$mock}}.{{$method.Name}} got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) - } - {{ end }} - {{if $method.HasResults }} - mm_results := {{$m}}.{{$method.Name}}Mock.defaultExpectation.results - if mm_results == nil { - {{$m}}.t.Fatal("No results are set for the {{$mock}}.{{$method.Name}}") - } - {{$method.ReturnStruct "(*mm_results)" -}} - {{else}} - return - {{ end }} - } - if {{$m}}.func{{$method.Name}} != nil { - {{$method.Pass (printf "%s.func" $m)}} - } - {{$m}}.t.Fatalf("Unexpected call to {{$mock}}.{{$method.Name}}.{{range $method.Params}} %v{{end}}", {{ $method.ParamsNames }} ) - {{if $method.HasResults}}return{{end}} - } - - // {{$method.Name}}AfterCounter returns a count of finished {{$mock}}.{{$method.Name}} invocations - func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Name}}AfterCounter() uint64 { - return mm_atomic.LoadUint64(&{{$m}}.after{{$method.Name}}Counter) - } - - // {{$method.Name}}BeforeCounter returns a count of {{$mock}}.{{$method.Name}} invocations - func ({{$m}} *{{$mock}}{{(paramsRef)}}) {{$method.Name}}BeforeCounter() uint64 { - return mm_atomic.LoadUint64(&{{$m}}.before{{$method.Name}}Counter) - } - - {{ if $method.HasParams }} - // Calls returns a list of arguments used in each call to {{$mock}}.{{$method.Name}}. - // The list is in the same order as the calls were made (i.e. recent calls have a higher index) - func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Calls() []*{{$mock}}{{$method.Name}}Params{{(paramsRef)}} { - {{$m}}.mutex.RLock() - - argCopy := make([]*{{$mock}}{{$method.Name}}Params{{(paramsRef)}}, len({{$m}}.callArgs)) - copy(argCopy, {{$m}}.callArgs) - - {{$m}}.mutex.RUnlock() - - return argCopy - } - {{ end }} - - // Minimock{{$method.Name}}Done returns true if the count of the {{$method.Name}} invocations corresponds - // the number of defined expectations - func (m *{{$mock}}{{(paramsRef)}}) Minimock{{$method.Name}}Done() bool { - if m.{{$method.Name}}Mock.optional { - // Optional methods provide '0 or more' call count restriction. - return true - } - - for _, e := range m.{{$method.Name}}Mock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - return false - } - } - - return m.{{$method.Name}}Mock.invocationsDone() - } - - // Minimock{{$method.Name}}Inspect logs each unmet expectation - func (m *{{$mock}}{{(paramsRef)}}) Minimock{{$method.Name}}Inspect() { - for _, e := range m.{{$method.Name}}Mock.expectations { - if mm_atomic.LoadUint64(&e.Counter) < 1 { - {{- if $method.HasParams}} - m.t.Errorf("Expected call to {{$mock}}.{{$method.Name}} with params: %#v", *e.params) - {{else}} - m.t.Error("Expected call to {{$mock}}.{{$method.Name}}") - {{end -}} - } - } - - after{{$method.Name}}Counter := mm_atomic.LoadUint64(&m.after{{$method.Name}}Counter) - // if default expectation was set then invocations count should be greater than zero - if m.{{$method.Name}}Mock.defaultExpectation != nil && after{{$method.Name}}Counter < 1 { - {{- if $method.HasParams}} - if m.{{$method.Name}}Mock.defaultExpectation.params == nil { - m.t.Error("Expected call to {{$mock}}.{{$method.Name}}") - } else { - m.t.Errorf("Expected call to {{$mock}}.{{$method.Name}} with params: %#v", *m.{{$method.Name}}Mock.defaultExpectation.params) - } - {{else}} - m.t.Error("Expected call to {{$mock}}.{{$method.Name}}") - {{end -}} - } - // if func was set then invocations count should be greater than zero - if m.func{{$method.Name}} != nil && after{{$method.Name}}Counter < 1 { - m.t.Error("Expected call to {{$mock}}.{{$method.Name}}") - } - - if !m.{{$method.Name}}Mock.invocationsDone() && after{{$method.Name}}Counter > 0 { - m.t.Errorf("Expected %d calls to {{$mock}}.{{$method.Name}} but found %d calls", - mm_atomic.LoadUint64(&m.{{$method.Name}}Mock.expectedInvocations), after{{$method.Name}}Counter) - } - } - {{end}} - - // MinimockFinish checks that all mocked methods have been called the expected number of times - func (m *{{$mock}}{{(paramsRef)}}) MinimockFinish() { - m.finishOnce.Do(func() { - if !m.minimockDone() { - {{- range $method := $.Interface.Methods }} - m.Minimock{{$method.Name}}Inspect() - {{ end -}} - } - }) - } - - // MinimockWait waits for all mocked methods to be called the expected number of times - func (m *{{$mock}}{{(paramsRef)}}) 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 *{{$mock}}{{(paramsRef)}}) minimockDone() bool { - done := true - return done {{ range $method := $.Interface.Methods }}&& - m.Minimock{{$method.Name}}Done(){{end -}} - } - ` -) +// BodyTemplate is used to generate mock body +// +//go:embed internal/templates/body.tmpl +var BodyTemplate string diff --git a/tests/actor_mock.go b/tests/actor_mock.go index 9994373..a3b0b4b 100644 --- a/tests/actor_mock.go +++ b/tests/actor_mock.go @@ -18,6 +18,7 @@ type ActorMock struct { finishOnce sync.Once funcAction func(firstParam string, secondParam int) (i1 int, err error) + funcActionOrigin string inspectFuncAction func(firstParam string, secondParam int) afterActionCounter uint64 beforeActionCounter uint64 @@ -49,16 +50,19 @@ type mActorMockAction struct { callArgs []*ActorMockActionParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // ActorMockActionExpectation specifies expectation struct of the actor.Action type ActorMockActionExpectation struct { - mock *ActorMock - params *ActorMockActionParams - paramPtrs *ActorMockActionParamPtrs - results *ActorMockActionResults - Counter uint64 + mock *ActorMock + params *ActorMockActionParams + paramPtrs *ActorMockActionParamPtrs + expectationOrigins ActorMockActionExpectationOrigins + results *ActorMockActionResults + returnOrigin string + Counter uint64 } // ActorMockActionParams contains parameters of the actor.Action @@ -79,6 +83,13 @@ type ActorMockActionResults struct { err error } +// ActorMockActionOrigins contains origins of expectations of the actor.Action +type ActorMockActionExpectationOrigins struct { + origin string + originFirstParam string + originSecondParam string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -104,6 +115,7 @@ func (mmAction *mActorMockAction) Expect(firstParam string, secondParam int) *mA } mmAction.defaultExpectation.params = &ActorMockActionParams{firstParam, secondParam} + mmAction.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) for _, e := range mmAction.expectations { if minimock.Equal(e.params, mmAction.defaultExpectation.params) { mmAction.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmAction.defaultExpectation.params) @@ -131,6 +143,7 @@ func (mmAction *mActorMockAction) ExpectFirstParamParam1(firstParam string) *mAc mmAction.defaultExpectation.paramPtrs = &ActorMockActionParamPtrs{} } mmAction.defaultExpectation.paramPtrs.firstParam = &firstParam + mmAction.defaultExpectation.expectationOrigins.originFirstParam = minimock.CallerInfo(1) return mmAction } @@ -153,6 +166,7 @@ func (mmAction *mActorMockAction) ExpectSecondParamParam2(secondParam int) *mAct mmAction.defaultExpectation.paramPtrs = &ActorMockActionParamPtrs{} } mmAction.defaultExpectation.paramPtrs.secondParam = &secondParam + mmAction.defaultExpectation.expectationOrigins.originSecondParam = minimock.CallerInfo(1) return mmAction } @@ -178,6 +192,7 @@ func (mmAction *mActorMockAction) Return(i1 int, err error) *ActorMock { mmAction.defaultExpectation = &ActorMockActionExpectation{mock: mmAction.mock} } mmAction.defaultExpectation.results = &ActorMockActionResults{i1, err} + mmAction.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmAction.mock } @@ -192,6 +207,7 @@ func (mmAction *mActorMockAction) Set(f func(firstParam string, secondParam int) } mmAction.mock.funcAction = f + mmAction.mock.funcActionOrigin = minimock.CallerInfo(1) return mmAction.mock } @@ -203,8 +219,9 @@ func (mmAction *mActorMockAction) When(firstParam string, secondParam int) *Acto } expectation := &ActorMockActionExpectation{ - mock: mmAction.mock, - params: &ActorMockActionParams{firstParam, secondParam}, + mock: mmAction.mock, + params: &ActorMockActionParams{firstParam, secondParam}, + expectationOrigins: ActorMockActionExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmAction.expectations = append(mmAction.expectations, expectation) return expectation @@ -222,6 +239,7 @@ func (mmAction *mActorMockAction) Times(n uint64) *mActorMockAction { mmAction.mock.t.Fatalf("Times of ActorMock.Action mock can not be zero") } mm_atomic.StoreUint64(&mmAction.expectedInvocations, n) + mmAction.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmAction } @@ -241,6 +259,8 @@ func (mmAction *ActorMock) Action(firstParam string, secondParam int) (i1 int, e mm_atomic.AddUint64(&mmAction.beforeActionCounter, 1) defer mm_atomic.AddUint64(&mmAction.afterActionCounter, 1) + mmAction.t.Helper() + if mmAction.inspectFuncAction != nil { mmAction.inspectFuncAction(firstParam, secondParam) } @@ -269,15 +289,18 @@ func (mmAction *ActorMock) Action(firstParam string, secondParam int) (i1 int, e if mm_want_ptrs != nil { if mm_want_ptrs.firstParam != nil && !minimock.Equal(*mm_want_ptrs.firstParam, mm_got.firstParam) { - mmAction.t.Errorf("ActorMock.Action got unexpected parameter firstParam, want: %#v, got: %#v%s\n", *mm_want_ptrs.firstParam, mm_got.firstParam, minimock.Diff(*mm_want_ptrs.firstParam, mm_got.firstParam)) + mmAction.t.Errorf("ActorMock.Action got unexpected parameter firstParam, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAction.ActionMock.defaultExpectation.expectationOrigins.originFirstParam, *mm_want_ptrs.firstParam, mm_got.firstParam, minimock.Diff(*mm_want_ptrs.firstParam, mm_got.firstParam)) } if mm_want_ptrs.secondParam != nil && !minimock.Equal(*mm_want_ptrs.secondParam, mm_got.secondParam) { - mmAction.t.Errorf("ActorMock.Action got unexpected parameter secondParam, want: %#v, got: %#v%s\n", *mm_want_ptrs.secondParam, mm_got.secondParam, minimock.Diff(*mm_want_ptrs.secondParam, mm_got.secondParam)) + mmAction.t.Errorf("ActorMock.Action got unexpected parameter secondParam, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAction.ActionMock.defaultExpectation.expectationOrigins.originSecondParam, *mm_want_ptrs.secondParam, mm_got.secondParam, minimock.Diff(*mm_want_ptrs.secondParam, mm_got.secondParam)) } } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmAction.t.Errorf("ActorMock.Action got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + mmAction.t.Errorf("ActorMock.Action got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAction.ActionMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmAction.ActionMock.defaultExpectation.results @@ -337,7 +360,7 @@ func (m *ActorMock) MinimockActionDone() bool { func (m *ActorMock) MinimockActionInspect() { for _, e := range m.ActionMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to ActorMock.Action with params: %#v", *e.params) + m.t.Errorf("Expected call to ActorMock.Action at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -345,19 +368,19 @@ func (m *ActorMock) MinimockActionInspect() { // if default expectation was set then invocations count should be greater than zero if m.ActionMock.defaultExpectation != nil && afterActionCounter < 1 { if m.ActionMock.defaultExpectation.params == nil { - m.t.Error("Expected call to ActorMock.Action") + m.t.Errorf("Expected call to ActorMock.Action at\n%s", m.ActionMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to ActorMock.Action with params: %#v", *m.ActionMock.defaultExpectation.params) + m.t.Errorf("Expected call to ActorMock.Action at\n%s with params: %#v", m.ActionMock.defaultExpectation.expectationOrigins.origin, *m.ActionMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcAction != nil && afterActionCounter < 1 { - m.t.Error("Expected call to ActorMock.Action") + m.t.Errorf("Expected call to ActorMock.Action at\n%s", m.funcActionOrigin) } if !m.ActionMock.invocationsDone() && afterActionCounter > 0 { - m.t.Errorf("Expected %d calls to ActorMock.Action but found %d calls", - mm_atomic.LoadUint64(&m.ActionMock.expectedInvocations), afterActionCounter) + m.t.Errorf("Expected %d calls to ActorMock.Action at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.ActionMock.expectedInvocations), m.ActionMock.expectedInvocationsOrigin, afterActionCounter) } } diff --git a/tests/actor_mock_test.go b/tests/actor_mock_test.go index 1f0a0ea..81bf401 100644 --- a/tests/actor_mock_test.go +++ b/tests/actor_mock_test.go @@ -8,7 +8,7 @@ import ( func TestActorMock_TestPassedWithBothExpectedParams(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() mock := NewActorMock(tester). ActionMock.ExpectFirstParamParam1("abc"). @@ -21,7 +21,7 @@ func TestActorMock_TestPassedWithBothExpectedParams(t *testing.T) { func TestActorMock_TestPassedWithOneExpectedParams(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() mock := NewActorMock(tester). ActionMock.ExpectFirstParamParam1("abc").Return(1, nil) @@ -33,11 +33,14 @@ func TestActorMock_TestPassedWithOneExpectedParams(t *testing.T) { func TestActorMock_TestFailedWithExpectedParams(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() - tester.ErrorfMock. - Expect("ActorMock.Action got unexpected parameter secondParam, want: %#v, got: %#v%s\n", 24, 25, ""). - Return() - + tester.CleanupMock.Return().HelperMock.Return() + tester.ErrorfMock.Set(func(format string, args ...interface{}) { + assert.Equal(t, "ActorMock.Action got unexpected parameter secondParam, expected at\n%s:\nwant: %#v\n got: %#v%s\n", format) + + assert.Equal(t, 24, args[1]) + assert.Equal(t, 25, args[2]) + assert.Equal(t, "", args[3]) + }) mock := NewActorMock(tester). ActionMock.ExpectFirstParamParam1("abc"). ExpectSecondParamParam2(24).Return(1, nil) diff --git a/tests/context_accepter_mock.go b/tests/context_accepter_mock.go index b1322f9..c6bd5f4 100644 --- a/tests/context_accepter_mock.go +++ b/tests/context_accepter_mock.go @@ -19,18 +19,21 @@ type ContextAccepterMock struct { finishOnce sync.Once funcAcceptContext func(ctx context.Context) + funcAcceptContextOrigin string inspectFuncAcceptContext func(ctx context.Context) afterAcceptContextCounter uint64 beforeAcceptContextCounter uint64 AcceptContextMock mContextAccepterMockAcceptContext funcAcceptContextWithOtherArgs func(ctx context.Context, i1 int) (i2 int, err error) + funcAcceptContextWithOtherArgsOrigin string inspectFuncAcceptContextWithOtherArgs func(ctx context.Context, i1 int) afterAcceptContextWithOtherArgsCounter uint64 beforeAcceptContextWithOtherArgsCounter uint64 AcceptContextWithOtherArgsMock mContextAccepterMockAcceptContextWithOtherArgs funcAcceptContextWithStructArgs func(ctx context.Context, s1 structArg) (i1 int, err error) + funcAcceptContextWithStructArgsOrigin string inspectFuncAcceptContextWithStructArgs func(ctx context.Context, s1 structArg) afterAcceptContextWithStructArgsCounter uint64 beforeAcceptContextWithStructArgsCounter uint64 @@ -68,16 +71,19 @@ type mContextAccepterMockAcceptContext struct { callArgs []*ContextAccepterMockAcceptContextParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // ContextAccepterMockAcceptContextExpectation specifies expectation struct of the contextAccepter.AcceptContext type ContextAccepterMockAcceptContextExpectation struct { - mock *ContextAccepterMock - params *ContextAccepterMockAcceptContextParams - paramPtrs *ContextAccepterMockAcceptContextParamPtrs + mock *ContextAccepterMock + params *ContextAccepterMockAcceptContextParams + paramPtrs *ContextAccepterMockAcceptContextParamPtrs + expectationOrigins ContextAccepterMockAcceptContextExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // ContextAccepterMockAcceptContextParams contains parameters of the contextAccepter.AcceptContext @@ -90,6 +96,12 @@ type ContextAccepterMockAcceptContextParamPtrs struct { ctx *context.Context } +// ContextAccepterMockAcceptContextOrigins contains origins of expectations of the contextAccepter.AcceptContext +type ContextAccepterMockAcceptContextExpectationOrigins struct { + origin string + originCtx string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -115,6 +127,7 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) Expect(ctx context.Con } mmAcceptContext.defaultExpectation.params = &ContextAccepterMockAcceptContextParams{ctx} + mmAcceptContext.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -142,6 +155,7 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) ExpectCtxParam1(ctx co mmAcceptContext.defaultExpectation.paramPtrs = &ContextAccepterMockAcceptContextParamPtrs{} } mmAcceptContext.defaultExpectation.paramPtrs.ctx = &ctx + mmAcceptContext.defaultExpectation.expectationOrigins.originCtx = minimock.CallerInfo(1) return mmAcceptContext } @@ -167,6 +181,7 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) Return() *ContextAccep mmAcceptContext.defaultExpectation = &ContextAccepterMockAcceptContextExpectation{mock: mmAcceptContext.mock} } + mmAcceptContext.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmAcceptContext.mock } @@ -181,6 +196,7 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) Set(f func(ctx context } mmAcceptContext.mock.funcAcceptContext = f + mmAcceptContext.mock.funcAcceptContextOrigin = minimock.CallerInfo(1) return mmAcceptContext.mock } @@ -190,6 +206,7 @@ func (mmAcceptContext *mContextAccepterMockAcceptContext) Times(n uint64) *mCont mmAcceptContext.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContext mock can not be zero") } mm_atomic.StoreUint64(&mmAcceptContext.expectedInvocations, n) + mmAcceptContext.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmAcceptContext } @@ -209,6 +226,8 @@ func (mmAcceptContext *ContextAccepterMock) AcceptContext(ctx context.Context) { mm_atomic.AddUint64(&mmAcceptContext.beforeAcceptContextCounter, 1) defer mm_atomic.AddUint64(&mmAcceptContext.afterAcceptContextCounter, 1) + mmAcceptContext.t.Helper() + if mmAcceptContext.inspectFuncAcceptContext != nil { mmAcceptContext.inspectFuncAcceptContext(ctx) } @@ -237,11 +256,13 @@ func (mmAcceptContext *ContextAccepterMock) AcceptContext(ctx context.Context) { if mm_want_ptrs != nil { if mm_want_ptrs.ctx != nil && !minimock.Equal(*mm_want_ptrs.ctx, mm_got.ctx) { - mmAcceptContext.t.Errorf("ContextAccepterMock.AcceptContext got unexpected parameter ctx, want: %#v, got: %#v%s\n", *mm_want_ptrs.ctx, mm_got.ctx, minimock.Diff(*mm_want_ptrs.ctx, mm_got.ctx)) + mmAcceptContext.t.Errorf("ContextAccepterMock.AcceptContext got unexpected parameter ctx, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContext.AcceptContextMock.defaultExpectation.expectationOrigins.originCtx, *mm_want_ptrs.ctx, mm_got.ctx, minimock.Diff(*mm_want_ptrs.ctx, mm_got.ctx)) } } else 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)) + mmAcceptContext.t.Errorf("ContextAccepterMock.AcceptContext got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContext.AcceptContextMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -299,7 +320,7 @@ func (m *ContextAccepterMock) MinimockAcceptContextDone() bool { 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) + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContext at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -307,19 +328,19 @@ func (m *ContextAccepterMock) MinimockAcceptContextInspect() { // if default expectation was set then invocations count should be greater than zero if m.AcceptContextMock.defaultExpectation != nil && afterAcceptContextCounter < 1 { if m.AcceptContextMock.defaultExpectation.params == nil { - m.t.Error("Expected call to ContextAccepterMock.AcceptContext") + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContext at\n%s", m.AcceptContextMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to ContextAccepterMock.AcceptContext with params: %#v", *m.AcceptContextMock.defaultExpectation.params) + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContext at\n%s with params: %#v", m.AcceptContextMock.defaultExpectation.expectationOrigins.origin, *m.AcceptContextMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcAcceptContext != nil && afterAcceptContextCounter < 1 { - m.t.Error("Expected call to ContextAccepterMock.AcceptContext") + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContext at\n%s", m.funcAcceptContextOrigin) } if !m.AcceptContextMock.invocationsDone() && afterAcceptContextCounter > 0 { - m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContext but found %d calls", - mm_atomic.LoadUint64(&m.AcceptContextMock.expectedInvocations), afterAcceptContextCounter) + m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContext at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.AcceptContextMock.expectedInvocations), m.AcceptContextMock.expectedInvocationsOrigin, afterAcceptContextCounter) } } @@ -332,16 +353,19 @@ type mContextAccepterMockAcceptContextWithOtherArgs struct { callArgs []*ContextAccepterMockAcceptContextWithOtherArgsParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // ContextAccepterMockAcceptContextWithOtherArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithOtherArgs type ContextAccepterMockAcceptContextWithOtherArgsExpectation struct { - mock *ContextAccepterMock - params *ContextAccepterMockAcceptContextWithOtherArgsParams - paramPtrs *ContextAccepterMockAcceptContextWithOtherArgsParamPtrs - results *ContextAccepterMockAcceptContextWithOtherArgsResults - Counter uint64 + mock *ContextAccepterMock + params *ContextAccepterMockAcceptContextWithOtherArgsParams + paramPtrs *ContextAccepterMockAcceptContextWithOtherArgsParamPtrs + expectationOrigins ContextAccepterMockAcceptContextWithOtherArgsExpectationOrigins + results *ContextAccepterMockAcceptContextWithOtherArgsResults + returnOrigin string + Counter uint64 } // ContextAccepterMockAcceptContextWithOtherArgsParams contains parameters of the contextAccepter.AcceptContextWithOtherArgs @@ -362,6 +386,13 @@ type ContextAccepterMockAcceptContextWithOtherArgsResults struct { err error } +// ContextAccepterMockAcceptContextWithOtherArgsOrigins contains origins of expectations of the contextAccepter.AcceptContextWithOtherArgs +type ContextAccepterMockAcceptContextWithOtherArgsExpectationOrigins struct { + origin string + originCtx string + originI1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -387,6 +418,7 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr } mmAcceptContextWithOtherArgs.defaultExpectation.params = &ContextAccepterMockAcceptContextWithOtherArgsParams{ctx, i1} + mmAcceptContextWithOtherArgs.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -414,6 +446,7 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr mmAcceptContextWithOtherArgs.defaultExpectation.paramPtrs = &ContextAccepterMockAcceptContextWithOtherArgsParamPtrs{} } mmAcceptContextWithOtherArgs.defaultExpectation.paramPtrs.ctx = &ctx + mmAcceptContextWithOtherArgs.defaultExpectation.expectationOrigins.originCtx = minimock.CallerInfo(1) return mmAcceptContextWithOtherArgs } @@ -436,6 +469,7 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr mmAcceptContextWithOtherArgs.defaultExpectation.paramPtrs = &ContextAccepterMockAcceptContextWithOtherArgsParamPtrs{} } mmAcceptContextWithOtherArgs.defaultExpectation.paramPtrs.i1 = &i1 + mmAcceptContextWithOtherArgs.defaultExpectation.expectationOrigins.originI1 = minimock.CallerInfo(1) return mmAcceptContextWithOtherArgs } @@ -461,6 +495,7 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr mmAcceptContextWithOtherArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithOtherArgsExpectation{mock: mmAcceptContextWithOtherArgs.mock} } mmAcceptContextWithOtherArgs.defaultExpectation.results = &ContextAccepterMockAcceptContextWithOtherArgsResults{i2, err} + mmAcceptContextWithOtherArgs.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmAcceptContextWithOtherArgs.mock } @@ -475,6 +510,7 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr } mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgs = f + mmAcceptContextWithOtherArgs.mock.funcAcceptContextWithOtherArgsOrigin = minimock.CallerInfo(1) return mmAcceptContextWithOtherArgs.mock } @@ -486,8 +522,9 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr } expectation := &ContextAccepterMockAcceptContextWithOtherArgsExpectation{ - mock: mmAcceptContextWithOtherArgs.mock, - params: &ContextAccepterMockAcceptContextWithOtherArgsParams{ctx, i1}, + mock: mmAcceptContextWithOtherArgs.mock, + params: &ContextAccepterMockAcceptContextWithOtherArgsParams{ctx, i1}, + expectationOrigins: ContextAccepterMockAcceptContextWithOtherArgsExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmAcceptContextWithOtherArgs.expectations = append(mmAcceptContextWithOtherArgs.expectations, expectation) return expectation @@ -505,6 +542,7 @@ func (mmAcceptContextWithOtherArgs *mContextAccepterMockAcceptContextWithOtherAr mmAcceptContextWithOtherArgs.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContextWithOtherArgs mock can not be zero") } mm_atomic.StoreUint64(&mmAcceptContextWithOtherArgs.expectedInvocations, n) + mmAcceptContextWithOtherArgs.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmAcceptContextWithOtherArgs } @@ -524,6 +562,8 @@ func (mmAcceptContextWithOtherArgs *ContextAccepterMock) AcceptContextWithOtherA mm_atomic.AddUint64(&mmAcceptContextWithOtherArgs.beforeAcceptContextWithOtherArgsCounter, 1) defer mm_atomic.AddUint64(&mmAcceptContextWithOtherArgs.afterAcceptContextWithOtherArgsCounter, 1) + mmAcceptContextWithOtherArgs.t.Helper() + if mmAcceptContextWithOtherArgs.inspectFuncAcceptContextWithOtherArgs != nil { mmAcceptContextWithOtherArgs.inspectFuncAcceptContextWithOtherArgs(ctx, i1) } @@ -552,15 +592,18 @@ func (mmAcceptContextWithOtherArgs *ContextAccepterMock) AcceptContextWithOtherA if mm_want_ptrs != nil { if mm_want_ptrs.ctx != nil && !minimock.Equal(*mm_want_ptrs.ctx, mm_got.ctx) { - mmAcceptContextWithOtherArgs.t.Errorf("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameter ctx, want: %#v, got: %#v%s\n", *mm_want_ptrs.ctx, mm_got.ctx, minimock.Diff(*mm_want_ptrs.ctx, mm_got.ctx)) + mmAcceptContextWithOtherArgs.t.Errorf("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameter ctx, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation.expectationOrigins.originCtx, *mm_want_ptrs.ctx, mm_got.ctx, minimock.Diff(*mm_want_ptrs.ctx, mm_got.ctx)) } if mm_want_ptrs.i1 != nil && !minimock.Equal(*mm_want_ptrs.i1, mm_got.i1) { - mmAcceptContextWithOtherArgs.t.Errorf("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameter i1, want: %#v, got: %#v%s\n", *mm_want_ptrs.i1, mm_got.i1, minimock.Diff(*mm_want_ptrs.i1, mm_got.i1)) + mmAcceptContextWithOtherArgs.t.Errorf("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameter i1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation.expectationOrigins.originI1, *mm_want_ptrs.i1, mm_got.i1, minimock.Diff(*mm_want_ptrs.i1, mm_got.i1)) } } else 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)) + mmAcceptContextWithOtherArgs.t.Errorf("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmAcceptContextWithOtherArgs.AcceptContextWithOtherArgsMock.defaultExpectation.results @@ -620,7 +663,7 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsDone() bool { 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) + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -628,19 +671,19 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithOtherArgsInspect() { // if default expectation was set then invocations count should be greater than zero if m.AcceptContextWithOtherArgsMock.defaultExpectation != nil && afterAcceptContextWithOtherArgsCounter < 1 { if m.AcceptContextWithOtherArgsMock.defaultExpectation.params == nil { - m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs") + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs at\n%s", m.AcceptContextWithOtherArgsMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs with params: %#v", *m.AcceptContextWithOtherArgsMock.defaultExpectation.params) + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs at\n%s with params: %#v", m.AcceptContextWithOtherArgsMock.defaultExpectation.expectationOrigins.origin, *m.AcceptContextWithOtherArgsMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcAcceptContextWithOtherArgs != nil && afterAcceptContextWithOtherArgsCounter < 1 { - m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs") + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithOtherArgs at\n%s", m.funcAcceptContextWithOtherArgsOrigin) } if !m.AcceptContextWithOtherArgsMock.invocationsDone() && afterAcceptContextWithOtherArgsCounter > 0 { - m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContextWithOtherArgs but found %d calls", - mm_atomic.LoadUint64(&m.AcceptContextWithOtherArgsMock.expectedInvocations), afterAcceptContextWithOtherArgsCounter) + m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContextWithOtherArgs at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.AcceptContextWithOtherArgsMock.expectedInvocations), m.AcceptContextWithOtherArgsMock.expectedInvocationsOrigin, afterAcceptContextWithOtherArgsCounter) } } @@ -653,16 +696,19 @@ type mContextAccepterMockAcceptContextWithStructArgs struct { callArgs []*ContextAccepterMockAcceptContextWithStructArgsParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // ContextAccepterMockAcceptContextWithStructArgsExpectation specifies expectation struct of the contextAccepter.AcceptContextWithStructArgs type ContextAccepterMockAcceptContextWithStructArgsExpectation struct { - mock *ContextAccepterMock - params *ContextAccepterMockAcceptContextWithStructArgsParams - paramPtrs *ContextAccepterMockAcceptContextWithStructArgsParamPtrs - results *ContextAccepterMockAcceptContextWithStructArgsResults - Counter uint64 + mock *ContextAccepterMock + params *ContextAccepterMockAcceptContextWithStructArgsParams + paramPtrs *ContextAccepterMockAcceptContextWithStructArgsParamPtrs + expectationOrigins ContextAccepterMockAcceptContextWithStructArgsExpectationOrigins + results *ContextAccepterMockAcceptContextWithStructArgsResults + returnOrigin string + Counter uint64 } // ContextAccepterMockAcceptContextWithStructArgsParams contains parameters of the contextAccepter.AcceptContextWithStructArgs @@ -683,6 +729,13 @@ type ContextAccepterMockAcceptContextWithStructArgsResults struct { err error } +// ContextAccepterMockAcceptContextWithStructArgsOrigins contains origins of expectations of the contextAccepter.AcceptContextWithStructArgs +type ContextAccepterMockAcceptContextWithStructArgsExpectationOrigins struct { + origin string + originCtx string + originS1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -708,6 +761,7 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct } mmAcceptContextWithStructArgs.defaultExpectation.params = &ContextAccepterMockAcceptContextWithStructArgsParams{ctx, s1} + mmAcceptContextWithStructArgs.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) for _, e := range mmAcceptContextWithStructArgs.expectations { if minimock.Equal(e.params, mmAcceptContextWithStructArgs.defaultExpectation.params) { mmAcceptContextWithStructArgs.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmAcceptContextWithStructArgs.defaultExpectation.params) @@ -735,6 +789,7 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs = &ContextAccepterMockAcceptContextWithStructArgsParamPtrs{} } mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs.ctx = &ctx + mmAcceptContextWithStructArgs.defaultExpectation.expectationOrigins.originCtx = minimock.CallerInfo(1) return mmAcceptContextWithStructArgs } @@ -757,6 +812,7 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs = &ContextAccepterMockAcceptContextWithStructArgsParamPtrs{} } mmAcceptContextWithStructArgs.defaultExpectation.paramPtrs.s1 = &s1 + mmAcceptContextWithStructArgs.defaultExpectation.expectationOrigins.originS1 = minimock.CallerInfo(1) return mmAcceptContextWithStructArgs } @@ -782,6 +838,7 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct mmAcceptContextWithStructArgs.defaultExpectation = &ContextAccepterMockAcceptContextWithStructArgsExpectation{mock: mmAcceptContextWithStructArgs.mock} } mmAcceptContextWithStructArgs.defaultExpectation.results = &ContextAccepterMockAcceptContextWithStructArgsResults{i1, err} + mmAcceptContextWithStructArgs.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmAcceptContextWithStructArgs.mock } @@ -796,6 +853,7 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct } mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgs = f + mmAcceptContextWithStructArgs.mock.funcAcceptContextWithStructArgsOrigin = minimock.CallerInfo(1) return mmAcceptContextWithStructArgs.mock } @@ -807,8 +865,9 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct } expectation := &ContextAccepterMockAcceptContextWithStructArgsExpectation{ - mock: mmAcceptContextWithStructArgs.mock, - params: &ContextAccepterMockAcceptContextWithStructArgsParams{ctx, s1}, + mock: mmAcceptContextWithStructArgs.mock, + params: &ContextAccepterMockAcceptContextWithStructArgsParams{ctx, s1}, + expectationOrigins: ContextAccepterMockAcceptContextWithStructArgsExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmAcceptContextWithStructArgs.expectations = append(mmAcceptContextWithStructArgs.expectations, expectation) return expectation @@ -826,6 +885,7 @@ func (mmAcceptContextWithStructArgs *mContextAccepterMockAcceptContextWithStruct mmAcceptContextWithStructArgs.mock.t.Fatalf("Times of ContextAccepterMock.AcceptContextWithStructArgs mock can not be zero") } mm_atomic.StoreUint64(&mmAcceptContextWithStructArgs.expectedInvocations, n) + mmAcceptContextWithStructArgs.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmAcceptContextWithStructArgs } @@ -845,6 +905,8 @@ func (mmAcceptContextWithStructArgs *ContextAccepterMock) AcceptContextWithStruc mm_atomic.AddUint64(&mmAcceptContextWithStructArgs.beforeAcceptContextWithStructArgsCounter, 1) defer mm_atomic.AddUint64(&mmAcceptContextWithStructArgs.afterAcceptContextWithStructArgsCounter, 1) + mmAcceptContextWithStructArgs.t.Helper() + if mmAcceptContextWithStructArgs.inspectFuncAcceptContextWithStructArgs != nil { mmAcceptContextWithStructArgs.inspectFuncAcceptContextWithStructArgs(ctx, s1) } @@ -873,15 +935,18 @@ func (mmAcceptContextWithStructArgs *ContextAccepterMock) AcceptContextWithStruc if mm_want_ptrs != nil { if mm_want_ptrs.ctx != nil && !minimock.Equal(*mm_want_ptrs.ctx, mm_got.ctx) { - mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameter ctx, want: %#v, got: %#v%s\n", *mm_want_ptrs.ctx, mm_got.ctx, minimock.Diff(*mm_want_ptrs.ctx, mm_got.ctx)) + mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameter ctx, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.expectationOrigins.originCtx, *mm_want_ptrs.ctx, mm_got.ctx, minimock.Diff(*mm_want_ptrs.ctx, mm_got.ctx)) } if mm_want_ptrs.s1 != nil && !minimock.Equal(*mm_want_ptrs.s1, mm_got.s1) { - mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameter s1, want: %#v, got: %#v%s\n", *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) + mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameter s1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.expectationOrigins.originS1, *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) } } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + mmAcceptContextWithStructArgs.t.Errorf("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmAcceptContextWithStructArgs.AcceptContextWithStructArgsMock.defaultExpectation.results @@ -941,7 +1006,7 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsDone() bool { func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsInspect() { for _, e := range m.AcceptContextWithStructArgsMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs with params: %#v", *e.params) + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -949,19 +1014,19 @@ func (m *ContextAccepterMock) MinimockAcceptContextWithStructArgsInspect() { // if default expectation was set then invocations count should be greater than zero if m.AcceptContextWithStructArgsMock.defaultExpectation != nil && afterAcceptContextWithStructArgsCounter < 1 { if m.AcceptContextWithStructArgsMock.defaultExpectation.params == nil { - m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithStructArgs") + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs at\n%s", m.AcceptContextWithStructArgsMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs with params: %#v", *m.AcceptContextWithStructArgsMock.defaultExpectation.params) + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs at\n%s with params: %#v", m.AcceptContextWithStructArgsMock.defaultExpectation.expectationOrigins.origin, *m.AcceptContextWithStructArgsMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcAcceptContextWithStructArgs != nil && afterAcceptContextWithStructArgsCounter < 1 { - m.t.Error("Expected call to ContextAccepterMock.AcceptContextWithStructArgs") + m.t.Errorf("Expected call to ContextAccepterMock.AcceptContextWithStructArgs at\n%s", m.funcAcceptContextWithStructArgsOrigin) } if !m.AcceptContextWithStructArgsMock.invocationsDone() && afterAcceptContextWithStructArgsCounter > 0 { - m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs but found %d calls", - mm_atomic.LoadUint64(&m.AcceptContextWithStructArgsMock.expectedInvocations), afterAcceptContextWithStructArgsCounter) + m.t.Errorf("Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.AcceptContextWithStructArgsMock.expectedInvocations), m.AcceptContextWithStructArgsMock.expectedInvocationsOrigin, afterAcceptContextWithStructArgsCounter) } } diff --git a/tests/context_accepter_mock_test.go b/tests/context_accepter_mock_test.go index eca9e46..23b6eee 100644 --- a/tests/context_accepter_mock_test.go +++ b/tests/context_accepter_mock_test.go @@ -13,10 +13,10 @@ func TestContextAccepterMock_AnyContext(t *testing.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) + assert.Equal(t, "ContextAccepterMock.AcceptContext got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", s) mockCalled = true - }).CleanupMock.Return() + }).CleanupMock.Return().HelperMock.Return() mock := NewContextAccepterMock(tester). AcceptContextMock.Expect(context.Background()).Return() @@ -28,7 +28,7 @@ func TestContextAccepterMock_AnyContext(t *testing.T) { func TestContextAccepterMock_TodoContextMatchesAnycontext(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() mock := NewContextAccepterMock(tester). AcceptContextMock.Expect(minimock.AnyContext).Return() @@ -38,7 +38,7 @@ func TestContextAccepterMock_TodoContextMatchesAnycontext(t *testing.T) { func TestContextAccepterMock_WhenThenMatchAnycontext(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() mock := NewContextAccepterMock(tester). AcceptContextWithOtherArgsMock.When(minimock.AnyContext, 1).Then(42, nil) @@ -50,20 +50,22 @@ func TestContextAccepterMock_WhenThenMatchAnycontext(t *testing.T) { func TestContextAccepterMock_DiffWithoutAnyContext(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() - tester.ErrorfMock. - Expect("ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameters, want: %#v, got: %#v%s\n", - ContextAccepterMockAcceptContextWithOtherArgsParams{ - ctx: minimock.AnyContext, - i1: 24, - }, - ContextAccepterMockAcceptContextWithOtherArgsParams{ - ctx: context.Background(), - i1: 123, - }, - "\n\nDiff:\n--- Expected params\n+++ Actual params\n@@ -4,3 +4,3 @@\n },\n- i1: (int) 24\n+ i1: (int) 123\n }\n"). - Return() + tester.ErrorfMock.Set(func(format string, args ...interface{}) { + assert.Equal(t, "ContextAccepterMock.AcceptContextWithOtherArgs got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", format) + + assert.Equal(t, ContextAccepterMockAcceptContextWithOtherArgsParams{ + ctx: minimock.AnyContext, + i1: 24, + }, args[1]) + assert.Equal(t, ContextAccepterMockAcceptContextWithOtherArgsParams{ + ctx: context.Background(), + i1: 123, + }, args[2]) + + assert.Equal(t, "\n\nDiff:\n--- Expected params\n+++ Actual params\n@@ -4,3 +4,3 @@\n },\n- i1: (int) 24\n+ i1: (int) 123\n }\n", args[3]) + }) mock := NewContextAccepterMock(tester). AcceptContextWithOtherArgsMock.Expect(minimock.AnyContext, 24).Return(1, nil) @@ -73,26 +75,29 @@ func TestContextAccepterMock_DiffWithoutAnyContext(t *testing.T) { func TestContextAccepterMock_DiffInStructArgWithoutAnyContext(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() - - tester.ErrorfMock. - Expect("ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameters, want: %#v, got: %#v%s\n", - ContextAccepterMockAcceptContextWithStructArgsParams{ - ctx: minimock.AnyContext, - s1: structArg{ - a: 124, - b: "abcd", - }, + tester.CleanupMock.Return().HelperMock.Return() + + tester.ErrorfMock.Set(func(format string, args ...interface{}) { + assert.Equal(t, "ContextAccepterMock.AcceptContextWithStructArgs got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", format) + + assert.Equal(t, ContextAccepterMockAcceptContextWithStructArgsParams{ + ctx: minimock.AnyContext, + s1: structArg{ + a: 124, + b: "abcd", }, - ContextAccepterMockAcceptContextWithStructArgsParams{ - ctx: context.Background(), - s1: structArg{ - a: 123, - b: "abcd", - }, + }, args[1]) + + assert.Equal(t, ContextAccepterMockAcceptContextWithStructArgsParams{ + ctx: context.Background(), + s1: structArg{ + a: 123, + b: "abcd", }, - "\n\nDiff:\n--- Expected params\n+++ Actual params\n@@ -5,3 +5,3 @@\n s1: (tests.structArg) {\n- a: (int) 124,\n+ a: (int) 123,\n b: (string) (len=4) \"abcd\"\n"). - Return() + }, args[2]) + + assert.Equal(t, "\n\nDiff:\n--- Expected params\n+++ Actual params\n@@ -5,3 +5,3 @@\n s1: (tests.structArg) {\n- a: (int) 124,\n+ a: (int) 123,\n b: (string) (len=4) \"abcd\"\n", args[3]) + }) mock := NewContextAccepterMock(tester). AcceptContextWithStructArgsMock.Expect(minimock.AnyContext, structArg{ @@ -109,7 +114,7 @@ func TestContextAccepterMock_DiffInStructArgWithoutAnyContext(t *testing.T) { func TestContextAccepterMock_TimesSuccess(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() mock := NewContextAccepterMock(tester). AcceptContextWithStructArgsMock.Times(2).Expect(minimock.AnyContext, structArg{ @@ -137,16 +142,20 @@ func TestContextAccepterMock_TimesSuccess(t *testing.T) { func TestContextAccepterMock_TimesFailure(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return(). - ErrorfMock.Expect("Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs but found %d calls", uint64(1), uint64(2)). - Return() + tester.CleanupMock.Return().HelperMock.Return(). + ErrorfMock.Set(func(format string, args ...interface{}) { + assert.Equal(t, "Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs at\n%s but found %d calls", format) + assert.Equal(t, uint64(1), args[0]) + assert.Equal(t, uint64(2), args[2]) + }) // Expected 1 calls to ContextAccepterMock.AcceptContextWithStructArgs but found 2 calls mock := NewContextAccepterMock(tester). - AcceptContextWithStructArgsMock.Times(1).Expect(minimock.AnyContext, structArg{ - a: 124, - b: "abcd", - }). + AcceptContextWithStructArgsMock.Times(1). + Expect(minimock.AnyContext, structArg{ + a: 124, + b: "abcd", + }). Return(1, nil). AcceptContextMock. Times(1).Return() @@ -181,7 +190,7 @@ func TestContextAccepterMock_TimesZero(t *testing.T) { func TestContextAccepterMock_ExpectedCall(t *testing.T) { tester := NewTesterMock(t) tester.CleanupMock.Times(1).Return(). - ErrorMock.Expect("Expected call to ContextAccepterMock.AcceptContext").Times(1). + ErrorfMock.ExpectFormatParam1("Expected call to ContextAccepterMock.AcceptContext at\n%s").Times(1). Return() mock := NewContextAccepterMock(tester).AcceptContextMock.Return() diff --git a/tests/formatter_alias_mock.go b/tests/formatter_alias_mock.go index 84fa9ca..9631614 100644 --- a/tests/formatter_alias_mock.go +++ b/tests/formatter_alias_mock.go @@ -18,6 +18,7 @@ type FormatterAliasMock struct { finishOnce sync.Once funcFormat func(s1 string, p1 ...interface{}) (s2 string) + funcFormatOrigin string inspectFuncFormat func(s1 string, p1 ...interface{}) afterFormatCounter uint64 beforeFormatCounter uint64 @@ -49,16 +50,19 @@ type mFormatterAliasMockFormat struct { callArgs []*FormatterAliasMockFormatParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // FormatterAliasMockFormatExpectation specifies expectation struct of the formatterAlias.Format type FormatterAliasMockFormatExpectation struct { - mock *FormatterAliasMock - params *FormatterAliasMockFormatParams - paramPtrs *FormatterAliasMockFormatParamPtrs - results *FormatterAliasMockFormatResults - Counter uint64 + mock *FormatterAliasMock + params *FormatterAliasMockFormatParams + paramPtrs *FormatterAliasMockFormatParamPtrs + expectationOrigins FormatterAliasMockFormatExpectationOrigins + results *FormatterAliasMockFormatResults + returnOrigin string + Counter uint64 } // FormatterAliasMockFormatParams contains parameters of the formatterAlias.Format @@ -78,6 +82,13 @@ type FormatterAliasMockFormatResults struct { s2 string } +// FormatterAliasMockFormatOrigins contains origins of expectations of the formatterAlias.Format +type FormatterAliasMockFormatExpectationOrigins struct { + origin string + originS1 string + originP1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -103,6 +114,7 @@ func (mmFormat *mFormatterAliasMockFormat) Expect(s1 string, p1 ...interface{}) } mmFormat.defaultExpectation.params = &FormatterAliasMockFormatParams{s1, p1} + mmFormat.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -130,6 +142,7 @@ func (mmFormat *mFormatterAliasMockFormat) ExpectS1Param1(s1 string) *mFormatter mmFormat.defaultExpectation.paramPtrs = &FormatterAliasMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.s1 = &s1 + mmFormat.defaultExpectation.expectationOrigins.originS1 = minimock.CallerInfo(1) return mmFormat } @@ -152,6 +165,7 @@ func (mmFormat *mFormatterAliasMockFormat) ExpectP1Param2(p1 ...interface{}) *mF mmFormat.defaultExpectation.paramPtrs = &FormatterAliasMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.p1 = &p1 + mmFormat.defaultExpectation.expectationOrigins.originP1 = minimock.CallerInfo(1) return mmFormat } @@ -177,6 +191,7 @@ func (mmFormat *mFormatterAliasMockFormat) Return(s2 string) *FormatterAliasMock mmFormat.defaultExpectation = &FormatterAliasMockFormatExpectation{mock: mmFormat.mock} } mmFormat.defaultExpectation.results = &FormatterAliasMockFormatResults{s2} + mmFormat.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -191,6 +206,7 @@ func (mmFormat *mFormatterAliasMockFormat) Set(f func(s1 string, p1 ...interface } mmFormat.mock.funcFormat = f + mmFormat.mock.funcFormatOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -202,8 +218,9 @@ func (mmFormat *mFormatterAliasMockFormat) When(s1 string, p1 ...interface{}) *F } expectation := &FormatterAliasMockFormatExpectation{ - mock: mmFormat.mock, - params: &FormatterAliasMockFormatParams{s1, p1}, + mock: mmFormat.mock, + params: &FormatterAliasMockFormatParams{s1, p1}, + expectationOrigins: FormatterAliasMockFormatExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmFormat.expectations = append(mmFormat.expectations, expectation) return expectation @@ -221,6 +238,7 @@ func (mmFormat *mFormatterAliasMockFormat) Times(n uint64) *mFormatterAliasMockF mmFormat.mock.t.Fatalf("Times of FormatterAliasMock.Format mock can not be zero") } mm_atomic.StoreUint64(&mmFormat.expectedInvocations, n) + mmFormat.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFormat } @@ -240,6 +258,8 @@ func (mmFormat *FormatterAliasMock) Format(s1 string, p1 ...interface{}) (s2 str mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) defer mm_atomic.AddUint64(&mmFormat.afterFormatCounter, 1) + mmFormat.t.Helper() + if mmFormat.inspectFuncFormat != nil { mmFormat.inspectFuncFormat(s1, p1...) } @@ -268,15 +288,18 @@ func (mmFormat *FormatterAliasMock) Format(s1 string, p1 ...interface{}) (s2 str if mm_want_ptrs != nil { if mm_want_ptrs.s1 != nil && !minimock.Equal(*mm_want_ptrs.s1, mm_got.s1) { - mmFormat.t.Errorf("FormatterAliasMock.Format got unexpected parameter s1, want: %#v, got: %#v%s\n", *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) + mmFormat.t.Errorf("FormatterAliasMock.Format got unexpected parameter s1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originS1, *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) } if mm_want_ptrs.p1 != nil && !minimock.Equal(*mm_want_ptrs.p1, mm_got.p1) { - mmFormat.t.Errorf("FormatterAliasMock.Format got unexpected parameter p1, want: %#v, got: %#v%s\n", *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) + mmFormat.t.Errorf("FormatterAliasMock.Format got unexpected parameter p1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originP1, *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) } } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFormat.t.Errorf("FormatterAliasMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + mmFormat.t.Errorf("FormatterAliasMock.Format got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmFormat.FormatMock.defaultExpectation.results @@ -336,7 +359,7 @@ func (m *FormatterAliasMock) MinimockFormatDone() bool { func (m *FormatterAliasMock) MinimockFormatInspect() { for _, e := range m.FormatMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to FormatterAliasMock.Format with params: %#v", *e.params) + m.t.Errorf("Expected call to FormatterAliasMock.Format at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -344,19 +367,19 @@ func (m *FormatterAliasMock) MinimockFormatInspect() { // if default expectation was set then invocations count should be greater than zero if m.FormatMock.defaultExpectation != nil && afterFormatCounter < 1 { if m.FormatMock.defaultExpectation.params == nil { - m.t.Error("Expected call to FormatterAliasMock.Format") + m.t.Errorf("Expected call to FormatterAliasMock.Format at\n%s", m.FormatMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to FormatterAliasMock.Format with params: %#v", *m.FormatMock.defaultExpectation.params) + m.t.Errorf("Expected call to FormatterAliasMock.Format at\n%s with params: %#v", m.FormatMock.defaultExpectation.expectationOrigins.origin, *m.FormatMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFormat != nil && afterFormatCounter < 1 { - m.t.Error("Expected call to FormatterAliasMock.Format") + m.t.Errorf("Expected call to FormatterAliasMock.Format at\n%s", m.funcFormatOrigin) } if !m.FormatMock.invocationsDone() && afterFormatCounter > 0 { - m.t.Errorf("Expected %d calls to FormatterAliasMock.Format but found %d calls", - mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), afterFormatCounter) + m.t.Errorf("Expected %d calls to FormatterAliasMock.Format at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), m.FormatMock.expectedInvocationsOrigin, afterFormatCounter) } } diff --git a/tests/formatter_mock.go b/tests/formatter_mock.go index 72ba2fc..d00192a 100644 --- a/tests/formatter_mock.go +++ b/tests/formatter_mock.go @@ -18,6 +18,7 @@ type FormatterMock struct { finishOnce sync.Once funcFormat func(s1 string, p1 ...interface{}) (s2 string) + funcFormatOrigin string inspectFuncFormat func(s1 string, p1 ...interface{}) afterFormatCounter uint64 beforeFormatCounter uint64 @@ -49,16 +50,19 @@ type mFormatterMockFormat struct { callArgs []*FormatterMockFormatParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // FormatterMockFormatExpectation specifies expectation struct of the Formatter.Format type FormatterMockFormatExpectation struct { - mock *FormatterMock - params *FormatterMockFormatParams - paramPtrs *FormatterMockFormatParamPtrs - results *FormatterMockFormatResults - Counter uint64 + mock *FormatterMock + params *FormatterMockFormatParams + paramPtrs *FormatterMockFormatParamPtrs + expectationOrigins FormatterMockFormatExpectationOrigins + results *FormatterMockFormatResults + returnOrigin string + Counter uint64 } // FormatterMockFormatParams contains parameters of the Formatter.Format @@ -78,6 +82,13 @@ type FormatterMockFormatResults struct { s2 string } +// FormatterMockFormatOrigins contains origins of expectations of the Formatter.Format +type FormatterMockFormatExpectationOrigins struct { + origin string + originS1 string + originP1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -103,6 +114,7 @@ func (mmFormat *mFormatterMockFormat) Expect(s1 string, p1 ...interface{}) *mFor } mmFormat.defaultExpectation.params = &FormatterMockFormatParams{s1, p1} + mmFormat.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -130,6 +142,7 @@ func (mmFormat *mFormatterMockFormat) ExpectS1Param1(s1 string) *mFormatterMockF mmFormat.defaultExpectation.paramPtrs = &FormatterMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.s1 = &s1 + mmFormat.defaultExpectation.expectationOrigins.originS1 = minimock.CallerInfo(1) return mmFormat } @@ -152,6 +165,7 @@ func (mmFormat *mFormatterMockFormat) ExpectP1Param2(p1 ...interface{}) *mFormat mmFormat.defaultExpectation.paramPtrs = &FormatterMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.p1 = &p1 + mmFormat.defaultExpectation.expectationOrigins.originP1 = minimock.CallerInfo(1) return mmFormat } @@ -177,6 +191,7 @@ func (mmFormat *mFormatterMockFormat) Return(s2 string) *FormatterMock { mmFormat.defaultExpectation = &FormatterMockFormatExpectation{mock: mmFormat.mock} } mmFormat.defaultExpectation.results = &FormatterMockFormatResults{s2} + mmFormat.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -191,6 +206,7 @@ func (mmFormat *mFormatterMockFormat) Set(f func(s1 string, p1 ...interface{}) ( } mmFormat.mock.funcFormat = f + mmFormat.mock.funcFormatOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -202,8 +218,9 @@ func (mmFormat *mFormatterMockFormat) When(s1 string, p1 ...interface{}) *Format } expectation := &FormatterMockFormatExpectation{ - mock: mmFormat.mock, - params: &FormatterMockFormatParams{s1, p1}, + mock: mmFormat.mock, + params: &FormatterMockFormatParams{s1, p1}, + expectationOrigins: FormatterMockFormatExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmFormat.expectations = append(mmFormat.expectations, expectation) return expectation @@ -221,6 +238,7 @@ func (mmFormat *mFormatterMockFormat) Times(n uint64) *mFormatterMockFormat { mmFormat.mock.t.Fatalf("Times of FormatterMock.Format mock can not be zero") } mm_atomic.StoreUint64(&mmFormat.expectedInvocations, n) + mmFormat.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFormat } @@ -240,6 +258,8 @@ func (mmFormat *FormatterMock) Format(s1 string, p1 ...interface{}) (s2 string) mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) defer mm_atomic.AddUint64(&mmFormat.afterFormatCounter, 1) + mmFormat.t.Helper() + if mmFormat.inspectFuncFormat != nil { mmFormat.inspectFuncFormat(s1, p1...) } @@ -268,15 +288,18 @@ func (mmFormat *FormatterMock) Format(s1 string, p1 ...interface{}) (s2 string) if mm_want_ptrs != nil { if mm_want_ptrs.s1 != nil && !minimock.Equal(*mm_want_ptrs.s1, mm_got.s1) { - mmFormat.t.Errorf("FormatterMock.Format got unexpected parameter s1, want: %#v, got: %#v%s\n", *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) + mmFormat.t.Errorf("FormatterMock.Format got unexpected parameter s1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originS1, *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) } if mm_want_ptrs.p1 != nil && !minimock.Equal(*mm_want_ptrs.p1, mm_got.p1) { - mmFormat.t.Errorf("FormatterMock.Format got unexpected parameter p1, want: %#v, got: %#v%s\n", *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) + mmFormat.t.Errorf("FormatterMock.Format got unexpected parameter p1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originP1, *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) } } else 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)) + mmFormat.t.Errorf("FormatterMock.Format got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmFormat.FormatMock.defaultExpectation.results @@ -336,7 +359,7 @@ func (m *FormatterMock) MinimockFormatDone() bool { 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) + m.t.Errorf("Expected call to FormatterMock.Format at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -344,19 +367,19 @@ func (m *FormatterMock) MinimockFormatInspect() { // if default expectation was set then invocations count should be greater than zero if m.FormatMock.defaultExpectation != nil && afterFormatCounter < 1 { if m.FormatMock.defaultExpectation.params == nil { - m.t.Error("Expected call to FormatterMock.Format") + m.t.Errorf("Expected call to FormatterMock.Format at\n%s", m.FormatMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to FormatterMock.Format with params: %#v", *m.FormatMock.defaultExpectation.params) + m.t.Errorf("Expected call to FormatterMock.Format at\n%s with params: %#v", m.FormatMock.defaultExpectation.expectationOrigins.origin, *m.FormatMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFormat != nil && afterFormatCounter < 1 { - m.t.Error("Expected call to FormatterMock.Format") + m.t.Errorf("Expected call to FormatterMock.Format at\n%s", m.funcFormatOrigin) } if !m.FormatMock.invocationsDone() && afterFormatCounter > 0 { - m.t.Errorf("Expected %d calls to FormatterMock.Format but found %d calls", - mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), afterFormatCounter) + m.t.Errorf("Expected %d calls to FormatterMock.Format at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), m.FormatMock.expectedInvocationsOrigin, afterFormatCounter) } } diff --git a/tests/formatter_mock_test.go b/tests/formatter_mock_test.go index 71e0022..0906adb 100644 --- a/tests/formatter_mock_test.go +++ b/tests/formatter_mock_test.go @@ -25,7 +25,7 @@ func TestFormatterMock_ImplementsStringer(t *testing.T) { func TestFormatterMock_CleanupIsCalled(t *testing.T) { tester := NewTesterMock(t) tester.CleanupMock.Set(t.Cleanup) - tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return() + tester.ErrorfMock.ExpectFormatParam1("Expected call to FormatterMock.Format at\n%s").Return() NewFormatterMock(tester).FormatMock.Return("") } @@ -38,7 +38,7 @@ func TestFormatterMock_UnmockedCallFailsTest(t *testing.T) { assert.Equal(t, "this call fails because Format method isn't mocked", args[0]) mockCalled = true - }).CleanupMock.Return() + }).CleanupMock.Return().HelperMock.Return() formatterMock := NewFormatterMock(tester) dummyFormatter{formatterMock}.Format("this call fails because Format method isn't mocked") @@ -46,7 +46,7 @@ func TestFormatterMock_UnmockedCallFailsTest(t *testing.T) { } func TestFormatterMock_MockedCallSucceeds(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t).CleanupMock.Return().HelperMock.Return() formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Set(func(format string, args ...interface{}) string { @@ -58,7 +58,7 @@ func TestFormatterMock_MockedCallSucceeds(t *testing.T) { } func TestFormatterMock_Wait(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t).CleanupMock.Return().HelperMock.Return() formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Set(func(format string, args ...interface{}) string { @@ -74,7 +74,7 @@ func TestFormatterMock_Wait(t *testing.T) { } func TestFormatterMock_Expect(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t).CleanupMock.Return().HelperMock.Return() formatterMock := NewFormatterMock(tester).FormatMock.Expect("Hello", "world", "!").Return("") @@ -87,13 +87,13 @@ func TestFormatterMock_Expect(t *testing.T) { func TestFormatterMock_ExpectDifferentArguments(t *testing.T) { assert.Panics(t, func() { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t).CleanupMock.Return().HelperMock.Return() tester.ErrorfMock.Set(func(s string, args ...interface{}) { - assert.Equal(t, "FormatterMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", s) - require.Len(t, args, 3) - assert.Equal(t, FormatterMockFormatParams{s1: "expected"}, args[0]) - assert.Equal(t, FormatterMockFormatParams{s1: "actual"}, args[1]) + assert.Equal(t, "FormatterMock.Format got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", s) + require.Len(t, args, 4) + assert.Equal(t, FormatterMockFormatParams{s1: "expected"}, args[1]) + assert.Equal(t, FormatterMockFormatParams{s1: "actual"}, args[2]) }) tester.FatalMock.Expect("No results are set for the FormatterMock.Format").Return() @@ -116,7 +116,7 @@ func TestFormatterMock_ExpectAfterSet(t *testing.T) { } func TestFormatterMock_ExpectAfterWhen(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t).CleanupMock.Return().HelperMock.Return() tester.FatalfMock.Expect("Expectation set by When has same params: %#v", FormatterMockFormatParams{s1: "Should not work", p1: nil}).Return() @@ -129,7 +129,7 @@ func TestFormatterMock_ExpectAfterWhen(t *testing.T) { } func TestFormatterMock_Return(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t).CleanupMock.Return().HelperMock.Return() formatterMock := NewFormatterMock(tester).FormatMock.Return("Hello world!") df := dummyFormatter{formatterMock} @@ -137,7 +137,8 @@ func TestFormatterMock_Return(t *testing.T) { } func TestFormatterMock_ReturnAfterSet(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() tester.FatalfMock.Expect("FormatterMock.Format mock is already set by Set").Return() @@ -149,10 +150,10 @@ 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) + tester.CleanupMock.Return() - tester := NewTesterMock(t).CleanupMock.Return() - - tester.ErrorMock.Expect("Expected call to FormatterMock.Format") + tester.ErrorfMock.ExpectFormatParam1("Expected call to FormatterMock.Format at\n%s") formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Return("") @@ -160,7 +161,8 @@ func TestFormatterMock_ReturnWithoutExpectForFixedArgsMethod(t *testing.T) { } func TestFormatterMock_Set(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return().HelperMock.Return() formatterMock := NewFormatterMock(tester).FormatMock.Set(func(string, ...interface{}) string { return "set" @@ -171,7 +173,8 @@ func TestFormatterMock_Set(t *testing.T) { } func TestFormatterMock_SetAfterExpect(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() tester.FatalfMock.Expect("Default expectation is already set for the Formatter.Format method").Return() @@ -182,7 +185,8 @@ func TestFormatterMock_SetAfterExpect(t *testing.T) { } func TestFormatterMock_SetAfterWhen(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() tester.FatalfMock.Expect("Some expectations are already set for the Formatter.Format method").Return() @@ -204,7 +208,8 @@ func TestFormatterMockFormat_WhenThen(t *testing.T) { } func TestFormatterMockFormat_WhenAfterSet(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() tester.FatalfMock.Expect("FormatterMock.Format mock is already set by Set").Return() @@ -227,9 +232,10 @@ func TestFormatterMock_MinimockFormatDone(t *testing.T) { } func TestFormatterMock_MinimockFinish(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() - tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return() + tester.ErrorfMock.ExpectFormatParam1("Expected call to FormatterMock.Format at\n%s").Return() formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Set(func(string, ...interface{}) string { return "" }) @@ -238,11 +244,10 @@ func TestFormatterMock_MinimockFinish(t *testing.T) { } func TestFormatterMock_MinimockFinish_WithNoMetExpectations(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() - tester.ErrorfMock.Set(func(m string, args ...interface{}) { - assert.Equal(t, m, "Expected call to FormatterMock.Format with params: %#v") - }) + tester.ErrorfMock.ExpectFormatParam1("Expected call to FormatterMock.Format at\n%s with params: %#v") formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Expect("a").Return("a") @@ -252,9 +257,11 @@ func TestFormatterMock_MinimockFinish_WithNoMetExpectations(t *testing.T) { } func TestFormatterMock_MinimockWait(t *testing.T) { - tester := NewTesterMock(t).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() - tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return() + tester.ErrorfMock. + ExpectFormatParam1("Expected call to FormatterMock.Format at\n%s").Return() formatterMock := NewFormatterMock(tester) formatterMock.FormatMock.Set(func(string, ...interface{}) string { return "" }) @@ -264,7 +271,8 @@ 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).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return() formatterMock := NewFormatterMock(tester) calls := formatterMock.FormatMock.Calls() @@ -275,7 +283,8 @@ 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).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return().HelperMock.Return() // Arguments used for each mock call expected := []*FormatterMockFormatParams{ @@ -297,7 +306,8 @@ 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).CleanupMock.Return() + tester := NewTesterMock(t) + tester.CleanupMock.Return().HelperMock.Return() expected := []*FormatterMockFormatParams{ {"a1", []interface{}{"a1"}}, diff --git a/tests/formatter_type_mock.go b/tests/formatter_type_mock.go index 3ca88ee..bafb576 100644 --- a/tests/formatter_type_mock.go +++ b/tests/formatter_type_mock.go @@ -18,6 +18,7 @@ type FormatterTypeMock struct { finishOnce sync.Once funcFormat func(s1 string, p1 ...interface{}) (s2 string) + funcFormatOrigin string inspectFuncFormat func(s1 string, p1 ...interface{}) afterFormatCounter uint64 beforeFormatCounter uint64 @@ -49,16 +50,19 @@ type mFormatterTypeMockFormat struct { callArgs []*FormatterTypeMockFormatParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // FormatterTypeMockFormatExpectation specifies expectation struct of the formatterType.Format type FormatterTypeMockFormatExpectation struct { - mock *FormatterTypeMock - params *FormatterTypeMockFormatParams - paramPtrs *FormatterTypeMockFormatParamPtrs - results *FormatterTypeMockFormatResults - Counter uint64 + mock *FormatterTypeMock + params *FormatterTypeMockFormatParams + paramPtrs *FormatterTypeMockFormatParamPtrs + expectationOrigins FormatterTypeMockFormatExpectationOrigins + results *FormatterTypeMockFormatResults + returnOrigin string + Counter uint64 } // FormatterTypeMockFormatParams contains parameters of the formatterType.Format @@ -78,6 +82,13 @@ type FormatterTypeMockFormatResults struct { s2 string } +// FormatterTypeMockFormatOrigins contains origins of expectations of the formatterType.Format +type FormatterTypeMockFormatExpectationOrigins struct { + origin string + originS1 string + originP1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -103,6 +114,7 @@ func (mmFormat *mFormatterTypeMockFormat) Expect(s1 string, p1 ...interface{}) * } mmFormat.defaultExpectation.params = &FormatterTypeMockFormatParams{s1, p1} + mmFormat.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -130,6 +142,7 @@ func (mmFormat *mFormatterTypeMockFormat) ExpectS1Param1(s1 string) *mFormatterT mmFormat.defaultExpectation.paramPtrs = &FormatterTypeMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.s1 = &s1 + mmFormat.defaultExpectation.expectationOrigins.originS1 = minimock.CallerInfo(1) return mmFormat } @@ -152,6 +165,7 @@ func (mmFormat *mFormatterTypeMockFormat) ExpectP1Param2(p1 ...interface{}) *mFo mmFormat.defaultExpectation.paramPtrs = &FormatterTypeMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.p1 = &p1 + mmFormat.defaultExpectation.expectationOrigins.originP1 = minimock.CallerInfo(1) return mmFormat } @@ -177,6 +191,7 @@ func (mmFormat *mFormatterTypeMockFormat) Return(s2 string) *FormatterTypeMock { mmFormat.defaultExpectation = &FormatterTypeMockFormatExpectation{mock: mmFormat.mock} } mmFormat.defaultExpectation.results = &FormatterTypeMockFormatResults{s2} + mmFormat.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -191,6 +206,7 @@ func (mmFormat *mFormatterTypeMockFormat) Set(f func(s1 string, p1 ...interface{ } mmFormat.mock.funcFormat = f + mmFormat.mock.funcFormatOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -202,8 +218,9 @@ func (mmFormat *mFormatterTypeMockFormat) When(s1 string, p1 ...interface{}) *Fo } expectation := &FormatterTypeMockFormatExpectation{ - mock: mmFormat.mock, - params: &FormatterTypeMockFormatParams{s1, p1}, + mock: mmFormat.mock, + params: &FormatterTypeMockFormatParams{s1, p1}, + expectationOrigins: FormatterTypeMockFormatExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmFormat.expectations = append(mmFormat.expectations, expectation) return expectation @@ -221,6 +238,7 @@ func (mmFormat *mFormatterTypeMockFormat) Times(n uint64) *mFormatterTypeMockFor mmFormat.mock.t.Fatalf("Times of FormatterTypeMock.Format mock can not be zero") } mm_atomic.StoreUint64(&mmFormat.expectedInvocations, n) + mmFormat.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFormat } @@ -240,6 +258,8 @@ func (mmFormat *FormatterTypeMock) Format(s1 string, p1 ...interface{}) (s2 stri mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) defer mm_atomic.AddUint64(&mmFormat.afterFormatCounter, 1) + mmFormat.t.Helper() + if mmFormat.inspectFuncFormat != nil { mmFormat.inspectFuncFormat(s1, p1...) } @@ -268,15 +288,18 @@ func (mmFormat *FormatterTypeMock) Format(s1 string, p1 ...interface{}) (s2 stri if mm_want_ptrs != nil { if mm_want_ptrs.s1 != nil && !minimock.Equal(*mm_want_ptrs.s1, mm_got.s1) { - mmFormat.t.Errorf("FormatterTypeMock.Format got unexpected parameter s1, want: %#v, got: %#v%s\n", *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) + mmFormat.t.Errorf("FormatterTypeMock.Format got unexpected parameter s1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originS1, *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) } if mm_want_ptrs.p1 != nil && !minimock.Equal(*mm_want_ptrs.p1, mm_got.p1) { - mmFormat.t.Errorf("FormatterTypeMock.Format got unexpected parameter p1, want: %#v, got: %#v%s\n", *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) + mmFormat.t.Errorf("FormatterTypeMock.Format got unexpected parameter p1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originP1, *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) } } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFormat.t.Errorf("FormatterTypeMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + mmFormat.t.Errorf("FormatterTypeMock.Format got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmFormat.FormatMock.defaultExpectation.results @@ -336,7 +359,7 @@ func (m *FormatterTypeMock) MinimockFormatDone() bool { func (m *FormatterTypeMock) MinimockFormatInspect() { for _, e := range m.FormatMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to FormatterTypeMock.Format with params: %#v", *e.params) + m.t.Errorf("Expected call to FormatterTypeMock.Format at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -344,19 +367,19 @@ func (m *FormatterTypeMock) MinimockFormatInspect() { // if default expectation was set then invocations count should be greater than zero if m.FormatMock.defaultExpectation != nil && afterFormatCounter < 1 { if m.FormatMock.defaultExpectation.params == nil { - m.t.Error("Expected call to FormatterTypeMock.Format") + m.t.Errorf("Expected call to FormatterTypeMock.Format at\n%s", m.FormatMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to FormatterTypeMock.Format with params: %#v", *m.FormatMock.defaultExpectation.params) + m.t.Errorf("Expected call to FormatterTypeMock.Format at\n%s with params: %#v", m.FormatMock.defaultExpectation.expectationOrigins.origin, *m.FormatMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFormat != nil && afterFormatCounter < 1 { - m.t.Error("Expected call to FormatterTypeMock.Format") + m.t.Errorf("Expected call to FormatterTypeMock.Format at\n%s", m.funcFormatOrigin) } if !m.FormatMock.invocationsDone() && afterFormatCounter > 0 { - m.t.Errorf("Expected %d calls to FormatterTypeMock.Format but found %d calls", - mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), afterFormatCounter) + m.t.Errorf("Expected %d calls to FormatterTypeMock.Format at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), m.FormatMock.expectedInvocationsOrigin, afterFormatCounter) } } diff --git a/tests/formatter_with_custom_name_mock.go b/tests/formatter_with_custom_name_mock.go index 9f439db..271bb1c 100644 --- a/tests/formatter_with_custom_name_mock.go +++ b/tests/formatter_with_custom_name_mock.go @@ -18,6 +18,7 @@ type CustomFormatterNameMock struct { finishOnce sync.Once funcFormat func(s1 string, p1 ...interface{}) (s2 string) + funcFormatOrigin string inspectFuncFormat func(s1 string, p1 ...interface{}) afterFormatCounter uint64 beforeFormatCounter uint64 @@ -49,16 +50,19 @@ type mCustomFormatterNameMockFormat struct { callArgs []*CustomFormatterNameMockFormatParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // CustomFormatterNameMockFormatExpectation specifies expectation struct of the Formatter.Format type CustomFormatterNameMockFormatExpectation struct { - mock *CustomFormatterNameMock - params *CustomFormatterNameMockFormatParams - paramPtrs *CustomFormatterNameMockFormatParamPtrs - results *CustomFormatterNameMockFormatResults - Counter uint64 + mock *CustomFormatterNameMock + params *CustomFormatterNameMockFormatParams + paramPtrs *CustomFormatterNameMockFormatParamPtrs + expectationOrigins CustomFormatterNameMockFormatExpectationOrigins + results *CustomFormatterNameMockFormatResults + returnOrigin string + Counter uint64 } // CustomFormatterNameMockFormatParams contains parameters of the Formatter.Format @@ -78,6 +82,13 @@ type CustomFormatterNameMockFormatResults struct { s2 string } +// CustomFormatterNameMockFormatOrigins contains origins of expectations of the Formatter.Format +type CustomFormatterNameMockFormatExpectationOrigins struct { + origin string + originS1 string + originP1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -103,6 +114,7 @@ func (mmFormat *mCustomFormatterNameMockFormat) Expect(s1 string, p1 ...interfac } mmFormat.defaultExpectation.params = &CustomFormatterNameMockFormatParams{s1, p1} + mmFormat.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -130,6 +142,7 @@ func (mmFormat *mCustomFormatterNameMockFormat) ExpectS1Param1(s1 string) *mCust mmFormat.defaultExpectation.paramPtrs = &CustomFormatterNameMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.s1 = &s1 + mmFormat.defaultExpectation.expectationOrigins.originS1 = minimock.CallerInfo(1) return mmFormat } @@ -152,6 +165,7 @@ func (mmFormat *mCustomFormatterNameMockFormat) ExpectP1Param2(p1 ...interface{} mmFormat.defaultExpectation.paramPtrs = &CustomFormatterNameMockFormatParamPtrs{} } mmFormat.defaultExpectation.paramPtrs.p1 = &p1 + mmFormat.defaultExpectation.expectationOrigins.originP1 = minimock.CallerInfo(1) return mmFormat } @@ -177,6 +191,7 @@ func (mmFormat *mCustomFormatterNameMockFormat) Return(s2 string) *CustomFormatt mmFormat.defaultExpectation = &CustomFormatterNameMockFormatExpectation{mock: mmFormat.mock} } mmFormat.defaultExpectation.results = &CustomFormatterNameMockFormatResults{s2} + mmFormat.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -191,6 +206,7 @@ func (mmFormat *mCustomFormatterNameMockFormat) Set(f func(s1 string, p1 ...inte } mmFormat.mock.funcFormat = f + mmFormat.mock.funcFormatOrigin = minimock.CallerInfo(1) return mmFormat.mock } @@ -202,8 +218,9 @@ func (mmFormat *mCustomFormatterNameMockFormat) When(s1 string, p1 ...interface{ } expectation := &CustomFormatterNameMockFormatExpectation{ - mock: mmFormat.mock, - params: &CustomFormatterNameMockFormatParams{s1, p1}, + mock: mmFormat.mock, + params: &CustomFormatterNameMockFormatParams{s1, p1}, + expectationOrigins: CustomFormatterNameMockFormatExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmFormat.expectations = append(mmFormat.expectations, expectation) return expectation @@ -221,6 +238,7 @@ func (mmFormat *mCustomFormatterNameMockFormat) Times(n uint64) *mCustomFormatte mmFormat.mock.t.Fatalf("Times of CustomFormatterNameMock.Format mock can not be zero") } mm_atomic.StoreUint64(&mmFormat.expectedInvocations, n) + mmFormat.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFormat } @@ -240,6 +258,8 @@ func (mmFormat *CustomFormatterNameMock) Format(s1 string, p1 ...interface{}) (s mm_atomic.AddUint64(&mmFormat.beforeFormatCounter, 1) defer mm_atomic.AddUint64(&mmFormat.afterFormatCounter, 1) + mmFormat.t.Helper() + if mmFormat.inspectFuncFormat != nil { mmFormat.inspectFuncFormat(s1, p1...) } @@ -268,15 +288,18 @@ func (mmFormat *CustomFormatterNameMock) Format(s1 string, p1 ...interface{}) (s if mm_want_ptrs != nil { if mm_want_ptrs.s1 != nil && !minimock.Equal(*mm_want_ptrs.s1, mm_got.s1) { - mmFormat.t.Errorf("CustomFormatterNameMock.Format got unexpected parameter s1, want: %#v, got: %#v%s\n", *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) + mmFormat.t.Errorf("CustomFormatterNameMock.Format got unexpected parameter s1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originS1, *mm_want_ptrs.s1, mm_got.s1, minimock.Diff(*mm_want_ptrs.s1, mm_got.s1)) } if mm_want_ptrs.p1 != nil && !minimock.Equal(*mm_want_ptrs.p1, mm_got.p1) { - mmFormat.t.Errorf("CustomFormatterNameMock.Format got unexpected parameter p1, want: %#v, got: %#v%s\n", *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) + mmFormat.t.Errorf("CustomFormatterNameMock.Format got unexpected parameter p1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.originP1, *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) } } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmFormat.t.Errorf("CustomFormatterNameMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + mmFormat.t.Errorf("CustomFormatterNameMock.Format got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFormat.FormatMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmFormat.FormatMock.defaultExpectation.results @@ -336,7 +359,7 @@ func (m *CustomFormatterNameMock) MinimockFormatDone() bool { func (m *CustomFormatterNameMock) MinimockFormatInspect() { for _, e := range m.FormatMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to CustomFormatterNameMock.Format with params: %#v", *e.params) + m.t.Errorf("Expected call to CustomFormatterNameMock.Format at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -344,19 +367,19 @@ func (m *CustomFormatterNameMock) MinimockFormatInspect() { // if default expectation was set then invocations count should be greater than zero if m.FormatMock.defaultExpectation != nil && afterFormatCounter < 1 { if m.FormatMock.defaultExpectation.params == nil { - m.t.Error("Expected call to CustomFormatterNameMock.Format") + m.t.Errorf("Expected call to CustomFormatterNameMock.Format at\n%s", m.FormatMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to CustomFormatterNameMock.Format with params: %#v", *m.FormatMock.defaultExpectation.params) + m.t.Errorf("Expected call to CustomFormatterNameMock.Format at\n%s with params: %#v", m.FormatMock.defaultExpectation.expectationOrigins.origin, *m.FormatMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFormat != nil && afterFormatCounter < 1 { - m.t.Error("Expected call to CustomFormatterNameMock.Format") + m.t.Errorf("Expected call to CustomFormatterNameMock.Format at\n%s", m.funcFormatOrigin) } if !m.FormatMock.invocationsDone() && afterFormatCounter > 0 { - m.t.Errorf("Expected %d calls to CustomFormatterNameMock.Format but found %d calls", - mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), afterFormatCounter) + m.t.Errorf("Expected %d calls to CustomFormatterNameMock.Format at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FormatMock.expectedInvocations), m.FormatMock.expectedInvocationsOrigin, afterFormatCounter) } } diff --git a/tests/formatter_with_custom_name_mock_test.go b/tests/formatter_with_custom_name_mock_test.go index 89fca3d..3a4e379 100644 --- a/tests/formatter_with_custom_name_mock_test.go +++ b/tests/formatter_with_custom_name_mock_test.go @@ -20,7 +20,7 @@ func TestCustomFormatterNameMock_ImplementsStringer(t *testing.T) { func TestCustomFormatterNameMock_UnmockedCallFailsTest(t *testing.T) { var mockCalled bool tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() tester.FatalfMock.Set(func(s string, args ...interface{}) { assert.Equal(t, "Unexpected call to CustomFormatterNameMock.Format. %v %v", s) @@ -38,7 +38,7 @@ func TestCustomFormatterNameMock_UnmockedCallFailsTest(t *testing.T) { func TestCustomFormatterNameMock_MockedCallSucceeds(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() formatterMock := NewCustomFormatterNameMock(tester) formatterMock.FormatMock.Set(func(format string, args ...interface{}) string { @@ -52,7 +52,7 @@ func TestCustomFormatterNameMock_MockedCallSucceeds(t *testing.T) { func TestCustomFormatterNameMock_Wait(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() formatterMock := NewCustomFormatterNameMock(tester) formatterMock.FormatMock.Set(func(format string, args ...interface{}) string { @@ -69,7 +69,7 @@ func TestCustomFormatterNameMock_Wait(t *testing.T) { func TestCustomFormatterNameMock_Expect(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() formatterMock := NewCustomFormatterNameMock(tester).FormatMock.Expect("Hello", "world", "!").Return("") @@ -83,15 +83,15 @@ func TestCustomFormatterNameMock_Expect(t *testing.T) { func TestCustomFormatterNameMock_ExpectDifferentArguments(t *testing.T) { assert.Panics(t, func() { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() defer tester.MinimockFinish() tester.ErrorfMock.Set(func(s string, args ...interface{}) { - assert.Equal(t, "CustomFormatterNameMock.Format got unexpected parameters, want: %#v, got: %#v%s\n", s) - require.Len(t, args, 3) - assert.Equal(t, CustomFormatterNameMockFormatParams{s1: "expected"}, args[0]) - assert.Equal(t, CustomFormatterNameMockFormatParams{s1: "actual"}, args[1]) + assert.Equal(t, "CustomFormatterNameMock.Format got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", s) + require.Len(t, args, 4) + assert.Equal(t, CustomFormatterNameMockFormatParams{s1: "expected"}, args[1]) + assert.Equal(t, CustomFormatterNameMockFormatParams{s1: "actual"}, args[2]) }) tester.FatalMock.Expect("No results are set for the CustomFormatterNameMock.Format").Return() @@ -120,7 +120,7 @@ func TestCustomFormatterNameMock_ExpectAfterWhen(t *testing.T) { tester := NewTesterMock(t) defer tester.MinimockFinish() - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() tester.FatalfMock.Expect("Expectation set by When has same params: %#v", CustomFormatterNameMockFormatParams{s1: "Should not work", p1: nil}).Return() @@ -134,7 +134,7 @@ func TestCustomFormatterNameMock_ExpectAfterWhen(t *testing.T) { func TestCustomFormatterNameMock_Return(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() formatterMock := NewCustomFormatterNameMock(tester).FormatMock.Return("Hello world!") df := dummyFormatter{formatterMock} @@ -163,7 +163,8 @@ func TestCustomFormatterNameMock_ReturnWithoutExpectForFixedArgsMethod(t *testin tester.CleanupMock.Return() - tester.ErrorMock.Expect("Expected call to CustomFormatterNameMock.Format") + tester.ErrorfMock.ExpectFormatParam1("Expected call to CustomFormatterNameMock.Format at\n%s"). + Return() formatterMock := NewCustomFormatterNameMock(tester) formatterMock.FormatMock.Return("") @@ -172,7 +173,7 @@ func TestCustomFormatterNameMock_ReturnWithoutExpectForFixedArgsMethod(t *testin func TestCustomFormatterNameMock_Set(t *testing.T) { tester := NewTesterMock(t) - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() formatterMock := NewCustomFormatterNameMock(tester).FormatMock.Set(func(string, ...interface{}) string { return "set" @@ -255,7 +256,8 @@ func TestCustomFormatterNameMock_MinimockFinish(t *testing.T) { tester.CleanupMock.Return() - tester.ErrorMock.Expect("Expected call to CustomFormatterNameMock.Format").Return() + tester.ErrorfMock.ExpectFormatParam1("Expected call to CustomFormatterNameMock.Format at\n%s"). + Return() formatterMock := NewCustomFormatterNameMock(tester) formatterMock.FormatMock.Set(func(string, ...interface{}) string { return "" }) @@ -269,9 +271,7 @@ func TestCustomFormatterNameMock_MinimockFinish_WithNoMetExpectations(t *testing tester.CleanupMock.Return() - tester.ErrorfMock.Set(func(m string, args ...interface{}) { - assert.Equal(t, m, "Expected call to CustomFormatterNameMock.Format with params: %#v") - }) + tester.ErrorfMock.ExpectFormatParam1("Expected call to CustomFormatterNameMock.Format at\n%s with params: %#v") formatterMock := NewCustomFormatterNameMock(tester) formatterMock.FormatMock.Expect("a").Return("a") @@ -286,7 +286,8 @@ func TestCustomFormatterNameMock_MinimockWait(t *testing.T) { tester.CleanupMock.Return() - tester.ErrorMock.Expect("Expected call to CustomFormatterNameMock.Format").Return() + tester.ErrorfMock.ExpectFormatParam1("Expected call to CustomFormatterNameMock.Format at\n%s"). + Return() formatterMock := NewCustomFormatterNameMock(tester) formatterMock.FormatMock.Set(func(string, ...interface{}) string { return "" }) @@ -313,7 +314,7 @@ func TestCustomFormatterNameMock_Calls(t *testing.T) { tester := NewTesterMock(t) defer tester.MinimockFinish() - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() // Arguments used for each mock call expected := []*CustomFormatterNameMockFormatParams{ @@ -338,7 +339,7 @@ func TestCustomFormatterNameMock_CallsReturnsCopy(t *testing.T) { tester := NewTesterMock(t) defer tester.MinimockFinish() - tester.CleanupMock.Return() + tester.CleanupMock.Return().HelperMock.Return() expected := []*CustomFormatterNameMockFormatParams{ {"a1", []interface{}{"a1"}}, diff --git a/tests/generic_complex_union.go b/tests/generic_complex_union.go index 195a97f..378dd2d 100644 --- a/tests/generic_complex_union.go +++ b/tests/generic_complex_union.go @@ -18,6 +18,7 @@ type GenericComplexUnionMock[T complexUnion] struct { finishOnce sync.Once funcName func(t1 T) + funcNameOrigin string inspectFuncName func(t1 T) afterNameCounter uint64 beforeNameCounter uint64 @@ -49,16 +50,19 @@ type mGenericComplexUnionMockName[T complexUnion] struct { callArgs []*GenericComplexUnionMockNameParams[T] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericComplexUnionMockNameExpectation specifies expectation struct of the genericComplexUnion.Name type GenericComplexUnionMockNameExpectation[T complexUnion] struct { - mock *GenericComplexUnionMock[T] - params *GenericComplexUnionMockNameParams[T] - paramPtrs *GenericComplexUnionMockNameParamPtrs[T] + mock *GenericComplexUnionMock[T] + params *GenericComplexUnionMockNameParams[T] + paramPtrs *GenericComplexUnionMockNameParamPtrs[T] + expectationOrigins GenericComplexUnionMockNameExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // GenericComplexUnionMockNameParams contains parameters of the genericComplexUnion.Name @@ -71,6 +75,12 @@ type GenericComplexUnionMockNameParamPtrs[T complexUnion] struct { t1 *T } +// GenericComplexUnionMockNameOrigins contains origins of expectations of the genericComplexUnion.Name +type GenericComplexUnionMockNameExpectationOrigins struct { + origin string + originT1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -96,6 +106,7 @@ func (mmName *mGenericComplexUnionMockName[T]) Expect(t1 T) *mGenericComplexUnio } mmName.defaultExpectation.params = &GenericComplexUnionMockNameParams[T]{t1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -123,6 +134,7 @@ func (mmName *mGenericComplexUnionMockName[T]) ExpectT1Param1(t1 T) *mGenericCom mmName.defaultExpectation.paramPtrs = &GenericComplexUnionMockNameParamPtrs[T]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -148,6 +160,7 @@ func (mmName *mGenericComplexUnionMockName[T]) Return() *GenericComplexUnionMock mmName.defaultExpectation = &GenericComplexUnionMockNameExpectation[T]{mock: mmName.mock} } + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -162,6 +175,7 @@ func (mmName *mGenericComplexUnionMockName[T]) Set(f func(t1 T)) *GenericComplex } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -171,6 +185,7 @@ func (mmName *mGenericComplexUnionMockName[T]) Times(n uint64) *mGenericComplexU mmName.mock.t.Fatalf("Times of GenericComplexUnionMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -190,6 +205,8 @@ func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1) } @@ -218,11 +235,13 @@ func (mmName *GenericComplexUnionMock[T]) Name(t1 T) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericComplexUnionMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericComplexUnionMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } } else 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)) + mmName.t.Errorf("GenericComplexUnionMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -280,7 +299,7 @@ func (m *GenericComplexUnionMock[T]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericComplexUnionMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -288,19 +307,19 @@ func (m *GenericComplexUnionMock[T]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericComplexUnionMock.Name") + m.t.Errorf("Expected call to GenericComplexUnionMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericComplexUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericComplexUnionMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericComplexUnionMock.Name") + m.t.Errorf("Expected call to GenericComplexUnionMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericComplexUnionMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericComplexUnionMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_in.go b/tests/generic_in.go index fd6df26..bf8a959 100644 --- a/tests/generic_in.go +++ b/tests/generic_in.go @@ -18,6 +18,7 @@ type GenericInMock[T any] struct { finishOnce sync.Once funcName func(t1 T) + funcNameOrigin string inspectFuncName func(t1 T) afterNameCounter uint64 beforeNameCounter uint64 @@ -49,16 +50,19 @@ type mGenericInMockName[T any] struct { callArgs []*GenericInMockNameParams[T] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericInMockNameExpectation specifies expectation struct of the genericIn.Name type GenericInMockNameExpectation[T any] struct { - mock *GenericInMock[T] - params *GenericInMockNameParams[T] - paramPtrs *GenericInMockNameParamPtrs[T] + mock *GenericInMock[T] + params *GenericInMockNameParams[T] + paramPtrs *GenericInMockNameParamPtrs[T] + expectationOrigins GenericInMockNameExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // GenericInMockNameParams contains parameters of the genericIn.Name @@ -71,6 +75,12 @@ type GenericInMockNameParamPtrs[T any] struct { t1 *T } +// GenericInMockNameOrigins contains origins of expectations of the genericIn.Name +type GenericInMockNameExpectationOrigins struct { + origin string + originT1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -96,6 +106,7 @@ func (mmName *mGenericInMockName[T]) Expect(t1 T) *mGenericInMockName[T] { } mmName.defaultExpectation.params = &GenericInMockNameParams[T]{t1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -123,6 +134,7 @@ func (mmName *mGenericInMockName[T]) ExpectT1Param1(t1 T) *mGenericInMockName[T] mmName.defaultExpectation.paramPtrs = &GenericInMockNameParamPtrs[T]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -148,6 +160,7 @@ func (mmName *mGenericInMockName[T]) Return() *GenericInMock[T] { mmName.defaultExpectation = &GenericInMockNameExpectation[T]{mock: mmName.mock} } + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -162,6 +175,7 @@ func (mmName *mGenericInMockName[T]) Set(f func(t1 T)) *GenericInMock[T] { } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -171,6 +185,7 @@ func (mmName *mGenericInMockName[T]) Times(n uint64) *mGenericInMockName[T] { mmName.mock.t.Fatalf("Times of GenericInMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -190,6 +205,8 @@ func (mmName *GenericInMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1) } @@ -218,11 +235,13 @@ func (mmName *GenericInMock[T]) Name(t1 T) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericInMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericInMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } } else 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)) + mmName.t.Errorf("GenericInMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -280,7 +299,7 @@ func (m *GenericInMock[T]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericInMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -288,19 +307,19 @@ func (m *GenericInMock[T]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInMock.Name") + m.t.Errorf("Expected call to GenericInMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericInMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericInMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericInMock.Name") + m.t.Errorf("Expected call to GenericInMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericInMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericInMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_inline_union.go b/tests/generic_inline_union.go index 25b65a6..a00d36c 100644 --- a/tests/generic_inline_union.go +++ b/tests/generic_inline_union.go @@ -18,6 +18,7 @@ type GenericInlineUnionMock[T int | float64] struct { finishOnce sync.Once funcName func(t1 T) + funcNameOrigin string inspectFuncName func(t1 T) afterNameCounter uint64 beforeNameCounter uint64 @@ -49,16 +50,19 @@ type mGenericInlineUnionMockName[T int | float64] struct { callArgs []*GenericInlineUnionMockNameParams[T] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericInlineUnionMockNameExpectation specifies expectation struct of the genericInlineUnion.Name type GenericInlineUnionMockNameExpectation[T int | float64] struct { - mock *GenericInlineUnionMock[T] - params *GenericInlineUnionMockNameParams[T] - paramPtrs *GenericInlineUnionMockNameParamPtrs[T] + mock *GenericInlineUnionMock[T] + params *GenericInlineUnionMockNameParams[T] + paramPtrs *GenericInlineUnionMockNameParamPtrs[T] + expectationOrigins GenericInlineUnionMockNameExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // GenericInlineUnionMockNameParams contains parameters of the genericInlineUnion.Name @@ -71,6 +75,12 @@ type GenericInlineUnionMockNameParamPtrs[T int | float64] struct { t1 *T } +// GenericInlineUnionMockNameOrigins contains origins of expectations of the genericInlineUnion.Name +type GenericInlineUnionMockNameExpectationOrigins struct { + origin string + originT1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -96,6 +106,7 @@ func (mmName *mGenericInlineUnionMockName[T]) Expect(t1 T) *mGenericInlineUnionM } mmName.defaultExpectation.params = &GenericInlineUnionMockNameParams[T]{t1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -123,6 +134,7 @@ func (mmName *mGenericInlineUnionMockName[T]) ExpectT1Param1(t1 T) *mGenericInli mmName.defaultExpectation.paramPtrs = &GenericInlineUnionMockNameParamPtrs[T]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -148,6 +160,7 @@ func (mmName *mGenericInlineUnionMockName[T]) Return() *GenericInlineUnionMock[T mmName.defaultExpectation = &GenericInlineUnionMockNameExpectation[T]{mock: mmName.mock} } + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -162,6 +175,7 @@ func (mmName *mGenericInlineUnionMockName[T]) Set(f func(t1 T)) *GenericInlineUn } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -171,6 +185,7 @@ func (mmName *mGenericInlineUnionMockName[T]) Times(n uint64) *mGenericInlineUni mmName.mock.t.Fatalf("Times of GenericInlineUnionMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -190,6 +205,8 @@ func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1) } @@ -218,11 +235,13 @@ func (mmName *GenericInlineUnionMock[T]) Name(t1 T) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericInlineUnionMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericInlineUnionMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } } else 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)) + mmName.t.Errorf("GenericInlineUnionMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -280,7 +299,7 @@ func (m *GenericInlineUnionMock[T]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericInlineUnionMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -288,19 +307,19 @@ func (m *GenericInlineUnionMock[T]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInlineUnionMock.Name") + m.t.Errorf("Expected call to GenericInlineUnionMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericInlineUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericInlineUnionMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericInlineUnionMock.Name") + m.t.Errorf("Expected call to GenericInlineUnionMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericInlineUnionMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericInlineUnionMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_inline_with_many_options.go b/tests/generic_inline_with_many_options.go index f729925..c98ac39 100644 --- a/tests/generic_inline_with_many_options.go +++ b/tests/generic_inline_with_many_options.go @@ -18,6 +18,7 @@ type GenericInlineUnionWithManyTypesMock[T int | float64 | string] struct { finishOnce sync.Once funcName func(t1 T) + funcNameOrigin string inspectFuncName func(t1 T) afterNameCounter uint64 beforeNameCounter uint64 @@ -49,16 +50,19 @@ type mGenericInlineUnionWithManyTypesMockName[T int | float64 | string] struct { callArgs []*GenericInlineUnionWithManyTypesMockNameParams[T] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericInlineUnionWithManyTypesMockNameExpectation specifies expectation struct of the genericInlineUnionWithManyTypes.Name type GenericInlineUnionWithManyTypesMockNameExpectation[T int | float64 | string] struct { - mock *GenericInlineUnionWithManyTypesMock[T] - params *GenericInlineUnionWithManyTypesMockNameParams[T] - paramPtrs *GenericInlineUnionWithManyTypesMockNameParamPtrs[T] + mock *GenericInlineUnionWithManyTypesMock[T] + params *GenericInlineUnionWithManyTypesMockNameParams[T] + paramPtrs *GenericInlineUnionWithManyTypesMockNameParamPtrs[T] + expectationOrigins GenericInlineUnionWithManyTypesMockNameExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // GenericInlineUnionWithManyTypesMockNameParams contains parameters of the genericInlineUnionWithManyTypes.Name @@ -71,6 +75,12 @@ type GenericInlineUnionWithManyTypesMockNameParamPtrs[T int | float64 | string] t1 *T } +// GenericInlineUnionWithManyTypesMockNameOrigins contains origins of expectations of the genericInlineUnionWithManyTypes.Name +type GenericInlineUnionWithManyTypesMockNameExpectationOrigins struct { + origin string + originT1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -96,6 +106,7 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Expect(t1 T) *mGeneri } mmName.defaultExpectation.params = &GenericInlineUnionWithManyTypesMockNameParams[T]{t1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -123,6 +134,7 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) ExpectT1Param1(t1 T) mmName.defaultExpectation.paramPtrs = &GenericInlineUnionWithManyTypesMockNameParamPtrs[T]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -148,6 +160,7 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Return() *GenericInli mmName.defaultExpectation = &GenericInlineUnionWithManyTypesMockNameExpectation[T]{mock: mmName.mock} } + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -162,6 +175,7 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Set(f func(t1 T)) *Ge } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -171,6 +185,7 @@ func (mmName *mGenericInlineUnionWithManyTypesMockName[T]) Times(n uint64) *mGen mmName.mock.t.Fatalf("Times of GenericInlineUnionWithManyTypesMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -190,6 +205,8 @@ func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1) } @@ -218,11 +235,13 @@ func (mmName *GenericInlineUnionWithManyTypesMock[T]) Name(t1 T) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } } else 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)) + mmName.t.Errorf("GenericInlineUnionWithManyTypesMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -280,7 +299,7 @@ func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -288,19 +307,19 @@ func (m *GenericInlineUnionWithManyTypesMock[T]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericInlineUnionWithManyTypesMock.Name") + m.t.Errorf("Expected call to GenericInlineUnionWithManyTypesMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericInlineUnionWithManyTypesMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericInlineUnionWithManyTypesMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_inout.go b/tests/generic_inout.go index 7d51a04..27f859b 100644 --- a/tests/generic_inout.go +++ b/tests/generic_inout.go @@ -18,6 +18,7 @@ type GenericInoutMock[T any] struct { finishOnce sync.Once funcName func(t1 T) (t2 T) + funcNameOrigin string inspectFuncName func(t1 T) afterNameCounter uint64 beforeNameCounter uint64 @@ -49,16 +50,19 @@ type mGenericInoutMockName[T any] struct { callArgs []*GenericInoutMockNameParams[T] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericInoutMockNameExpectation specifies expectation struct of the genericInout.Name type GenericInoutMockNameExpectation[T any] struct { - mock *GenericInoutMock[T] - params *GenericInoutMockNameParams[T] - paramPtrs *GenericInoutMockNameParamPtrs[T] - results *GenericInoutMockNameResults[T] - Counter uint64 + mock *GenericInoutMock[T] + params *GenericInoutMockNameParams[T] + paramPtrs *GenericInoutMockNameParamPtrs[T] + expectationOrigins GenericInoutMockNameExpectationOrigins + results *GenericInoutMockNameResults[T] + returnOrigin string + Counter uint64 } // GenericInoutMockNameParams contains parameters of the genericInout.Name @@ -76,6 +80,12 @@ type GenericInoutMockNameResults[T any] struct { t2 T } +// GenericInoutMockNameOrigins contains origins of expectations of the genericInout.Name +type GenericInoutMockNameExpectationOrigins struct { + origin string + originT1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -101,6 +111,7 @@ func (mmName *mGenericInoutMockName[T]) Expect(t1 T) *mGenericInoutMockName[T] { } mmName.defaultExpectation.params = &GenericInoutMockNameParams[T]{t1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -128,6 +139,7 @@ func (mmName *mGenericInoutMockName[T]) ExpectT1Param1(t1 T) *mGenericInoutMockN mmName.defaultExpectation.paramPtrs = &GenericInoutMockNameParamPtrs[T]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -153,6 +165,7 @@ func (mmName *mGenericInoutMockName[T]) Return(t2 T) *GenericInoutMock[T] { mmName.defaultExpectation = &GenericInoutMockNameExpectation[T]{mock: mmName.mock} } mmName.defaultExpectation.results = &GenericInoutMockNameResults[T]{t2} + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -167,6 +180,7 @@ func (mmName *mGenericInoutMockName[T]) Set(f func(t1 T) (t2 T)) *GenericInoutMo } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -178,8 +192,9 @@ func (mmName *mGenericInoutMockName[T]) When(t1 T) *GenericInoutMockNameExpectat } expectation := &GenericInoutMockNameExpectation[T]{ - mock: mmName.mock, - params: &GenericInoutMockNameParams[T]{t1}, + mock: mmName.mock, + params: &GenericInoutMockNameParams[T]{t1}, + expectationOrigins: GenericInoutMockNameExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmName.expectations = append(mmName.expectations, expectation) return expectation @@ -197,6 +212,7 @@ func (mmName *mGenericInoutMockName[T]) Times(n uint64) *mGenericInoutMockName[T mmName.mock.t.Fatalf("Times of GenericInoutMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -216,6 +232,8 @@ func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1) } @@ -244,11 +262,13 @@ func (mmName *GenericInoutMock[T]) Name(t1 T) (t2 T) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericInoutMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericInoutMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } } else 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)) + mmName.t.Errorf("GenericInoutMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmName.NameMock.defaultExpectation.results @@ -308,7 +328,7 @@ func (m *GenericInoutMock[T]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericInoutMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -316,19 +336,19 @@ func (m *GenericInoutMock[T]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericInoutMock.Name") + m.t.Errorf("Expected call to GenericInoutMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericInoutMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericInoutMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericInoutMock.Name") + m.t.Errorf("Expected call to GenericInoutMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericInoutMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericInoutMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_multiple_args_with_different_types.go b/tests/generic_multiple_args_with_different_types.go index e435c42..9ca161a 100644 --- a/tests/generic_multiple_args_with_different_types.go +++ b/tests/generic_multiple_args_with_different_types.go @@ -19,6 +19,7 @@ type GenericMultipleTypesMock[T proto.Message, K any] struct { finishOnce sync.Once funcName func(t1 T, k1 K) + funcNameOrigin string inspectFuncName func(t1 T, k1 K) afterNameCounter uint64 beforeNameCounter uint64 @@ -50,16 +51,19 @@ type mGenericMultipleTypesMockName[T proto.Message, K any] struct { callArgs []*GenericMultipleTypesMockNameParams[T, K] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericMultipleTypesMockNameExpectation specifies expectation struct of the genericMultipleTypes.Name type GenericMultipleTypesMockNameExpectation[T proto.Message, K any] struct { - mock *GenericMultipleTypesMock[T, K] - params *GenericMultipleTypesMockNameParams[T, K] - paramPtrs *GenericMultipleTypesMockNameParamPtrs[T, K] + mock *GenericMultipleTypesMock[T, K] + params *GenericMultipleTypesMockNameParams[T, K] + paramPtrs *GenericMultipleTypesMockNameParamPtrs[T, K] + expectationOrigins GenericMultipleTypesMockNameExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // GenericMultipleTypesMockNameParams contains parameters of the genericMultipleTypes.Name @@ -74,6 +78,13 @@ type GenericMultipleTypesMockNameParamPtrs[T proto.Message, K any] struct { k1 *K } +// GenericMultipleTypesMockNameOrigins contains origins of expectations of the genericMultipleTypes.Name +type GenericMultipleTypesMockNameExpectationOrigins struct { + origin string + originT1 string + originK1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -99,6 +110,7 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Expect(t1 T, k1 K) *mGenericM } mmName.defaultExpectation.params = &GenericMultipleTypesMockNameParams[T, K]{t1, k1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -126,6 +138,7 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) ExpectT1Param1(t1 T) *mGeneri mmName.defaultExpectation.paramPtrs = &GenericMultipleTypesMockNameParamPtrs[T, K]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -148,6 +161,7 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) ExpectK1Param2(k1 K) *mGeneri mmName.defaultExpectation.paramPtrs = &GenericMultipleTypesMockNameParamPtrs[T, K]{} } mmName.defaultExpectation.paramPtrs.k1 = &k1 + mmName.defaultExpectation.expectationOrigins.originK1 = minimock.CallerInfo(1) return mmName } @@ -173,6 +187,7 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Return() *GenericMultipleType mmName.defaultExpectation = &GenericMultipleTypesMockNameExpectation[T, K]{mock: mmName.mock} } + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -187,6 +202,7 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Set(f func(t1 T, k1 K)) *Gene } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -196,6 +212,7 @@ func (mmName *mGenericMultipleTypesMockName[T, K]) Times(n uint64) *mGenericMult mmName.mock.t.Fatalf("Times of GenericMultipleTypesMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -215,6 +232,8 @@ func (mmName *GenericMultipleTypesMock[T, K]) Name(t1 T, k1 K) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1, k1) } @@ -243,15 +262,18 @@ func (mmName *GenericMultipleTypesMock[T, K]) Name(t1 T, k1 K) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } if mm_want_ptrs.k1 != nil && !minimock.Equal(*mm_want_ptrs.k1, mm_got.k1) { - mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameter k1, want: %#v, got: %#v%s\n", *mm_want_ptrs.k1, mm_got.k1, minimock.Diff(*mm_want_ptrs.k1, mm_got.k1)) + mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameter k1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originK1, *mm_want_ptrs.k1, mm_got.k1, minimock.Diff(*mm_want_ptrs.k1, mm_got.k1)) } } else 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)) + mmName.t.Errorf("GenericMultipleTypesMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -309,7 +331,7 @@ func (m *GenericMultipleTypesMock[T, K]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericMultipleTypesMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -317,19 +339,19 @@ func (m *GenericMultipleTypesMock[T, K]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericMultipleTypesMock.Name") + m.t.Errorf("Expected call to GenericMultipleTypesMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericMultipleTypesMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericMultipleTypesMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericMultipleTypesMock.Name") + m.t.Errorf("Expected call to GenericMultipleTypesMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericMultipleTypesMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericMultipleTypesMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_out.go b/tests/generic_out.go index b81d64c..3b0a5d2 100644 --- a/tests/generic_out.go +++ b/tests/generic_out.go @@ -18,6 +18,7 @@ type GenericOutMock[T any] struct { finishOnce sync.Once funcName func() (t1 T) + funcNameOrigin string inspectFuncName func() afterNameCounter uint64 beforeNameCounter uint64 @@ -45,15 +46,17 @@ type mGenericOutMockName[T any] struct { defaultExpectation *GenericOutMockNameExpectation[T] expectations []*GenericOutMockNameExpectation[T] - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericOutMockNameExpectation specifies expectation struct of the genericOut.Name type GenericOutMockNameExpectation[T any] struct { mock *GenericOutMock[T] - results *GenericOutMockNameResults[T] - Counter uint64 + results *GenericOutMockNameResults[T] + returnOrigin string + Counter uint64 } // GenericOutMockNameResults contains results of the genericOut.Name @@ -105,6 +108,7 @@ func (mmName *mGenericOutMockName[T]) Return(t1 T) *GenericOutMock[T] { mmName.defaultExpectation = &GenericOutMockNameExpectation[T]{mock: mmName.mock} } mmName.defaultExpectation.results = &GenericOutMockNameResults[T]{t1} + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -119,6 +123,7 @@ func (mmName *mGenericOutMockName[T]) Set(f func() (t1 T)) *GenericOutMock[T] { } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -128,6 +133,7 @@ func (mmName *mGenericOutMockName[T]) Times(n uint64) *mGenericOutMockName[T] { mmName.mock.t.Fatalf("Times of GenericOutMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -147,6 +153,8 @@ func (mmName *GenericOutMock[T]) Name() (t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName() } @@ -205,16 +213,16 @@ func (m *GenericOutMock[T]) MinimockNameInspect() { afterNameCounter := mm_atomic.LoadUint64(&m.afterNameCounter) // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericOutMock.Name") + m.t.Errorf("Expected call to GenericOutMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericOutMock.Name") + m.t.Errorf("Expected call to GenericOutMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericOutMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericOutMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_simple_union.go b/tests/generic_simple_union.go index 9088970..9e1911b 100644 --- a/tests/generic_simple_union.go +++ b/tests/generic_simple_union.go @@ -18,6 +18,7 @@ type GenericSimpleUnionMock[T simpleUnion] struct { finishOnce sync.Once funcName func(t1 T) + funcNameOrigin string inspectFuncName func(t1 T) afterNameCounter uint64 beforeNameCounter uint64 @@ -49,16 +50,19 @@ type mGenericSimpleUnionMockName[T simpleUnion] struct { callArgs []*GenericSimpleUnionMockNameParams[T] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericSimpleUnionMockNameExpectation specifies expectation struct of the genericSimpleUnion.Name type GenericSimpleUnionMockNameExpectation[T simpleUnion] struct { - mock *GenericSimpleUnionMock[T] - params *GenericSimpleUnionMockNameParams[T] - paramPtrs *GenericSimpleUnionMockNameParamPtrs[T] + mock *GenericSimpleUnionMock[T] + params *GenericSimpleUnionMockNameParams[T] + paramPtrs *GenericSimpleUnionMockNameParamPtrs[T] + expectationOrigins GenericSimpleUnionMockNameExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // GenericSimpleUnionMockNameParams contains parameters of the genericSimpleUnion.Name @@ -71,6 +75,12 @@ type GenericSimpleUnionMockNameParamPtrs[T simpleUnion] struct { t1 *T } +// GenericSimpleUnionMockNameOrigins contains origins of expectations of the genericSimpleUnion.Name +type GenericSimpleUnionMockNameExpectationOrigins struct { + origin string + originT1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -96,6 +106,7 @@ func (mmName *mGenericSimpleUnionMockName[T]) Expect(t1 T) *mGenericSimpleUnionM } mmName.defaultExpectation.params = &GenericSimpleUnionMockNameParams[T]{t1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -123,6 +134,7 @@ func (mmName *mGenericSimpleUnionMockName[T]) ExpectT1Param1(t1 T) *mGenericSimp mmName.defaultExpectation.paramPtrs = &GenericSimpleUnionMockNameParamPtrs[T]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -148,6 +160,7 @@ func (mmName *mGenericSimpleUnionMockName[T]) Return() *GenericSimpleUnionMock[T mmName.defaultExpectation = &GenericSimpleUnionMockNameExpectation[T]{mock: mmName.mock} } + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -162,6 +175,7 @@ func (mmName *mGenericSimpleUnionMockName[T]) Set(f func(t1 T)) *GenericSimpleUn } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -171,6 +185,7 @@ func (mmName *mGenericSimpleUnionMockName[T]) Times(n uint64) *mGenericSimpleUni mmName.mock.t.Fatalf("Times of GenericSimpleUnionMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -190,6 +205,8 @@ func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1) } @@ -218,11 +235,13 @@ func (mmName *GenericSimpleUnionMock[T]) Name(t1 T) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericSimpleUnionMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericSimpleUnionMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } } else 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)) + mmName.t.Errorf("GenericSimpleUnionMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -280,7 +299,7 @@ func (m *GenericSimpleUnionMock[T]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -288,19 +307,19 @@ func (m *GenericSimpleUnionMock[T]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericSimpleUnionMock.Name") + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericSimpleUnionMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericSimpleUnionMock.Name") + m.t.Errorf("Expected call to GenericSimpleUnionMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericSimpleUnionMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericSimpleUnionMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/generic_specific.go b/tests/generic_specific.go index 667cd73..a883964 100644 --- a/tests/generic_specific.go +++ b/tests/generic_specific.go @@ -19,6 +19,7 @@ type GenericSpecificMock[T proto.Message] struct { finishOnce sync.Once funcName func(t1 T) + funcNameOrigin string inspectFuncName func(t1 T) afterNameCounter uint64 beforeNameCounter uint64 @@ -50,16 +51,19 @@ type mGenericSpecificMockName[T proto.Message] struct { callArgs []*GenericSpecificMockNameParams[T] mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // GenericSpecificMockNameExpectation specifies expectation struct of the genericSpecific.Name type GenericSpecificMockNameExpectation[T proto.Message] struct { - mock *GenericSpecificMock[T] - params *GenericSpecificMockNameParams[T] - paramPtrs *GenericSpecificMockNameParamPtrs[T] + mock *GenericSpecificMock[T] + params *GenericSpecificMockNameParams[T] + paramPtrs *GenericSpecificMockNameParamPtrs[T] + expectationOrigins GenericSpecificMockNameExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // GenericSpecificMockNameParams contains parameters of the genericSpecific.Name @@ -72,6 +76,12 @@ type GenericSpecificMockNameParamPtrs[T proto.Message] struct { t1 *T } +// GenericSpecificMockNameOrigins contains origins of expectations of the genericSpecific.Name +type GenericSpecificMockNameExpectationOrigins struct { + origin string + originT1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -97,6 +107,7 @@ func (mmName *mGenericSpecificMockName[T]) Expect(t1 T) *mGenericSpecificMockNam } mmName.defaultExpectation.params = &GenericSpecificMockNameParams[T]{t1} + mmName.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -124,6 +135,7 @@ func (mmName *mGenericSpecificMockName[T]) ExpectT1Param1(t1 T) *mGenericSpecifi mmName.defaultExpectation.paramPtrs = &GenericSpecificMockNameParamPtrs[T]{} } mmName.defaultExpectation.paramPtrs.t1 = &t1 + mmName.defaultExpectation.expectationOrigins.originT1 = minimock.CallerInfo(1) return mmName } @@ -149,6 +161,7 @@ func (mmName *mGenericSpecificMockName[T]) Return() *GenericSpecificMock[T] { mmName.defaultExpectation = &GenericSpecificMockNameExpectation[T]{mock: mmName.mock} } + mmName.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -163,6 +176,7 @@ func (mmName *mGenericSpecificMockName[T]) Set(f func(t1 T)) *GenericSpecificMoc } mmName.mock.funcName = f + mmName.mock.funcNameOrigin = minimock.CallerInfo(1) return mmName.mock } @@ -172,6 +186,7 @@ func (mmName *mGenericSpecificMockName[T]) Times(n uint64) *mGenericSpecificMock mmName.mock.t.Fatalf("Times of GenericSpecificMock.Name mock can not be zero") } mm_atomic.StoreUint64(&mmName.expectedInvocations, n) + mmName.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmName } @@ -191,6 +206,8 @@ func (mmName *GenericSpecificMock[T]) Name(t1 T) { mm_atomic.AddUint64(&mmName.beforeNameCounter, 1) defer mm_atomic.AddUint64(&mmName.afterNameCounter, 1) + mmName.t.Helper() + if mmName.inspectFuncName != nil { mmName.inspectFuncName(t1) } @@ -219,11 +236,13 @@ func (mmName *GenericSpecificMock[T]) Name(t1 T) { if mm_want_ptrs != nil { if mm_want_ptrs.t1 != nil && !minimock.Equal(*mm_want_ptrs.t1, mm_got.t1) { - mmName.t.Errorf("GenericSpecificMock.Name got unexpected parameter t1, want: %#v, got: %#v%s\n", *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) + mmName.t.Errorf("GenericSpecificMock.Name got unexpected parameter t1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.originT1, *mm_want_ptrs.t1, mm_got.t1, minimock.Diff(*mm_want_ptrs.t1, mm_got.t1)) } } else 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)) + mmName.t.Errorf("GenericSpecificMock.Name got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmName.NameMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -281,7 +300,7 @@ func (m *GenericSpecificMock[T]) MinimockNameDone() bool { 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) + m.t.Errorf("Expected call to GenericSpecificMock.Name at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -289,19 +308,19 @@ func (m *GenericSpecificMock[T]) MinimockNameInspect() { // if default expectation was set then invocations count should be greater than zero if m.NameMock.defaultExpectation != nil && afterNameCounter < 1 { if m.NameMock.defaultExpectation.params == nil { - m.t.Error("Expected call to GenericSpecificMock.Name") + m.t.Errorf("Expected call to GenericSpecificMock.Name at\n%s", m.NameMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to GenericSpecificMock.Name with params: %#v", *m.NameMock.defaultExpectation.params) + m.t.Errorf("Expected call to GenericSpecificMock.Name at\n%s with params: %#v", m.NameMock.defaultExpectation.expectationOrigins.origin, *m.NameMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcName != nil && afterNameCounter < 1 { - m.t.Error("Expected call to GenericSpecificMock.Name") + m.t.Errorf("Expected call to GenericSpecificMock.Name at\n%s", m.funcNameOrigin) } if !m.NameMock.invocationsDone() && afterNameCounter > 0 { - m.t.Errorf("Expected %d calls to GenericSpecificMock.Name but found %d calls", - mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), afterNameCounter) + m.t.Errorf("Expected %d calls to GenericSpecificMock.Name at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.NameMock.expectedInvocations), m.NameMock.expectedInvocationsOrigin, afterNameCounter) } } diff --git a/tests/package_name_specified_test.go b/tests/package_name_specified_test.go index 0fccc87..a7b6e4f 100644 --- a/tests/package_name_specified_test.go +++ b/tests/package_name_specified_test.go @@ -12,49 +12,62 @@ import ( "github.com/gojuno/minimock/v3" ) -// TesterMock implements minimock.Tester +// TesterMock implements mm_minimock.Tester type TesterMock struct { t minimock.Tester finishOnce sync.Once funcCleanup func(f func()) + funcCleanupOrigin string inspectFuncCleanup func(f func()) afterCleanupCounter uint64 beforeCleanupCounter uint64 CleanupMock mTesterMockCleanup funcError func(p1 ...interface{}) + funcErrorOrigin string inspectFuncError func(p1 ...interface{}) afterErrorCounter uint64 beforeErrorCounter uint64 ErrorMock mTesterMockError funcErrorf func(format string, args ...interface{}) + funcErrorfOrigin string inspectFuncErrorf func(format string, args ...interface{}) afterErrorfCounter uint64 beforeErrorfCounter uint64 ErrorfMock mTesterMockErrorf funcFailNow func() + funcFailNowOrigin string inspectFuncFailNow func() afterFailNowCounter uint64 beforeFailNowCounter uint64 FailNowMock mTesterMockFailNow funcFatal func(args ...interface{}) + funcFatalOrigin string inspectFuncFatal func(args ...interface{}) afterFatalCounter uint64 beforeFatalCounter uint64 FatalMock mTesterMockFatal funcFatalf func(format string, args ...interface{}) + funcFatalfOrigin string inspectFuncFatalf func(format string, args ...interface{}) afterFatalfCounter uint64 beforeFatalfCounter uint64 FatalfMock mTesterMockFatalf + + funcHelper func() + funcHelperOrigin string + inspectFuncHelper func() + afterHelperCounter uint64 + beforeHelperCounter uint64 + HelperMock mTesterMockHelper } -// NewTesterMock returns a mock for minimock.Tester +// NewTesterMock returns a mock for mm_minimock.Tester func NewTesterMock(t minimock.Tester) *TesterMock { m := &TesterMock{t: t} @@ -79,6 +92,8 @@ func NewTesterMock(t minimock.Tester) *TesterMock { m.FatalfMock = mTesterMockFatalf{mock: m} m.FatalfMock.callArgs = []*TesterMockFatalfParams{} + m.HelperMock = mTesterMockHelper{mock: m} + t.Cleanup(m.MinimockFinish) return m @@ -93,16 +108,19 @@ type mTesterMockCleanup struct { callArgs []*TesterMockCleanupParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockCleanupExpectation specifies expectation struct of the Tester.Cleanup type TesterMockCleanupExpectation struct { - mock *TesterMock - params *TesterMockCleanupParams - paramPtrs *TesterMockCleanupParamPtrs + mock *TesterMock + params *TesterMockCleanupParams + paramPtrs *TesterMockCleanupParamPtrs + expectationOrigins TesterMockCleanupExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockCleanupParams contains parameters of the Tester.Cleanup @@ -115,6 +133,12 @@ type TesterMockCleanupParamPtrs struct { f *func() } +// TesterMockCleanupOrigins contains origins of expectations of the Tester.Cleanup +type TesterMockCleanupExpectationOrigins struct { + origin string + originF string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -140,6 +164,7 @@ func (mmCleanup *mTesterMockCleanup) Expect(f func()) *mTesterMockCleanup { } mmCleanup.defaultExpectation.params = &TesterMockCleanupParams{f} + mmCleanup.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -167,6 +192,7 @@ func (mmCleanup *mTesterMockCleanup) ExpectFParam1(f func()) *mTesterMockCleanup mmCleanup.defaultExpectation.paramPtrs = &TesterMockCleanupParamPtrs{} } mmCleanup.defaultExpectation.paramPtrs.f = &f + mmCleanup.defaultExpectation.expectationOrigins.originF = minimock.CallerInfo(1) return mmCleanup } @@ -192,6 +218,7 @@ func (mmCleanup *mTesterMockCleanup) Return() *TesterMock { mmCleanup.defaultExpectation = &TesterMockCleanupExpectation{mock: mmCleanup.mock} } + mmCleanup.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmCleanup.mock } @@ -206,6 +233,7 @@ func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { } mmCleanup.mock.funcCleanup = f + mmCleanup.mock.funcCleanupOrigin = minimock.CallerInfo(1) return mmCleanup.mock } @@ -215,6 +243,7 @@ func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { mmCleanup.mock.t.Fatalf("Times of TesterMock.Cleanup mock can not be zero") } mm_atomic.StoreUint64(&mmCleanup.expectedInvocations, n) + mmCleanup.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmCleanup } @@ -229,11 +258,13 @@ func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Cleanup implements minimock.Tester +// Cleanup implements mm_minimock.Tester func (mmCleanup *TesterMock) Cleanup(f func()) { mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) defer mm_atomic.AddUint64(&mmCleanup.afterCleanupCounter, 1) + mmCleanup.t.Helper() + if mmCleanup.inspectFuncCleanup != nil { mmCleanup.inspectFuncCleanup(f) } @@ -262,11 +293,13 @@ func (mmCleanup *TesterMock) Cleanup(f func()) { if mm_want_ptrs != nil { if mm_want_ptrs.f != nil && !minimock.Equal(*mm_want_ptrs.f, mm_got.f) { - mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameter f, want: %#v, got: %#v%s\n", *mm_want_ptrs.f, mm_got.f, minimock.Diff(*mm_want_ptrs.f, mm_got.f)) + mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameter f, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmCleanup.CleanupMock.defaultExpectation.expectationOrigins.originF, *mm_want_ptrs.f, mm_got.f, minimock.Diff(*mm_want_ptrs.f, mm_got.f)) } } else 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)) + mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmCleanup.CleanupMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -324,7 +357,7 @@ func (m *TesterMock) MinimockCleanupDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -332,19 +365,19 @@ func (m *TesterMock) MinimockCleanupInspect() { // if default expectation was set then invocations count should be greater than zero if m.CleanupMock.defaultExpectation != nil && afterCleanupCounter < 1 { if m.CleanupMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Cleanup") + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s", m.CleanupMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Cleanup with params: %#v", *m.CleanupMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s with params: %#v", m.CleanupMock.defaultExpectation.expectationOrigins.origin, *m.CleanupMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcCleanup != nil && afterCleanupCounter < 1 { - m.t.Error("Expected call to TesterMock.Cleanup") + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s", m.funcCleanupOrigin) } if !m.CleanupMock.invocationsDone() && afterCleanupCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Cleanup but found %d calls", - mm_atomic.LoadUint64(&m.CleanupMock.expectedInvocations), afterCleanupCounter) + m.t.Errorf("Expected %d calls to TesterMock.Cleanup at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.CleanupMock.expectedInvocations), m.CleanupMock.expectedInvocationsOrigin, afterCleanupCounter) } } @@ -357,16 +390,19 @@ type mTesterMockError struct { callArgs []*TesterMockErrorParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockErrorExpectation specifies expectation struct of the Tester.Error type TesterMockErrorExpectation struct { - mock *TesterMock - params *TesterMockErrorParams - paramPtrs *TesterMockErrorParamPtrs + mock *TesterMock + params *TesterMockErrorParams + paramPtrs *TesterMockErrorParamPtrs + expectationOrigins TesterMockErrorExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockErrorParams contains parameters of the Tester.Error @@ -379,6 +415,12 @@ type TesterMockErrorParamPtrs struct { p1 *[]interface{} } +// TesterMockErrorOrigins contains origins of expectations of the Tester.Error +type TesterMockErrorExpectationOrigins struct { + origin string + originP1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -404,6 +446,7 @@ func (mmError *mTesterMockError) Expect(p1 ...interface{}) *mTesterMockError { } mmError.defaultExpectation.params = &TesterMockErrorParams{p1} + mmError.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -431,6 +474,7 @@ func (mmError *mTesterMockError) ExpectP1Param1(p1 ...interface{}) *mTesterMockE mmError.defaultExpectation.paramPtrs = &TesterMockErrorParamPtrs{} } mmError.defaultExpectation.paramPtrs.p1 = &p1 + mmError.defaultExpectation.expectationOrigins.originP1 = minimock.CallerInfo(1) return mmError } @@ -456,6 +500,7 @@ func (mmError *mTesterMockError) Return() *TesterMock { mmError.defaultExpectation = &TesterMockErrorExpectation{mock: mmError.mock} } + mmError.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmError.mock } @@ -470,6 +515,7 @@ func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { } mmError.mock.funcError = f + mmError.mock.funcErrorOrigin = minimock.CallerInfo(1) return mmError.mock } @@ -479,6 +525,7 @@ func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { mmError.mock.t.Fatalf("Times of TesterMock.Error mock can not be zero") } mm_atomic.StoreUint64(&mmError.expectedInvocations, n) + mmError.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmError } @@ -493,11 +540,13 @@ func (mmError *mTesterMockError) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Error implements minimock.Tester +// Error implements mm_minimock.Tester func (mmError *TesterMock) Error(p1 ...interface{}) { mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) defer mm_atomic.AddUint64(&mmError.afterErrorCounter, 1) + mmError.t.Helper() + if mmError.inspectFuncError != nil { mmError.inspectFuncError(p1...) } @@ -526,11 +575,13 @@ func (mmError *TesterMock) Error(p1 ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.p1 != nil && !minimock.Equal(*mm_want_ptrs.p1, mm_got.p1) { - mmError.t.Errorf("TesterMock.Error got unexpected parameter p1, want: %#v, got: %#v%s\n", *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) + mmError.t.Errorf("TesterMock.Error got unexpected parameter p1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmError.ErrorMock.defaultExpectation.expectationOrigins.originP1, *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) } } else 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)) + mmError.t.Errorf("TesterMock.Error got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmError.ErrorMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -588,7 +639,7 @@ func (m *TesterMock) MinimockErrorDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Error at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -596,19 +647,19 @@ func (m *TesterMock) MinimockErrorInspect() { // if default expectation was set then invocations count should be greater than zero if m.ErrorMock.defaultExpectation != nil && afterErrorCounter < 1 { if m.ErrorMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Error") + m.t.Errorf("Expected call to TesterMock.Error at\n%s", m.ErrorMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *m.ErrorMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Error at\n%s with params: %#v", m.ErrorMock.defaultExpectation.expectationOrigins.origin, *m.ErrorMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcError != nil && afterErrorCounter < 1 { - m.t.Error("Expected call to TesterMock.Error") + m.t.Errorf("Expected call to TesterMock.Error at\n%s", m.funcErrorOrigin) } if !m.ErrorMock.invocationsDone() && afterErrorCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Error but found %d calls", - mm_atomic.LoadUint64(&m.ErrorMock.expectedInvocations), afterErrorCounter) + m.t.Errorf("Expected %d calls to TesterMock.Error at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.ErrorMock.expectedInvocations), m.ErrorMock.expectedInvocationsOrigin, afterErrorCounter) } } @@ -621,16 +672,19 @@ type mTesterMockErrorf struct { callArgs []*TesterMockErrorfParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf type TesterMockErrorfExpectation struct { - mock *TesterMock - params *TesterMockErrorfParams - paramPtrs *TesterMockErrorfParamPtrs + mock *TesterMock + params *TesterMockErrorfParams + paramPtrs *TesterMockErrorfParamPtrs + expectationOrigins TesterMockErrorfExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockErrorfParams contains parameters of the Tester.Errorf @@ -645,6 +699,13 @@ type TesterMockErrorfParamPtrs struct { args *[]interface{} } +// TesterMockErrorfOrigins contains origins of expectations of the Tester.Errorf +type TesterMockErrorfExpectationOrigins struct { + origin string + originFormat string + originArgs string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -670,6 +731,7 @@ func (mmErrorf *mTesterMockErrorf) Expect(format string, args ...interface{}) *m } mmErrorf.defaultExpectation.params = &TesterMockErrorfParams{format, args} + mmErrorf.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -697,6 +759,7 @@ func (mmErrorf *mTesterMockErrorf) ExpectFormatParam1(format string) *mTesterMoc mmErrorf.defaultExpectation.paramPtrs = &TesterMockErrorfParamPtrs{} } mmErrorf.defaultExpectation.paramPtrs.format = &format + mmErrorf.defaultExpectation.expectationOrigins.originFormat = minimock.CallerInfo(1) return mmErrorf } @@ -719,6 +782,7 @@ func (mmErrorf *mTesterMockErrorf) ExpectArgsParam2(args ...interface{}) *mTeste mmErrorf.defaultExpectation.paramPtrs = &TesterMockErrorfParamPtrs{} } mmErrorf.defaultExpectation.paramPtrs.args = &args + mmErrorf.defaultExpectation.expectationOrigins.originArgs = minimock.CallerInfo(1) return mmErrorf } @@ -744,6 +808,7 @@ func (mmErrorf *mTesterMockErrorf) Return() *TesterMock { mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{mock: mmErrorf.mock} } + mmErrorf.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmErrorf.mock } @@ -758,6 +823,7 @@ func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{} } mmErrorf.mock.funcErrorf = f + mmErrorf.mock.funcErrorfOrigin = minimock.CallerInfo(1) return mmErrorf.mock } @@ -767,6 +833,7 @@ func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { mmErrorf.mock.t.Fatalf("Times of TesterMock.Errorf mock can not be zero") } mm_atomic.StoreUint64(&mmErrorf.expectedInvocations, n) + mmErrorf.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmErrorf } @@ -781,11 +848,13 @@ func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Errorf implements minimock.Tester +// Errorf implements mm_minimock.Tester func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) defer mm_atomic.AddUint64(&mmErrorf.afterErrorfCounter, 1) + mmErrorf.t.Helper() + if mmErrorf.inspectFuncErrorf != nil { mmErrorf.inspectFuncErrorf(format, args...) } @@ -814,15 +883,18 @@ func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.format != nil && !minimock.Equal(*mm_want_ptrs.format, mm_got.format) { - mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter format, want: %#v, got: %#v%s\n", *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) + mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter format, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmErrorf.ErrorfMock.defaultExpectation.expectationOrigins.originFormat, *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) } if mm_want_ptrs.args != nil && !minimock.Equal(*mm_want_ptrs.args, mm_got.args) { - mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter args, want: %#v, got: %#v%s\n", *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) + mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter args, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmErrorf.ErrorfMock.defaultExpectation.expectationOrigins.originArgs, *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) } } else 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)) + mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmErrorf.ErrorfMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -880,7 +952,7 @@ func (m *TesterMock) MinimockErrorfDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -888,19 +960,19 @@ func (m *TesterMock) MinimockErrorfInspect() { // if default expectation was set then invocations count should be greater than zero if m.ErrorfMock.defaultExpectation != nil && afterErrorfCounter < 1 { if m.ErrorfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Errorf") + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s", m.ErrorfMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *m.ErrorfMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s with params: %#v", m.ErrorfMock.defaultExpectation.expectationOrigins.origin, *m.ErrorfMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcErrorf != nil && afterErrorfCounter < 1 { - m.t.Error("Expected call to TesterMock.Errorf") + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s", m.funcErrorfOrigin) } if !m.ErrorfMock.invocationsDone() && afterErrorfCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Errorf but found %d calls", - mm_atomic.LoadUint64(&m.ErrorfMock.expectedInvocations), afterErrorfCounter) + m.t.Errorf("Expected %d calls to TesterMock.Errorf at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.ErrorfMock.expectedInvocations), m.ErrorfMock.expectedInvocationsOrigin, afterErrorfCounter) } } @@ -910,14 +982,16 @@ type mTesterMockFailNow struct { defaultExpectation *TesterMockFailNowExpectation expectations []*TesterMockFailNowExpectation - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockFailNowExpectation specifies expectation struct of the Tester.FailNow type TesterMockFailNowExpectation struct { mock *TesterMock - Counter uint64 + returnOrigin string + Counter uint64 } // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning @@ -964,6 +1038,7 @@ func (mmFailNow *mTesterMockFailNow) Return() *TesterMock { mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{mock: mmFailNow.mock} } + mmFailNow.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFailNow.mock } @@ -978,6 +1053,7 @@ func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { } mmFailNow.mock.funcFailNow = f + mmFailNow.mock.funcFailNowOrigin = minimock.CallerInfo(1) return mmFailNow.mock } @@ -987,6 +1063,7 @@ func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { mmFailNow.mock.t.Fatalf("Times of TesterMock.FailNow mock can not be zero") } mm_atomic.StoreUint64(&mmFailNow.expectedInvocations, n) + mmFailNow.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFailNow } @@ -1001,11 +1078,13 @@ func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// FailNow implements minimock.Tester +// FailNow implements mm_minimock.Tester func (mmFailNow *TesterMock) FailNow() { mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) defer mm_atomic.AddUint64(&mmFailNow.afterFailNowCounter, 1) + mmFailNow.t.Helper() + if mmFailNow.inspectFuncFailNow != nil { mmFailNow.inspectFuncFailNow() } @@ -1062,16 +1141,16 @@ func (m *TesterMock) MinimockFailNowInspect() { afterFailNowCounter := mm_atomic.LoadUint64(&m.afterFailNowCounter) // if default expectation was set then invocations count should be greater than zero if m.FailNowMock.defaultExpectation != nil && afterFailNowCounter < 1 { - m.t.Error("Expected call to TesterMock.FailNow") + m.t.Errorf("Expected call to TesterMock.FailNow at\n%s", m.FailNowMock.defaultExpectation.returnOrigin) } // if func was set then invocations count should be greater than zero if m.funcFailNow != nil && afterFailNowCounter < 1 { - m.t.Error("Expected call to TesterMock.FailNow") + m.t.Errorf("Expected call to TesterMock.FailNow at\n%s", m.funcFailNowOrigin) } if !m.FailNowMock.invocationsDone() && afterFailNowCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.FailNow but found %d calls", - mm_atomic.LoadUint64(&m.FailNowMock.expectedInvocations), afterFailNowCounter) + m.t.Errorf("Expected %d calls to TesterMock.FailNow at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FailNowMock.expectedInvocations), m.FailNowMock.expectedInvocationsOrigin, afterFailNowCounter) } } @@ -1084,16 +1163,19 @@ type mTesterMockFatal struct { callArgs []*TesterMockFatalParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal type TesterMockFatalExpectation struct { - mock *TesterMock - params *TesterMockFatalParams - paramPtrs *TesterMockFatalParamPtrs + mock *TesterMock + params *TesterMockFatalParams + paramPtrs *TesterMockFatalParamPtrs + expectationOrigins TesterMockFatalExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockFatalParams contains parameters of the Tester.Fatal @@ -1106,6 +1188,12 @@ type TesterMockFatalParamPtrs struct { args *[]interface{} } +// TesterMockFatalOrigins contains origins of expectations of the Tester.Fatal +type TesterMockFatalExpectationOrigins struct { + origin string + originArgs string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -1131,6 +1219,7 @@ func (mmFatal *mTesterMockFatal) Expect(args ...interface{}) *mTesterMockFatal { } mmFatal.defaultExpectation.params = &TesterMockFatalParams{args} + mmFatal.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -1158,6 +1247,7 @@ func (mmFatal *mTesterMockFatal) ExpectArgsParam1(args ...interface{}) *mTesterM mmFatal.defaultExpectation.paramPtrs = &TesterMockFatalParamPtrs{} } mmFatal.defaultExpectation.paramPtrs.args = &args + mmFatal.defaultExpectation.expectationOrigins.originArgs = minimock.CallerInfo(1) return mmFatal } @@ -1183,6 +1273,7 @@ func (mmFatal *mTesterMockFatal) Return() *TesterMock { mmFatal.defaultExpectation = &TesterMockFatalExpectation{mock: mmFatal.mock} } + mmFatal.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFatal.mock } @@ -1197,6 +1288,7 @@ func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { } mmFatal.mock.funcFatal = f + mmFatal.mock.funcFatalOrigin = minimock.CallerInfo(1) return mmFatal.mock } @@ -1206,6 +1298,7 @@ func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { mmFatal.mock.t.Fatalf("Times of TesterMock.Fatal mock can not be zero") } mm_atomic.StoreUint64(&mmFatal.expectedInvocations, n) + mmFatal.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFatal } @@ -1220,11 +1313,13 @@ func (mmFatal *mTesterMockFatal) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Fatal implements minimock.Tester +// Fatal implements mm_minimock.Tester func (mmFatal *TesterMock) Fatal(args ...interface{}) { mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) defer mm_atomic.AddUint64(&mmFatal.afterFatalCounter, 1) + mmFatal.t.Helper() + if mmFatal.inspectFuncFatal != nil { mmFatal.inspectFuncFatal(args...) } @@ -1253,11 +1348,13 @@ func (mmFatal *TesterMock) Fatal(args ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.args != nil && !minimock.Equal(*mm_want_ptrs.args, mm_got.args) { - mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameter args, want: %#v, got: %#v%s\n", *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) + mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameter args, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatal.FatalMock.defaultExpectation.expectationOrigins.originArgs, *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) } } else 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)) + mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatal.FatalMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -1315,7 +1412,7 @@ func (m *TesterMock) MinimockFatalDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -1323,19 +1420,19 @@ func (m *TesterMock) MinimockFatalInspect() { // if default expectation was set then invocations count should be greater than zero if m.FatalMock.defaultExpectation != nil && afterFatalCounter < 1 { if m.FatalMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatal") + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s", m.FatalMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *m.FatalMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s with params: %#v", m.FatalMock.defaultExpectation.expectationOrigins.origin, *m.FatalMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFatal != nil && afterFatalCounter < 1 { - m.t.Error("Expected call to TesterMock.Fatal") + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s", m.funcFatalOrigin) } if !m.FatalMock.invocationsDone() && afterFatalCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Fatal but found %d calls", - mm_atomic.LoadUint64(&m.FatalMock.expectedInvocations), afterFatalCounter) + m.t.Errorf("Expected %d calls to TesterMock.Fatal at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FatalMock.expectedInvocations), m.FatalMock.expectedInvocationsOrigin, afterFatalCounter) } } @@ -1348,16 +1445,19 @@ type mTesterMockFatalf struct { callArgs []*TesterMockFatalfParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf type TesterMockFatalfExpectation struct { - mock *TesterMock - params *TesterMockFatalfParams - paramPtrs *TesterMockFatalfParamPtrs + mock *TesterMock + params *TesterMockFatalfParams + paramPtrs *TesterMockFatalfParamPtrs + expectationOrigins TesterMockFatalfExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockFatalfParams contains parameters of the Tester.Fatalf @@ -1372,6 +1472,13 @@ type TesterMockFatalfParamPtrs struct { args *[]interface{} } +// TesterMockFatalfOrigins contains origins of expectations of the Tester.Fatalf +type TesterMockFatalfExpectationOrigins struct { + origin string + originFormat string + originArgs string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -1397,6 +1504,7 @@ func (mmFatalf *mTesterMockFatalf) Expect(format string, args ...interface{}) *m } mmFatalf.defaultExpectation.params = &TesterMockFatalfParams{format, args} + mmFatalf.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -1424,6 +1532,7 @@ func (mmFatalf *mTesterMockFatalf) ExpectFormatParam1(format string) *mTesterMoc mmFatalf.defaultExpectation.paramPtrs = &TesterMockFatalfParamPtrs{} } mmFatalf.defaultExpectation.paramPtrs.format = &format + mmFatalf.defaultExpectation.expectationOrigins.originFormat = minimock.CallerInfo(1) return mmFatalf } @@ -1446,6 +1555,7 @@ func (mmFatalf *mTesterMockFatalf) ExpectArgsParam2(args ...interface{}) *mTeste mmFatalf.defaultExpectation.paramPtrs = &TesterMockFatalfParamPtrs{} } mmFatalf.defaultExpectation.paramPtrs.args = &args + mmFatalf.defaultExpectation.expectationOrigins.originArgs = minimock.CallerInfo(1) return mmFatalf } @@ -1471,6 +1581,7 @@ func (mmFatalf *mTesterMockFatalf) Return() *TesterMock { mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{mock: mmFatalf.mock} } + mmFatalf.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFatalf.mock } @@ -1485,6 +1596,7 @@ func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{} } mmFatalf.mock.funcFatalf = f + mmFatalf.mock.funcFatalfOrigin = minimock.CallerInfo(1) return mmFatalf.mock } @@ -1494,6 +1606,7 @@ func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { mmFatalf.mock.t.Fatalf("Times of TesterMock.Fatalf mock can not be zero") } mm_atomic.StoreUint64(&mmFatalf.expectedInvocations, n) + mmFatalf.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFatalf } @@ -1508,11 +1621,13 @@ func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Fatalf implements minimock.Tester +// Fatalf implements mm_minimock.Tester func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) defer mm_atomic.AddUint64(&mmFatalf.afterFatalfCounter, 1) + mmFatalf.t.Helper() + if mmFatalf.inspectFuncFatalf != nil { mmFatalf.inspectFuncFatalf(format, args...) } @@ -1541,15 +1656,18 @@ func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.format != nil && !minimock.Equal(*mm_want_ptrs.format, mm_got.format) { - mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter format, want: %#v, got: %#v%s\n", *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) + mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter format, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatalf.FatalfMock.defaultExpectation.expectationOrigins.originFormat, *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) } if mm_want_ptrs.args != nil && !minimock.Equal(*mm_want_ptrs.args, mm_got.args) { - mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter args, want: %#v, got: %#v%s\n", *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) + mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter args, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatalf.FatalfMock.defaultExpectation.expectationOrigins.originArgs, *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) } } else 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)) + mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatalf.FatalfMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -1607,7 +1725,7 @@ func (m *TesterMock) MinimockFatalfDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -1615,19 +1733,197 @@ func (m *TesterMock) MinimockFatalfInspect() { // if default expectation was set then invocations count should be greater than zero if m.FatalfMock.defaultExpectation != nil && afterFatalfCounter < 1 { if m.FatalfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatalf") + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s", m.FatalfMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *m.FatalfMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s with params: %#v", m.FatalfMock.defaultExpectation.expectationOrigins.origin, *m.FatalfMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFatalf != nil && afterFatalfCounter < 1 { - m.t.Error("Expected call to TesterMock.Fatalf") + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s", m.funcFatalfOrigin) } if !m.FatalfMock.invocationsDone() && afterFatalfCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Fatalf but found %d calls", - mm_atomic.LoadUint64(&m.FatalfMock.expectedInvocations), afterFatalfCounter) + m.t.Errorf("Expected %d calls to TesterMock.Fatalf at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FatalfMock.expectedInvocations), m.FatalfMock.expectedInvocationsOrigin, afterFatalfCounter) + } +} + +type mTesterMockHelper struct { + optional bool + mock *TesterMock + defaultExpectation *TesterMockHelperExpectation + expectations []*TesterMockHelperExpectation + + expectedInvocations uint64 + expectedInvocationsOrigin string +} + +// TesterMockHelperExpectation specifies expectation struct of the Tester.Helper +type TesterMockHelperExpectation struct { + mock *TesterMock + + returnOrigin string + Counter uint64 +} + +// Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning +// the test will fail minimock's automatic final call check if the mocked method was not called at least once. +// Optional() makes method check to work in '0 or more' mode. +// It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to +// catch the problems when the expected method call is totally skipped during test run. +func (mmHelper *mTesterMockHelper) Optional() *mTesterMockHelper { + mmHelper.optional = true + return mmHelper +} + +// Expect sets up expected params for Tester.Helper +func (mmHelper *mTesterMockHelper) Expect() *mTesterMockHelper { + if mmHelper.mock.funcHelper != nil { + mmHelper.mock.t.Fatalf("TesterMock.Helper mock is already set by Set") + } + + if mmHelper.defaultExpectation == nil { + mmHelper.defaultExpectation = &TesterMockHelperExpectation{} + } + + return mmHelper +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Helper +func (mmHelper *mTesterMockHelper) Inspect(f func()) *mTesterMockHelper { + if mmHelper.mock.inspectFuncHelper != nil { + mmHelper.mock.t.Fatalf("Inspect function is already set for TesterMock.Helper") + } + + mmHelper.mock.inspectFuncHelper = f + + return mmHelper +} + +// Return sets up results that will be returned by Tester.Helper +func (mmHelper *mTesterMockHelper) Return() *TesterMock { + if mmHelper.mock.funcHelper != nil { + mmHelper.mock.t.Fatalf("TesterMock.Helper mock is already set by Set") + } + + if mmHelper.defaultExpectation == nil { + mmHelper.defaultExpectation = &TesterMockHelperExpectation{mock: mmHelper.mock} + } + + mmHelper.defaultExpectation.returnOrigin = minimock.CallerInfo(1) + return mmHelper.mock +} + +// Set uses given function f to mock the Tester.Helper method +func (mmHelper *mTesterMockHelper) Set(f func()) *TesterMock { + if mmHelper.defaultExpectation != nil { + mmHelper.mock.t.Fatalf("Default expectation is already set for the Tester.Helper method") + } + + if len(mmHelper.expectations) > 0 { + mmHelper.mock.t.Fatalf("Some expectations are already set for the Tester.Helper method") + } + + mmHelper.mock.funcHelper = f + mmHelper.mock.funcHelperOrigin = minimock.CallerInfo(1) + return mmHelper.mock +} + +// Times sets number of times Tester.Helper should be invoked +func (mmHelper *mTesterMockHelper) Times(n uint64) *mTesterMockHelper { + if n == 0 { + mmHelper.mock.t.Fatalf("Times of TesterMock.Helper mock can not be zero") + } + mm_atomic.StoreUint64(&mmHelper.expectedInvocations, n) + mmHelper.expectedInvocationsOrigin = minimock.CallerInfo(1) + return mmHelper +} + +func (mmHelper *mTesterMockHelper) invocationsDone() bool { + if len(mmHelper.expectations) == 0 && mmHelper.defaultExpectation == nil && mmHelper.mock.funcHelper == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmHelper.mock.afterHelperCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmHelper.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + +// Helper implements mm_minimock.Tester +func (mmHelper *TesterMock) Helper() { + mm_atomic.AddUint64(&mmHelper.beforeHelperCounter, 1) + defer mm_atomic.AddUint64(&mmHelper.afterHelperCounter, 1) + + mmHelper.t.Helper() + + if mmHelper.inspectFuncHelper != nil { + mmHelper.inspectFuncHelper() + } + + if mmHelper.HelperMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmHelper.HelperMock.defaultExpectation.Counter, 1) + + return + + } + if mmHelper.funcHelper != nil { + mmHelper.funcHelper() + return + } + mmHelper.t.Fatalf("Unexpected call to TesterMock.Helper.") + +} + +// HelperAfterCounter returns a count of finished TesterMock.Helper invocations +func (mmHelper *TesterMock) HelperAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmHelper.afterHelperCounter) +} + +// HelperBeforeCounter returns a count of TesterMock.Helper invocations +func (mmHelper *TesterMock) HelperBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmHelper.beforeHelperCounter) +} + +// MinimockHelperDone returns true if the count of the Helper invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockHelperDone() bool { + if m.HelperMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + + for _, e := range m.HelperMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + return m.HelperMock.invocationsDone() +} + +// MinimockHelperInspect logs each unmet expectation +func (m *TesterMock) MinimockHelperInspect() { + for _, e := range m.HelperMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Error("Expected call to TesterMock.Helper") + } + } + + afterHelperCounter := mm_atomic.LoadUint64(&m.afterHelperCounter) + // if default expectation was set then invocations count should be greater than zero + if m.HelperMock.defaultExpectation != nil && afterHelperCounter < 1 { + m.t.Errorf("Expected call to TesterMock.Helper at\n%s", m.HelperMock.defaultExpectation.returnOrigin) + } + // if func was set then invocations count should be greater than zero + if m.funcHelper != nil && afterHelperCounter < 1 { + m.t.Errorf("Expected call to TesterMock.Helper at\n%s", m.funcHelperOrigin) + } + + if !m.HelperMock.invocationsDone() && afterHelperCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Helper at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.HelperMock.expectedInvocations), m.HelperMock.expectedInvocationsOrigin, afterHelperCounter) } } @@ -1646,6 +1942,8 @@ func (m *TesterMock) MinimockFinish() { m.MinimockFatalInspect() m.MinimockFatalfInspect() + + m.MinimockHelperInspect() } }) } @@ -1674,5 +1972,6 @@ func (m *TesterMock) minimockDone() bool { m.MinimockErrorfDone() && m.MinimockFailNowDone() && m.MinimockFatalDone() && - m.MinimockFatalfDone() + m.MinimockFatalfDone() && + m.MinimockHelperDone() } diff --git a/tests/reader_mock.go b/tests/reader_mock.go index cf2dd7a..04decdd 100644 --- a/tests/reader_mock.go +++ b/tests/reader_mock.go @@ -18,6 +18,7 @@ type ReaderMock struct { finishOnce sync.Once funcRead func(p []byte) (n int, err error) + funcReadOrigin string inspectFuncRead func(p []byte) afterReadCounter uint64 beforeReadCounter uint64 @@ -49,16 +50,19 @@ type mReaderMockRead struct { callArgs []*ReaderMockReadParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // ReaderMockReadExpectation specifies expectation struct of the reader.Read type ReaderMockReadExpectation struct { - mock *ReaderMock - params *ReaderMockReadParams - paramPtrs *ReaderMockReadParamPtrs - results *ReaderMockReadResults - Counter uint64 + mock *ReaderMock + params *ReaderMockReadParams + paramPtrs *ReaderMockReadParamPtrs + expectationOrigins ReaderMockReadExpectationOrigins + results *ReaderMockReadResults + returnOrigin string + Counter uint64 } // ReaderMockReadParams contains parameters of the reader.Read @@ -77,6 +81,12 @@ type ReaderMockReadResults struct { err error } +// ReaderMockReadOrigins contains origins of expectations of the reader.Read +type ReaderMockReadExpectationOrigins struct { + origin string + originP string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -102,6 +112,7 @@ func (mmRead *mReaderMockRead) Expect(p []byte) *mReaderMockRead { } mmRead.defaultExpectation.params = &ReaderMockReadParams{p} + mmRead.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) for _, e := range mmRead.expectations { if minimock.Equal(e.params, mmRead.defaultExpectation.params) { mmRead.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmRead.defaultExpectation.params) @@ -129,6 +140,7 @@ func (mmRead *mReaderMockRead) ExpectPParam1(p []byte) *mReaderMockRead { mmRead.defaultExpectation.paramPtrs = &ReaderMockReadParamPtrs{} } mmRead.defaultExpectation.paramPtrs.p = &p + mmRead.defaultExpectation.expectationOrigins.originP = minimock.CallerInfo(1) return mmRead } @@ -154,6 +166,7 @@ func (mmRead *mReaderMockRead) Return(n int, err error) *ReaderMock { mmRead.defaultExpectation = &ReaderMockReadExpectation{mock: mmRead.mock} } mmRead.defaultExpectation.results = &ReaderMockReadResults{n, err} + mmRead.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmRead.mock } @@ -168,6 +181,7 @@ func (mmRead *mReaderMockRead) Set(f func(p []byte) (n int, err error)) *ReaderM } mmRead.mock.funcRead = f + mmRead.mock.funcReadOrigin = minimock.CallerInfo(1) return mmRead.mock } @@ -179,8 +193,9 @@ func (mmRead *mReaderMockRead) When(p []byte) *ReaderMockReadExpectation { } expectation := &ReaderMockReadExpectation{ - mock: mmRead.mock, - params: &ReaderMockReadParams{p}, + mock: mmRead.mock, + params: &ReaderMockReadParams{p}, + expectationOrigins: ReaderMockReadExpectationOrigins{origin: minimock.CallerInfo(1)}, } mmRead.expectations = append(mmRead.expectations, expectation) return expectation @@ -198,6 +213,7 @@ func (mmRead *mReaderMockRead) Times(n uint64) *mReaderMockRead { mmRead.mock.t.Fatalf("Times of ReaderMock.Read mock can not be zero") } mm_atomic.StoreUint64(&mmRead.expectedInvocations, n) + mmRead.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmRead } @@ -217,6 +233,8 @@ func (mmRead *ReaderMock) Read(p []byte) (n int, err error) { mm_atomic.AddUint64(&mmRead.beforeReadCounter, 1) defer mm_atomic.AddUint64(&mmRead.afterReadCounter, 1) + mmRead.t.Helper() + if mmRead.inspectFuncRead != nil { mmRead.inspectFuncRead(p) } @@ -245,11 +263,13 @@ func (mmRead *ReaderMock) Read(p []byte) (n int, err error) { if mm_want_ptrs != nil { if mm_want_ptrs.p != nil && !minimock.Equal(*mm_want_ptrs.p, mm_got.p) { - mmRead.t.Errorf("ReaderMock.Read got unexpected parameter p, want: %#v, got: %#v%s\n", *mm_want_ptrs.p, mm_got.p, minimock.Diff(*mm_want_ptrs.p, mm_got.p)) + mmRead.t.Errorf("ReaderMock.Read got unexpected parameter p, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmRead.ReadMock.defaultExpectation.expectationOrigins.originP, *mm_want_ptrs.p, mm_got.p, minimock.Diff(*mm_want_ptrs.p, mm_got.p)) } } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { - mmRead.t.Errorf("ReaderMock.Read got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) + mmRead.t.Errorf("ReaderMock.Read got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmRead.ReadMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } mm_results := mmRead.ReadMock.defaultExpectation.results @@ -309,7 +329,7 @@ func (m *ReaderMock) MinimockReadDone() bool { func (m *ReaderMock) MinimockReadInspect() { for _, e := range m.ReadMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { - m.t.Errorf("Expected call to ReaderMock.Read with params: %#v", *e.params) + m.t.Errorf("Expected call to ReaderMock.Read at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -317,19 +337,19 @@ func (m *ReaderMock) MinimockReadInspect() { // if default expectation was set then invocations count should be greater than zero if m.ReadMock.defaultExpectation != nil && afterReadCounter < 1 { if m.ReadMock.defaultExpectation.params == nil { - m.t.Error("Expected call to ReaderMock.Read") + m.t.Errorf("Expected call to ReaderMock.Read at\n%s", m.ReadMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to ReaderMock.Read with params: %#v", *m.ReadMock.defaultExpectation.params) + m.t.Errorf("Expected call to ReaderMock.Read at\n%s with params: %#v", m.ReadMock.defaultExpectation.expectationOrigins.origin, *m.ReadMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcRead != nil && afterReadCounter < 1 { - m.t.Error("Expected call to ReaderMock.Read") + m.t.Errorf("Expected call to ReaderMock.Read at\n%s", m.funcReadOrigin) } if !m.ReadMock.invocationsDone() && afterReadCounter > 0 { - m.t.Errorf("Expected %d calls to ReaderMock.Read but found %d calls", - mm_atomic.LoadUint64(&m.ReadMock.expectedInvocations), afterReadCounter) + m.t.Errorf("Expected %d calls to ReaderMock.Read at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.ReadMock.expectedInvocations), m.ReadMock.expectedInvocationsOrigin, afterReadCounter) } } diff --git a/tests/tester_mock_test.go b/tests/tester_mock_test.go index 93f2f73..aedd7c0 100644 --- a/tests/tester_mock_test.go +++ b/tests/tester_mock_test.go @@ -12,49 +12,62 @@ import ( "github.com/gojuno/minimock/v3" ) -// TesterMock implements minimock.Tester +// TesterMock implements mm_minimock.Tester type TesterMock struct { t minimock.Tester finishOnce sync.Once funcCleanup func(f func()) + funcCleanupOrigin string inspectFuncCleanup func(f func()) afterCleanupCounter uint64 beforeCleanupCounter uint64 CleanupMock mTesterMockCleanup funcError func(p1 ...interface{}) + funcErrorOrigin string inspectFuncError func(p1 ...interface{}) afterErrorCounter uint64 beforeErrorCounter uint64 ErrorMock mTesterMockError funcErrorf func(format string, args ...interface{}) + funcErrorfOrigin string inspectFuncErrorf func(format string, args ...interface{}) afterErrorfCounter uint64 beforeErrorfCounter uint64 ErrorfMock mTesterMockErrorf funcFailNow func() + funcFailNowOrigin string inspectFuncFailNow func() afterFailNowCounter uint64 beforeFailNowCounter uint64 FailNowMock mTesterMockFailNow funcFatal func(args ...interface{}) + funcFatalOrigin string inspectFuncFatal func(args ...interface{}) afterFatalCounter uint64 beforeFatalCounter uint64 FatalMock mTesterMockFatal funcFatalf func(format string, args ...interface{}) + funcFatalfOrigin string inspectFuncFatalf func(format string, args ...interface{}) afterFatalfCounter uint64 beforeFatalfCounter uint64 FatalfMock mTesterMockFatalf + + funcHelper func() + funcHelperOrigin string + inspectFuncHelper func() + afterHelperCounter uint64 + beforeHelperCounter uint64 + HelperMock mTesterMockHelper } -// NewTesterMock returns a mock for minimock.Tester +// NewTesterMock returns a mock for mm_minimock.Tester func NewTesterMock(t minimock.Tester) *TesterMock { m := &TesterMock{t: t} @@ -79,6 +92,8 @@ func NewTesterMock(t minimock.Tester) *TesterMock { m.FatalfMock = mTesterMockFatalf{mock: m} m.FatalfMock.callArgs = []*TesterMockFatalfParams{} + m.HelperMock = mTesterMockHelper{mock: m} + t.Cleanup(m.MinimockFinish) return m @@ -93,16 +108,19 @@ type mTesterMockCleanup struct { callArgs []*TesterMockCleanupParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockCleanupExpectation specifies expectation struct of the Tester.Cleanup type TesterMockCleanupExpectation struct { - mock *TesterMock - params *TesterMockCleanupParams - paramPtrs *TesterMockCleanupParamPtrs + mock *TesterMock + params *TesterMockCleanupParams + paramPtrs *TesterMockCleanupParamPtrs + expectationOrigins TesterMockCleanupExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockCleanupParams contains parameters of the Tester.Cleanup @@ -115,6 +133,12 @@ type TesterMockCleanupParamPtrs struct { f *func() } +// TesterMockCleanupOrigins contains origins of expectations of the Tester.Cleanup +type TesterMockCleanupExpectationOrigins struct { + origin string + originF string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -140,6 +164,7 @@ func (mmCleanup *mTesterMockCleanup) Expect(f func()) *mTesterMockCleanup { } mmCleanup.defaultExpectation.params = &TesterMockCleanupParams{f} + mmCleanup.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -167,6 +192,7 @@ func (mmCleanup *mTesterMockCleanup) ExpectFParam1(f func()) *mTesterMockCleanup mmCleanup.defaultExpectation.paramPtrs = &TesterMockCleanupParamPtrs{} } mmCleanup.defaultExpectation.paramPtrs.f = &f + mmCleanup.defaultExpectation.expectationOrigins.originF = minimock.CallerInfo(1) return mmCleanup } @@ -192,6 +218,7 @@ func (mmCleanup *mTesterMockCleanup) Return() *TesterMock { mmCleanup.defaultExpectation = &TesterMockCleanupExpectation{mock: mmCleanup.mock} } + mmCleanup.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmCleanup.mock } @@ -206,6 +233,7 @@ func (mmCleanup *mTesterMockCleanup) Set(f func(f func())) *TesterMock { } mmCleanup.mock.funcCleanup = f + mmCleanup.mock.funcCleanupOrigin = minimock.CallerInfo(1) return mmCleanup.mock } @@ -215,6 +243,7 @@ func (mmCleanup *mTesterMockCleanup) Times(n uint64) *mTesterMockCleanup { mmCleanup.mock.t.Fatalf("Times of TesterMock.Cleanup mock can not be zero") } mm_atomic.StoreUint64(&mmCleanup.expectedInvocations, n) + mmCleanup.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmCleanup } @@ -229,11 +258,13 @@ func (mmCleanup *mTesterMockCleanup) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Cleanup implements minimock.Tester +// Cleanup implements mm_minimock.Tester func (mmCleanup *TesterMock) Cleanup(f func()) { mm_atomic.AddUint64(&mmCleanup.beforeCleanupCounter, 1) defer mm_atomic.AddUint64(&mmCleanup.afterCleanupCounter, 1) + mmCleanup.t.Helper() + if mmCleanup.inspectFuncCleanup != nil { mmCleanup.inspectFuncCleanup(f) } @@ -262,11 +293,13 @@ func (mmCleanup *TesterMock) Cleanup(f func()) { if mm_want_ptrs != nil { if mm_want_ptrs.f != nil && !minimock.Equal(*mm_want_ptrs.f, mm_got.f) { - mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameter f, want: %#v, got: %#v%s\n", *mm_want_ptrs.f, mm_got.f, minimock.Diff(*mm_want_ptrs.f, mm_got.f)) + mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameter f, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmCleanup.CleanupMock.defaultExpectation.expectationOrigins.originF, *mm_want_ptrs.f, mm_got.f, minimock.Diff(*mm_want_ptrs.f, mm_got.f)) } } else 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)) + mmCleanup.t.Errorf("TesterMock.Cleanup got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmCleanup.CleanupMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -324,7 +357,7 @@ func (m *TesterMock) MinimockCleanupDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -332,19 +365,19 @@ func (m *TesterMock) MinimockCleanupInspect() { // if default expectation was set then invocations count should be greater than zero if m.CleanupMock.defaultExpectation != nil && afterCleanupCounter < 1 { if m.CleanupMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Cleanup") + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s", m.CleanupMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Cleanup with params: %#v", *m.CleanupMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s with params: %#v", m.CleanupMock.defaultExpectation.expectationOrigins.origin, *m.CleanupMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcCleanup != nil && afterCleanupCounter < 1 { - m.t.Error("Expected call to TesterMock.Cleanup") + m.t.Errorf("Expected call to TesterMock.Cleanup at\n%s", m.funcCleanupOrigin) } if !m.CleanupMock.invocationsDone() && afterCleanupCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Cleanup but found %d calls", - mm_atomic.LoadUint64(&m.CleanupMock.expectedInvocations), afterCleanupCounter) + m.t.Errorf("Expected %d calls to TesterMock.Cleanup at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.CleanupMock.expectedInvocations), m.CleanupMock.expectedInvocationsOrigin, afterCleanupCounter) } } @@ -357,16 +390,19 @@ type mTesterMockError struct { callArgs []*TesterMockErrorParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockErrorExpectation specifies expectation struct of the Tester.Error type TesterMockErrorExpectation struct { - mock *TesterMock - params *TesterMockErrorParams - paramPtrs *TesterMockErrorParamPtrs + mock *TesterMock + params *TesterMockErrorParams + paramPtrs *TesterMockErrorParamPtrs + expectationOrigins TesterMockErrorExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockErrorParams contains parameters of the Tester.Error @@ -379,6 +415,12 @@ type TesterMockErrorParamPtrs struct { p1 *[]interface{} } +// TesterMockErrorOrigins contains origins of expectations of the Tester.Error +type TesterMockErrorExpectationOrigins struct { + origin string + originP1 string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -404,6 +446,7 @@ func (mmError *mTesterMockError) Expect(p1 ...interface{}) *mTesterMockError { } mmError.defaultExpectation.params = &TesterMockErrorParams{p1} + mmError.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -431,6 +474,7 @@ func (mmError *mTesterMockError) ExpectP1Param1(p1 ...interface{}) *mTesterMockE mmError.defaultExpectation.paramPtrs = &TesterMockErrorParamPtrs{} } mmError.defaultExpectation.paramPtrs.p1 = &p1 + mmError.defaultExpectation.expectationOrigins.originP1 = minimock.CallerInfo(1) return mmError } @@ -456,6 +500,7 @@ func (mmError *mTesterMockError) Return() *TesterMock { mmError.defaultExpectation = &TesterMockErrorExpectation{mock: mmError.mock} } + mmError.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmError.mock } @@ -470,6 +515,7 @@ func (mmError *mTesterMockError) Set(f func(p1 ...interface{})) *TesterMock { } mmError.mock.funcError = f + mmError.mock.funcErrorOrigin = minimock.CallerInfo(1) return mmError.mock } @@ -479,6 +525,7 @@ func (mmError *mTesterMockError) Times(n uint64) *mTesterMockError { mmError.mock.t.Fatalf("Times of TesterMock.Error mock can not be zero") } mm_atomic.StoreUint64(&mmError.expectedInvocations, n) + mmError.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmError } @@ -493,11 +540,13 @@ func (mmError *mTesterMockError) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Error implements minimock.Tester +// Error implements mm_minimock.Tester func (mmError *TesterMock) Error(p1 ...interface{}) { mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) defer mm_atomic.AddUint64(&mmError.afterErrorCounter, 1) + mmError.t.Helper() + if mmError.inspectFuncError != nil { mmError.inspectFuncError(p1...) } @@ -526,11 +575,13 @@ func (mmError *TesterMock) Error(p1 ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.p1 != nil && !minimock.Equal(*mm_want_ptrs.p1, mm_got.p1) { - mmError.t.Errorf("TesterMock.Error got unexpected parameter p1, want: %#v, got: %#v%s\n", *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) + mmError.t.Errorf("TesterMock.Error got unexpected parameter p1, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmError.ErrorMock.defaultExpectation.expectationOrigins.originP1, *mm_want_ptrs.p1, mm_got.p1, minimock.Diff(*mm_want_ptrs.p1, mm_got.p1)) } } else 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)) + mmError.t.Errorf("TesterMock.Error got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmError.ErrorMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -588,7 +639,7 @@ func (m *TesterMock) MinimockErrorDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Error at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -596,19 +647,19 @@ func (m *TesterMock) MinimockErrorInspect() { // if default expectation was set then invocations count should be greater than zero if m.ErrorMock.defaultExpectation != nil && afterErrorCounter < 1 { if m.ErrorMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Error") + m.t.Errorf("Expected call to TesterMock.Error at\n%s", m.ErrorMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Error with params: %#v", *m.ErrorMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Error at\n%s with params: %#v", m.ErrorMock.defaultExpectation.expectationOrigins.origin, *m.ErrorMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcError != nil && afterErrorCounter < 1 { - m.t.Error("Expected call to TesterMock.Error") + m.t.Errorf("Expected call to TesterMock.Error at\n%s", m.funcErrorOrigin) } if !m.ErrorMock.invocationsDone() && afterErrorCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Error but found %d calls", - mm_atomic.LoadUint64(&m.ErrorMock.expectedInvocations), afterErrorCounter) + m.t.Errorf("Expected %d calls to TesterMock.Error at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.ErrorMock.expectedInvocations), m.ErrorMock.expectedInvocationsOrigin, afterErrorCounter) } } @@ -621,16 +672,19 @@ type mTesterMockErrorf struct { callArgs []*TesterMockErrorfParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockErrorfExpectation specifies expectation struct of the Tester.Errorf type TesterMockErrorfExpectation struct { - mock *TesterMock - params *TesterMockErrorfParams - paramPtrs *TesterMockErrorfParamPtrs + mock *TesterMock + params *TesterMockErrorfParams + paramPtrs *TesterMockErrorfParamPtrs + expectationOrigins TesterMockErrorfExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockErrorfParams contains parameters of the Tester.Errorf @@ -645,6 +699,13 @@ type TesterMockErrorfParamPtrs struct { args *[]interface{} } +// TesterMockErrorfOrigins contains origins of expectations of the Tester.Errorf +type TesterMockErrorfExpectationOrigins struct { + origin string + originFormat string + originArgs string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -670,6 +731,7 @@ func (mmErrorf *mTesterMockErrorf) Expect(format string, args ...interface{}) *m } mmErrorf.defaultExpectation.params = &TesterMockErrorfParams{format, args} + mmErrorf.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -697,6 +759,7 @@ func (mmErrorf *mTesterMockErrorf) ExpectFormatParam1(format string) *mTesterMoc mmErrorf.defaultExpectation.paramPtrs = &TesterMockErrorfParamPtrs{} } mmErrorf.defaultExpectation.paramPtrs.format = &format + mmErrorf.defaultExpectation.expectationOrigins.originFormat = minimock.CallerInfo(1) return mmErrorf } @@ -719,6 +782,7 @@ func (mmErrorf *mTesterMockErrorf) ExpectArgsParam2(args ...interface{}) *mTeste mmErrorf.defaultExpectation.paramPtrs = &TesterMockErrorfParamPtrs{} } mmErrorf.defaultExpectation.paramPtrs.args = &args + mmErrorf.defaultExpectation.expectationOrigins.originArgs = minimock.CallerInfo(1) return mmErrorf } @@ -744,6 +808,7 @@ func (mmErrorf *mTesterMockErrorf) Return() *TesterMock { mmErrorf.defaultExpectation = &TesterMockErrorfExpectation{mock: mmErrorf.mock} } + mmErrorf.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmErrorf.mock } @@ -758,6 +823,7 @@ func (mmErrorf *mTesterMockErrorf) Set(f func(format string, args ...interface{} } mmErrorf.mock.funcErrorf = f + mmErrorf.mock.funcErrorfOrigin = minimock.CallerInfo(1) return mmErrorf.mock } @@ -767,6 +833,7 @@ func (mmErrorf *mTesterMockErrorf) Times(n uint64) *mTesterMockErrorf { mmErrorf.mock.t.Fatalf("Times of TesterMock.Errorf mock can not be zero") } mm_atomic.StoreUint64(&mmErrorf.expectedInvocations, n) + mmErrorf.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmErrorf } @@ -781,11 +848,13 @@ func (mmErrorf *mTesterMockErrorf) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Errorf implements minimock.Tester +// Errorf implements mm_minimock.Tester func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) defer mm_atomic.AddUint64(&mmErrorf.afterErrorfCounter, 1) + mmErrorf.t.Helper() + if mmErrorf.inspectFuncErrorf != nil { mmErrorf.inspectFuncErrorf(format, args...) } @@ -814,15 +883,18 @@ func (mmErrorf *TesterMock) Errorf(format string, args ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.format != nil && !minimock.Equal(*mm_want_ptrs.format, mm_got.format) { - mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter format, want: %#v, got: %#v%s\n", *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) + mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter format, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmErrorf.ErrorfMock.defaultExpectation.expectationOrigins.originFormat, *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) } if mm_want_ptrs.args != nil && !minimock.Equal(*mm_want_ptrs.args, mm_got.args) { - mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter args, want: %#v, got: %#v%s\n", *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) + mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameter args, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmErrorf.ErrorfMock.defaultExpectation.expectationOrigins.originArgs, *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) } } else 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)) + mmErrorf.t.Errorf("TesterMock.Errorf got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmErrorf.ErrorfMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -880,7 +952,7 @@ func (m *TesterMock) MinimockErrorfDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -888,19 +960,19 @@ func (m *TesterMock) MinimockErrorfInspect() { // if default expectation was set then invocations count should be greater than zero if m.ErrorfMock.defaultExpectation != nil && afterErrorfCounter < 1 { if m.ErrorfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Errorf") + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s", m.ErrorfMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Errorf with params: %#v", *m.ErrorfMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s with params: %#v", m.ErrorfMock.defaultExpectation.expectationOrigins.origin, *m.ErrorfMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcErrorf != nil && afterErrorfCounter < 1 { - m.t.Error("Expected call to TesterMock.Errorf") + m.t.Errorf("Expected call to TesterMock.Errorf at\n%s", m.funcErrorfOrigin) } if !m.ErrorfMock.invocationsDone() && afterErrorfCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Errorf but found %d calls", - mm_atomic.LoadUint64(&m.ErrorfMock.expectedInvocations), afterErrorfCounter) + m.t.Errorf("Expected %d calls to TesterMock.Errorf at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.ErrorfMock.expectedInvocations), m.ErrorfMock.expectedInvocationsOrigin, afterErrorfCounter) } } @@ -910,14 +982,16 @@ type mTesterMockFailNow struct { defaultExpectation *TesterMockFailNowExpectation expectations []*TesterMockFailNowExpectation - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockFailNowExpectation specifies expectation struct of the Tester.FailNow type TesterMockFailNowExpectation struct { mock *TesterMock - Counter uint64 + returnOrigin string + Counter uint64 } // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning @@ -964,6 +1038,7 @@ func (mmFailNow *mTesterMockFailNow) Return() *TesterMock { mmFailNow.defaultExpectation = &TesterMockFailNowExpectation{mock: mmFailNow.mock} } + mmFailNow.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFailNow.mock } @@ -978,6 +1053,7 @@ func (mmFailNow *mTesterMockFailNow) Set(f func()) *TesterMock { } mmFailNow.mock.funcFailNow = f + mmFailNow.mock.funcFailNowOrigin = minimock.CallerInfo(1) return mmFailNow.mock } @@ -987,6 +1063,7 @@ func (mmFailNow *mTesterMockFailNow) Times(n uint64) *mTesterMockFailNow { mmFailNow.mock.t.Fatalf("Times of TesterMock.FailNow mock can not be zero") } mm_atomic.StoreUint64(&mmFailNow.expectedInvocations, n) + mmFailNow.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFailNow } @@ -1001,11 +1078,13 @@ func (mmFailNow *mTesterMockFailNow) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// FailNow implements minimock.Tester +// FailNow implements mm_minimock.Tester func (mmFailNow *TesterMock) FailNow() { mm_atomic.AddUint64(&mmFailNow.beforeFailNowCounter, 1) defer mm_atomic.AddUint64(&mmFailNow.afterFailNowCounter, 1) + mmFailNow.t.Helper() + if mmFailNow.inspectFuncFailNow != nil { mmFailNow.inspectFuncFailNow() } @@ -1062,16 +1141,16 @@ func (m *TesterMock) MinimockFailNowInspect() { afterFailNowCounter := mm_atomic.LoadUint64(&m.afterFailNowCounter) // if default expectation was set then invocations count should be greater than zero if m.FailNowMock.defaultExpectation != nil && afterFailNowCounter < 1 { - m.t.Error("Expected call to TesterMock.FailNow") + m.t.Errorf("Expected call to TesterMock.FailNow at\n%s", m.FailNowMock.defaultExpectation.returnOrigin) } // if func was set then invocations count should be greater than zero if m.funcFailNow != nil && afterFailNowCounter < 1 { - m.t.Error("Expected call to TesterMock.FailNow") + m.t.Errorf("Expected call to TesterMock.FailNow at\n%s", m.funcFailNowOrigin) } if !m.FailNowMock.invocationsDone() && afterFailNowCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.FailNow but found %d calls", - mm_atomic.LoadUint64(&m.FailNowMock.expectedInvocations), afterFailNowCounter) + m.t.Errorf("Expected %d calls to TesterMock.FailNow at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FailNowMock.expectedInvocations), m.FailNowMock.expectedInvocationsOrigin, afterFailNowCounter) } } @@ -1084,16 +1163,19 @@ type mTesterMockFatal struct { callArgs []*TesterMockFatalParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockFatalExpectation specifies expectation struct of the Tester.Fatal type TesterMockFatalExpectation struct { - mock *TesterMock - params *TesterMockFatalParams - paramPtrs *TesterMockFatalParamPtrs + mock *TesterMock + params *TesterMockFatalParams + paramPtrs *TesterMockFatalParamPtrs + expectationOrigins TesterMockFatalExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockFatalParams contains parameters of the Tester.Fatal @@ -1106,6 +1188,12 @@ type TesterMockFatalParamPtrs struct { args *[]interface{} } +// TesterMockFatalOrigins contains origins of expectations of the Tester.Fatal +type TesterMockFatalExpectationOrigins struct { + origin string + originArgs string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -1131,6 +1219,7 @@ func (mmFatal *mTesterMockFatal) Expect(args ...interface{}) *mTesterMockFatal { } mmFatal.defaultExpectation.params = &TesterMockFatalParams{args} + mmFatal.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -1158,6 +1247,7 @@ func (mmFatal *mTesterMockFatal) ExpectArgsParam1(args ...interface{}) *mTesterM mmFatal.defaultExpectation.paramPtrs = &TesterMockFatalParamPtrs{} } mmFatal.defaultExpectation.paramPtrs.args = &args + mmFatal.defaultExpectation.expectationOrigins.originArgs = minimock.CallerInfo(1) return mmFatal } @@ -1183,6 +1273,7 @@ func (mmFatal *mTesterMockFatal) Return() *TesterMock { mmFatal.defaultExpectation = &TesterMockFatalExpectation{mock: mmFatal.mock} } + mmFatal.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFatal.mock } @@ -1197,6 +1288,7 @@ func (mmFatal *mTesterMockFatal) Set(f func(args ...interface{})) *TesterMock { } mmFatal.mock.funcFatal = f + mmFatal.mock.funcFatalOrigin = minimock.CallerInfo(1) return mmFatal.mock } @@ -1206,6 +1298,7 @@ func (mmFatal *mTesterMockFatal) Times(n uint64) *mTesterMockFatal { mmFatal.mock.t.Fatalf("Times of TesterMock.Fatal mock can not be zero") } mm_atomic.StoreUint64(&mmFatal.expectedInvocations, n) + mmFatal.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFatal } @@ -1220,11 +1313,13 @@ func (mmFatal *mTesterMockFatal) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Fatal implements minimock.Tester +// Fatal implements mm_minimock.Tester func (mmFatal *TesterMock) Fatal(args ...interface{}) { mm_atomic.AddUint64(&mmFatal.beforeFatalCounter, 1) defer mm_atomic.AddUint64(&mmFatal.afterFatalCounter, 1) + mmFatal.t.Helper() + if mmFatal.inspectFuncFatal != nil { mmFatal.inspectFuncFatal(args...) } @@ -1253,11 +1348,13 @@ func (mmFatal *TesterMock) Fatal(args ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.args != nil && !minimock.Equal(*mm_want_ptrs.args, mm_got.args) { - mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameter args, want: %#v, got: %#v%s\n", *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) + mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameter args, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatal.FatalMock.defaultExpectation.expectationOrigins.originArgs, *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) } } else 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)) + mmFatal.t.Errorf("TesterMock.Fatal got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatal.FatalMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -1315,7 +1412,7 @@ func (m *TesterMock) MinimockFatalDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -1323,19 +1420,19 @@ func (m *TesterMock) MinimockFatalInspect() { // if default expectation was set then invocations count should be greater than zero if m.FatalMock.defaultExpectation != nil && afterFatalCounter < 1 { if m.FatalMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatal") + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s", m.FatalMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Fatal with params: %#v", *m.FatalMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s with params: %#v", m.FatalMock.defaultExpectation.expectationOrigins.origin, *m.FatalMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFatal != nil && afterFatalCounter < 1 { - m.t.Error("Expected call to TesterMock.Fatal") + m.t.Errorf("Expected call to TesterMock.Fatal at\n%s", m.funcFatalOrigin) } if !m.FatalMock.invocationsDone() && afterFatalCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Fatal but found %d calls", - mm_atomic.LoadUint64(&m.FatalMock.expectedInvocations), afterFatalCounter) + m.t.Errorf("Expected %d calls to TesterMock.Fatal at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FatalMock.expectedInvocations), m.FatalMock.expectedInvocationsOrigin, afterFatalCounter) } } @@ -1348,16 +1445,19 @@ type mTesterMockFatalf struct { callArgs []*TesterMockFatalfParams mutex sync.RWMutex - expectedInvocations uint64 + expectedInvocations uint64 + expectedInvocationsOrigin string } // TesterMockFatalfExpectation specifies expectation struct of the Tester.Fatalf type TesterMockFatalfExpectation struct { - mock *TesterMock - params *TesterMockFatalfParams - paramPtrs *TesterMockFatalfParamPtrs + mock *TesterMock + params *TesterMockFatalfParams + paramPtrs *TesterMockFatalfParamPtrs + expectationOrigins TesterMockFatalfExpectationOrigins - Counter uint64 + returnOrigin string + Counter uint64 } // TesterMockFatalfParams contains parameters of the Tester.Fatalf @@ -1372,6 +1472,13 @@ type TesterMockFatalfParamPtrs struct { args *[]interface{} } +// TesterMockFatalfOrigins contains origins of expectations of the Tester.Fatalf +type TesterMockFatalfExpectationOrigins struct { + origin string + originFormat string + originArgs string +} + // Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning // the test will fail minimock's automatic final call check if the mocked method was not called at least once. // Optional() makes method check to work in '0 or more' mode. @@ -1397,6 +1504,7 @@ func (mmFatalf *mTesterMockFatalf) Expect(format string, args ...interface{}) *m } mmFatalf.defaultExpectation.params = &TesterMockFatalfParams{format, args} + mmFatalf.defaultExpectation.expectationOrigins.origin = minimock.CallerInfo(1) 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) @@ -1424,6 +1532,7 @@ func (mmFatalf *mTesterMockFatalf) ExpectFormatParam1(format string) *mTesterMoc mmFatalf.defaultExpectation.paramPtrs = &TesterMockFatalfParamPtrs{} } mmFatalf.defaultExpectation.paramPtrs.format = &format + mmFatalf.defaultExpectation.expectationOrigins.originFormat = minimock.CallerInfo(1) return mmFatalf } @@ -1446,6 +1555,7 @@ func (mmFatalf *mTesterMockFatalf) ExpectArgsParam2(args ...interface{}) *mTeste mmFatalf.defaultExpectation.paramPtrs = &TesterMockFatalfParamPtrs{} } mmFatalf.defaultExpectation.paramPtrs.args = &args + mmFatalf.defaultExpectation.expectationOrigins.originArgs = minimock.CallerInfo(1) return mmFatalf } @@ -1471,6 +1581,7 @@ func (mmFatalf *mTesterMockFatalf) Return() *TesterMock { mmFatalf.defaultExpectation = &TesterMockFatalfExpectation{mock: mmFatalf.mock} } + mmFatalf.defaultExpectation.returnOrigin = minimock.CallerInfo(1) return mmFatalf.mock } @@ -1485,6 +1596,7 @@ func (mmFatalf *mTesterMockFatalf) Set(f func(format string, args ...interface{} } mmFatalf.mock.funcFatalf = f + mmFatalf.mock.funcFatalfOrigin = minimock.CallerInfo(1) return mmFatalf.mock } @@ -1494,6 +1606,7 @@ func (mmFatalf *mTesterMockFatalf) Times(n uint64) *mTesterMockFatalf { mmFatalf.mock.t.Fatalf("Times of TesterMock.Fatalf mock can not be zero") } mm_atomic.StoreUint64(&mmFatalf.expectedInvocations, n) + mmFatalf.expectedInvocationsOrigin = minimock.CallerInfo(1) return mmFatalf } @@ -1508,11 +1621,13 @@ func (mmFatalf *mTesterMockFatalf) invocationsDone() bool { return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } -// Fatalf implements minimock.Tester +// Fatalf implements mm_minimock.Tester func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { mm_atomic.AddUint64(&mmFatalf.beforeFatalfCounter, 1) defer mm_atomic.AddUint64(&mmFatalf.afterFatalfCounter, 1) + mmFatalf.t.Helper() + if mmFatalf.inspectFuncFatalf != nil { mmFatalf.inspectFuncFatalf(format, args...) } @@ -1541,15 +1656,18 @@ func (mmFatalf *TesterMock) Fatalf(format string, args ...interface{}) { if mm_want_ptrs != nil { if mm_want_ptrs.format != nil && !minimock.Equal(*mm_want_ptrs.format, mm_got.format) { - mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter format, want: %#v, got: %#v%s\n", *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) + mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter format, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatalf.FatalfMock.defaultExpectation.expectationOrigins.originFormat, *mm_want_ptrs.format, mm_got.format, minimock.Diff(*mm_want_ptrs.format, mm_got.format)) } if mm_want_ptrs.args != nil && !minimock.Equal(*mm_want_ptrs.args, mm_got.args) { - mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter args, want: %#v, got: %#v%s\n", *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) + mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameter args, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatalf.FatalfMock.defaultExpectation.expectationOrigins.originArgs, *mm_want_ptrs.args, mm_got.args, minimock.Diff(*mm_want_ptrs.args, mm_got.args)) } } else 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)) + mmFatalf.t.Errorf("TesterMock.Fatalf got unexpected parameters, expected at\n%s:\nwant: %#v\n got: %#v%s\n", + mmFatalf.FatalfMock.defaultExpectation.expectationOrigins.origin, *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } return @@ -1607,7 +1725,7 @@ func (m *TesterMock) MinimockFatalfDone() bool { 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) + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s with params: %#v", e.expectationOrigins.origin, *e.params) } } @@ -1615,19 +1733,197 @@ func (m *TesterMock) MinimockFatalfInspect() { // if default expectation was set then invocations count should be greater than zero if m.FatalfMock.defaultExpectation != nil && afterFatalfCounter < 1 { if m.FatalfMock.defaultExpectation.params == nil { - m.t.Error("Expected call to TesterMock.Fatalf") + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s", m.FatalfMock.defaultExpectation.returnOrigin) } else { - m.t.Errorf("Expected call to TesterMock.Fatalf with params: %#v", *m.FatalfMock.defaultExpectation.params) + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s with params: %#v", m.FatalfMock.defaultExpectation.expectationOrigins.origin, *m.FatalfMock.defaultExpectation.params) } } // if func was set then invocations count should be greater than zero if m.funcFatalf != nil && afterFatalfCounter < 1 { - m.t.Error("Expected call to TesterMock.Fatalf") + m.t.Errorf("Expected call to TesterMock.Fatalf at\n%s", m.funcFatalfOrigin) } if !m.FatalfMock.invocationsDone() && afterFatalfCounter > 0 { - m.t.Errorf("Expected %d calls to TesterMock.Fatalf but found %d calls", - mm_atomic.LoadUint64(&m.FatalfMock.expectedInvocations), afterFatalfCounter) + m.t.Errorf("Expected %d calls to TesterMock.Fatalf at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.FatalfMock.expectedInvocations), m.FatalfMock.expectedInvocationsOrigin, afterFatalfCounter) + } +} + +type mTesterMockHelper struct { + optional bool + mock *TesterMock + defaultExpectation *TesterMockHelperExpectation + expectations []*TesterMockHelperExpectation + + expectedInvocations uint64 + expectedInvocationsOrigin string +} + +// TesterMockHelperExpectation specifies expectation struct of the Tester.Helper +type TesterMockHelperExpectation struct { + mock *TesterMock + + returnOrigin string + Counter uint64 +} + +// Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning +// the test will fail minimock's automatic final call check if the mocked method was not called at least once. +// Optional() makes method check to work in '0 or more' mode. +// It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to +// catch the problems when the expected method call is totally skipped during test run. +func (mmHelper *mTesterMockHelper) Optional() *mTesterMockHelper { + mmHelper.optional = true + return mmHelper +} + +// Expect sets up expected params for Tester.Helper +func (mmHelper *mTesterMockHelper) Expect() *mTesterMockHelper { + if mmHelper.mock.funcHelper != nil { + mmHelper.mock.t.Fatalf("TesterMock.Helper mock is already set by Set") + } + + if mmHelper.defaultExpectation == nil { + mmHelper.defaultExpectation = &TesterMockHelperExpectation{} + } + + return mmHelper +} + +// Inspect accepts an inspector function that has same arguments as the Tester.Helper +func (mmHelper *mTesterMockHelper) Inspect(f func()) *mTesterMockHelper { + if mmHelper.mock.inspectFuncHelper != nil { + mmHelper.mock.t.Fatalf("Inspect function is already set for TesterMock.Helper") + } + + mmHelper.mock.inspectFuncHelper = f + + return mmHelper +} + +// Return sets up results that will be returned by Tester.Helper +func (mmHelper *mTesterMockHelper) Return() *TesterMock { + if mmHelper.mock.funcHelper != nil { + mmHelper.mock.t.Fatalf("TesterMock.Helper mock is already set by Set") + } + + if mmHelper.defaultExpectation == nil { + mmHelper.defaultExpectation = &TesterMockHelperExpectation{mock: mmHelper.mock} + } + + mmHelper.defaultExpectation.returnOrigin = minimock.CallerInfo(1) + return mmHelper.mock +} + +// Set uses given function f to mock the Tester.Helper method +func (mmHelper *mTesterMockHelper) Set(f func()) *TesterMock { + if mmHelper.defaultExpectation != nil { + mmHelper.mock.t.Fatalf("Default expectation is already set for the Tester.Helper method") + } + + if len(mmHelper.expectations) > 0 { + mmHelper.mock.t.Fatalf("Some expectations are already set for the Tester.Helper method") + } + + mmHelper.mock.funcHelper = f + mmHelper.mock.funcHelperOrigin = minimock.CallerInfo(1) + return mmHelper.mock +} + +// Times sets number of times Tester.Helper should be invoked +func (mmHelper *mTesterMockHelper) Times(n uint64) *mTesterMockHelper { + if n == 0 { + mmHelper.mock.t.Fatalf("Times of TesterMock.Helper mock can not be zero") + } + mm_atomic.StoreUint64(&mmHelper.expectedInvocations, n) + mmHelper.expectedInvocationsOrigin = minimock.CallerInfo(1) + return mmHelper +} + +func (mmHelper *mTesterMockHelper) invocationsDone() bool { + if len(mmHelper.expectations) == 0 && mmHelper.defaultExpectation == nil && mmHelper.mock.funcHelper == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmHelper.mock.afterHelperCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmHelper.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + +// Helper implements mm_minimock.Tester +func (mmHelper *TesterMock) Helper() { + mm_atomic.AddUint64(&mmHelper.beforeHelperCounter, 1) + defer mm_atomic.AddUint64(&mmHelper.afterHelperCounter, 1) + + mmHelper.t.Helper() + + if mmHelper.inspectFuncHelper != nil { + mmHelper.inspectFuncHelper() + } + + if mmHelper.HelperMock.defaultExpectation != nil { + mm_atomic.AddUint64(&mmHelper.HelperMock.defaultExpectation.Counter, 1) + + return + + } + if mmHelper.funcHelper != nil { + mmHelper.funcHelper() + return + } + mmHelper.t.Fatalf("Unexpected call to TesterMock.Helper.") + +} + +// HelperAfterCounter returns a count of finished TesterMock.Helper invocations +func (mmHelper *TesterMock) HelperAfterCounter() uint64 { + return mm_atomic.LoadUint64(&mmHelper.afterHelperCounter) +} + +// HelperBeforeCounter returns a count of TesterMock.Helper invocations +func (mmHelper *TesterMock) HelperBeforeCounter() uint64 { + return mm_atomic.LoadUint64(&mmHelper.beforeHelperCounter) +} + +// MinimockHelperDone returns true if the count of the Helper invocations corresponds +// the number of defined expectations +func (m *TesterMock) MinimockHelperDone() bool { + if m.HelperMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + + for _, e := range m.HelperMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + return false + } + } + + return m.HelperMock.invocationsDone() +} + +// MinimockHelperInspect logs each unmet expectation +func (m *TesterMock) MinimockHelperInspect() { + for _, e := range m.HelperMock.expectations { + if mm_atomic.LoadUint64(&e.Counter) < 1 { + m.t.Error("Expected call to TesterMock.Helper") + } + } + + afterHelperCounter := mm_atomic.LoadUint64(&m.afterHelperCounter) + // if default expectation was set then invocations count should be greater than zero + if m.HelperMock.defaultExpectation != nil && afterHelperCounter < 1 { + m.t.Errorf("Expected call to TesterMock.Helper at\n%s", m.HelperMock.defaultExpectation.returnOrigin) + } + // if func was set then invocations count should be greater than zero + if m.funcHelper != nil && afterHelperCounter < 1 { + m.t.Errorf("Expected call to TesterMock.Helper at\n%s", m.funcHelperOrigin) + } + + if !m.HelperMock.invocationsDone() && afterHelperCounter > 0 { + m.t.Errorf("Expected %d calls to TesterMock.Helper at\n%s but found %d calls", + mm_atomic.LoadUint64(&m.HelperMock.expectedInvocations), m.HelperMock.expectedInvocationsOrigin, afterHelperCounter) } } @@ -1646,6 +1942,8 @@ func (m *TesterMock) MinimockFinish() { m.MinimockFatalInspect() m.MinimockFatalfInspect() + + m.MinimockHelperInspect() } }) } @@ -1674,5 +1972,6 @@ func (m *TesterMock) minimockDone() bool { m.MinimockErrorfDone() && m.MinimockFailNowDone() && m.MinimockFatalDone() && - m.MinimockFatalfDone() + m.MinimockFatalfDone() && + m.MinimockHelperDone() }