github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/should/call.go (about) 1 package should 2 3 import ( 4 "reflect" 5 "github.com/v2pro/plz/test" 6 "runtime" 7 "github.com/v2pro/plz/test/testify/assert" 8 ) 9 10 type ReturnValue []interface{} 11 12 var errorType = reflect.TypeOf((*error)(nil)).Elem() 13 14 func Call(f interface{}, argObjs ...interface{}) ReturnValue { 15 argVals := make([]reflect.Value, len(argObjs)) 16 for i := 0; i < len(argObjs); i++ { 17 argVals[i] = reflect.ValueOf(argObjs[i]) 18 } 19 retVals := reflect.ValueOf(f).Call(argVals) 20 retObjs := make([]interface{}, len(retVals)) 21 for i := 0; i < len(retVals); i++ { 22 retObjs[i] = retVals[i].Interface() 23 } 24 if len(retVals) > 0 && retVals[len(retVals)-1].Type().Implements(errorType) { 25 errObj := retVals[len(retVals)-1].Interface() 26 err, _ := errObj.(error) 27 t := test.CurrentT() 28 if assert.NoError(t, err) { 29 return ReturnValue(retObjs) 30 } 31 test.Helper() 32 _, file, line, ok := runtime.Caller(1) 33 if !ok { 34 t.Error("check failed") 35 return ReturnValue(retObjs) 36 } 37 t.Error(test.ExtractFailedLines(file, line)) 38 } 39 return ReturnValue(retObjs) 40 } 41 42 func (ret ReturnValue) Set(objs ...interface{}) { 43 for i, obj := range objs { 44 reflect.ValueOf(obj).Elem().Set(reflect.ValueOf(ret[i])) 45 } 46 }