Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. package testhelper
  2.  
  3. import (
  4. "path"
  5. "reflect"
  6. "runtime"
  7. "strings"
  8. )
  9. // GetFileFunc takes a function and returns a string formatted as
  10. // "basedir/filename/function" to make it easier to identify the
  11. // source in printed test names (which will consequently appear as
  12. // "TestFoo/basedir/filename/function")
  13. func GetFileFunc(f interface{}) string {
  14. pc := reflect.ValueOf(f).Pointer()
  15. fpc := runtime.FuncForPC(pc)
  16. parts := strings.Split(path.Base(fpc.Name()), ".")
  17.  
  18. file, _ := fpc.FileLine(pc)
  19. base := path.Base(file)
  20.  
  21. // parts[0] => basedir
  22. // base => filename
  23. // parts[1] => function
  24. name := path.Join(parts[0], base, parts[1])
  25. return name
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement