utils.go 832 B

12345678910111213141516171819202122232425262728
  1. // Package gotest contains internal functionality. Although this package
  2. // contains one or more exported names it is not intended for public
  3. // consumption. See the examples package for how to use this project.
  4. package gotest
  5. import (
  6. "runtime"
  7. "strings"
  8. )
  9. func ResolveExternalCaller() (file string, line int, name string) {
  10. var caller_id uintptr
  11. callers := runtime.Callers(0, callStack)
  12. for x := 0; x < callers; x++ {
  13. caller_id, file, line, _ = runtime.Caller(x)
  14. if strings.HasSuffix(file, "_test.go") || strings.HasSuffix(file, "_tests.go") {
  15. name = runtime.FuncForPC(caller_id).Name()
  16. return
  17. }
  18. }
  19. file, line, name = "<unknown file>", -1, "<unknown name>"
  20. return // panic?
  21. }
  22. const maxStackDepth = 100 // This had better be enough...
  23. var callStack []uintptr = make([]uintptr, maxStackDepth, maxStackDepth)