Skip to content

Commit be992af

Browse files
committed
trial
1 parent fb67df6 commit be992af

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

mock/mock.go

+31-23
Original file line numberDiff line numberDiff line change
@@ -1205,31 +1205,28 @@ func assertOpts(expected, actual interface{}) (expectedFmt, actualFmt string) {
12051205
expectedOpts := reflect.ValueOf(expected)
12061206
actualOpts := reflect.ValueOf(actual)
12071207

1208-
var expectedNames []string
1209-
for i := 0; i < expectedOpts.Len(); i++ {
1210-
expectedNames = append(expectedNames, funcName(expectedOpts.Index(i).Interface()))
1211-
}
1212-
var actualNames []string
1213-
for i := 0; i < actualOpts.Len(); i++ {
1214-
actualNames = append(actualNames, funcName(actualOpts.Index(i).Interface()))
1215-
}
1216-
if !assert.ObjectsAreEqual(expectedNames, actualNames) {
1217-
expectedFmt = fmt.Sprintf("%v", expectedNames)
1218-
actualFmt = fmt.Sprintf("%v", actualNames)
1208+
if expectedOpts.Len() != actualOpts.Len() {
1209+
expectedFmt = fmt.Sprintf("%v", expected)
1210+
actualFmt = fmt.Sprintf("%v", actual)
12191211
return
12201212
}
12211213

1214+
var funcNames []string
1215+
12221216
for i := 0; i < expectedOpts.Len(); i++ {
1223-
expectedOpt := expectedOpts.Index(i).Interface()
1224-
actualOpt := actualOpts.Index(i).Interface()
1217+
expectedFunc := getRuntimeFunc(expectedOpts.Index(i).Interface())
1218+
funcNames = append(funcNames, funcName(getRuntimeFunc(expectedFunc)))
12251219

1226-
expectedFunc := expectedNames[i]
1227-
actualFunc := actualNames[i]
1228-
if expectedFunc != actualFunc {
1229-
expectedFmt = expectedFunc
1230-
actualFmt = actualFunc
1220+
if actualFunc := getRuntimeFunc(actualOpts.Index(i).Interface()); !isFuncSame(expectedFunc, actualFunc) {
1221+
expectedFmt = funcName(expectedFunc)
1222+
actualFmt = funcName(actualFunc)
12311223
return
12321224
}
1225+
}
1226+
1227+
for i := 0; i < expectedOpts.Len(); i++ {
1228+
expectedOpt := expectedOpts.Index(i).Interface()
1229+
actualOpt := actualOpts.Index(i).Interface()
12331230

12341231
ot := reflect.TypeOf(expectedOpt)
12351232
var expectedValues []reflect.Value
@@ -1249,8 +1246,8 @@ func assertOpts(expected, actual interface{}) (expectedFmt, actualFmt string) {
12491246

12501247
for i := 0; i < ot.NumIn(); i++ {
12511248
if expectedArg, actualArg := expectedValues[i].Interface(), actualValues[i].Interface(); !assert.ObjectsAreEqual(expectedArg, actualArg) {
1252-
expectedFmt = fmt.Sprintf("%s(%T) -> %#v", expectedNames[i], expectedArg, expectedArg)
1253-
actualFmt = fmt.Sprintf("%s(%T) -> %#v", expectedNames[i], actualArg, actualArg)
1249+
expectedFmt = fmt.Sprintf("%s(%T) -> %#v", funcNames[i], expectedArg, expectedArg)
1250+
actualFmt = fmt.Sprintf("%s(%T) -> %#v", funcNames[i], actualArg, actualArg)
12541251
return
12551252
}
12561253
}
@@ -1259,9 +1256,13 @@ func assertOpts(expected, actual interface{}) (expectedFmt, actualFmt string) {
12591256
return "", ""
12601257
}
12611258

1262-
func funcName(opt interface{}) string {
1263-
n := runtime.FuncForPC(reflect.ValueOf(opt).Pointer()).Name()
1264-
trimmed := strings.TrimSuffix(path.Base(n), path.Ext(n))
1259+
func getRuntimeFunc(opt interface{}) *runtime.Func {
1260+
return runtime.FuncForPC(reflect.ValueOf(opt).Pointer())
1261+
}
1262+
1263+
func funcName(f *runtime.Func) string {
1264+
name := f.Name()
1265+
trimmed := strings.TrimSuffix(path.Base(name), path.Ext(name))
12651266
splitted := strings.Split(trimmed, ".")
12661267

12671268
if len(splitted) == 0 {
@@ -1270,3 +1271,10 @@ func funcName(opt interface{}) string {
12701271

12711272
return splitted[len(splitted)-1]
12721273
}
1274+
1275+
func isFuncSame(f1, f2 *runtime.Func) bool {
1276+
f1File, f1Loc := f1.FileLine(f1.Entry())
1277+
f2File, f2Loc := f2.FileLine(f2.Entry())
1278+
1279+
return f1File == f2File && f1Loc == f2Loc
1280+
}

mock/mock_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ func Test_Mock_AssertExpectationsFunctionalOptionsType_Indirectly(t *testing.T)
15501550

15511551
}
15521552

1553-
func Test_Mock_AssertExpectationsFunctionalOptionsType_Diff_Name(t *testing.T) {
1553+
func Test_Mock_AssertExpectationsFunctionalOptionsType_Diff_Func(t *testing.T) {
15541554

15551555
var mockedService = new(TestExampleImplementation)
15561556

0 commit comments

Comments
 (0)