update go mock

This commit is contained in:
James Bardin 2018-12-10 17:15:53 -05:00
parent 8ab5698e2a
commit f5a2d49eef
6 changed files with 83 additions and 48 deletions

2
go.mod
View File

@ -42,7 +42,7 @@ require (
github.com/gogo/protobuf v0.0.0-20180821102207-98f6aa8b3bcf // indirect github.com/gogo/protobuf v0.0.0-20180821102207-98f6aa8b3bcf // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7 // indirect github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7 // indirect
github.com/golang/mock v1.1.1 github.com/golang/mock v1.2.0
github.com/golang/protobuf v1.2.0 github.com/golang/protobuf v1.2.0
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect
github.com/google/go-cmp v0.2.0 github.com/google/go-cmp v0.2.0

2
go.sum
View File

@ -91,6 +91,8 @@ github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7 h1:u4bArs140e9+A
github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20180513044358-24b0969c4cb7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8= github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk=
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=

View File

@ -23,7 +23,7 @@ import (
// Call represents an expected call to a mock. // Call represents an expected call to a mock.
type Call struct { type Call struct {
t TestReporter // for triggering test failures on invalid call setup t TestHelper // for triggering test failures on invalid call setup
receiver interface{} // the receiver of the method call receiver interface{} // the receiver of the method call
method string // the name of the method method string // the name of the method
@ -46,10 +46,8 @@ type Call struct {
// newCall creates a *Call. It requires the method type in order to support // newCall creates a *Call. It requires the method type in order to support
// unexported methods. // unexported methods.
func newCall(t TestReporter, receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call { func newCall(t TestHelper, receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call {
if h, ok := t.(testHelper); ok { t.Helper()
h.Helper()
}
// TODO: check arity, types. // TODO: check arity, types.
margs := make([]Matcher, len(args)) margs := make([]Matcher, len(args))
@ -159,9 +157,7 @@ func (c *Call) Do(f interface{}) *Call {
// Return declares the values to be returned by the mocked function call. // Return declares the values to be returned by the mocked function call.
func (c *Call) Return(rets ...interface{}) *Call { func (c *Call) Return(rets ...interface{}) *Call {
if h, ok := c.t.(testHelper); ok { c.t.Helper()
h.Helper()
}
mt := c.methodType mt := c.methodType
if len(rets) != mt.NumOut() { if len(rets) != mt.NumOut() {
@ -209,9 +205,7 @@ func (c *Call) Times(n int) *Call {
// indirected through a pointer. Or, in the case of a slice, SetArg // indirected through a pointer. Or, in the case of a slice, SetArg
// will copy value's elements into the nth argument. // will copy value's elements into the nth argument.
func (c *Call) SetArg(n int, value interface{}) *Call { func (c *Call) SetArg(n int, value interface{}) *Call {
if h, ok := c.t.(testHelper); ok { c.t.Helper()
h.Helper()
}
mt := c.methodType mt := c.methodType
// TODO: This will break on variadic methods. // TODO: This will break on variadic methods.
@ -264,9 +258,7 @@ func (c *Call) isPreReq(other *Call) bool {
// After declares that the call may only match after preReq has been exhausted. // After declares that the call may only match after preReq has been exhausted.
func (c *Call) After(preReq *Call) *Call { func (c *Call) After(preReq *Call) *Call {
if h, ok := c.t.(testHelper); ok { c.t.Helper()
h.Helper()
}
if c == preReq { if c == preReq {
c.t.Fatalf("A call isn't allowed to be its own prerequisite") c.t.Fatalf("A call isn't allowed to be its own prerequisite")

View File

@ -56,8 +56,8 @@
package gomock package gomock
import ( import (
"context"
"fmt" "fmt"
"golang.org/x/net/context"
"reflect" "reflect"
"runtime" "runtime"
"sync" "sync"
@ -70,45 +70,73 @@ type TestReporter interface {
Fatalf(format string, args ...interface{}) Fatalf(format string, args ...interface{})
} }
// TestHelper is a TestReporter that has the Helper method. It is satisfied
// by the standard library's *testing.T.
type TestHelper interface {
TestReporter
Helper()
}
// A Controller represents the top-level control of a mock ecosystem. // A Controller represents the top-level control of a mock ecosystem.
// It defines the scope and lifetime of mock objects, as well as their expectations. // It defines the scope and lifetime of mock objects, as well as their expectations.
// It is safe to call Controller's methods from multiple goroutines. // It is safe to call Controller's methods from multiple goroutines.
type Controller struct { type Controller struct {
// T should only be called within a generated mock. It is not intended to
// be used in user code and may be changed in future versions. T is the
// TestReporter passed in when creating the Controller via NewController.
// If the TestReporter does not implment a TestHelper it will be wrapped
// with a nopTestHelper.
T TestHelper
mu sync.Mutex mu sync.Mutex
t TestReporter
expectedCalls *callSet expectedCalls *callSet
finished bool finished bool
} }
func NewController(t TestReporter) *Controller { func NewController(t TestReporter) *Controller {
h, ok := t.(TestHelper)
if !ok {
h = nopTestHelper{t}
}
return &Controller{ return &Controller{
t: t, T: h,
expectedCalls: newCallSet(), expectedCalls: newCallSet(),
} }
} }
type cancelReporter struct { type cancelReporter struct {
t TestReporter TestHelper
cancel func() cancel func()
} }
func (r *cancelReporter) Errorf(format string, args ...interface{}) { r.t.Errorf(format, args...) } func (r *cancelReporter) Errorf(format string, args ...interface{}) {
r.TestHelper.Errorf(format, args...)
}
func (r *cancelReporter) Fatalf(format string, args ...interface{}) { func (r *cancelReporter) Fatalf(format string, args ...interface{}) {
defer r.cancel() defer r.cancel()
r.t.Fatalf(format, args...) r.TestHelper.Fatalf(format, args...)
} }
// WithContext returns a new Controller and a Context, which is cancelled on any // WithContext returns a new Controller and a Context, which is cancelled on any
// fatal failure. // fatal failure.
func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context) { func WithContext(ctx context.Context, t TestReporter) (*Controller, context.Context) {
h, ok := t.(TestHelper)
if !ok {
h = nopTestHelper{t}
}
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
return NewController(&cancelReporter{t, cancel}), ctx return NewController(&cancelReporter{h, cancel}), ctx
} }
type nopTestHelper struct {
TestReporter
}
func (h nopTestHelper) Helper() {}
func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call { func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...interface{}) *Call {
if h, ok := ctrl.t.(testHelper); ok { ctrl.T.Helper()
h.Helper()
}
recv := reflect.ValueOf(receiver) recv := reflect.ValueOf(receiver)
for i := 0; i < recv.Type().NumMethod(); i++ { for i := 0; i < recv.Type().NumMethod(); i++ {
@ -116,16 +144,14 @@ func (ctrl *Controller) RecordCall(receiver interface{}, method string, args ...
return ctrl.RecordCallWithMethodType(receiver, method, recv.Method(i).Type(), args...) return ctrl.RecordCallWithMethodType(receiver, method, recv.Method(i).Type(), args...)
} }
} }
ctrl.t.Fatalf("gomock: failed finding method %s on %T", method, receiver) ctrl.T.Fatalf("gomock: failed finding method %s on %T", method, receiver)
panic("unreachable") panic("unreachable")
} }
func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call { func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method string, methodType reflect.Type, args ...interface{}) *Call {
if h, ok := ctrl.t.(testHelper); ok { ctrl.T.Helper()
h.Helper()
}
call := newCall(ctrl.t, receiver, method, methodType, args...) call := newCall(ctrl.T, receiver, method, methodType, args...)
ctrl.mu.Lock() ctrl.mu.Lock()
defer ctrl.mu.Unlock() defer ctrl.mu.Unlock()
@ -135,19 +161,18 @@ func (ctrl *Controller) RecordCallWithMethodType(receiver interface{}, method st
} }
func (ctrl *Controller) Call(receiver interface{}, method string, args ...interface{}) []interface{} { func (ctrl *Controller) Call(receiver interface{}, method string, args ...interface{}) []interface{} {
if h, ok := ctrl.t.(testHelper); ok { ctrl.T.Helper()
h.Helper()
}
// Nest this code so we can use defer to make sure the lock is released. // Nest this code so we can use defer to make sure the lock is released.
actions := func() []func([]interface{}) []interface{} { actions := func() []func([]interface{}) []interface{} {
ctrl.T.Helper()
ctrl.mu.Lock() ctrl.mu.Lock()
defer ctrl.mu.Unlock() defer ctrl.mu.Unlock()
expected, err := ctrl.expectedCalls.FindMatch(receiver, method, args) expected, err := ctrl.expectedCalls.FindMatch(receiver, method, args)
if err != nil { if err != nil {
origin := callerInfo(2) origin := callerInfo(2)
ctrl.t.Fatalf("Unexpected call to %T.%v(%v) at %s because: %s", receiver, method, args, origin, err) ctrl.T.Fatalf("Unexpected call to %T.%v(%v) at %s because: %s", receiver, method, args, origin, err)
} }
// Two things happen here: // Two things happen here:
@ -176,15 +201,13 @@ func (ctrl *Controller) Call(receiver interface{}, method string, args ...interf
} }
func (ctrl *Controller) Finish() { func (ctrl *Controller) Finish() {
if h, ok := ctrl.t.(testHelper); ok { ctrl.T.Helper()
h.Helper()
}
ctrl.mu.Lock() ctrl.mu.Lock()
defer ctrl.mu.Unlock() defer ctrl.mu.Unlock()
if ctrl.finished { if ctrl.finished {
ctrl.t.Fatalf("Controller.Finish was called more than once. It has to be called exactly once.") ctrl.T.Fatalf("Controller.Finish was called more than once. It has to be called exactly once.")
} }
ctrl.finished = true ctrl.finished = true
@ -197,10 +220,10 @@ func (ctrl *Controller) Finish() {
// Check that all remaining expected calls are satisfied. // Check that all remaining expected calls are satisfied.
failures := ctrl.expectedCalls.Failures() failures := ctrl.expectedCalls.Failures()
for _, call := range failures { for _, call := range failures {
ctrl.t.Errorf("missing call(s) to %v", call) ctrl.T.Errorf("missing call(s) to %v", call)
} }
if len(failures) != 0 { if len(failures) != 0 {
ctrl.t.Fatalf("aborting test due to missing call(s)") ctrl.T.Fatalf("aborting test due to missing call(s)")
} }
} }
@ -210,8 +233,3 @@ func callerInfo(skip int) string {
} }
return "unknown file" return "unknown file"
} }
type testHelper interface {
TestReporter
Helper()
}

View File

@ -1,5 +1,3 @@
//go:generate mockgen -destination mock_matcher/mock_matcher.go github.com/golang/mock/gomock Matcher
// Copyright 2010 Google Inc. // Copyright 2010 Google Inc.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -87,6 +85,18 @@ func (n notMatcher) String() string {
return "not(" + n.m.String() + ")" return "not(" + n.m.String() + ")"
} }
type assignableToTypeOfMatcher struct {
targetType reflect.Type
}
func (m assignableToTypeOfMatcher) Matches(x interface{}) bool {
return reflect.TypeOf(x).AssignableTo(m.targetType)
}
func (m assignableToTypeOfMatcher) String() string {
return "is assignable to " + m.targetType.Name()
}
// Constructors // Constructors
func Any() Matcher { return anyMatcher{} } func Any() Matcher { return anyMatcher{} }
func Eq(x interface{}) Matcher { return eqMatcher{x} } func Eq(x interface{}) Matcher { return eqMatcher{x} }
@ -97,3 +107,16 @@ func Not(x interface{}) Matcher {
} }
return notMatcher{Eq(x)} return notMatcher{Eq(x)}
} }
// AssignableToTypeOf is a Matcher that matches if the parameter to the mock
// function is assignable to the type of the parameter to this function.
//
// Example usage:
//
// dbMock.EXPECT().
// Insert(gomock.AssignableToTypeOf(&EmployeeRecord{})).
// Return(errors.New("DB error"))
//
func AssignableToTypeOf(x interface{}) Matcher {
return assignableToTypeOfMatcher{reflect.TypeOf(x)}
}

2
vendor/modules.txt vendored
View File

@ -224,7 +224,7 @@ github.com/go-test/deep
github.com/gogo/protobuf/gogoproto github.com/gogo/protobuf/gogoproto
github.com/gogo/protobuf/proto github.com/gogo/protobuf/proto
github.com/gogo/protobuf/protoc-gen-gogo/descriptor github.com/gogo/protobuf/protoc-gen-gogo/descriptor
# github.com/golang/mock v1.1.1 # github.com/golang/mock v1.2.0
github.com/golang/mock/gomock github.com/golang/mock/gomock
# github.com/golang/protobuf v1.2.0 # github.com/golang/protobuf v1.2.0
github.com/golang/protobuf/proto github.com/golang/protobuf/proto