The mock.IsMethodCallable function is refactored

pull/900/head
Ogoyukin 2020-02-19 20:50:06 +09:00 committed by Boyan Soubachov
parent 3a72ffb6d8
commit 9f1c28b404
2 changed files with 17 additions and 19 deletions

View File

@ -536,7 +536,6 @@ func (m *Mock) IsMethodCallable(t TestingT, methodName string, arguments ...inte
m.mutex.Lock() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
var isCallable = false
for _, v := range m.ExpectedCalls { for _, v := range m.ExpectedCalls {
if v.Method != methodName { if v.Method != methodName {
continue continue
@ -548,16 +547,15 @@ func (m *Mock) IsMethodCallable(t TestingT, methodName string, arguments ...inte
continue continue
} }
if isArgsEqual(v.Arguments, arguments) { if isArgsEqual(v.Arguments, arguments) {
isCallable = true return true
break
} }
} }
return isCallable return false
} }
// Compares arguments // isArgsEqual compares arguments
func isArgsEqual(expected Arguments, args []interface{}) bool { func isArgsEqual(expected Arguments, args []interface{}) bool {
if len(args) != len(args) { if len(expected) != len(args) {
return false return false
} }
for i, v := range args { for i, v := range args {

View File

@ -1146,27 +1146,27 @@ func Test_Mock_AssertNotCalled(t *testing.T) {
func Test_Mock_IsMethodCallable(t *testing.T) { func Test_Mock_IsMethodCallable(t *testing.T) {
var mockedService = new(TestExampleImplementation) var mockedService = new(TestExampleImplementation)
arg := []Call{{Repeatability: 1}, {Repeatability: 1}} arg := []Call{{Repeatability: 1}, {Repeatability: 2}}
arg2 := []Call{{Repeatability: 1}, {Repeatability: 2}} arg2 := []Call{{Repeatability: 1}, {Repeatability: 1}}
arg3 := []Call{{Repeatability: 1}, {Repeatability: 1}} arg3 := []Call{{Repeatability: 1}, {Repeatability: 1}}
mockedService.On("Test_Mock_IsMethodCallable", arg).Return(true).Twice() mockedService.On("Test_Mock_IsMethodCallable", arg2).Return(true).Twice()
assert.False(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg2))
assert.True(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg))
assert.True(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg3))
mockedService.MethodCalled("Test_Mock_IsMethodCallable", arg)
mockedService.MethodCalled("Test_Mock_IsMethodCallable", arg)
assert.False(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg)) assert.False(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg))
assert.True(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg2))
assert.True(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg3))
mockedService.MethodCalled("Test_Mock_IsMethodCallable", arg2)
mockedService.MethodCalled("Test_Mock_IsMethodCallable", arg2)
assert.False(t, mockedService.IsMethodCallable(t, "Test_Mock_IsMethodCallable", arg2))
} }
func TestIsArgsEqual(t *testing.T) { func TestIsArgsEqual(t *testing.T) {
var expected = Arguments{5, 3, 4, 6, 7, 2} var expected = Arguments{5, 3, 4, 6, 7, 2}
var args = make([]interface{}, 6) var args = make([]interface{}, 5)
for i := 0; i < len(expected); i++ { for i := 1; i < len(expected); i++ {
args[i] = expected[i] args[i-1] = expected[i]
} }
args[2] = expected[1] args[2] = expected[1]
assert.False(t, isArgsEqual(expected, args)) assert.False(t, isArgsEqual(expected, args))