utils.go 985 B

12345678910111213141516171819202122232425262728293031323334353637
  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. "fmt"
  7. "runtime"
  8. "strings"
  9. )
  10. func FormatExternalFileAndLine() string {
  11. file, line, _ := ResolveExternalCaller()
  12. if line == -1 {
  13. return "<unknown caller!>" // panic?
  14. }
  15. return fmt.Sprintf("%s:%d", file, line)
  16. }
  17. func ResolveExternalCaller() (file string, line int, name string) {
  18. var caller_id uintptr
  19. callers := runtime.Callers(0, callStack)
  20. for x := 0; x < callers; x++ {
  21. caller_id, file, line, _ = runtime.Caller(x)
  22. if strings.HasSuffix(file, "_test.go") {
  23. name = runtime.FuncForPC(caller_id).Name()
  24. return
  25. }
  26. }
  27. file, line, name = "<unkown file>", -1, "<unknown name>"
  28. return // panic?
  29. }
  30. const maxStackDepth = 100 // This had better be enough...
  31. var callStack []uintptr = make([]uintptr, maxStackDepth, maxStackDepth)