@@ -218,7 +218,7 @@ type Mock struct {
218
218
// this data completely allowing you to do whatever you like with it.
219
219
testData objx.Map
220
220
221
- mutex sync.Mutex
221
+ mutex * sync.Mutex
222
222
}
223
223
224
224
// String provides a %v format string for Mock.
@@ -232,7 +232,6 @@ func (m *Mock) String() string {
232
232
// TestData holds any data that might be useful for testing. Testify ignores
233
233
// this data completely allowing you to do whatever you like with it.
234
234
func (m * Mock ) TestData () objx.Map {
235
-
236
235
if m .testData == nil {
237
236
m .testData = make (objx.Map )
238
237
}
@@ -246,6 +245,10 @@ func (m *Mock) TestData() objx.Map {
246
245
247
246
// Test sets the test struct variable of the mock object
248
247
func (m * Mock ) Test (t TestingT ) {
248
+ if m .mutex == nil {
249
+ m .mutex = & sync.Mutex {}
250
+ }
251
+
249
252
m .mutex .Lock ()
250
253
defer m .mutex .Unlock ()
251
254
m .test = t
@@ -276,6 +279,9 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call {
276
279
}
277
280
}
278
281
282
+ // Since we start mocks with the .On() function, m.mutex should be reset
283
+ m .mutex = & sync.Mutex {}
284
+
279
285
m .mutex .Lock ()
280
286
defer m .mutex .Unlock ()
281
287
c := newCall (m , methodName , assert .CallerInfo (), arguments ... )
@@ -354,7 +360,6 @@ func (m *Mock) findClosestCall(method string, arguments ...interface{}) (*Call,
354
360
}
355
361
356
362
func callString (method string , arguments Arguments , includeArgumentValues bool ) string {
357
-
358
363
var argValsString string
359
364
if includeArgumentValues {
360
365
var argVals []string
@@ -378,10 +383,10 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
378
383
panic ("Couldn't get the caller information" )
379
384
}
380
385
functionPath := runtime .FuncForPC (pc ).Name ()
381
- //Next four lines are required to use GCCGO function naming conventions.
382
- //For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock
383
- //uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree
384
- //With GCCGO we need to remove interface information starting from pN<dd>.
386
+ // Next four lines are required to use GCCGO function naming conventions.
387
+ // For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock
388
+ // uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree
389
+ // With GCCGO we need to remove interface information starting from pN<dd>.
385
390
re := regexp .MustCompile ("\\ .pN\\ d+_" )
386
391
if re .MatchString (functionPath ) {
387
392
functionPath = re .Split (functionPath , - 1 )[0 ]
@@ -397,7 +402,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
397
402
// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
398
403
func (m * Mock ) MethodCalled (methodName string , arguments ... interface {}) Arguments {
399
404
m .mutex .Lock ()
400
- //TODO: could combine expected and closes in single loop
405
+ // TODO: could combine expected and closes in single loop
401
406
found , call := m .findExpectedCall (methodName , arguments ... )
402
407
403
408
if found < 0 {
@@ -781,12 +786,12 @@ func (args Arguments) Is(objects ...interface{}) bool {
781
786
//
782
787
// Returns the diff string and number of differences found.
783
788
func (args Arguments ) Diff (objects []interface {}) (string , int ) {
784
- //TODO: could return string as error and nil for No difference
789
+ // TODO: could return string as error and nil for No difference
785
790
786
- var output = "\n "
791
+ output : = "\n "
787
792
var differences int
788
793
789
- var maxArgCount = len (args )
794
+ maxArgCount : = len (args )
790
795
if len (objects ) > maxArgCount {
791
796
maxArgCount = len (objects )
792
797
}
@@ -819,22 +824,19 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
819
824
output = fmt .Sprintf ("%s\t %d: FAIL: %s not matched by %s\n " , output , i , actualFmt , matcher )
820
825
}
821
826
} else if reflect .TypeOf (expected ) == reflect .TypeOf ((* AnythingOfTypeArgument )(nil )).Elem () {
822
-
823
827
// type checking
824
828
if reflect .TypeOf (actual ).Name () != string (expected .(AnythingOfTypeArgument )) && reflect .TypeOf (actual ).String () != string (expected .(AnythingOfTypeArgument )) {
825
829
// not match
826
830
differences ++
827
831
output = fmt .Sprintf ("%s\t %d: FAIL: type %s != type %s - %s\n " , output , i , expected , reflect .TypeOf (actual ).Name (), actualFmt )
828
832
}
829
-
830
833
} else if reflect .TypeOf (expected ) == reflect .TypeOf ((* IsTypeArgument )(nil )) {
831
834
t := expected .(* IsTypeArgument ).t
832
835
if reflect .TypeOf (t ) != reflect .TypeOf (actual ) {
833
836
differences ++
834
837
output = fmt .Sprintf ("%s\t %d: FAIL: type %s != type %s - %s\n " , output , i , reflect .TypeOf (t ).Name (), reflect .TypeOf (actual ).Name (), actualFmt )
835
838
}
836
839
} else {
837
-
838
840
// normal checking
839
841
840
842
if assert .ObjectsAreEqual (expected , Anything ) || assert .ObjectsAreEqual (actual , Anything ) || assert .ObjectsAreEqual (actual , expected ) {
@@ -854,7 +856,6 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
854
856
}
855
857
856
858
return output , differences
857
-
858
859
}
859
860
860
861
// Assert compares the arguments with the specified objects and fails if
@@ -876,7 +877,6 @@ func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
876
877
t .Errorf ("%sArguments do not match." , assert .CallerInfo ())
877
878
878
879
return false
879
-
880
880
}
881
881
882
882
// String gets the argument at the specified index. Panics if there is no argument, or
@@ -885,7 +885,6 @@ func (args Arguments) Assert(t TestingT, objects ...interface{}) bool {
885
885
// If no index is provided, String() returns a complete string representation
886
886
// of the arguments.
887
887
func (args Arguments ) String (indexOrNil ... int ) string {
888
-
889
888
if len (indexOrNil ) == 0 {
890
889
// normal String() method - return a string representation of the args
891
890
var argsStr []string
@@ -895,7 +894,7 @@ func (args Arguments) String(indexOrNil ...int) string {
895
894
return strings .Join (argsStr , "," )
896
895
} else if len (indexOrNil ) == 1 {
897
896
// Index has been specified - get the argument at that index
898
- var index = indexOrNil [0 ]
897
+ index : = indexOrNil [0 ]
899
898
var s string
900
899
var ok bool
901
900
if s , ok = args .Get (index ).(string ); ! ok {
@@ -905,7 +904,6 @@ func (args Arguments) String(indexOrNil ...int) string {
905
904
}
906
905
907
906
panic (fmt .Sprintf ("assert: arguments: Wrong number of arguments passed to String. Must be 0 or 1, not %d" , len (indexOrNil )))
908
-
909
907
}
910
908
911
909
// Int gets the argument at the specified index. Panics if there is no argument, or
0 commit comments