Skip to content

Commit

Permalink
Improve errors from mock assertions
Browse files Browse the repository at this point in the history
Mark the assert helpers as helpers for Go Versions that support
`t.Helper()`
  • Loading branch information
Ernesto Jiménez authored and ernesto-jimenez committed Mar 3, 2018
1 parent 0bfbef4 commit b89eecf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ type assertExpectationser interface {
//
// Calls may have occurred in any order.
func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
for _, obj := range testObjects {
if m, ok := obj.(Mock); ok {
t.Logf("Deprecated mock.AssertExpectationsForObjects(myMock.Mock) use mock.AssertExpectationsForObjects(myMock)")
Expand All @@ -399,6 +402,9 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
// AssertExpectations asserts that everything specified with On and Return was
// in fact called as expected. Calls may have occurred in any order.
func (m *Mock) AssertExpectations(t TestingT) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
var somethingMissing bool
Expand Down Expand Up @@ -431,6 +437,9 @@ func (m *Mock) AssertExpectations(t TestingT) bool {

// AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
var actualCalls int
Expand All @@ -445,6 +454,9 @@ func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls
// AssertCalled asserts that the method was called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
if !m.methodWasCalled(methodName, arguments) {
Expand All @@ -465,6 +477,9 @@ func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interfac
// AssertNotCalled asserts that the method was not called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
if m.methodWasCalled(methodName, arguments) {
Expand Down Expand Up @@ -679,6 +694,9 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
// Assert compares the arguments with the specified objects and fails if
// they do not exactly match.
func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}

// get the differences
diff, diffCount := args.Diff(objects)
Expand Down Expand Up @@ -826,3 +844,7 @@ var spewConfig = spew.ConfigState{
DisableCapacities: true,
SortKeys: true,
}

type tHelper interface {
Helper()
}

0 comments on commit b89eecf

Please sign in to comment.