summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/stretchr/testify/mock/mock.go
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2018-06-21 13:10:40 -0700
committerHarrison Healey <harrisonmhealey@gmail.com>2018-06-21 16:10:40 -0400
commit8526739066ccb00ccd24b74650a7d7b284442985 (patch)
tree282512ae2ad95c98a9ca82de304a410b6b56685c /vendor/github.com/stretchr/testify/mock/mock.go
parenta59ccaa8b3844895dde3980e6224fef46ff4a1c8 (diff)
downloadchat-8526739066ccb00ccd24b74650a7d7b284442985.tar.gz
chat-8526739066ccb00ccd24b74650a7d7b284442985.tar.bz2
chat-8526739066ccb00ccd24b74650a7d7b284442985.zip
MM-10934 Update server dependencies. (#8981)
* Changing throttled import path. * Upgrading dependencies.
Diffstat (limited to 'vendor/github.com/stretchr/testify/mock/mock.go')
-rw-r--r--vendor/github.com/stretchr/testify/mock/mock.go128
1 files changed, 99 insertions, 29 deletions
diff --git a/vendor/github.com/stretchr/testify/mock/mock.go b/vendor/github.com/stretchr/testify/mock/mock.go
index 1e232b56e..cc4f642b5 100644
--- a/vendor/github.com/stretchr/testify/mock/mock.go
+++ b/vendor/github.com/stretchr/testify/mock/mock.go
@@ -42,6 +42,9 @@ type Call struct {
// this method is called.
ReturnArguments Arguments
+ // Holds the caller info for the On() call
+ callerInfo []string
+
// The number of times to return the return arguments when setting
// expectations. 0 means to always return the value.
Repeatability int
@@ -64,12 +67,13 @@ type Call struct {
RunFn func(Arguments)
}
-func newCall(parent *Mock, methodName string, methodArguments ...interface{}) *Call {
+func newCall(parent *Mock, methodName string, callerInfo []string, methodArguments ...interface{}) *Call {
return &Call{
Parent: parent,
Method: methodName,
Arguments: methodArguments,
ReturnArguments: make([]interface{}, 0),
+ callerInfo: callerInfo,
Repeatability: 0,
WaitFor: nil,
RunFn: nil,
@@ -187,6 +191,10 @@ type Mock struct {
// Holds the calls that were made to this mocked object.
Calls []Call
+ // test is An optional variable that holds the test struct, to be used when an
+ // invalid mock call was made.
+ test TestingT
+
// TestData holds any data that might be useful for testing. Testify ignores
// this data completely allowing you to do whatever you like with it.
testData objx.Map
@@ -209,6 +217,27 @@ func (m *Mock) TestData() objx.Map {
Setting expectations
*/
+// Test sets the test struct variable of the mock object
+func (m *Mock) Test(t TestingT) {
+ m.mutex.Lock()
+ defer m.mutex.Unlock()
+ m.test = t
+}
+
+// fail fails the current test with the given formatted format and args.
+// In case that a test was defined, it uses the test APIs for failing a test,
+// otherwise it uses panic.
+func (m *Mock) fail(format string, args ...interface{}) {
+ m.mutex.Lock()
+ defer m.mutex.Unlock()
+
+ if m.test == nil {
+ panic(fmt.Sprintf(format, args...))
+ }
+ m.test.Errorf(format, args...)
+ m.test.FailNow()
+}
+
// On starts a description of an expectation of the specified method
// being called.
//
@@ -222,7 +251,7 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
m.mutex.Lock()
defer m.mutex.Unlock()
- c := newCall(m, methodName, arguments...)
+ c := newCall(m, methodName, assert.CallerInfo(), arguments...)
m.ExpectedCalls = append(m.ExpectedCalls, c)
return c
}
@@ -245,27 +274,25 @@ func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *
return -1, nil
}
-func (m *Mock) findClosestCall(method string, arguments ...interface{}) (bool, *Call) {
- diffCount := 0
+func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call, string) {
+ var diffCount int
var closestCall *Call
+ var err string
for _, call := range m.expectedCalls() {
if call.Method == method {
- _, tempDiffCount := call.Arguments.Diff(arguments)
+ errInfo, tempDiffCount := call.Arguments.Diff(arguments)
if tempDiffCount < diffCount || diffCount == 0 {
diffCount = tempDiffCount
closestCall = call
+ err = errInfo
}
}
}
- if closestCall == nil {
- return false, nil
- }
-
- return true, closestCall
+ return closestCall, err
}
func callString(method string, arguments Arguments, includeArgumentValues bool) string {
@@ -312,6 +339,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments {
m.mutex.Lock()
+ //TODO: could combine expected and closes in single loop
found, call := m.findExpectedCall(methodName, arguments...)
if found < 0 {
@@ -322,13 +350,18 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
// b) the arguments are not what was expected, or
// c) the developer has forgotten to add an accompanying On...Return pair.
- closestFound, closestCall := m.findClosestCall(methodName, arguments...)
+ closestCall, mismatch := m.findClosestCall(methodName, arguments...)
m.mutex.Unlock()
- if closestFound {
- panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\n", callString(methodName, arguments, true), callString(methodName, closestCall.Arguments, true), diffArguments(closestCall.Arguments, arguments)))
+ if closestCall != nil {
+ m.fail("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\nDiff: %s",
+ callString(methodName, arguments, true),
+ callString(methodName, closestCall.Arguments, true),
+ diffArguments(closestCall.Arguments, arguments),
+ strings.TrimSpace(mismatch),
+ )
} else {
- panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo()))
+ m.fail("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo())
}
}
@@ -340,7 +373,7 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
call.totalCalls++
// add the call
- m.Calls = append(m.Calls, *newCall(m, methodName, arguments...))
+ m.Calls = append(m.Calls, *newCall(m, methodName, assert.CallerInfo(), arguments...))
m.mutex.Unlock()
// block if specified
@@ -378,6 +411,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)")
@@ -385,6 +421,7 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
}
m := obj.(assertExpectationser)
if !m.AssertExpectations(t) {
+ t.Logf("Expectations didn't match for Mock: %+v", reflect.TypeOf(m))
return false
}
}
@@ -394,6 +431,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
@@ -405,12 +445,12 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
if !expectedCall.optional && !m.methodWasCalled(expectedCall.Method, expectedCall.Arguments) && expectedCall.totalCalls == 0 {
somethingMissing = true
failedExpectations++
- t.Logf("FAIL:\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
+ t.Logf("FAIL:\t%s(%s)\n\t\tat: %s", expectedCall.Method, expectedCall.Arguments.String(), expectedCall.callerInfo)
} else {
if expectedCall.Repeatability > 0 {
somethingMissing = true
failedExpectations++
- t.Logf("FAIL:\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
+ t.Logf("FAIL:\t%s(%s)\n\t\tat: %s", expectedCall.Method, expectedCall.Arguments.String(), expectedCall.callerInfo)
} else {
t.Logf("PASS:\t%s(%s)", expectedCall.Method, expectedCall.Arguments.String())
}
@@ -426,6 +466,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
@@ -440,11 +483,22 @@ 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 !assert.True(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method should have been called with %d argument(s), but was not.", methodName, len(arguments))) {
- t.Logf("%v", m.expectedCalls())
- return false
+ if !m.methodWasCalled(methodName, arguments) {
+ var calledWithArgs []string
+ for _, call := range m.calls() {
+ calledWithArgs = append(calledWithArgs, fmt.Sprintf("%v", call.Arguments))
+ }
+ if len(calledWithArgs) == 0 {
+ return assert.Fail(t, "Should have called with given arguments",
+ fmt.Sprintf("Expected %q to have been called with:\n%v\nbut no actual calls happened", methodName, arguments))
+ }
+ return assert.Fail(t, "Should have called with given arguments",
+ fmt.Sprintf("Expected %q to have been called with:\n%v\nbut actual calls were:\n %v", methodName, arguments, strings.Join(calledWithArgs, "\n")))
}
return true
}
@@ -452,11 +506,14 @@ 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 !assert.False(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method was called with %d argument(s), but should NOT have been.", methodName, len(arguments))) {
- t.Logf("%v", m.expectedCalls())
- return false
+ if m.methodWasCalled(methodName, arguments) {
+ return assert.Fail(t, "Should not have called with given arguments",
+ fmt.Sprintf("Expected %q to not have been called with:\n%v\nbut actually it was.", methodName, arguments))
}
return true
}
@@ -496,7 +553,7 @@ type Arguments []interface{}
const (
// Anything is used in Diff and Assert when the argument being tested
// shouldn't be taken into consideration.
- Anything string = "mock.Anything"
+ Anything = "mock.Anything"
)
// AnythingOfTypeArgument is a string that contains the type of an argument
@@ -599,6 +656,7 @@ func (args Arguments) Is(objects ...interface{}) bool {
//
// Returns the diff string and number of differences found.
func (args Arguments) Diff(objects []interface{}) (string, int) {
+ //TODO: could return string as error and nil for No difference
var output = "\n"
var differences int
@@ -610,25 +668,30 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
for i := 0; i < maxArgCount; i++ {
var actual, expected interface{}
+ var actualFmt, expectedFmt string
if len(objects) <= i {
actual = "(Missing)"
+ actualFmt = "(Missing)"
} else {
actual = objects[i]
+ actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual)
}
if len(args) <= i {
expected = "(Missing)"
+ expectedFmt = "(Missing)"
} else {
expected = args[i]
+ expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected)
}
if matcher, ok := expected.(argumentMatcher); ok {
if matcher.Matches(actual) {
- output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actual, matcher)
+ output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher)
} else {
differences++
- output = fmt.Sprintf("%s\t%d: PASS: %s not matched by %s\n", output, i, actual, matcher)
+ output = fmt.Sprintf("%s\t%d: PASS: %s not matched by %s\n", output, i, actualFmt, matcher)
}
} else if reflect.TypeOf(expected) == reflect.TypeOf((*AnythingOfTypeArgument)(nil)).Elem() {
@@ -636,7 +699,7 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
if reflect.TypeOf(actual).Name() != string(expected.(AnythingOfTypeArgument)) && reflect.TypeOf(actual).String() != string(expected.(AnythingOfTypeArgument)) {
// not match
differences++
- output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actual)
+ output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt)
}
} else {
@@ -645,11 +708,11 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
if assert.ObjectsAreEqual(expected, Anything) || assert.ObjectsAreEqual(actual, Anything) || assert.ObjectsAreEqual(actual, expected) {
// match
- output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actual, expected)
+ output = fmt.Sprintf("%s\t%d: PASS: %s == %s\n", output, i, actualFmt, expectedFmt)
} else {
// not match
differences++
- output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actual, expected)
+ output = fmt.Sprintf("%s\t%d: FAIL: %s != %s\n", output, i, actualFmt, expectedFmt)
}
}
@@ -666,6 +729,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)
@@ -813,3 +879,7 @@ var spewConfig = spew.ConfigState{
DisableCapacities: true,
SortKeys: true,
}
+
+type tHelper interface {
+ Helper()
+}