caller_test.go 843 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package runtimeutil
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestCallerFuncPos(t *testing.T) {
  8. assert.True(t, strings.Contains(moo(), "TestCallerFuncPos:"))
  9. }
  10. func TestCaller(t *testing.T) {
  11. assert.True(t, strings.Contains(foo(1), "TestCaller"))
  12. assert.True(t, strings.Contains(bar(2), "TestCaller"))
  13. file, line, funcName := Caller(0)
  14. assert.True(t, strings.Contains(file, "caller_test.go"))
  15. assert.True(t, strings.Contains(funcName, ".TestCaller"))
  16. assert.True(t, line != 0)
  17. }
  18. func TestCallerFuncName(t *testing.T) {
  19. assert.True(t, strings.Contains(baz(), "TestCallerFuncName"))
  20. }
  21. func foo(skip int) string {
  22. _, _, s := Caller(skip)
  23. return s
  24. }
  25. func bar(skip int) string {
  26. return foo(skip)
  27. }
  28. func moo() string {
  29. return CallerFuncPos(1)
  30. }
  31. func baz() string {
  32. return CallerFuncName(1)
  33. }