diff --git a/go.mod b/go.mod index d72f9301..bf95f7a4 100644 --- a/go.mod +++ b/go.mod @@ -5,33 +5,34 @@ go 1.22.2 require ( github.com/PuerkitoBio/purell v1.2.1 github.com/bmatcuk/doublestar/v4 v4.6.1 - github.com/charmbracelet/lipgloss v0.10.0 + github.com/charmbracelet/lipgloss v0.11.0 github.com/charmbracelet/log v0.4.0 github.com/deckarep/golang-set/v2 v2.6.0 github.com/dustin/go-humanize v1.0.1 github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a github.com/gobuffalo/validate v2.0.4+incompatible - github.com/gojuno/minimock/v3 v3.3.7 + github.com/gojuno/minimock/v3 v3.3.12 github.com/gorilla/mux v1.8.1 - github.com/hashicorp/go-version v1.6.0 + github.com/hashicorp/go-version v1.7.0 github.com/mitchellh/mapstructure v1.5.0 github.com/muesli/termenv v0.15.2 github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 github.com/samber/lo v1.39.0 github.com/spf13/afero v1.11.0 github.com/spf13/pflag v1.0.5 - github.com/spf13/viper v1.18.2 + github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 - golang.org/x/net v0.24.0 + golang.org/x/net v0.26.0 ) require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/x/ansi v0.1.2 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/muesli/reflow v0.3.0 // indirect - github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/locafero v0.6.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect go.uber.org/multierr v1.11.0 // indirect @@ -44,14 +45,14 @@ require ( github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible - github.com/pelletier/go-toml/v2 v2.2.1 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect - golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect - golang.org/x/sys v0.19.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 54f77dc1..e2d46bf0 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,12 @@ github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwN github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= +github.com/charmbracelet/lipgloss v0.11.0 h1:UoAcbQ6Qml8hDwSWs0Y1cB5TEQuZkDPH/ZqwWWYTG4g= +github.com/charmbracelet/lipgloss v0.11.0/go.mod h1:1UdRTH9gYgpcdNN5oBtjbu/IzNKtzVtb7sqN1t9LNn8= github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM= +github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY= +github.com/charmbracelet/x/ansi v0.1.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= @@ -28,12 +32,16 @@ github.com/gobuffalo/validate v2.0.4+incompatible h1:ZTxozrIw8qQ5nfhShmc4izjYPTs github.com/gobuffalo/validate v2.0.4+incompatible/go.mod h1:N+EtDe0J8252BgfzQUChBgfd6L93m9weay53EWFVsMM= github.com/gojuno/minimock/v3 v3.3.7 h1:0hKsGb72PuXRIoBl/8lSte/jUHRXf1kVp+jEt7kcHhU= github.com/gojuno/minimock/v3 v3.3.7/go.mod h1:vzti9ksNd88AUv2OeP9fDgsqb9xqh8iegLr/aiTOdt8= +github.com/gojuno/minimock/v3 v3.3.12 h1:Gjf8ZysVt3UxF/UrMnyUZ27Q2b9TdkeUBUXt/tZtmkg= +github.com/gojuno/minimock/v3 v3.3.12/go.mod h1:WtJbR+15lbzpUHoOFtT7Sv1rR885bFxoyHrzoMOmK/k= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= +github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -59,6 +67,8 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg= github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI= github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -72,6 +82,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= +github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= @@ -86,6 +98,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -101,13 +115,21 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f h1:99ci1mjWVBWwJiEKYY6jWa4d2nTQVIEhZIptnrVb1XY= golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI= +golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY= +golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/testing/mocks/closer_mock.go b/testing/mocks/closer_mock.go index aa9ab24c..2ac61451 100644 --- a/testing/mocks/closer_mock.go +++ b/testing/mocks/closer_mock.go @@ -1,4 +1,4 @@ -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. +// Code generated by http://github.com/gojuno/minimock (v3.3.12). DO NOT EDIT. package mocks @@ -40,9 +40,12 @@ func NewCloserMock(t minimock.Tester) *CloserMock { } type mCloserMockClose struct { + optional bool mock *CloserMock defaultExpectation *CloserMockCloseExpectation expectations []*CloserMockCloseExpectation + + expectedInvocations uint64 } // CloserMockCloseExpectation specifies expectation struct of the Closer.Close @@ -58,6 +61,16 @@ type CloserMockCloseResults struct { err error } +// 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 (mmClose *mCloserMockClose) Optional() *mCloserMockClose { + mmClose.optional = true + return mmClose +} + // Expect sets up expected params for Closer.Close func (mmClose *mCloserMockClose) Expect() *mCloserMockClose { if mmClose.mock.funcClose != nil { @@ -109,6 +122,26 @@ func (mmClose *mCloserMockClose) Set(f func() (err error)) *CloserMock { return mmClose.mock } +// Times sets number of times Closer.Close should be invoked +func (mmClose *mCloserMockClose) Times(n uint64) *mCloserMockClose { + if n == 0 { + mmClose.mock.t.Fatalf("Times of CloserMock.Close mock can not be zero") + } + mm_atomic.StoreUint64(&mmClose.expectedInvocations, n) + return mmClose +} + +func (mmClose *mCloserMockClose) invocationsDone() bool { + if len(mmClose.expectations) == 0 && mmClose.defaultExpectation == nil && mmClose.mock.funcClose == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmClose.mock.afterCloseCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmClose.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Close implements io.Closer func (mmClose *CloserMock) Close() (err error) { mm_atomic.AddUint64(&mmClose.beforeCloseCounter, 1) @@ -147,21 +180,18 @@ func (mmClose *CloserMock) CloseBeforeCounter() uint64 { // MinimockCloseDone returns true if the count of the Close invocations corresponds // the number of defined expectations func (m *CloserMock) MinimockCloseDone() bool { + if m.CloseMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.CloseMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.CloseMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCloseCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcClose != nil && mm_atomic.LoadUint64(&m.afterCloseCounter) < 1 { - return false - } - return true + return m.CloseMock.invocationsDone() } // MinimockCloseInspect logs each unmet expectation @@ -172,14 +202,20 @@ func (m *CloserMock) MinimockCloseInspect() { } } + afterCloseCounter := mm_atomic.LoadUint64(&m.afterCloseCounter) // if default expectation was set then invocations count should be greater than zero - if m.CloseMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterCloseCounter) < 1 { + if m.CloseMock.defaultExpectation != nil && afterCloseCounter < 1 { m.t.Error("Expected call to CloserMock.Close") } // if func was set then invocations count should be greater than zero - if m.funcClose != nil && mm_atomic.LoadUint64(&m.afterCloseCounter) < 1 { + if m.funcClose != nil && afterCloseCounter < 1 { m.t.Error("Expected call to CloserMock.Close") } + + if !m.CloseMock.invocationsDone() && afterCloseCounter > 0 { + m.t.Errorf("Expected %d calls to CloserMock.Close but found %d calls", + mm_atomic.LoadUint64(&m.CloseMock.expectedInvocations), afterCloseCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times @@ -187,7 +223,6 @@ func (m *CloserMock) MinimockFinish() { m.finishOnce.Do(func() { if !m.minimockDone() { m.MinimockCloseInspect() - m.t.FailNow() } }) } diff --git a/testing/mocks/http_client_mock.go b/testing/mocks/http_client_mock.go index a76bb4eb..b7615108 100644 --- a/testing/mocks/http_client_mock.go +++ b/testing/mocks/http_client_mock.go @@ -1,4 +1,4 @@ -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. +// Code generated by http://github.com/gojuno/minimock (v3.3.12). DO NOT EDIT. package mocks @@ -42,20 +42,24 @@ func NewHTTPClientMock(t minimock.Tester) *HTTPClientMock { } type mHTTPClientMockDo struct { + optional bool mock *HTTPClientMock defaultExpectation *HTTPClientMockDoExpectation expectations []*HTTPClientMockDoExpectation callArgs []*HTTPClientMockDoParams mutex sync.RWMutex + + expectedInvocations uint64 } // HTTPClientMockDoExpectation specifies expectation struct of the HTTPClient.Do type HTTPClientMockDoExpectation struct { - mock *HTTPClientMock - params *HTTPClientMockDoParams - results *HTTPClientMockDoResults - Counter uint64 + mock *HTTPClientMock + params *HTTPClientMockDoParams + paramPtrs *HTTPClientMockDoParamPtrs + results *HTTPClientMockDoResults + Counter uint64 } // HTTPClientMockDoParams contains parameters of the HTTPClient.Do @@ -63,12 +67,27 @@ type HTTPClientMockDoParams struct { req *http.Request } +// HTTPClientMockDoParamPtrs contains pointers to parameters of the HTTPClient.Do +type HTTPClientMockDoParamPtrs struct { + req **http.Request +} + // HTTPClientMockDoResults contains results of the HTTPClient.Do type HTTPClientMockDoResults struct { rp1 *http.Response err error } +// 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 (mmDo *mHTTPClientMockDo) Optional() *mHTTPClientMockDo { + mmDo.optional = true + return mmDo +} + // Expect sets up expected params for HTTPClient.Do func (mmDo *mHTTPClientMockDo) Expect(req *http.Request) *mHTTPClientMockDo { if mmDo.mock.funcDo != nil { @@ -79,6 +98,10 @@ func (mmDo *mHTTPClientMockDo) Expect(req *http.Request) *mHTTPClientMockDo { mmDo.defaultExpectation = &HTTPClientMockDoExpectation{} } + if mmDo.defaultExpectation.paramPtrs != nil { + mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by ExpectParams functions") + } + mmDo.defaultExpectation.params = &HTTPClientMockDoParams{req} for _, e := range mmDo.expectations { if minimock.Equal(e.params, mmDo.defaultExpectation.params) { @@ -89,6 +112,28 @@ func (mmDo *mHTTPClientMockDo) Expect(req *http.Request) *mHTTPClientMockDo { return mmDo } +// ExpectReqParam1 sets up expected param req for HTTPClient.Do +func (mmDo *mHTTPClientMockDo) ExpectReqParam1(req *http.Request) *mHTTPClientMockDo { + if mmDo.mock.funcDo != nil { + mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by Set") + } + + if mmDo.defaultExpectation == nil { + mmDo.defaultExpectation = &HTTPClientMockDoExpectation{} + } + + if mmDo.defaultExpectation.params != nil { + mmDo.mock.t.Fatalf("HTTPClientMock.Do mock is already set by Expect") + } + + if mmDo.defaultExpectation.paramPtrs == nil { + mmDo.defaultExpectation.paramPtrs = &HTTPClientMockDoParamPtrs{} + } + mmDo.defaultExpectation.paramPtrs.req = &req + + return mmDo +} + // Inspect accepts an inspector function that has same arguments as the HTTPClient.Do func (mmDo *mHTTPClientMockDo) Inspect(f func(req *http.Request)) *mHTTPClientMockDo { if mmDo.mock.inspectFuncDo != nil { @@ -148,6 +193,26 @@ func (e *HTTPClientMockDoExpectation) Then(rp1 *http.Response, err error) *HTTPC return e.mock } +// Times sets number of times HTTPClient.Do should be invoked +func (mmDo *mHTTPClientMockDo) Times(n uint64) *mHTTPClientMockDo { + if n == 0 { + mmDo.mock.t.Fatalf("Times of HTTPClientMock.Do mock can not be zero") + } + mm_atomic.StoreUint64(&mmDo.expectedInvocations, n) + return mmDo +} + +func (mmDo *mHTTPClientMockDo) invocationsDone() bool { + if len(mmDo.expectations) == 0 && mmDo.defaultExpectation == nil && mmDo.mock.funcDo == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmDo.mock.afterDoCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmDo.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Do implements contracts.HTTPClient func (mmDo *HTTPClientMock) Do(req *http.Request) (rp1 *http.Response, err error) { mm_atomic.AddUint64(&mmDo.beforeDoCounter, 1) @@ -174,8 +239,17 @@ func (mmDo *HTTPClientMock) Do(req *http.Request) (rp1 *http.Response, err error if mmDo.DoMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmDo.DoMock.defaultExpectation.Counter, 1) mm_want := mmDo.DoMock.defaultExpectation.params + mm_want_ptrs := mmDo.DoMock.defaultExpectation.paramPtrs + mm_got := HTTPClientMockDoParams{req} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.req != nil && !minimock.Equal(*mm_want_ptrs.req, mm_got.req) { + mmDo.t.Errorf("HTTPClientMock.Do got unexpected parameter req, want: %#v, got: %#v%s\n", *mm_want_ptrs.req, mm_got.req, minimock.Diff(*mm_want_ptrs.req, mm_got.req)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmDo.t.Errorf("HTTPClientMock.Do got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -218,21 +292,18 @@ func (mmDo *mHTTPClientMockDo) Calls() []*HTTPClientMockDoParams { // MinimockDoDone returns true if the count of the Do invocations corresponds // the number of defined expectations func (m *HTTPClientMock) MinimockDoDone() bool { + if m.DoMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.DoMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.DoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcDo != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 { - return false - } - return true + return m.DoMock.invocationsDone() } // MinimockDoInspect logs each unmet expectation @@ -243,8 +314,9 @@ func (m *HTTPClientMock) MinimockDoInspect() { } } + afterDoCounter := mm_atomic.LoadUint64(&m.afterDoCounter) // if default expectation was set then invocations count should be greater than zero - if m.DoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 { + if m.DoMock.defaultExpectation != nil && afterDoCounter < 1 { if m.DoMock.defaultExpectation.params == nil { m.t.Error("Expected call to HTTPClientMock.Do") } else { @@ -252,9 +324,14 @@ func (m *HTTPClientMock) MinimockDoInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcDo != nil && mm_atomic.LoadUint64(&m.afterDoCounter) < 1 { + if m.funcDo != nil && afterDoCounter < 1 { m.t.Error("Expected call to HTTPClientMock.Do") } + + if !m.DoMock.invocationsDone() && afterDoCounter > 0 { + m.t.Errorf("Expected %d calls to HTTPClientMock.Do but found %d calls", + mm_atomic.LoadUint64(&m.DoMock.expectedInvocations), afterDoCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times @@ -262,7 +339,6 @@ func (m *HTTPClientMock) MinimockFinish() { m.finishOnce.Do(func() { if !m.minimockDone() { m.MinimockDoInspect() - m.t.FailNow() } }) } diff --git a/testing/mocks/logger_mock.go b/testing/mocks/logger_mock.go index 8563daa7..746db1b5 100644 --- a/testing/mocks/logger_mock.go +++ b/testing/mocks/logger_mock.go @@ -1,4 +1,4 @@ -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. +// Code generated by http://github.com/gojuno/minimock (v3.3.12). DO NOT EDIT. package mocks @@ -122,18 +122,22 @@ func NewLoggerMock(t minimock.Tester) *LoggerMock { } type mLoggerMockDebug struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockDebugExpectation expectations []*LoggerMockDebugExpectation callArgs []*LoggerMockDebugParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockDebugExpectation specifies expectation struct of the Logger.Debug type LoggerMockDebugExpectation struct { - mock *LoggerMock - params *LoggerMockDebugParams + mock *LoggerMock + params *LoggerMockDebugParams + paramPtrs *LoggerMockDebugParamPtrs Counter uint64 } @@ -144,6 +148,22 @@ type LoggerMockDebugParams struct { keyvals []any } +// LoggerMockDebugParamPtrs contains pointers to parameters of the Logger.Debug +type LoggerMockDebugParamPtrs struct { + msg *any + keyvals *[]any +} + +// 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 (mmDebug *mLoggerMockDebug) Optional() *mLoggerMockDebug { + mmDebug.optional = true + return mmDebug +} + // Expect sets up expected params for Logger.Debug func (mmDebug *mLoggerMockDebug) Expect(msg any, keyvals ...any) *mLoggerMockDebug { if mmDebug.mock.funcDebug != nil { @@ -154,6 +174,10 @@ func (mmDebug *mLoggerMockDebug) Expect(msg any, keyvals ...any) *mLoggerMockDeb mmDebug.defaultExpectation = &LoggerMockDebugExpectation{} } + if mmDebug.defaultExpectation.paramPtrs != nil { + mmDebug.mock.t.Fatalf("LoggerMock.Debug mock is already set by ExpectParams functions") + } + mmDebug.defaultExpectation.params = &LoggerMockDebugParams{msg, keyvals} for _, e := range mmDebug.expectations { if minimock.Equal(e.params, mmDebug.defaultExpectation.params) { @@ -164,6 +188,50 @@ func (mmDebug *mLoggerMockDebug) Expect(msg any, keyvals ...any) *mLoggerMockDeb return mmDebug } +// ExpectMsgParam1 sets up expected param msg for Logger.Debug +func (mmDebug *mLoggerMockDebug) ExpectMsgParam1(msg any) *mLoggerMockDebug { + if mmDebug.mock.funcDebug != nil { + mmDebug.mock.t.Fatalf("LoggerMock.Debug mock is already set by Set") + } + + if mmDebug.defaultExpectation == nil { + mmDebug.defaultExpectation = &LoggerMockDebugExpectation{} + } + + if mmDebug.defaultExpectation.params != nil { + mmDebug.mock.t.Fatalf("LoggerMock.Debug mock is already set by Expect") + } + + if mmDebug.defaultExpectation.paramPtrs == nil { + mmDebug.defaultExpectation.paramPtrs = &LoggerMockDebugParamPtrs{} + } + mmDebug.defaultExpectation.paramPtrs.msg = &msg + + return mmDebug +} + +// ExpectKeyvalsParam2 sets up expected param keyvals for Logger.Debug +func (mmDebug *mLoggerMockDebug) ExpectKeyvalsParam2(keyvals ...any) *mLoggerMockDebug { + if mmDebug.mock.funcDebug != nil { + mmDebug.mock.t.Fatalf("LoggerMock.Debug mock is already set by Set") + } + + if mmDebug.defaultExpectation == nil { + mmDebug.defaultExpectation = &LoggerMockDebugExpectation{} + } + + if mmDebug.defaultExpectation.params != nil { + mmDebug.mock.t.Fatalf("LoggerMock.Debug mock is already set by Expect") + } + + if mmDebug.defaultExpectation.paramPtrs == nil { + mmDebug.defaultExpectation.paramPtrs = &LoggerMockDebugParamPtrs{} + } + mmDebug.defaultExpectation.paramPtrs.keyvals = &keyvals + + return mmDebug +} + // Inspect accepts an inspector function that has same arguments as the Logger.Debug func (mmDebug *mLoggerMockDebug) Inspect(f func(msg any, keyvals ...any)) *mLoggerMockDebug { if mmDebug.mock.inspectFuncDebug != nil { @@ -202,6 +270,26 @@ func (mmDebug *mLoggerMockDebug) Set(f func(msg any, keyvals ...any)) *LoggerMoc return mmDebug.mock } +// Times sets number of times Logger.Debug should be invoked +func (mmDebug *mLoggerMockDebug) Times(n uint64) *mLoggerMockDebug { + if n == 0 { + mmDebug.mock.t.Fatalf("Times of LoggerMock.Debug mock can not be zero") + } + mm_atomic.StoreUint64(&mmDebug.expectedInvocations, n) + return mmDebug +} + +func (mmDebug *mLoggerMockDebug) invocationsDone() bool { + if len(mmDebug.expectations) == 0 && mmDebug.defaultExpectation == nil && mmDebug.mock.funcDebug == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmDebug.mock.afterDebugCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmDebug.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Debug implements contracts.Logger func (mmDebug *LoggerMock) Debug(msg any, keyvals ...any) { mm_atomic.AddUint64(&mmDebug.beforeDebugCounter, 1) @@ -228,8 +316,21 @@ func (mmDebug *LoggerMock) Debug(msg any, keyvals ...any) { if mmDebug.DebugMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmDebug.DebugMock.defaultExpectation.Counter, 1) mm_want := mmDebug.DebugMock.defaultExpectation.params + mm_want_ptrs := mmDebug.DebugMock.defaultExpectation.paramPtrs + mm_got := LoggerMockDebugParams{msg, keyvals} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.msg != nil && !minimock.Equal(*mm_want_ptrs.msg, mm_got.msg) { + mmDebug.t.Errorf("LoggerMock.Debug got unexpected parameter msg, want: %#v, got: %#v%s\n", *mm_want_ptrs.msg, mm_got.msg, minimock.Diff(*mm_want_ptrs.msg, mm_got.msg)) + } + + if mm_want_ptrs.keyvals != nil && !minimock.Equal(*mm_want_ptrs.keyvals, mm_got.keyvals) { + mmDebug.t.Errorf("LoggerMock.Debug got unexpected parameter keyvals, want: %#v, got: %#v%s\n", *mm_want_ptrs.keyvals, mm_got.keyvals, minimock.Diff(*mm_want_ptrs.keyvals, mm_got.keyvals)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmDebug.t.Errorf("LoggerMock.Debug got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -270,21 +371,18 @@ func (mmDebug *mLoggerMockDebug) Calls() []*LoggerMockDebugParams { // MinimockDebugDone returns true if the count of the Debug invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockDebugDone() bool { + if m.DebugMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.DebugMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.DebugMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDebugCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcDebug != nil && mm_atomic.LoadUint64(&m.afterDebugCounter) < 1 { - return false - } - return true + return m.DebugMock.invocationsDone() } // MinimockDebugInspect logs each unmet expectation @@ -295,8 +393,9 @@ func (m *LoggerMock) MinimockDebugInspect() { } } + afterDebugCounter := mm_atomic.LoadUint64(&m.afterDebugCounter) // if default expectation was set then invocations count should be greater than zero - if m.DebugMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDebugCounter) < 1 { + if m.DebugMock.defaultExpectation != nil && afterDebugCounter < 1 { if m.DebugMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Debug") } else { @@ -304,24 +403,33 @@ func (m *LoggerMock) MinimockDebugInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcDebug != nil && mm_atomic.LoadUint64(&m.afterDebugCounter) < 1 { + if m.funcDebug != nil && afterDebugCounter < 1 { m.t.Error("Expected call to LoggerMock.Debug") } + + if !m.DebugMock.invocationsDone() && afterDebugCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Debug but found %d calls", + mm_atomic.LoadUint64(&m.DebugMock.expectedInvocations), afterDebugCounter) + } } type mLoggerMockDebugf struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockDebugfExpectation expectations []*LoggerMockDebugfExpectation callArgs []*LoggerMockDebugfParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockDebugfExpectation specifies expectation struct of the Logger.Debugf type LoggerMockDebugfExpectation struct { - mock *LoggerMock - params *LoggerMockDebugfParams + mock *LoggerMock + params *LoggerMockDebugfParams + paramPtrs *LoggerMockDebugfParamPtrs Counter uint64 } @@ -332,6 +440,22 @@ type LoggerMockDebugfParams struct { a []any } +// LoggerMockDebugfParamPtrs contains pointers to parameters of the Logger.Debugf +type LoggerMockDebugfParamPtrs struct { + template *string + a *[]any +} + +// 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 (mmDebugf *mLoggerMockDebugf) Optional() *mLoggerMockDebugf { + mmDebugf.optional = true + return mmDebugf +} + // Expect sets up expected params for Logger.Debugf func (mmDebugf *mLoggerMockDebugf) Expect(template string, a ...any) *mLoggerMockDebugf { if mmDebugf.mock.funcDebugf != nil { @@ -342,6 +466,10 @@ func (mmDebugf *mLoggerMockDebugf) Expect(template string, a ...any) *mLoggerMoc mmDebugf.defaultExpectation = &LoggerMockDebugfExpectation{} } + if mmDebugf.defaultExpectation.paramPtrs != nil { + mmDebugf.mock.t.Fatalf("LoggerMock.Debugf mock is already set by ExpectParams functions") + } + mmDebugf.defaultExpectation.params = &LoggerMockDebugfParams{template, a} for _, e := range mmDebugf.expectations { if minimock.Equal(e.params, mmDebugf.defaultExpectation.params) { @@ -352,6 +480,50 @@ func (mmDebugf *mLoggerMockDebugf) Expect(template string, a ...any) *mLoggerMoc return mmDebugf } +// ExpectTemplateParam1 sets up expected param template for Logger.Debugf +func (mmDebugf *mLoggerMockDebugf) ExpectTemplateParam1(template string) *mLoggerMockDebugf { + if mmDebugf.mock.funcDebugf != nil { + mmDebugf.mock.t.Fatalf("LoggerMock.Debugf mock is already set by Set") + } + + if mmDebugf.defaultExpectation == nil { + mmDebugf.defaultExpectation = &LoggerMockDebugfExpectation{} + } + + if mmDebugf.defaultExpectation.params != nil { + mmDebugf.mock.t.Fatalf("LoggerMock.Debugf mock is already set by Expect") + } + + if mmDebugf.defaultExpectation.paramPtrs == nil { + mmDebugf.defaultExpectation.paramPtrs = &LoggerMockDebugfParamPtrs{} + } + mmDebugf.defaultExpectation.paramPtrs.template = &template + + return mmDebugf +} + +// ExpectAParam2 sets up expected param a for Logger.Debugf +func (mmDebugf *mLoggerMockDebugf) ExpectAParam2(a ...any) *mLoggerMockDebugf { + if mmDebugf.mock.funcDebugf != nil { + mmDebugf.mock.t.Fatalf("LoggerMock.Debugf mock is already set by Set") + } + + if mmDebugf.defaultExpectation == nil { + mmDebugf.defaultExpectation = &LoggerMockDebugfExpectation{} + } + + if mmDebugf.defaultExpectation.params != nil { + mmDebugf.mock.t.Fatalf("LoggerMock.Debugf mock is already set by Expect") + } + + if mmDebugf.defaultExpectation.paramPtrs == nil { + mmDebugf.defaultExpectation.paramPtrs = &LoggerMockDebugfParamPtrs{} + } + mmDebugf.defaultExpectation.paramPtrs.a = &a + + return mmDebugf +} + // Inspect accepts an inspector function that has same arguments as the Logger.Debugf func (mmDebugf *mLoggerMockDebugf) Inspect(f func(template string, a ...any)) *mLoggerMockDebugf { if mmDebugf.mock.inspectFuncDebugf != nil { @@ -390,6 +562,26 @@ func (mmDebugf *mLoggerMockDebugf) Set(f func(template string, a ...any)) *Logge return mmDebugf.mock } +// Times sets number of times Logger.Debugf should be invoked +func (mmDebugf *mLoggerMockDebugf) Times(n uint64) *mLoggerMockDebugf { + if n == 0 { + mmDebugf.mock.t.Fatalf("Times of LoggerMock.Debugf mock can not be zero") + } + mm_atomic.StoreUint64(&mmDebugf.expectedInvocations, n) + return mmDebugf +} + +func (mmDebugf *mLoggerMockDebugf) invocationsDone() bool { + if len(mmDebugf.expectations) == 0 && mmDebugf.defaultExpectation == nil && mmDebugf.mock.funcDebugf == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmDebugf.mock.afterDebugfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmDebugf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Debugf implements contracts.Logger func (mmDebugf *LoggerMock) Debugf(template string, a ...any) { mm_atomic.AddUint64(&mmDebugf.beforeDebugfCounter, 1) @@ -416,8 +608,21 @@ func (mmDebugf *LoggerMock) Debugf(template string, a ...any) { if mmDebugf.DebugfMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmDebugf.DebugfMock.defaultExpectation.Counter, 1) mm_want := mmDebugf.DebugfMock.defaultExpectation.params + mm_want_ptrs := mmDebugf.DebugfMock.defaultExpectation.paramPtrs + mm_got := LoggerMockDebugfParams{template, a} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.template != nil && !minimock.Equal(*mm_want_ptrs.template, mm_got.template) { + mmDebugf.t.Errorf("LoggerMock.Debugf got unexpected parameter template, want: %#v, got: %#v%s\n", *mm_want_ptrs.template, mm_got.template, minimock.Diff(*mm_want_ptrs.template, mm_got.template)) + } + + if mm_want_ptrs.a != nil && !minimock.Equal(*mm_want_ptrs.a, mm_got.a) { + mmDebugf.t.Errorf("LoggerMock.Debugf got unexpected parameter a, want: %#v, got: %#v%s\n", *mm_want_ptrs.a, mm_got.a, minimock.Diff(*mm_want_ptrs.a, mm_got.a)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmDebugf.t.Errorf("LoggerMock.Debugf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -458,21 +663,18 @@ func (mmDebugf *mLoggerMockDebugf) Calls() []*LoggerMockDebugfParams { // MinimockDebugfDone returns true if the count of the Debugf invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockDebugfDone() bool { + if m.DebugfMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.DebugfMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.DebugfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDebugfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcDebugf != nil && mm_atomic.LoadUint64(&m.afterDebugfCounter) < 1 { - return false - } - return true + return m.DebugfMock.invocationsDone() } // MinimockDebugfInspect logs each unmet expectation @@ -483,8 +685,9 @@ func (m *LoggerMock) MinimockDebugfInspect() { } } + afterDebugfCounter := mm_atomic.LoadUint64(&m.afterDebugfCounter) // if default expectation was set then invocations count should be greater than zero - if m.DebugfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterDebugfCounter) < 1 { + if m.DebugfMock.defaultExpectation != nil && afterDebugfCounter < 1 { if m.DebugfMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Debugf") } else { @@ -492,24 +695,33 @@ func (m *LoggerMock) MinimockDebugfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcDebugf != nil && mm_atomic.LoadUint64(&m.afterDebugfCounter) < 1 { + if m.funcDebugf != nil && afterDebugfCounter < 1 { m.t.Error("Expected call to LoggerMock.Debugf") } + + if !m.DebugfMock.invocationsDone() && afterDebugfCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Debugf but found %d calls", + mm_atomic.LoadUint64(&m.DebugfMock.expectedInvocations), afterDebugfCounter) + } } type mLoggerMockError struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockErrorExpectation expectations []*LoggerMockErrorExpectation callArgs []*LoggerMockErrorParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockErrorExpectation specifies expectation struct of the Logger.Error type LoggerMockErrorExpectation struct { - mock *LoggerMock - params *LoggerMockErrorParams + mock *LoggerMock + params *LoggerMockErrorParams + paramPtrs *LoggerMockErrorParamPtrs Counter uint64 } @@ -520,6 +732,22 @@ type LoggerMockErrorParams struct { keyvals []any } +// LoggerMockErrorParamPtrs contains pointers to parameters of the Logger.Error +type LoggerMockErrorParamPtrs struct { + msg *any + keyvals *[]any +} + +// 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 (mmError *mLoggerMockError) Optional() *mLoggerMockError { + mmError.optional = true + return mmError +} + // Expect sets up expected params for Logger.Error func (mmError *mLoggerMockError) Expect(msg any, keyvals ...any) *mLoggerMockError { if mmError.mock.funcError != nil { @@ -530,6 +758,10 @@ func (mmError *mLoggerMockError) Expect(msg any, keyvals ...any) *mLoggerMockErr mmError.defaultExpectation = &LoggerMockErrorExpectation{} } + if mmError.defaultExpectation.paramPtrs != nil { + mmError.mock.t.Fatalf("LoggerMock.Error mock is already set by ExpectParams functions") + } + mmError.defaultExpectation.params = &LoggerMockErrorParams{msg, keyvals} for _, e := range mmError.expectations { if minimock.Equal(e.params, mmError.defaultExpectation.params) { @@ -540,6 +772,50 @@ func (mmError *mLoggerMockError) Expect(msg any, keyvals ...any) *mLoggerMockErr return mmError } +// ExpectMsgParam1 sets up expected param msg for Logger.Error +func (mmError *mLoggerMockError) ExpectMsgParam1(msg any) *mLoggerMockError { + if mmError.mock.funcError != nil { + mmError.mock.t.Fatalf("LoggerMock.Error mock is already set by Set") + } + + if mmError.defaultExpectation == nil { + mmError.defaultExpectation = &LoggerMockErrorExpectation{} + } + + if mmError.defaultExpectation.params != nil { + mmError.mock.t.Fatalf("LoggerMock.Error mock is already set by Expect") + } + + if mmError.defaultExpectation.paramPtrs == nil { + mmError.defaultExpectation.paramPtrs = &LoggerMockErrorParamPtrs{} + } + mmError.defaultExpectation.paramPtrs.msg = &msg + + return mmError +} + +// ExpectKeyvalsParam2 sets up expected param keyvals for Logger.Error +func (mmError *mLoggerMockError) ExpectKeyvalsParam2(keyvals ...any) *mLoggerMockError { + if mmError.mock.funcError != nil { + mmError.mock.t.Fatalf("LoggerMock.Error mock is already set by Set") + } + + if mmError.defaultExpectation == nil { + mmError.defaultExpectation = &LoggerMockErrorExpectation{} + } + + if mmError.defaultExpectation.params != nil { + mmError.mock.t.Fatalf("LoggerMock.Error mock is already set by Expect") + } + + if mmError.defaultExpectation.paramPtrs == nil { + mmError.defaultExpectation.paramPtrs = &LoggerMockErrorParamPtrs{} + } + mmError.defaultExpectation.paramPtrs.keyvals = &keyvals + + return mmError +} + // Inspect accepts an inspector function that has same arguments as the Logger.Error func (mmError *mLoggerMockError) Inspect(f func(msg any, keyvals ...any)) *mLoggerMockError { if mmError.mock.inspectFuncError != nil { @@ -578,6 +854,26 @@ func (mmError *mLoggerMockError) Set(f func(msg any, keyvals ...any)) *LoggerMoc return mmError.mock } +// Times sets number of times Logger.Error should be invoked +func (mmError *mLoggerMockError) Times(n uint64) *mLoggerMockError { + if n == 0 { + mmError.mock.t.Fatalf("Times of LoggerMock.Error mock can not be zero") + } + mm_atomic.StoreUint64(&mmError.expectedInvocations, n) + return mmError +} + +func (mmError *mLoggerMockError) invocationsDone() bool { + if len(mmError.expectations) == 0 && mmError.defaultExpectation == nil && mmError.mock.funcError == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmError.mock.afterErrorCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmError.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Error implements contracts.Logger func (mmError *LoggerMock) Error(msg any, keyvals ...any) { mm_atomic.AddUint64(&mmError.beforeErrorCounter, 1) @@ -604,8 +900,21 @@ func (mmError *LoggerMock) Error(msg any, keyvals ...any) { if mmError.ErrorMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmError.ErrorMock.defaultExpectation.Counter, 1) mm_want := mmError.ErrorMock.defaultExpectation.params + mm_want_ptrs := mmError.ErrorMock.defaultExpectation.paramPtrs + mm_got := LoggerMockErrorParams{msg, keyvals} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.msg != nil && !minimock.Equal(*mm_want_ptrs.msg, mm_got.msg) { + mmError.t.Errorf("LoggerMock.Error got unexpected parameter msg, want: %#v, got: %#v%s\n", *mm_want_ptrs.msg, mm_got.msg, minimock.Diff(*mm_want_ptrs.msg, mm_got.msg)) + } + + if mm_want_ptrs.keyvals != nil && !minimock.Equal(*mm_want_ptrs.keyvals, mm_got.keyvals) { + mmError.t.Errorf("LoggerMock.Error got unexpected parameter keyvals, want: %#v, got: %#v%s\n", *mm_want_ptrs.keyvals, mm_got.keyvals, minimock.Diff(*mm_want_ptrs.keyvals, mm_got.keyvals)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmError.t.Errorf("LoggerMock.Error got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -646,21 +955,18 @@ func (mmError *mLoggerMockError) Calls() []*LoggerMockErrorParams { // MinimockErrorDone returns true if the count of the Error invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockErrorDone() bool { + if m.ErrorMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.ErrorMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { - return false - } - return true + return m.ErrorMock.invocationsDone() } // MinimockErrorInspect logs each unmet expectation @@ -671,8 +977,9 @@ func (m *LoggerMock) MinimockErrorInspect() { } } + afterErrorCounter := mm_atomic.LoadUint64(&m.afterErrorCounter) // if default expectation was set then invocations count should be greater than zero - if m.ErrorMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + if m.ErrorMock.defaultExpectation != nil && afterErrorCounter < 1 { if m.ErrorMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Error") } else { @@ -680,24 +987,33 @@ func (m *LoggerMock) MinimockErrorInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcError != nil && mm_atomic.LoadUint64(&m.afterErrorCounter) < 1 { + if m.funcError != nil && afterErrorCounter < 1 { m.t.Error("Expected call to LoggerMock.Error") } + + if !m.ErrorMock.invocationsDone() && afterErrorCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Error but found %d calls", + mm_atomic.LoadUint64(&m.ErrorMock.expectedInvocations), afterErrorCounter) + } } type mLoggerMockErrorf struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockErrorfExpectation expectations []*LoggerMockErrorfExpectation callArgs []*LoggerMockErrorfParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockErrorfExpectation specifies expectation struct of the Logger.Errorf type LoggerMockErrorfExpectation struct { - mock *LoggerMock - params *LoggerMockErrorfParams + mock *LoggerMock + params *LoggerMockErrorfParams + paramPtrs *LoggerMockErrorfParamPtrs Counter uint64 } @@ -708,6 +1024,22 @@ type LoggerMockErrorfParams struct { a []any } +// LoggerMockErrorfParamPtrs contains pointers to parameters of the Logger.Errorf +type LoggerMockErrorfParamPtrs struct { + template *string + a *[]any +} + +// 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 (mmErrorf *mLoggerMockErrorf) Optional() *mLoggerMockErrorf { + mmErrorf.optional = true + return mmErrorf +} + // Expect sets up expected params for Logger.Errorf func (mmErrorf *mLoggerMockErrorf) Expect(template string, a ...any) *mLoggerMockErrorf { if mmErrorf.mock.funcErrorf != nil { @@ -718,6 +1050,10 @@ func (mmErrorf *mLoggerMockErrorf) Expect(template string, a ...any) *mLoggerMoc mmErrorf.defaultExpectation = &LoggerMockErrorfExpectation{} } + if mmErrorf.defaultExpectation.paramPtrs != nil { + mmErrorf.mock.t.Fatalf("LoggerMock.Errorf mock is already set by ExpectParams functions") + } + mmErrorf.defaultExpectation.params = &LoggerMockErrorfParams{template, a} for _, e := range mmErrorf.expectations { if minimock.Equal(e.params, mmErrorf.defaultExpectation.params) { @@ -728,6 +1064,50 @@ func (mmErrorf *mLoggerMockErrorf) Expect(template string, a ...any) *mLoggerMoc return mmErrorf } +// ExpectTemplateParam1 sets up expected param template for Logger.Errorf +func (mmErrorf *mLoggerMockErrorf) ExpectTemplateParam1(template string) *mLoggerMockErrorf { + if mmErrorf.mock.funcErrorf != nil { + mmErrorf.mock.t.Fatalf("LoggerMock.Errorf mock is already set by Set") + } + + if mmErrorf.defaultExpectation == nil { + mmErrorf.defaultExpectation = &LoggerMockErrorfExpectation{} + } + + if mmErrorf.defaultExpectation.params != nil { + mmErrorf.mock.t.Fatalf("LoggerMock.Errorf mock is already set by Expect") + } + + if mmErrorf.defaultExpectation.paramPtrs == nil { + mmErrorf.defaultExpectation.paramPtrs = &LoggerMockErrorfParamPtrs{} + } + mmErrorf.defaultExpectation.paramPtrs.template = &template + + return mmErrorf +} + +// ExpectAParam2 sets up expected param a for Logger.Errorf +func (mmErrorf *mLoggerMockErrorf) ExpectAParam2(a ...any) *mLoggerMockErrorf { + if mmErrorf.mock.funcErrorf != nil { + mmErrorf.mock.t.Fatalf("LoggerMock.Errorf mock is already set by Set") + } + + if mmErrorf.defaultExpectation == nil { + mmErrorf.defaultExpectation = &LoggerMockErrorfExpectation{} + } + + if mmErrorf.defaultExpectation.params != nil { + mmErrorf.mock.t.Fatalf("LoggerMock.Errorf mock is already set by Expect") + } + + if mmErrorf.defaultExpectation.paramPtrs == nil { + mmErrorf.defaultExpectation.paramPtrs = &LoggerMockErrorfParamPtrs{} + } + mmErrorf.defaultExpectation.paramPtrs.a = &a + + return mmErrorf +} + // Inspect accepts an inspector function that has same arguments as the Logger.Errorf func (mmErrorf *mLoggerMockErrorf) Inspect(f func(template string, a ...any)) *mLoggerMockErrorf { if mmErrorf.mock.inspectFuncErrorf != nil { @@ -766,6 +1146,26 @@ func (mmErrorf *mLoggerMockErrorf) Set(f func(template string, a ...any)) *Logge return mmErrorf.mock } +// Times sets number of times Logger.Errorf should be invoked +func (mmErrorf *mLoggerMockErrorf) Times(n uint64) *mLoggerMockErrorf { + if n == 0 { + mmErrorf.mock.t.Fatalf("Times of LoggerMock.Errorf mock can not be zero") + } + mm_atomic.StoreUint64(&mmErrorf.expectedInvocations, n) + return mmErrorf +} + +func (mmErrorf *mLoggerMockErrorf) invocationsDone() bool { + if len(mmErrorf.expectations) == 0 && mmErrorf.defaultExpectation == nil && mmErrorf.mock.funcErrorf == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmErrorf.mock.afterErrorfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmErrorf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Errorf implements contracts.Logger func (mmErrorf *LoggerMock) Errorf(template string, a ...any) { mm_atomic.AddUint64(&mmErrorf.beforeErrorfCounter, 1) @@ -792,8 +1192,21 @@ func (mmErrorf *LoggerMock) Errorf(template string, a ...any) { if mmErrorf.ErrorfMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmErrorf.ErrorfMock.defaultExpectation.Counter, 1) mm_want := mmErrorf.ErrorfMock.defaultExpectation.params + mm_want_ptrs := mmErrorf.ErrorfMock.defaultExpectation.paramPtrs + mm_got := LoggerMockErrorfParams{template, a} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.template != nil && !minimock.Equal(*mm_want_ptrs.template, mm_got.template) { + mmErrorf.t.Errorf("LoggerMock.Errorf got unexpected parameter template, want: %#v, got: %#v%s\n", *mm_want_ptrs.template, mm_got.template, minimock.Diff(*mm_want_ptrs.template, mm_got.template)) + } + + if mm_want_ptrs.a != nil && !minimock.Equal(*mm_want_ptrs.a, mm_got.a) { + mmErrorf.t.Errorf("LoggerMock.Errorf got unexpected parameter a, want: %#v, got: %#v%s\n", *mm_want_ptrs.a, mm_got.a, minimock.Diff(*mm_want_ptrs.a, mm_got.a)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmErrorf.t.Errorf("LoggerMock.Errorf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -834,21 +1247,18 @@ func (mmErrorf *mLoggerMockErrorf) Calls() []*LoggerMockErrorfParams { // MinimockErrorfDone returns true if the count of the Errorf invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockErrorfDone() bool { + if m.ErrorfMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.ErrorfMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { - return false - } - return true + return m.ErrorfMock.invocationsDone() } // MinimockErrorfInspect logs each unmet expectation @@ -859,8 +1269,9 @@ func (m *LoggerMock) MinimockErrorfInspect() { } } + afterErrorfCounter := mm_atomic.LoadUint64(&m.afterErrorfCounter) // if default expectation was set then invocations count should be greater than zero - if m.ErrorfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + if m.ErrorfMock.defaultExpectation != nil && afterErrorfCounter < 1 { if m.ErrorfMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Errorf") } else { @@ -868,24 +1279,33 @@ func (m *LoggerMock) MinimockErrorfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcErrorf != nil && mm_atomic.LoadUint64(&m.afterErrorfCounter) < 1 { + if m.funcErrorf != nil && afterErrorfCounter < 1 { m.t.Error("Expected call to LoggerMock.Errorf") } + + if !m.ErrorfMock.invocationsDone() && afterErrorfCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Errorf but found %d calls", + mm_atomic.LoadUint64(&m.ErrorfMock.expectedInvocations), afterErrorfCounter) + } } type mLoggerMockInfo struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockInfoExpectation expectations []*LoggerMockInfoExpectation callArgs []*LoggerMockInfoParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockInfoExpectation specifies expectation struct of the Logger.Info type LoggerMockInfoExpectation struct { - mock *LoggerMock - params *LoggerMockInfoParams + mock *LoggerMock + params *LoggerMockInfoParams + paramPtrs *LoggerMockInfoParamPtrs Counter uint64 } @@ -896,6 +1316,22 @@ type LoggerMockInfoParams struct { keyvals []any } +// LoggerMockInfoParamPtrs contains pointers to parameters of the Logger.Info +type LoggerMockInfoParamPtrs struct { + msg *any + keyvals *[]any +} + +// 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 (mmInfo *mLoggerMockInfo) Optional() *mLoggerMockInfo { + mmInfo.optional = true + return mmInfo +} + // Expect sets up expected params for Logger.Info func (mmInfo *mLoggerMockInfo) Expect(msg any, keyvals ...any) *mLoggerMockInfo { if mmInfo.mock.funcInfo != nil { @@ -906,6 +1342,10 @@ func (mmInfo *mLoggerMockInfo) Expect(msg any, keyvals ...any) *mLoggerMockInfo mmInfo.defaultExpectation = &LoggerMockInfoExpectation{} } + if mmInfo.defaultExpectation.paramPtrs != nil { + mmInfo.mock.t.Fatalf("LoggerMock.Info mock is already set by ExpectParams functions") + } + mmInfo.defaultExpectation.params = &LoggerMockInfoParams{msg, keyvals} for _, e := range mmInfo.expectations { if minimock.Equal(e.params, mmInfo.defaultExpectation.params) { @@ -916,6 +1356,50 @@ func (mmInfo *mLoggerMockInfo) Expect(msg any, keyvals ...any) *mLoggerMockInfo return mmInfo } +// ExpectMsgParam1 sets up expected param msg for Logger.Info +func (mmInfo *mLoggerMockInfo) ExpectMsgParam1(msg any) *mLoggerMockInfo { + if mmInfo.mock.funcInfo != nil { + mmInfo.mock.t.Fatalf("LoggerMock.Info mock is already set by Set") + } + + if mmInfo.defaultExpectation == nil { + mmInfo.defaultExpectation = &LoggerMockInfoExpectation{} + } + + if mmInfo.defaultExpectation.params != nil { + mmInfo.mock.t.Fatalf("LoggerMock.Info mock is already set by Expect") + } + + if mmInfo.defaultExpectation.paramPtrs == nil { + mmInfo.defaultExpectation.paramPtrs = &LoggerMockInfoParamPtrs{} + } + mmInfo.defaultExpectation.paramPtrs.msg = &msg + + return mmInfo +} + +// ExpectKeyvalsParam2 sets up expected param keyvals for Logger.Info +func (mmInfo *mLoggerMockInfo) ExpectKeyvalsParam2(keyvals ...any) *mLoggerMockInfo { + if mmInfo.mock.funcInfo != nil { + mmInfo.mock.t.Fatalf("LoggerMock.Info mock is already set by Set") + } + + if mmInfo.defaultExpectation == nil { + mmInfo.defaultExpectation = &LoggerMockInfoExpectation{} + } + + if mmInfo.defaultExpectation.params != nil { + mmInfo.mock.t.Fatalf("LoggerMock.Info mock is already set by Expect") + } + + if mmInfo.defaultExpectation.paramPtrs == nil { + mmInfo.defaultExpectation.paramPtrs = &LoggerMockInfoParamPtrs{} + } + mmInfo.defaultExpectation.paramPtrs.keyvals = &keyvals + + return mmInfo +} + // Inspect accepts an inspector function that has same arguments as the Logger.Info func (mmInfo *mLoggerMockInfo) Inspect(f func(msg any, keyvals ...any)) *mLoggerMockInfo { if mmInfo.mock.inspectFuncInfo != nil { @@ -954,6 +1438,26 @@ func (mmInfo *mLoggerMockInfo) Set(f func(msg any, keyvals ...any)) *LoggerMock return mmInfo.mock } +// Times sets number of times Logger.Info should be invoked +func (mmInfo *mLoggerMockInfo) Times(n uint64) *mLoggerMockInfo { + if n == 0 { + mmInfo.mock.t.Fatalf("Times of LoggerMock.Info mock can not be zero") + } + mm_atomic.StoreUint64(&mmInfo.expectedInvocations, n) + return mmInfo +} + +func (mmInfo *mLoggerMockInfo) invocationsDone() bool { + if len(mmInfo.expectations) == 0 && mmInfo.defaultExpectation == nil && mmInfo.mock.funcInfo == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmInfo.mock.afterInfoCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmInfo.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Info implements contracts.Logger func (mmInfo *LoggerMock) Info(msg any, keyvals ...any) { mm_atomic.AddUint64(&mmInfo.beforeInfoCounter, 1) @@ -980,8 +1484,21 @@ func (mmInfo *LoggerMock) Info(msg any, keyvals ...any) { if mmInfo.InfoMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmInfo.InfoMock.defaultExpectation.Counter, 1) mm_want := mmInfo.InfoMock.defaultExpectation.params + mm_want_ptrs := mmInfo.InfoMock.defaultExpectation.paramPtrs + mm_got := LoggerMockInfoParams{msg, keyvals} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.msg != nil && !minimock.Equal(*mm_want_ptrs.msg, mm_got.msg) { + mmInfo.t.Errorf("LoggerMock.Info got unexpected parameter msg, want: %#v, got: %#v%s\n", *mm_want_ptrs.msg, mm_got.msg, minimock.Diff(*mm_want_ptrs.msg, mm_got.msg)) + } + + if mm_want_ptrs.keyvals != nil && !minimock.Equal(*mm_want_ptrs.keyvals, mm_got.keyvals) { + mmInfo.t.Errorf("LoggerMock.Info got unexpected parameter keyvals, want: %#v, got: %#v%s\n", *mm_want_ptrs.keyvals, mm_got.keyvals, minimock.Diff(*mm_want_ptrs.keyvals, mm_got.keyvals)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmInfo.t.Errorf("LoggerMock.Info got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -1022,21 +1539,18 @@ func (mmInfo *mLoggerMockInfo) Calls() []*LoggerMockInfoParams { // MinimockInfoDone returns true if the count of the Info invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockInfoDone() bool { + if m.InfoMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.InfoMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.InfoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterInfoCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcInfo != nil && mm_atomic.LoadUint64(&m.afterInfoCounter) < 1 { - return false - } - return true + return m.InfoMock.invocationsDone() } // MinimockInfoInspect logs each unmet expectation @@ -1047,8 +1561,9 @@ func (m *LoggerMock) MinimockInfoInspect() { } } + afterInfoCounter := mm_atomic.LoadUint64(&m.afterInfoCounter) // if default expectation was set then invocations count should be greater than zero - if m.InfoMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterInfoCounter) < 1 { + if m.InfoMock.defaultExpectation != nil && afterInfoCounter < 1 { if m.InfoMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Info") } else { @@ -1056,24 +1571,33 @@ func (m *LoggerMock) MinimockInfoInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcInfo != nil && mm_atomic.LoadUint64(&m.afterInfoCounter) < 1 { + if m.funcInfo != nil && afterInfoCounter < 1 { m.t.Error("Expected call to LoggerMock.Info") } + + if !m.InfoMock.invocationsDone() && afterInfoCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Info but found %d calls", + mm_atomic.LoadUint64(&m.InfoMock.expectedInvocations), afterInfoCounter) + } } type mLoggerMockInfof struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockInfofExpectation expectations []*LoggerMockInfofExpectation callArgs []*LoggerMockInfofParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockInfofExpectation specifies expectation struct of the Logger.Infof type LoggerMockInfofExpectation struct { - mock *LoggerMock - params *LoggerMockInfofParams + mock *LoggerMock + params *LoggerMockInfofParams + paramPtrs *LoggerMockInfofParamPtrs Counter uint64 } @@ -1084,6 +1608,22 @@ type LoggerMockInfofParams struct { a []any } +// LoggerMockInfofParamPtrs contains pointers to parameters of the Logger.Infof +type LoggerMockInfofParamPtrs struct { + template *string + a *[]any +} + +// 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 (mmInfof *mLoggerMockInfof) Optional() *mLoggerMockInfof { + mmInfof.optional = true + return mmInfof +} + // Expect sets up expected params for Logger.Infof func (mmInfof *mLoggerMockInfof) Expect(template string, a ...any) *mLoggerMockInfof { if mmInfof.mock.funcInfof != nil { @@ -1094,6 +1634,10 @@ func (mmInfof *mLoggerMockInfof) Expect(template string, a ...any) *mLoggerMockI mmInfof.defaultExpectation = &LoggerMockInfofExpectation{} } + if mmInfof.defaultExpectation.paramPtrs != nil { + mmInfof.mock.t.Fatalf("LoggerMock.Infof mock is already set by ExpectParams functions") + } + mmInfof.defaultExpectation.params = &LoggerMockInfofParams{template, a} for _, e := range mmInfof.expectations { if minimock.Equal(e.params, mmInfof.defaultExpectation.params) { @@ -1104,6 +1648,50 @@ func (mmInfof *mLoggerMockInfof) Expect(template string, a ...any) *mLoggerMockI return mmInfof } +// ExpectTemplateParam1 sets up expected param template for Logger.Infof +func (mmInfof *mLoggerMockInfof) ExpectTemplateParam1(template string) *mLoggerMockInfof { + if mmInfof.mock.funcInfof != nil { + mmInfof.mock.t.Fatalf("LoggerMock.Infof mock is already set by Set") + } + + if mmInfof.defaultExpectation == nil { + mmInfof.defaultExpectation = &LoggerMockInfofExpectation{} + } + + if mmInfof.defaultExpectation.params != nil { + mmInfof.mock.t.Fatalf("LoggerMock.Infof mock is already set by Expect") + } + + if mmInfof.defaultExpectation.paramPtrs == nil { + mmInfof.defaultExpectation.paramPtrs = &LoggerMockInfofParamPtrs{} + } + mmInfof.defaultExpectation.paramPtrs.template = &template + + return mmInfof +} + +// ExpectAParam2 sets up expected param a for Logger.Infof +func (mmInfof *mLoggerMockInfof) ExpectAParam2(a ...any) *mLoggerMockInfof { + if mmInfof.mock.funcInfof != nil { + mmInfof.mock.t.Fatalf("LoggerMock.Infof mock is already set by Set") + } + + if mmInfof.defaultExpectation == nil { + mmInfof.defaultExpectation = &LoggerMockInfofExpectation{} + } + + if mmInfof.defaultExpectation.params != nil { + mmInfof.mock.t.Fatalf("LoggerMock.Infof mock is already set by Expect") + } + + if mmInfof.defaultExpectation.paramPtrs == nil { + mmInfof.defaultExpectation.paramPtrs = &LoggerMockInfofParamPtrs{} + } + mmInfof.defaultExpectation.paramPtrs.a = &a + + return mmInfof +} + // Inspect accepts an inspector function that has same arguments as the Logger.Infof func (mmInfof *mLoggerMockInfof) Inspect(f func(template string, a ...any)) *mLoggerMockInfof { if mmInfof.mock.inspectFuncInfof != nil { @@ -1138,8 +1726,28 @@ func (mmInfof *mLoggerMockInfof) Set(f func(template string, a ...any)) *LoggerM mmInfof.mock.t.Fatalf("Some expectations are already set for the Logger.Infof method") } - mmInfof.mock.funcInfof = f - return mmInfof.mock + mmInfof.mock.funcInfof = f + return mmInfof.mock +} + +// Times sets number of times Logger.Infof should be invoked +func (mmInfof *mLoggerMockInfof) Times(n uint64) *mLoggerMockInfof { + if n == 0 { + mmInfof.mock.t.Fatalf("Times of LoggerMock.Infof mock can not be zero") + } + mm_atomic.StoreUint64(&mmInfof.expectedInvocations, n) + return mmInfof +} + +func (mmInfof *mLoggerMockInfof) invocationsDone() bool { + if len(mmInfof.expectations) == 0 && mmInfof.defaultExpectation == nil && mmInfof.mock.funcInfof == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmInfof.mock.afterInfofCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmInfof.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) } // Infof implements contracts.Logger @@ -1168,8 +1776,21 @@ func (mmInfof *LoggerMock) Infof(template string, a ...any) { if mmInfof.InfofMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmInfof.InfofMock.defaultExpectation.Counter, 1) mm_want := mmInfof.InfofMock.defaultExpectation.params + mm_want_ptrs := mmInfof.InfofMock.defaultExpectation.paramPtrs + mm_got := LoggerMockInfofParams{template, a} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.template != nil && !minimock.Equal(*mm_want_ptrs.template, mm_got.template) { + mmInfof.t.Errorf("LoggerMock.Infof got unexpected parameter template, want: %#v, got: %#v%s\n", *mm_want_ptrs.template, mm_got.template, minimock.Diff(*mm_want_ptrs.template, mm_got.template)) + } + + if mm_want_ptrs.a != nil && !minimock.Equal(*mm_want_ptrs.a, mm_got.a) { + mmInfof.t.Errorf("LoggerMock.Infof got unexpected parameter a, want: %#v, got: %#v%s\n", *mm_want_ptrs.a, mm_got.a, minimock.Diff(*mm_want_ptrs.a, mm_got.a)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmInfof.t.Errorf("LoggerMock.Infof got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -1210,21 +1831,18 @@ func (mmInfof *mLoggerMockInfof) Calls() []*LoggerMockInfofParams { // MinimockInfofDone returns true if the count of the Infof invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockInfofDone() bool { + if m.InfofMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.InfofMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.InfofMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterInfofCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcInfof != nil && mm_atomic.LoadUint64(&m.afterInfofCounter) < 1 { - return false - } - return true + return m.InfofMock.invocationsDone() } // MinimockInfofInspect logs each unmet expectation @@ -1235,8 +1853,9 @@ func (m *LoggerMock) MinimockInfofInspect() { } } + afterInfofCounter := mm_atomic.LoadUint64(&m.afterInfofCounter) // if default expectation was set then invocations count should be greater than zero - if m.InfofMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterInfofCounter) < 1 { + if m.InfofMock.defaultExpectation != nil && afterInfofCounter < 1 { if m.InfofMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Infof") } else { @@ -1244,24 +1863,33 @@ func (m *LoggerMock) MinimockInfofInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcInfof != nil && mm_atomic.LoadUint64(&m.afterInfofCounter) < 1 { + if m.funcInfof != nil && afterInfofCounter < 1 { m.t.Error("Expected call to LoggerMock.Infof") } + + if !m.InfofMock.invocationsDone() && afterInfofCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Infof but found %d calls", + mm_atomic.LoadUint64(&m.InfofMock.expectedInvocations), afterInfofCounter) + } } type mLoggerMockPrint struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockPrintExpectation expectations []*LoggerMockPrintExpectation callArgs []*LoggerMockPrintParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockPrintExpectation specifies expectation struct of the Logger.Print type LoggerMockPrintExpectation struct { - mock *LoggerMock - params *LoggerMockPrintParams + mock *LoggerMock + params *LoggerMockPrintParams + paramPtrs *LoggerMockPrintParamPtrs Counter uint64 } @@ -1272,6 +1900,22 @@ type LoggerMockPrintParams struct { keyvals []any } +// LoggerMockPrintParamPtrs contains pointers to parameters of the Logger.Print +type LoggerMockPrintParamPtrs struct { + msg *any + keyvals *[]any +} + +// 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 (mmPrint *mLoggerMockPrint) Optional() *mLoggerMockPrint { + mmPrint.optional = true + return mmPrint +} + // Expect sets up expected params for Logger.Print func (mmPrint *mLoggerMockPrint) Expect(msg any, keyvals ...any) *mLoggerMockPrint { if mmPrint.mock.funcPrint != nil { @@ -1282,6 +1926,10 @@ func (mmPrint *mLoggerMockPrint) Expect(msg any, keyvals ...any) *mLoggerMockPri mmPrint.defaultExpectation = &LoggerMockPrintExpectation{} } + if mmPrint.defaultExpectation.paramPtrs != nil { + mmPrint.mock.t.Fatalf("LoggerMock.Print mock is already set by ExpectParams functions") + } + mmPrint.defaultExpectation.params = &LoggerMockPrintParams{msg, keyvals} for _, e := range mmPrint.expectations { if minimock.Equal(e.params, mmPrint.defaultExpectation.params) { @@ -1292,6 +1940,50 @@ func (mmPrint *mLoggerMockPrint) Expect(msg any, keyvals ...any) *mLoggerMockPri return mmPrint } +// ExpectMsgParam1 sets up expected param msg for Logger.Print +func (mmPrint *mLoggerMockPrint) ExpectMsgParam1(msg any) *mLoggerMockPrint { + if mmPrint.mock.funcPrint != nil { + mmPrint.mock.t.Fatalf("LoggerMock.Print mock is already set by Set") + } + + if mmPrint.defaultExpectation == nil { + mmPrint.defaultExpectation = &LoggerMockPrintExpectation{} + } + + if mmPrint.defaultExpectation.params != nil { + mmPrint.mock.t.Fatalf("LoggerMock.Print mock is already set by Expect") + } + + if mmPrint.defaultExpectation.paramPtrs == nil { + mmPrint.defaultExpectation.paramPtrs = &LoggerMockPrintParamPtrs{} + } + mmPrint.defaultExpectation.paramPtrs.msg = &msg + + return mmPrint +} + +// ExpectKeyvalsParam2 sets up expected param keyvals for Logger.Print +func (mmPrint *mLoggerMockPrint) ExpectKeyvalsParam2(keyvals ...any) *mLoggerMockPrint { + if mmPrint.mock.funcPrint != nil { + mmPrint.mock.t.Fatalf("LoggerMock.Print mock is already set by Set") + } + + if mmPrint.defaultExpectation == nil { + mmPrint.defaultExpectation = &LoggerMockPrintExpectation{} + } + + if mmPrint.defaultExpectation.params != nil { + mmPrint.mock.t.Fatalf("LoggerMock.Print mock is already set by Expect") + } + + if mmPrint.defaultExpectation.paramPtrs == nil { + mmPrint.defaultExpectation.paramPtrs = &LoggerMockPrintParamPtrs{} + } + mmPrint.defaultExpectation.paramPtrs.keyvals = &keyvals + + return mmPrint +} + // Inspect accepts an inspector function that has same arguments as the Logger.Print func (mmPrint *mLoggerMockPrint) Inspect(f func(msg any, keyvals ...any)) *mLoggerMockPrint { if mmPrint.mock.inspectFuncPrint != nil { @@ -1330,6 +2022,26 @@ func (mmPrint *mLoggerMockPrint) Set(f func(msg any, keyvals ...any)) *LoggerMoc return mmPrint.mock } +// Times sets number of times Logger.Print should be invoked +func (mmPrint *mLoggerMockPrint) Times(n uint64) *mLoggerMockPrint { + if n == 0 { + mmPrint.mock.t.Fatalf("Times of LoggerMock.Print mock can not be zero") + } + mm_atomic.StoreUint64(&mmPrint.expectedInvocations, n) + return mmPrint +} + +func (mmPrint *mLoggerMockPrint) invocationsDone() bool { + if len(mmPrint.expectations) == 0 && mmPrint.defaultExpectation == nil && mmPrint.mock.funcPrint == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmPrint.mock.afterPrintCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmPrint.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Print implements contracts.Logger func (mmPrint *LoggerMock) Print(msg any, keyvals ...any) { mm_atomic.AddUint64(&mmPrint.beforePrintCounter, 1) @@ -1356,8 +2068,21 @@ func (mmPrint *LoggerMock) Print(msg any, keyvals ...any) { if mmPrint.PrintMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmPrint.PrintMock.defaultExpectation.Counter, 1) mm_want := mmPrint.PrintMock.defaultExpectation.params + mm_want_ptrs := mmPrint.PrintMock.defaultExpectation.paramPtrs + mm_got := LoggerMockPrintParams{msg, keyvals} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.msg != nil && !minimock.Equal(*mm_want_ptrs.msg, mm_got.msg) { + mmPrint.t.Errorf("LoggerMock.Print got unexpected parameter msg, want: %#v, got: %#v%s\n", *mm_want_ptrs.msg, mm_got.msg, minimock.Diff(*mm_want_ptrs.msg, mm_got.msg)) + } + + if mm_want_ptrs.keyvals != nil && !minimock.Equal(*mm_want_ptrs.keyvals, mm_got.keyvals) { + mmPrint.t.Errorf("LoggerMock.Print got unexpected parameter keyvals, want: %#v, got: %#v%s\n", *mm_want_ptrs.keyvals, mm_got.keyvals, minimock.Diff(*mm_want_ptrs.keyvals, mm_got.keyvals)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmPrint.t.Errorf("LoggerMock.Print got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -1398,21 +2123,18 @@ func (mmPrint *mLoggerMockPrint) Calls() []*LoggerMockPrintParams { // MinimockPrintDone returns true if the count of the Print invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockPrintDone() bool { + if m.PrintMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.PrintMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.PrintMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterPrintCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcPrint != nil && mm_atomic.LoadUint64(&m.afterPrintCounter) < 1 { - return false - } - return true + return m.PrintMock.invocationsDone() } // MinimockPrintInspect logs each unmet expectation @@ -1423,8 +2145,9 @@ func (m *LoggerMock) MinimockPrintInspect() { } } + afterPrintCounter := mm_atomic.LoadUint64(&m.afterPrintCounter) // if default expectation was set then invocations count should be greater than zero - if m.PrintMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterPrintCounter) < 1 { + if m.PrintMock.defaultExpectation != nil && afterPrintCounter < 1 { if m.PrintMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Print") } else { @@ -1432,24 +2155,33 @@ func (m *LoggerMock) MinimockPrintInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcPrint != nil && mm_atomic.LoadUint64(&m.afterPrintCounter) < 1 { + if m.funcPrint != nil && afterPrintCounter < 1 { m.t.Error("Expected call to LoggerMock.Print") } + + if !m.PrintMock.invocationsDone() && afterPrintCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Print but found %d calls", + mm_atomic.LoadUint64(&m.PrintMock.expectedInvocations), afterPrintCounter) + } } type mLoggerMockPrintf struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockPrintfExpectation expectations []*LoggerMockPrintfExpectation callArgs []*LoggerMockPrintfParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockPrintfExpectation specifies expectation struct of the Logger.Printf type LoggerMockPrintfExpectation struct { - mock *LoggerMock - params *LoggerMockPrintfParams + mock *LoggerMock + params *LoggerMockPrintfParams + paramPtrs *LoggerMockPrintfParamPtrs Counter uint64 } @@ -1460,6 +2192,22 @@ type LoggerMockPrintfParams struct { args []any } +// LoggerMockPrintfParamPtrs contains pointers to parameters of the Logger.Printf +type LoggerMockPrintfParamPtrs struct { + format *string + args *[]any +} + +// 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 (mmPrintf *mLoggerMockPrintf) Optional() *mLoggerMockPrintf { + mmPrintf.optional = true + return mmPrintf +} + // Expect sets up expected params for Logger.Printf func (mmPrintf *mLoggerMockPrintf) Expect(format string, args ...any) *mLoggerMockPrintf { if mmPrintf.mock.funcPrintf != nil { @@ -1470,6 +2218,10 @@ func (mmPrintf *mLoggerMockPrintf) Expect(format string, args ...any) *mLoggerMo mmPrintf.defaultExpectation = &LoggerMockPrintfExpectation{} } + if mmPrintf.defaultExpectation.paramPtrs != nil { + mmPrintf.mock.t.Fatalf("LoggerMock.Printf mock is already set by ExpectParams functions") + } + mmPrintf.defaultExpectation.params = &LoggerMockPrintfParams{format, args} for _, e := range mmPrintf.expectations { if minimock.Equal(e.params, mmPrintf.defaultExpectation.params) { @@ -1480,6 +2232,50 @@ func (mmPrintf *mLoggerMockPrintf) Expect(format string, args ...any) *mLoggerMo return mmPrintf } +// ExpectFormatParam1 sets up expected param format for Logger.Printf +func (mmPrintf *mLoggerMockPrintf) ExpectFormatParam1(format string) *mLoggerMockPrintf { + if mmPrintf.mock.funcPrintf != nil { + mmPrintf.mock.t.Fatalf("LoggerMock.Printf mock is already set by Set") + } + + if mmPrintf.defaultExpectation == nil { + mmPrintf.defaultExpectation = &LoggerMockPrintfExpectation{} + } + + if mmPrintf.defaultExpectation.params != nil { + mmPrintf.mock.t.Fatalf("LoggerMock.Printf mock is already set by Expect") + } + + if mmPrintf.defaultExpectation.paramPtrs == nil { + mmPrintf.defaultExpectation.paramPtrs = &LoggerMockPrintfParamPtrs{} + } + mmPrintf.defaultExpectation.paramPtrs.format = &format + + return mmPrintf +} + +// ExpectArgsParam2 sets up expected param args for Logger.Printf +func (mmPrintf *mLoggerMockPrintf) ExpectArgsParam2(args ...any) *mLoggerMockPrintf { + if mmPrintf.mock.funcPrintf != nil { + mmPrintf.mock.t.Fatalf("LoggerMock.Printf mock is already set by Set") + } + + if mmPrintf.defaultExpectation == nil { + mmPrintf.defaultExpectation = &LoggerMockPrintfExpectation{} + } + + if mmPrintf.defaultExpectation.params != nil { + mmPrintf.mock.t.Fatalf("LoggerMock.Printf mock is already set by Expect") + } + + if mmPrintf.defaultExpectation.paramPtrs == nil { + mmPrintf.defaultExpectation.paramPtrs = &LoggerMockPrintfParamPtrs{} + } + mmPrintf.defaultExpectation.paramPtrs.args = &args + + return mmPrintf +} + // Inspect accepts an inspector function that has same arguments as the Logger.Printf func (mmPrintf *mLoggerMockPrintf) Inspect(f func(format string, args ...any)) *mLoggerMockPrintf { if mmPrintf.mock.inspectFuncPrintf != nil { @@ -1518,6 +2314,26 @@ func (mmPrintf *mLoggerMockPrintf) Set(f func(format string, args ...any)) *Logg return mmPrintf.mock } +// Times sets number of times Logger.Printf should be invoked +func (mmPrintf *mLoggerMockPrintf) Times(n uint64) *mLoggerMockPrintf { + if n == 0 { + mmPrintf.mock.t.Fatalf("Times of LoggerMock.Printf mock can not be zero") + } + mm_atomic.StoreUint64(&mmPrintf.expectedInvocations, n) + return mmPrintf +} + +func (mmPrintf *mLoggerMockPrintf) invocationsDone() bool { + if len(mmPrintf.expectations) == 0 && mmPrintf.defaultExpectation == nil && mmPrintf.mock.funcPrintf == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmPrintf.mock.afterPrintfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmPrintf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Printf implements contracts.Logger func (mmPrintf *LoggerMock) Printf(format string, args ...any) { mm_atomic.AddUint64(&mmPrintf.beforePrintfCounter, 1) @@ -1544,8 +2360,21 @@ func (mmPrintf *LoggerMock) Printf(format string, args ...any) { if mmPrintf.PrintfMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmPrintf.PrintfMock.defaultExpectation.Counter, 1) mm_want := mmPrintf.PrintfMock.defaultExpectation.params + mm_want_ptrs := mmPrintf.PrintfMock.defaultExpectation.paramPtrs + mm_got := LoggerMockPrintfParams{format, args} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.format != nil && !minimock.Equal(*mm_want_ptrs.format, mm_got.format) { + mmPrintf.t.Errorf("LoggerMock.Printf 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)) + } + + if mm_want_ptrs.args != nil && !minimock.Equal(*mm_want_ptrs.args, mm_got.args) { + mmPrintf.t.Errorf("LoggerMock.Printf 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)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmPrintf.t.Errorf("LoggerMock.Printf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -1586,21 +2415,18 @@ func (mmPrintf *mLoggerMockPrintf) Calls() []*LoggerMockPrintfParams { // MinimockPrintfDone returns true if the count of the Printf invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockPrintfDone() bool { + if m.PrintfMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.PrintfMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.PrintfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterPrintfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcPrintf != nil && mm_atomic.LoadUint64(&m.afterPrintfCounter) < 1 { - return false - } - return true + return m.PrintfMock.invocationsDone() } // MinimockPrintfInspect logs each unmet expectation @@ -1611,8 +2437,9 @@ func (m *LoggerMock) MinimockPrintfInspect() { } } + afterPrintfCounter := mm_atomic.LoadUint64(&m.afterPrintfCounter) // if default expectation was set then invocations count should be greater than zero - if m.PrintfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterPrintfCounter) < 1 { + if m.PrintfMock.defaultExpectation != nil && afterPrintfCounter < 1 { if m.PrintfMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Printf") } else { @@ -1620,24 +2447,33 @@ func (m *LoggerMock) MinimockPrintfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcPrintf != nil && mm_atomic.LoadUint64(&m.afterPrintfCounter) < 1 { + if m.funcPrintf != nil && afterPrintfCounter < 1 { m.t.Error("Expected call to LoggerMock.Printf") } + + if !m.PrintfMock.invocationsDone() && afterPrintfCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Printf but found %d calls", + mm_atomic.LoadUint64(&m.PrintfMock.expectedInvocations), afterPrintfCounter) + } } type mLoggerMockWarn struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockWarnExpectation expectations []*LoggerMockWarnExpectation callArgs []*LoggerMockWarnParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockWarnExpectation specifies expectation struct of the Logger.Warn type LoggerMockWarnExpectation struct { - mock *LoggerMock - params *LoggerMockWarnParams + mock *LoggerMock + params *LoggerMockWarnParams + paramPtrs *LoggerMockWarnParamPtrs Counter uint64 } @@ -1648,6 +2484,22 @@ type LoggerMockWarnParams struct { keyvals []any } +// LoggerMockWarnParamPtrs contains pointers to parameters of the Logger.Warn +type LoggerMockWarnParamPtrs struct { + msg *any + keyvals *[]any +} + +// 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 (mmWarn *mLoggerMockWarn) Optional() *mLoggerMockWarn { + mmWarn.optional = true + return mmWarn +} + // Expect sets up expected params for Logger.Warn func (mmWarn *mLoggerMockWarn) Expect(msg any, keyvals ...any) *mLoggerMockWarn { if mmWarn.mock.funcWarn != nil { @@ -1658,6 +2510,10 @@ func (mmWarn *mLoggerMockWarn) Expect(msg any, keyvals ...any) *mLoggerMockWarn mmWarn.defaultExpectation = &LoggerMockWarnExpectation{} } + if mmWarn.defaultExpectation.paramPtrs != nil { + mmWarn.mock.t.Fatalf("LoggerMock.Warn mock is already set by ExpectParams functions") + } + mmWarn.defaultExpectation.params = &LoggerMockWarnParams{msg, keyvals} for _, e := range mmWarn.expectations { if minimock.Equal(e.params, mmWarn.defaultExpectation.params) { @@ -1668,6 +2524,50 @@ func (mmWarn *mLoggerMockWarn) Expect(msg any, keyvals ...any) *mLoggerMockWarn return mmWarn } +// ExpectMsgParam1 sets up expected param msg for Logger.Warn +func (mmWarn *mLoggerMockWarn) ExpectMsgParam1(msg any) *mLoggerMockWarn { + if mmWarn.mock.funcWarn != nil { + mmWarn.mock.t.Fatalf("LoggerMock.Warn mock is already set by Set") + } + + if mmWarn.defaultExpectation == nil { + mmWarn.defaultExpectation = &LoggerMockWarnExpectation{} + } + + if mmWarn.defaultExpectation.params != nil { + mmWarn.mock.t.Fatalf("LoggerMock.Warn mock is already set by Expect") + } + + if mmWarn.defaultExpectation.paramPtrs == nil { + mmWarn.defaultExpectation.paramPtrs = &LoggerMockWarnParamPtrs{} + } + mmWarn.defaultExpectation.paramPtrs.msg = &msg + + return mmWarn +} + +// ExpectKeyvalsParam2 sets up expected param keyvals for Logger.Warn +func (mmWarn *mLoggerMockWarn) ExpectKeyvalsParam2(keyvals ...any) *mLoggerMockWarn { + if mmWarn.mock.funcWarn != nil { + mmWarn.mock.t.Fatalf("LoggerMock.Warn mock is already set by Set") + } + + if mmWarn.defaultExpectation == nil { + mmWarn.defaultExpectation = &LoggerMockWarnExpectation{} + } + + if mmWarn.defaultExpectation.params != nil { + mmWarn.mock.t.Fatalf("LoggerMock.Warn mock is already set by Expect") + } + + if mmWarn.defaultExpectation.paramPtrs == nil { + mmWarn.defaultExpectation.paramPtrs = &LoggerMockWarnParamPtrs{} + } + mmWarn.defaultExpectation.paramPtrs.keyvals = &keyvals + + return mmWarn +} + // Inspect accepts an inspector function that has same arguments as the Logger.Warn func (mmWarn *mLoggerMockWarn) Inspect(f func(msg any, keyvals ...any)) *mLoggerMockWarn { if mmWarn.mock.inspectFuncWarn != nil { @@ -1706,6 +2606,26 @@ func (mmWarn *mLoggerMockWarn) Set(f func(msg any, keyvals ...any)) *LoggerMock return mmWarn.mock } +// Times sets number of times Logger.Warn should be invoked +func (mmWarn *mLoggerMockWarn) Times(n uint64) *mLoggerMockWarn { + if n == 0 { + mmWarn.mock.t.Fatalf("Times of LoggerMock.Warn mock can not be zero") + } + mm_atomic.StoreUint64(&mmWarn.expectedInvocations, n) + return mmWarn +} + +func (mmWarn *mLoggerMockWarn) invocationsDone() bool { + if len(mmWarn.expectations) == 0 && mmWarn.defaultExpectation == nil && mmWarn.mock.funcWarn == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmWarn.mock.afterWarnCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmWarn.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Warn implements contracts.Logger func (mmWarn *LoggerMock) Warn(msg any, keyvals ...any) { mm_atomic.AddUint64(&mmWarn.beforeWarnCounter, 1) @@ -1732,8 +2652,21 @@ func (mmWarn *LoggerMock) Warn(msg any, keyvals ...any) { if mmWarn.WarnMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmWarn.WarnMock.defaultExpectation.Counter, 1) mm_want := mmWarn.WarnMock.defaultExpectation.params + mm_want_ptrs := mmWarn.WarnMock.defaultExpectation.paramPtrs + mm_got := LoggerMockWarnParams{msg, keyvals} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.msg != nil && !minimock.Equal(*mm_want_ptrs.msg, mm_got.msg) { + mmWarn.t.Errorf("LoggerMock.Warn got unexpected parameter msg, want: %#v, got: %#v%s\n", *mm_want_ptrs.msg, mm_got.msg, minimock.Diff(*mm_want_ptrs.msg, mm_got.msg)) + } + + if mm_want_ptrs.keyvals != nil && !minimock.Equal(*mm_want_ptrs.keyvals, mm_got.keyvals) { + mmWarn.t.Errorf("LoggerMock.Warn got unexpected parameter keyvals, want: %#v, got: %#v%s\n", *mm_want_ptrs.keyvals, mm_got.keyvals, minimock.Diff(*mm_want_ptrs.keyvals, mm_got.keyvals)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmWarn.t.Errorf("LoggerMock.Warn got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -1774,21 +2707,18 @@ func (mmWarn *mLoggerMockWarn) Calls() []*LoggerMockWarnParams { // MinimockWarnDone returns true if the count of the Warn invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockWarnDone() bool { + if m.WarnMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.WarnMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.WarnMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterWarnCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcWarn != nil && mm_atomic.LoadUint64(&m.afterWarnCounter) < 1 { - return false - } - return true + return m.WarnMock.invocationsDone() } // MinimockWarnInspect logs each unmet expectation @@ -1799,8 +2729,9 @@ func (m *LoggerMock) MinimockWarnInspect() { } } + afterWarnCounter := mm_atomic.LoadUint64(&m.afterWarnCounter) // if default expectation was set then invocations count should be greater than zero - if m.WarnMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterWarnCounter) < 1 { + if m.WarnMock.defaultExpectation != nil && afterWarnCounter < 1 { if m.WarnMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Warn") } else { @@ -1808,24 +2739,33 @@ func (m *LoggerMock) MinimockWarnInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcWarn != nil && mm_atomic.LoadUint64(&m.afterWarnCounter) < 1 { + if m.funcWarn != nil && afterWarnCounter < 1 { m.t.Error("Expected call to LoggerMock.Warn") } + + if !m.WarnMock.invocationsDone() && afterWarnCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Warn but found %d calls", + mm_atomic.LoadUint64(&m.WarnMock.expectedInvocations), afterWarnCounter) + } } type mLoggerMockWarnf struct { + optional bool mock *LoggerMock defaultExpectation *LoggerMockWarnfExpectation expectations []*LoggerMockWarnfExpectation callArgs []*LoggerMockWarnfParams mutex sync.RWMutex + + expectedInvocations uint64 } // LoggerMockWarnfExpectation specifies expectation struct of the Logger.Warnf type LoggerMockWarnfExpectation struct { - mock *LoggerMock - params *LoggerMockWarnfParams + mock *LoggerMock + params *LoggerMockWarnfParams + paramPtrs *LoggerMockWarnfParamPtrs Counter uint64 } @@ -1836,6 +2776,22 @@ type LoggerMockWarnfParams struct { a []any } +// LoggerMockWarnfParamPtrs contains pointers to parameters of the Logger.Warnf +type LoggerMockWarnfParamPtrs struct { + template *string + a *[]any +} + +// 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 (mmWarnf *mLoggerMockWarnf) Optional() *mLoggerMockWarnf { + mmWarnf.optional = true + return mmWarnf +} + // Expect sets up expected params for Logger.Warnf func (mmWarnf *mLoggerMockWarnf) Expect(template string, a ...any) *mLoggerMockWarnf { if mmWarnf.mock.funcWarnf != nil { @@ -1846,6 +2802,10 @@ func (mmWarnf *mLoggerMockWarnf) Expect(template string, a ...any) *mLoggerMockW mmWarnf.defaultExpectation = &LoggerMockWarnfExpectation{} } + if mmWarnf.defaultExpectation.paramPtrs != nil { + mmWarnf.mock.t.Fatalf("LoggerMock.Warnf mock is already set by ExpectParams functions") + } + mmWarnf.defaultExpectation.params = &LoggerMockWarnfParams{template, a} for _, e := range mmWarnf.expectations { if minimock.Equal(e.params, mmWarnf.defaultExpectation.params) { @@ -1856,6 +2816,50 @@ func (mmWarnf *mLoggerMockWarnf) Expect(template string, a ...any) *mLoggerMockW return mmWarnf } +// ExpectTemplateParam1 sets up expected param template for Logger.Warnf +func (mmWarnf *mLoggerMockWarnf) ExpectTemplateParam1(template string) *mLoggerMockWarnf { + if mmWarnf.mock.funcWarnf != nil { + mmWarnf.mock.t.Fatalf("LoggerMock.Warnf mock is already set by Set") + } + + if mmWarnf.defaultExpectation == nil { + mmWarnf.defaultExpectation = &LoggerMockWarnfExpectation{} + } + + if mmWarnf.defaultExpectation.params != nil { + mmWarnf.mock.t.Fatalf("LoggerMock.Warnf mock is already set by Expect") + } + + if mmWarnf.defaultExpectation.paramPtrs == nil { + mmWarnf.defaultExpectation.paramPtrs = &LoggerMockWarnfParamPtrs{} + } + mmWarnf.defaultExpectation.paramPtrs.template = &template + + return mmWarnf +} + +// ExpectAParam2 sets up expected param a for Logger.Warnf +func (mmWarnf *mLoggerMockWarnf) ExpectAParam2(a ...any) *mLoggerMockWarnf { + if mmWarnf.mock.funcWarnf != nil { + mmWarnf.mock.t.Fatalf("LoggerMock.Warnf mock is already set by Set") + } + + if mmWarnf.defaultExpectation == nil { + mmWarnf.defaultExpectation = &LoggerMockWarnfExpectation{} + } + + if mmWarnf.defaultExpectation.params != nil { + mmWarnf.mock.t.Fatalf("LoggerMock.Warnf mock is already set by Expect") + } + + if mmWarnf.defaultExpectation.paramPtrs == nil { + mmWarnf.defaultExpectation.paramPtrs = &LoggerMockWarnfParamPtrs{} + } + mmWarnf.defaultExpectation.paramPtrs.a = &a + + return mmWarnf +} + // Inspect accepts an inspector function that has same arguments as the Logger.Warnf func (mmWarnf *mLoggerMockWarnf) Inspect(f func(template string, a ...any)) *mLoggerMockWarnf { if mmWarnf.mock.inspectFuncWarnf != nil { @@ -1894,6 +2898,26 @@ func (mmWarnf *mLoggerMockWarnf) Set(f func(template string, a ...any)) *LoggerM return mmWarnf.mock } +// Times sets number of times Logger.Warnf should be invoked +func (mmWarnf *mLoggerMockWarnf) Times(n uint64) *mLoggerMockWarnf { + if n == 0 { + mmWarnf.mock.t.Fatalf("Times of LoggerMock.Warnf mock can not be zero") + } + mm_atomic.StoreUint64(&mmWarnf.expectedInvocations, n) + return mmWarnf +} + +func (mmWarnf *mLoggerMockWarnf) invocationsDone() bool { + if len(mmWarnf.expectations) == 0 && mmWarnf.defaultExpectation == nil && mmWarnf.mock.funcWarnf == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmWarnf.mock.afterWarnfCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmWarnf.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Warnf implements contracts.Logger func (mmWarnf *LoggerMock) Warnf(template string, a ...any) { mm_atomic.AddUint64(&mmWarnf.beforeWarnfCounter, 1) @@ -1920,8 +2944,21 @@ func (mmWarnf *LoggerMock) Warnf(template string, a ...any) { if mmWarnf.WarnfMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmWarnf.WarnfMock.defaultExpectation.Counter, 1) mm_want := mmWarnf.WarnfMock.defaultExpectation.params + mm_want_ptrs := mmWarnf.WarnfMock.defaultExpectation.paramPtrs + mm_got := LoggerMockWarnfParams{template, a} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.template != nil && !minimock.Equal(*mm_want_ptrs.template, mm_got.template) { + mmWarnf.t.Errorf("LoggerMock.Warnf got unexpected parameter template, want: %#v, got: %#v%s\n", *mm_want_ptrs.template, mm_got.template, minimock.Diff(*mm_want_ptrs.template, mm_got.template)) + } + + if mm_want_ptrs.a != nil && !minimock.Equal(*mm_want_ptrs.a, mm_got.a) { + mmWarnf.t.Errorf("LoggerMock.Warnf got unexpected parameter a, want: %#v, got: %#v%s\n", *mm_want_ptrs.a, mm_got.a, minimock.Diff(*mm_want_ptrs.a, mm_got.a)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmWarnf.t.Errorf("LoggerMock.Warnf got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -1962,21 +2999,18 @@ func (mmWarnf *mLoggerMockWarnf) Calls() []*LoggerMockWarnfParams { // MinimockWarnfDone returns true if the count of the Warnf invocations corresponds // the number of defined expectations func (m *LoggerMock) MinimockWarnfDone() bool { + if m.WarnfMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.WarnfMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.WarnfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterWarnfCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcWarnf != nil && mm_atomic.LoadUint64(&m.afterWarnfCounter) < 1 { - return false - } - return true + return m.WarnfMock.invocationsDone() } // MinimockWarnfInspect logs each unmet expectation @@ -1987,8 +3021,9 @@ func (m *LoggerMock) MinimockWarnfInspect() { } } + afterWarnfCounter := mm_atomic.LoadUint64(&m.afterWarnfCounter) // if default expectation was set then invocations count should be greater than zero - if m.WarnfMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterWarnfCounter) < 1 { + if m.WarnfMock.defaultExpectation != nil && afterWarnfCounter < 1 { if m.WarnfMock.defaultExpectation.params == nil { m.t.Error("Expected call to LoggerMock.Warnf") } else { @@ -1996,9 +3031,14 @@ func (m *LoggerMock) MinimockWarnfInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcWarnf != nil && mm_atomic.LoadUint64(&m.afterWarnfCounter) < 1 { + if m.funcWarnf != nil && afterWarnfCounter < 1 { m.t.Error("Expected call to LoggerMock.Warnf") } + + if !m.WarnfMock.invocationsDone() && afterWarnfCounter > 0 { + m.t.Errorf("Expected %d calls to LoggerMock.Warnf but found %d calls", + mm_atomic.LoadUint64(&m.WarnfMock.expectedInvocations), afterWarnfCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times @@ -2024,7 +3064,6 @@ func (m *LoggerMock) MinimockFinish() { m.MinimockWarnInspect() m.MinimockWarnfInspect() - m.t.FailNow() } }) } diff --git a/testing/mocks/replacer_factory_mock.go b/testing/mocks/replacer_factory_mock.go index 40249625..fedd6c77 100644 --- a/testing/mocks/replacer_factory_mock.go +++ b/testing/mocks/replacer_factory_mock.go @@ -1,4 +1,4 @@ -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. +// Code generated by http://github.com/gojuno/minimock (v3.3.12). DO NOT EDIT. package mocks @@ -43,20 +43,24 @@ func NewReplacerFactoryMock(t minimock.Tester) *ReplacerFactoryMock { } type mReplacerFactoryMockMake struct { + optional bool mock *ReplacerFactoryMock defaultExpectation *ReplacerFactoryMockMakeExpectation expectations []*ReplacerFactoryMockMakeExpectation callArgs []*ReplacerFactoryMockMakeParams mutex sync.RWMutex + + expectedInvocations uint64 } // ReplacerFactoryMockMakeExpectation specifies expectation struct of the ReplacerFactory.Make type ReplacerFactoryMockMakeExpectation struct { - mock *ReplacerFactoryMock - params *ReplacerFactoryMockMakeParams - results *ReplacerFactoryMockMakeResults - Counter uint64 + mock *ReplacerFactoryMock + params *ReplacerFactoryMockMakeParams + paramPtrs *ReplacerFactoryMockMakeParamPtrs + results *ReplacerFactoryMockMakeResults + Counter uint64 } // ReplacerFactoryMockMakeParams contains parameters of the ReplacerFactory.Make @@ -64,6 +68,11 @@ type ReplacerFactoryMockMakeParams struct { requestURL *url.URL } +// ReplacerFactoryMockMakeParamPtrs contains pointers to parameters of the ReplacerFactory.Make +type ReplacerFactoryMockMakeParamPtrs struct { + requestURL **url.URL +} + // ReplacerFactoryMockMakeResults contains results of the ReplacerFactory.Make type ReplacerFactoryMockMakeResults struct { rp1 *mm_urlreplacer.Replacer @@ -71,6 +80,16 @@ type ReplacerFactoryMockMakeResults struct { err error } +// 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 (mmMake *mReplacerFactoryMockMake) Optional() *mReplacerFactoryMockMake { + mmMake.optional = true + return mmMake +} + // Expect sets up expected params for ReplacerFactory.Make func (mmMake *mReplacerFactoryMockMake) Expect(requestURL *url.URL) *mReplacerFactoryMockMake { if mmMake.mock.funcMake != nil { @@ -81,6 +100,10 @@ func (mmMake *mReplacerFactoryMockMake) Expect(requestURL *url.URL) *mReplacerFa mmMake.defaultExpectation = &ReplacerFactoryMockMakeExpectation{} } + if mmMake.defaultExpectation.paramPtrs != nil { + mmMake.mock.t.Fatalf("ReplacerFactoryMock.Make mock is already set by ExpectParams functions") + } + mmMake.defaultExpectation.params = &ReplacerFactoryMockMakeParams{requestURL} for _, e := range mmMake.expectations { if minimock.Equal(e.params, mmMake.defaultExpectation.params) { @@ -91,6 +114,28 @@ func (mmMake *mReplacerFactoryMockMake) Expect(requestURL *url.URL) *mReplacerFa return mmMake } +// ExpectRequestURLParam1 sets up expected param requestURL for ReplacerFactory.Make +func (mmMake *mReplacerFactoryMockMake) ExpectRequestURLParam1(requestURL *url.URL) *mReplacerFactoryMockMake { + if mmMake.mock.funcMake != nil { + mmMake.mock.t.Fatalf("ReplacerFactoryMock.Make mock is already set by Set") + } + + if mmMake.defaultExpectation == nil { + mmMake.defaultExpectation = &ReplacerFactoryMockMakeExpectation{} + } + + if mmMake.defaultExpectation.params != nil { + mmMake.mock.t.Fatalf("ReplacerFactoryMock.Make mock is already set by Expect") + } + + if mmMake.defaultExpectation.paramPtrs == nil { + mmMake.defaultExpectation.paramPtrs = &ReplacerFactoryMockMakeParamPtrs{} + } + mmMake.defaultExpectation.paramPtrs.requestURL = &requestURL + + return mmMake +} + // Inspect accepts an inspector function that has same arguments as the ReplacerFactory.Make func (mmMake *mReplacerFactoryMockMake) Inspect(f func(requestURL *url.URL)) *mReplacerFactoryMockMake { if mmMake.mock.inspectFuncMake != nil { @@ -150,6 +195,26 @@ func (e *ReplacerFactoryMockMakeExpectation) Then(rp1 *mm_urlreplacer.Replacer, return e.mock } +// Times sets number of times ReplacerFactory.Make should be invoked +func (mmMake *mReplacerFactoryMockMake) Times(n uint64) *mReplacerFactoryMockMake { + if n == 0 { + mmMake.mock.t.Fatalf("Times of ReplacerFactoryMock.Make mock can not be zero") + } + mm_atomic.StoreUint64(&mmMake.expectedInvocations, n) + return mmMake +} + +func (mmMake *mReplacerFactoryMockMake) invocationsDone() bool { + if len(mmMake.expectations) == 0 && mmMake.defaultExpectation == nil && mmMake.mock.funcMake == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmMake.mock.afterMakeCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmMake.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Make implements urlreplacer.ReplacerFactory func (mmMake *ReplacerFactoryMock) Make(requestURL *url.URL) (rp1 *mm_urlreplacer.Replacer, rp2 *mm_urlreplacer.Replacer, err error) { mm_atomic.AddUint64(&mmMake.beforeMakeCounter, 1) @@ -176,8 +241,17 @@ func (mmMake *ReplacerFactoryMock) Make(requestURL *url.URL) (rp1 *mm_urlreplace if mmMake.MakeMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmMake.MakeMock.defaultExpectation.Counter, 1) mm_want := mmMake.MakeMock.defaultExpectation.params + mm_want_ptrs := mmMake.MakeMock.defaultExpectation.paramPtrs + mm_got := ReplacerFactoryMockMakeParams{requestURL} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.requestURL != nil && !minimock.Equal(*mm_want_ptrs.requestURL, mm_got.requestURL) { + mmMake.t.Errorf("ReplacerFactoryMock.Make got unexpected parameter requestURL, want: %#v, got: %#v%s\n", *mm_want_ptrs.requestURL, mm_got.requestURL, minimock.Diff(*mm_want_ptrs.requestURL, mm_got.requestURL)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmMake.t.Errorf("ReplacerFactoryMock.Make got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -220,21 +294,18 @@ func (mmMake *mReplacerFactoryMockMake) Calls() []*ReplacerFactoryMockMakeParams // MinimockMakeDone returns true if the count of the Make invocations corresponds // the number of defined expectations func (m *ReplacerFactoryMock) MinimockMakeDone() bool { + if m.MakeMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.MakeMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.MakeMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterMakeCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcMake != nil && mm_atomic.LoadUint64(&m.afterMakeCounter) < 1 { - return false - } - return true + return m.MakeMock.invocationsDone() } // MinimockMakeInspect logs each unmet expectation @@ -245,8 +316,9 @@ func (m *ReplacerFactoryMock) MinimockMakeInspect() { } } + afterMakeCounter := mm_atomic.LoadUint64(&m.afterMakeCounter) // if default expectation was set then invocations count should be greater than zero - if m.MakeMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterMakeCounter) < 1 { + if m.MakeMock.defaultExpectation != nil && afterMakeCounter < 1 { if m.MakeMock.defaultExpectation.params == nil { m.t.Error("Expected call to ReplacerFactoryMock.Make") } else { @@ -254,9 +326,14 @@ func (m *ReplacerFactoryMock) MinimockMakeInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcMake != nil && mm_atomic.LoadUint64(&m.afterMakeCounter) < 1 { + if m.funcMake != nil && afterMakeCounter < 1 { m.t.Error("Expected call to ReplacerFactoryMock.Make") } + + if !m.MakeMock.invocationsDone() && afterMakeCounter > 0 { + m.t.Errorf("Expected %d calls to ReplacerFactoryMock.Make but found %d calls", + mm_atomic.LoadUint64(&m.MakeMock.expectedInvocations), afterMakeCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times @@ -264,7 +341,6 @@ func (m *ReplacerFactoryMock) MinimockFinish() { m.finishOnce.Do(func() { if !m.minimockDone() { m.MinimockMakeInspect() - m.t.FailNow() } }) } diff --git a/testing/mocks/writer_mock.go b/testing/mocks/writer_mock.go index 97a10adb..e813ea07 100644 --- a/testing/mocks/writer_mock.go +++ b/testing/mocks/writer_mock.go @@ -1,4 +1,4 @@ -// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT. +// Code generated by http://github.com/gojuno/minimock (v3.3.12). DO NOT EDIT. package mocks @@ -41,20 +41,24 @@ func NewWriterMock(t minimock.Tester) *WriterMock { } type mWriterMockWrite struct { + optional bool mock *WriterMock defaultExpectation *WriterMockWriteExpectation expectations []*WriterMockWriteExpectation callArgs []*WriterMockWriteParams mutex sync.RWMutex + + expectedInvocations uint64 } // WriterMockWriteExpectation specifies expectation struct of the Writer.Write type WriterMockWriteExpectation struct { - mock *WriterMock - params *WriterMockWriteParams - results *WriterMockWriteResults - Counter uint64 + mock *WriterMock + params *WriterMockWriteParams + paramPtrs *WriterMockWriteParamPtrs + results *WriterMockWriteResults + Counter uint64 } // WriterMockWriteParams contains parameters of the Writer.Write @@ -62,12 +66,27 @@ type WriterMockWriteParams struct { p []byte } +// WriterMockWriteParamPtrs contains pointers to parameters of the Writer.Write +type WriterMockWriteParamPtrs struct { + p *[]byte +} + // WriterMockWriteResults contains results of the Writer.Write type WriterMockWriteResults struct { n int err error } +// 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 (mmWrite *mWriterMockWrite) Optional() *mWriterMockWrite { + mmWrite.optional = true + return mmWrite +} + // Expect sets up expected params for Writer.Write func (mmWrite *mWriterMockWrite) Expect(p []byte) *mWriterMockWrite { if mmWrite.mock.funcWrite != nil { @@ -78,6 +97,10 @@ func (mmWrite *mWriterMockWrite) Expect(p []byte) *mWriterMockWrite { mmWrite.defaultExpectation = &WriterMockWriteExpectation{} } + if mmWrite.defaultExpectation.paramPtrs != nil { + mmWrite.mock.t.Fatalf("WriterMock.Write mock is already set by ExpectParams functions") + } + mmWrite.defaultExpectation.params = &WriterMockWriteParams{p} for _, e := range mmWrite.expectations { if minimock.Equal(e.params, mmWrite.defaultExpectation.params) { @@ -88,6 +111,28 @@ func (mmWrite *mWriterMockWrite) Expect(p []byte) *mWriterMockWrite { return mmWrite } +// ExpectPParam1 sets up expected param p for Writer.Write +func (mmWrite *mWriterMockWrite) ExpectPParam1(p []byte) *mWriterMockWrite { + if mmWrite.mock.funcWrite != nil { + mmWrite.mock.t.Fatalf("WriterMock.Write mock is already set by Set") + } + + if mmWrite.defaultExpectation == nil { + mmWrite.defaultExpectation = &WriterMockWriteExpectation{} + } + + if mmWrite.defaultExpectation.params != nil { + mmWrite.mock.t.Fatalf("WriterMock.Write mock is already set by Expect") + } + + if mmWrite.defaultExpectation.paramPtrs == nil { + mmWrite.defaultExpectation.paramPtrs = &WriterMockWriteParamPtrs{} + } + mmWrite.defaultExpectation.paramPtrs.p = &p + + return mmWrite +} + // Inspect accepts an inspector function that has same arguments as the Writer.Write func (mmWrite *mWriterMockWrite) Inspect(f func(p []byte)) *mWriterMockWrite { if mmWrite.mock.inspectFuncWrite != nil { @@ -147,6 +192,26 @@ func (e *WriterMockWriteExpectation) Then(n int, err error) *WriterMock { return e.mock } +// Times sets number of times Writer.Write should be invoked +func (mmWrite *mWriterMockWrite) Times(n uint64) *mWriterMockWrite { + if n == 0 { + mmWrite.mock.t.Fatalf("Times of WriterMock.Write mock can not be zero") + } + mm_atomic.StoreUint64(&mmWrite.expectedInvocations, n) + return mmWrite +} + +func (mmWrite *mWriterMockWrite) invocationsDone() bool { + if len(mmWrite.expectations) == 0 && mmWrite.defaultExpectation == nil && mmWrite.mock.funcWrite == nil { + return true + } + + totalInvocations := mm_atomic.LoadUint64(&mmWrite.mock.afterWriteCounter) + expectedInvocations := mm_atomic.LoadUint64(&mmWrite.expectedInvocations) + + return totalInvocations > 0 && (expectedInvocations == 0 || expectedInvocations == totalInvocations) +} + // Write implements io.Writer func (mmWrite *WriterMock) Write(p []byte) (n int, err error) { mm_atomic.AddUint64(&mmWrite.beforeWriteCounter, 1) @@ -173,8 +238,17 @@ func (mmWrite *WriterMock) Write(p []byte) (n int, err error) { if mmWrite.WriteMock.defaultExpectation != nil { mm_atomic.AddUint64(&mmWrite.WriteMock.defaultExpectation.Counter, 1) mm_want := mmWrite.WriteMock.defaultExpectation.params + mm_want_ptrs := mmWrite.WriteMock.defaultExpectation.paramPtrs + mm_got := WriterMockWriteParams{p} - if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { + + if mm_want_ptrs != nil { + + if mm_want_ptrs.p != nil && !minimock.Equal(*mm_want_ptrs.p, mm_got.p) { + mmWrite.t.Errorf("WriterMock.Write 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)) + } + + } else if mm_want != nil && !minimock.Equal(*mm_want, mm_got) { mmWrite.t.Errorf("WriterMock.Write got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got)) } @@ -217,21 +291,18 @@ func (mmWrite *mWriterMockWrite) Calls() []*WriterMockWriteParams { // MinimockWriteDone returns true if the count of the Write invocations corresponds // the number of defined expectations func (m *WriterMock) MinimockWriteDone() bool { + if m.WriteMock.optional { + // Optional methods provide '0 or more' call count restriction. + return true + } + for _, e := range m.WriteMock.expectations { if mm_atomic.LoadUint64(&e.Counter) < 1 { return false } } - // if default expectation was set then invocations count should be greater than zero - if m.WriteMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterWriteCounter) < 1 { - return false - } - // if func was set then invocations count should be greater than zero - if m.funcWrite != nil && mm_atomic.LoadUint64(&m.afterWriteCounter) < 1 { - return false - } - return true + return m.WriteMock.invocationsDone() } // MinimockWriteInspect logs each unmet expectation @@ -242,8 +313,9 @@ func (m *WriterMock) MinimockWriteInspect() { } } + afterWriteCounter := mm_atomic.LoadUint64(&m.afterWriteCounter) // if default expectation was set then invocations count should be greater than zero - if m.WriteMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterWriteCounter) < 1 { + if m.WriteMock.defaultExpectation != nil && afterWriteCounter < 1 { if m.WriteMock.defaultExpectation.params == nil { m.t.Error("Expected call to WriterMock.Write") } else { @@ -251,9 +323,14 @@ func (m *WriterMock) MinimockWriteInspect() { } } // if func was set then invocations count should be greater than zero - if m.funcWrite != nil && mm_atomic.LoadUint64(&m.afterWriteCounter) < 1 { + if m.funcWrite != nil && afterWriteCounter < 1 { m.t.Error("Expected call to WriterMock.Write") } + + if !m.WriteMock.invocationsDone() && afterWriteCounter > 0 { + m.t.Errorf("Expected %d calls to WriterMock.Write but found %d calls", + mm_atomic.LoadUint64(&m.WriteMock.expectedInvocations), afterWriteCounter) + } } // MinimockFinish checks that all mocked methods have been called the expected number of times @@ -261,7 +338,6 @@ func (m *WriterMock) MinimockFinish() { m.finishOnce.Do(func() { if !m.minimockDone() { m.MinimockWriteInspect() - m.t.FailNow() } }) }