Skip to content

Instantly share code, notes, and snippets.

@rekby
Last active May 1, 2019 22:58
Show Gist options
  • Save rekby/3e95b03c8220cd5da895e8ccaa66bd44 to your computer and use it in GitHub Desktop.
Save rekby/3e95b03c8220cd5da895e8ccaa66bd44 to your computer and use it in GitHub Desktop.
generated code for minimock return without expected bug https://github.com/gojuno/minimock/issues/31
package asd
// DO NOT EDIT!
// The code below was generated with http://github.com/gojuno/minimock (dev)
//go:generate minimock -i tmp.Int -o ./int_mock_test.go
import (
"sync/atomic"
"time"
"github.com/gojuno/minimock"
)
// IntMock implements Int
type IntMock struct {
t minimock.Tester
funcM func()
afterMCounter uint64
beforeMCounter uint64
MMock mIntMockM
funcMA func(i int)
afterMACounter uint64
beforeMACounter uint64
MAMock mIntMockMA
}
// NewIntMock returns a mock for Int
func NewIntMock(t minimock.Tester) *IntMock {
m := &IntMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.MMock = mIntMockM{mock: m}
m.MAMock = mIntMockMA{mock: m}
return m
}
type mIntMockM struct {
mock *IntMock
defaultExpectation *IntMockMExpectation
expectations []*IntMockMExpectation
}
// IntMockMExpectation specifies expectation struct of the Int.M
type IntMockMExpectation struct {
mock *IntMock
Counter uint64
}
// Expect sets up expected params for Int.M
func (m *mIntMockM) Expect() *mIntMockM {
if m.mock.funcM != nil {
m.mock.t.Fatalf("IntMock.M mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &IntMockMExpectation{}
}
return m
}
// Return sets up results that will be returned by Int.M
func (m *mIntMockM) Return() *IntMock {
if m.mock.funcM != nil {
m.mock.t.Fatalf("IntMock.M mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &IntMockMExpectation{mock: m.mock}
}
return m.mock
}
//Set uses given function f to mock the Int.M method
func (m *mIntMockM) Set(f func()) *IntMock {
if m.defaultExpectation != nil {
m.mock.t.Fatalf("Default expectation is already set for the Int.M method")
}
if len(m.expectations) > 0 {
m.mock.t.Fatalf("Some expectations are already set for the Int.M method")
}
m.mock.funcM = f
return m.mock
}
// M implements Int
func (m *IntMock) M() {
atomic.AddUint64(&m.beforeMCounter, 1)
defer atomic.AddUint64(&m.afterMCounter, 1)
if m.MMock.defaultExpectation != nil {
atomic.AddUint64(&m.MMock.defaultExpectation.Counter, 1)
return
}
if m.funcM != nil {
m.funcM()
return
}
m.t.Fatalf("Unexpected call to IntMock.M.")
}
// MAfterCounter returns a count of finished IntMock.M invocations
func (m *IntMock) MAfterCounter() uint64 {
return atomic.LoadUint64(&m.afterMCounter)
}
// MBeforeCounter returns a count of IntMock.M invocations
func (m *IntMock) MBeforeCounter() uint64 {
return atomic.LoadUint64(&m.beforeMCounter)
}
// MinimockMDone returns true if the count of the M invocations corresponds
// the number of defined expectations
func (m *IntMock) MinimockMDone() bool {
for _, e := range m.MMock.expectations {
if atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
// if default expectation was set then invocations count should be greater than zero
if m.MMock.defaultExpectation != nil && atomic.LoadUint64(&m.afterMCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcM != nil && atomic.LoadUint64(&m.afterMCounter) < 1 {
return false
}
return true
}
// MinimockMInspect logs each unmet expectation
func (m *IntMock) MinimockMInspect() {
for _, e := range m.MMock.expectations {
if atomic.LoadUint64(&e.Counter) < 1 {
m.t.Error("Expected call to IntMock.M")
}
}
// if default expectation was set then invocations count should be greater than zero
if m.MMock.defaultExpectation != nil && atomic.LoadUint64(&m.afterMCounter) < 1 {
m.t.Error("Expected call to IntMock.M")
}
// if func was set then invocations count should be greater than zero
if m.funcM != nil && atomic.LoadUint64(&m.afterMCounter) < 1 {
m.t.Error("Expected call to IntMock.M")
}
}
type mIntMockMA struct {
mock *IntMock
defaultExpectation *IntMockMAExpectation
expectations []*IntMockMAExpectation
}
// IntMockMAExpectation specifies expectation struct of the Int.MA
type IntMockMAExpectation struct {
mock *IntMock
params *IntMockMAParams
Counter uint64
}
// IntMockMAParams contains parameters of the Int.MA
type IntMockMAParams struct {
i int
}
// Expect sets up expected params for Int.MA
func (m *mIntMockMA) Expect(i int) *mIntMockMA {
if m.mock.funcMA != nil {
m.mock.t.Fatalf("IntMock.MA mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &IntMockMAExpectation{}
}
m.defaultExpectation.params = &IntMockMAParams{i}
for _, e := range m.expectations {
if minimock.Equal(e.params, m.defaultExpectation.params) {
m.mock.t.Fatalf("Expectation set by When has same params: %#v", *m.defaultExpectation.params)
}
}
return m
}
// Return sets up results that will be returned by Int.MA
func (m *mIntMockMA) Return() *IntMock {
if m.mock.funcMA != nil {
m.mock.t.Fatalf("IntMock.MA mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &IntMockMAExpectation{mock: m.mock}
}
return m.mock
}
//Set uses given function f to mock the Int.MA method
func (m *mIntMockMA) Set(f func(i int)) *IntMock {
if m.defaultExpectation != nil {
m.mock.t.Fatalf("Default expectation is already set for the Int.MA method")
}
if len(m.expectations) > 0 {
m.mock.t.Fatalf("Some expectations are already set for the Int.MA method")
}
m.mock.funcMA = f
return m.mock
}
// MA implements Int
func (m *IntMock) MA(i int) {
atomic.AddUint64(&m.beforeMACounter, 1)
defer atomic.AddUint64(&m.afterMACounter, 1)
for _, e := range m.MAMock.expectations {
if minimock.Equal(*e.params, IntMockMAParams{i}) {
atomic.AddUint64(&e.Counter, 1)
return
}
}
if m.MAMock.defaultExpectation != nil {
atomic.AddUint64(&m.MAMock.defaultExpectation.Counter, 1)
want := m.MAMock.defaultExpectation.params
got := IntMockMAParams{i}
if want != nil && !minimock.Equal(*want, got) {
m.t.Errorf("IntMock.MA got unexpected parameters, want: %#v, got: %#v%s\n", *want, got, minimock.Diff(*want, got))
}
return
}
if m.funcMA != nil {
m.funcMA(i)
return
}
m.t.Fatalf("Unexpected call to IntMock.MA. %v", i)
}
// MAAfterCounter returns a count of finished IntMock.MA invocations
func (m *IntMock) MAAfterCounter() uint64 {
return atomic.LoadUint64(&m.afterMACounter)
}
// MABeforeCounter returns a count of IntMock.MA invocations
func (m *IntMock) MABeforeCounter() uint64 {
return atomic.LoadUint64(&m.beforeMACounter)
}
// MinimockMADone returns true if the count of the MA invocations corresponds
// the number of defined expectations
func (m *IntMock) MinimockMADone() bool {
for _, e := range m.MAMock.expectations {
if atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
// if default expectation was set then invocations count should be greater than zero
if m.MAMock.defaultExpectation != nil && atomic.LoadUint64(&m.afterMACounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcMA != nil && atomic.LoadUint64(&m.afterMACounter) < 1 {
return false
}
return true
}
// MinimockMAInspect logs each unmet expectation
func (m *IntMock) MinimockMAInspect() {
for _, e := range m.MAMock.expectations {
if atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to IntMock.MA with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.MAMock.defaultExpectation != nil && atomic.LoadUint64(&m.afterMACounter) < 1 {
m.t.Errorf("Expected call to IntMock.MA with params: %#v", *m.MAMock.defaultExpectation.params)
}
// if func was set then invocations count should be greater than zero
if m.funcMA != nil && atomic.LoadUint64(&m.afterMACounter) < 1 {
m.t.Error("Expected call to IntMock.MA")
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *IntMock) MinimockFinish() {
if !m.minimockDone() {
m.MinimockMInspect()
m.MinimockMAInspect()
m.t.FailNow()
}
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *IntMock) MinimockWait(timeout time.Duration) {
timeoutCh := time.After(timeout)
for {
if m.minimockDone() {
return
}
select {
case <-timeoutCh:
m.MinimockFinish()
return
case <-time.After(10 * time.Millisecond):
}
}
}
func (m *IntMock) minimockDone() bool {
done := true
return done &&
m.MinimockMDone() &&
m.MinimockMADone()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment