Skip to content

Instantly share code, notes, and snippets.

@rekby
Last active April 27, 2019 15:39
Show Gist options
  • Save rekby/edc062095eca7ef7dc99be17d6717466 to your computer and use it in GitHub Desktop.
Save rekby/edc062095eca7ef7dc99be17d6717466 to your computer and use it in GitHub Desktop.
minimock example
package main
// DO NOT EDIT!
// The code below was generated with http://github.com/gojuno/minimock (dev)
//go:generate minimock -i tmp.Summer -o ./summer_mock_test.go
import (
"sync/atomic"
"time"
"context"
"github.com/gojuno/minimock"
)
// SummerMock implements Summer
type SummerMock struct {
t minimock.Tester
funcSum func(ctx context.Context, a int, b int) (i1 int)
afterSumCounter uint64
beforeSumCounter uint64
SumMock mSummerMockSum
}
// NewSummerMock returns a mock for Summer
func NewSummerMock(t minimock.Tester) *SummerMock {
m := &SummerMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.SumMock = mSummerMockSum{mock: m}
return m
}
type mSummerMockSum struct {
mock *SummerMock
defaultExpectation *SummerMockSumExpectation
expectations []*SummerMockSumExpectation
globalAutoChecks SummerMockSumComparer
}
func (m *mSummerMockSum) CtxIgnore() *mSummerMockSum {
m.globalAutoChecks.ctx = func(_, _ context.Context) bool {
return true
}
return m
}
func (m *mSummerMockSum) CtxNotNil() *mSummerMockSum {
m.globalAutoChecks.ctx = func(_, ctx context.Context) bool {
return ctx != nil
}
return m
}
func (m *mSummerMockSum) CtxSetFunction(check SummerMockSumCtxEqual) *mSummerMockSum {
m.globalAutoChecks.ctx = check
return m
}
func (m *mSummerMockSum) AIgnore() *mSummerMockSum {
m.globalAutoChecks.a = func(_, _ int) bool {
return true
}
return m
}
func (m *mSummerMockSum) ASetFunction(check SummerMockSumAEqual) *mSummerMockSum {
m.globalAutoChecks.a = check
return m
}
func (m *mSummerMockSum) BIgnore() *mSummerMockSum {
m.globalAutoChecks.b = func(_, _ int) bool {
return true
}
return m
}
func (m *mSummerMockSum) BSetFunction(check SummerMockSumBEqual) *mSummerMockSum {
m.globalAutoChecks.b = check
return m
}
// SummerMockSumExpectation specifies expectation struct of the Summer.Sum
type SummerMockSumExpectation struct {
mock *SummerMock
params *SummerMockSumParams
results *SummerMockSumResults
autocheckParams SummerMockSumComparer
Counter uint64
}
func (e *SummerMockSumExpectation) CtxIgnore() *SummerMockSumExpectation {
e.autocheckParams.ctx = func(_, _ context.Context) bool {
return true
}
return e
}
func (e *SummerMockSumExpectation) CtxNotNil() *SummerMockSumExpectation {
e.autocheckParams.ctx = func(_, ctx context.Context) bool {
return ctx != nil
}
return e
}
func (e *SummerMockSumExpectation) CtxSetFunc(f SummerMockSumCtxEqual) *SummerMockSumExpectation {
e.autocheckParams.ctx = f
return e
}
func (e *SummerMockSumExpectation) AIgnore() *SummerMockSumExpectation {
e.autocheckParams.a = func(_, _ int) bool {
return true
}
return e
}
func (e *SummerMockSumExpectation) ASetFunction(check SummerMockSumAEqual) *SummerMockSumExpectation {
e.autocheckParams.a = check
return e
}
func (e *SummerMockSumExpectation) BIgnore() *SummerMockSumExpectation {
e.autocheckParams.b = func(_, _ int) bool {
return true
}
return e
}
func (e *SummerMockSumExpectation) BSetFunction(check SummerMockSumBEqual) *SummerMockSumExpectation {
e.autocheckParams.b = check
return e
}
// SummerMockSumParams contains parameters of the Summer.Sum
type SummerMockSumParams struct {
ctx context.Context
a int
b int
}
type SummerMockSumCtxEqual func(expected, got context.Context) bool
type SummerMockSumAEqual func(expected, got int) bool
type SummerMockSumBEqual func(expected, got int) bool
type SummerMockSumComparer struct {
ctx SummerMockSumCtxEqual
a SummerMockSumAEqual
b SummerMockSumBEqual
}
func (c *SummerMockSumComparer) Equals(want, got *SummerMockSumParams) bool {
var equalCtx bool
if c.ctx == nil {
equalCtx = minimock.Equal(want.ctx, got.ctx)
} else {
equalCtx = c.ctx(want.ctx, got.ctx)
}
var equalA bool
if c.a == nil {
equalA = minimock.Equal(want.a, got.a)
} else {
equalA = c.a(want.a, got.a)
}
var equalB bool
if c.b == nil {
equalB = minimock.Equal(want.b, got.b)
} else {
equalB = c.b(want.b, got.b)
}
return equalCtx && equalA && equalB
}
// SummerMockSumResults contains results of the Summer.Sum
type SummerMockSumResults struct {
i1 int
}
// Expect sets up expected params for Summer.Sum
func (m *mSummerMockSum) Expect(ctx context.Context, a int, b int) *mSummerMockSum {
if m.mock.funcSum != nil {
m.mock.t.Fatalf("SummerMock.Sum mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &SummerMockSumExpectation{}
}
m.defaultExpectation.params = &SummerMockSumParams{ctx, a, b}
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 Summer.Sum
func (m *mSummerMockSum) Return(i1 int) *SummerMock {
if m.mock.funcSum != nil {
m.mock.t.Fatalf("SummerMock.Sum mock is already set by Set")
}
if m.defaultExpectation == nil {
m.defaultExpectation = &SummerMockSumExpectation{mock: m.mock}
}
m.defaultExpectation.results = &SummerMockSumResults{i1}
return m.mock
}
//Set uses given function f to mock the Summer.Sum method
func (m *mSummerMockSum) Set(f func(ctx context.Context, a int, b int) (i1 int)) *SummerMock {
if m.defaultExpectation != nil {
m.mock.t.Fatalf("Default expectation is already set for the Summer.Sum method")
}
if len(m.expectations) > 0 {
m.mock.t.Fatalf("Some expectations are already set for the Summer.Sum method")
}
m.mock.funcSum = f
return m.mock
}
// When sets expectation for the Summer.Sum which will trigger the result defined by the following
// Then helper
func (m *mSummerMockSum) When(ctx context.Context, a int, b int) *SummerMockSumExpectation {
if m.mock.funcSum != nil {
m.mock.t.Fatalf("SummerMock.Sum mock is already set by Set")
}
expectation := &SummerMockSumExpectation{
mock: m.mock,
params: &SummerMockSumParams{ctx, a, b},
autocheckParams: SummerMockSumComparer{
ctx: func(expected, got context.Context) bool {
if m.globalAutoChecks.ctx != nil {
return m.globalAutoChecks.ctx(expected, got)
}
return minimock.Equal(expected, got)
},
a: func(expected, got int) bool {
if m.globalAutoChecks.a != nil {
return m.globalAutoChecks.a(expected, got)
}
return minimock.Equal(expected, got)
},
b: func(expected, got int) bool {
if m.globalAutoChecks.b != nil {
return m.globalAutoChecks.b(expected, got)
}
return minimock.Equal(expected, got)
},
},
}
m.expectations = append(m.expectations, expectation)
return expectation
}
// Then sets up Summer.Sum return parameters for the expectation previously defined by the When method
func (e *SummerMockSumExpectation) Then(i1 int) *SummerMock {
e.results = &SummerMockSumResults{i1}
return e.mock
}
// Sum implements Summer
func (m *SummerMock) Sum(ctx context.Context, a int, b int) (i1 int) {
atomic.AddUint64(&m.beforeSumCounter, 1)
defer atomic.AddUint64(&m.afterSumCounter, 1)
got := SummerMockSumParams{ctx, a, b}
for _, e := range m.SumMock.expectations {
if e.autocheckParams.Equals(e.params, &got) {
atomic.AddUint64(&e.Counter, 1)
return e.results.i1
}
}
if m.SumMock.defaultExpectation != nil {
atomic.AddUint64(&m.SumMock.defaultExpectation.Counter, 1)
want := m.SumMock.defaultExpectation.params
if want != nil {
if !m.SumMock.globalAutoChecks.ctx(want.ctx, got.ctx) {
m.t.Errorf("SummerMock.Sum got unexpected parameter 'ctx', want: %#v, got: %#v%s\n", want.ctx, got.ctx, minimock.Diff(want.ctx, got.ctx))
}
if !m.SumMock.globalAutoChecks.a(want.a, got.a) {
m.t.Errorf("SummerMock.Sum got unexpected parameter 'a', want: %#v, got: %#v%s\n", want.a, got.a, minimock.Diff(want.a, got.a))
}
if !m.SumMock.globalAutoChecks.b(want.b, got.b) {
m.t.Errorf("SummerMock.Sum got unexpected parameter 'b', want: %#v, got: %#v%s\n", want.b, got.b, minimock.Diff(want.b, got.b))
}
}
results := m.SumMock.defaultExpectation.results
if results == nil {
m.t.Fatal("No results are set for the SummerMock.Sum")
}
return (*results).i1
}
if m.funcSum != nil {
return m.funcSum(ctx, a, b)
}
m.t.Fatalf("Unexpected call to SummerMock.Sum. %v %v %v", ctx, a, b)
return
}
// SumAfterCounter returns a count of finished SummerMock.Sum invocations
func (m *SummerMock) SumAfterCounter() uint64 {
return atomic.LoadUint64(&m.afterSumCounter)
}
// SumBeforeCounter returns a count of SummerMock.Sum invocations
func (m *SummerMock) SumBeforeCounter() uint64 {
return atomic.LoadUint64(&m.beforeSumCounter)
}
// MinimockSumDone returns true if the count of the Sum invocations corresponds
// the number of defined expectations
func (m *SummerMock) MinimockSumDone() bool {
for _, e := range m.SumMock.expectations {
if atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
// if default expectation was set then invocations count should be greater than zero
if m.SumMock.defaultExpectation != nil && atomic.LoadUint64(&m.afterSumCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcSum != nil && atomic.LoadUint64(&m.afterSumCounter) < 1 {
return false
}
return true
}
// MinimockSumInspect logs each unmet expectation
func (m *SummerMock) MinimockSumInspect() {
for _, e := range m.SumMock.expectations {
if atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to SummerMock.Sum with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.SumMock.defaultExpectation != nil && atomic.LoadUint64(&m.afterSumCounter) < 1 {
m.t.Errorf("Expected call to SummerMock.Sum with params: %#v", *m.SumMock.defaultExpectation.params)
}
// if func was set then invocations count should be greater than zero
if m.funcSum != nil && atomic.LoadUint64(&m.afterSumCounter) < 1 {
m.t.Error("Expected call to SummerMock.Sum")
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *SummerMock) MinimockFinish() {
if !m.minimockDone() {
m.MinimockSumInspect()
m.t.FailNow()
}
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *SummerMock) 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 *SummerMock) minimockDone() bool {
done := true
return done &&
m.MinimockSumDone()
}
package main
import (
"context"
"testing"
"time"
)
type Summer interface {
Sum(ctx context.Context, a, b int) int
}
func limitedTimeSumm(ctx context.Context, summer Summer, a, b int) int {
ctx, _ = context.WithTimeout(ctx, time.Millisecond)
return summer.Sum(ctx, a, b)
}
func limitedTimeWithoutContext(ctx context.Context, summer Summer, a, b int) int {
return summer.Sum(nil, a, b)
}
func TestLimitedTimeSumm(t *testing.T) {
summerMock := NewSummerMock(t)
summerMock.SumMock.Set(func(_ context.Context, a int, b int) (i1 int) {
if a == 1 && b == 2 {
return 3
}
if a == 2 && b == 3 {
return 3
}
panic("unexpected")
})
res := limitedTimeSumm(context.Background(), summerMock, 1, 2)
if res != 3 {
t.Error(res)
}
}
func TestLimitedTimeSumm_New(t *testing.T) {
summerMock := NewSummerMock(t)
summerMock.SumMock.CtxNotNil()
summerMock.SumMock.When(nil, 1, 2).Then(3)
res := limitedTimeSumm(context.Background(), summerMock, 1, 2)
if res != 3 {
t.Error(res)
}
}
func TestLimitedTimeSumm_NewFailed(t *testing.T) {
summerMock := NewSummerMock(t)
summerMock.SumMock.CtxNotNil()
summerMock.SumMock.When(nil, 1, 2).Then(3)
res := limitedTimeWithoutContext(context.Background(), summerMock, 1, 2)
if res != 3 {
t.Error(res)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment