stack_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package debug
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. type T int
  10. func (t *T) ptrmethod() []byte {
  11. return Stack()
  12. }
  13. func (t T) method() []byte {
  14. return t.ptrmethod()
  15. }
  16. /*
  17. The traceback should look something like this, modulo line numbers and hex constants.
  18. Don't worry much about the base levels, but check the ones in our own package.
  19. /Users/r/go/src/pkg/runtime/debug/stack_test.go:15 (0x13878)
  20. (*T).ptrmethod: return Stack()
  21. /Users/r/go/src/pkg/runtime/debug/stack_test.go:18 (0x138dd)
  22. T.method: return t.ptrmethod()
  23. /Users/r/go/src/pkg/runtime/debug/stack_test.go:23 (0x13920)
  24. TestStack: b := T(0).method()
  25. /Users/r/go/src/pkg/testing/testing.go:132 (0x14a7a)
  26. tRunner: test.F(t)
  27. /Users/r/go/src/pkg/runtime/proc.c:145 (0xc970)
  28. ???: runtime·unlock(&runtime·sched);
  29. */
  30. func TestStack(t *testing.T) {
  31. b := T(0).method()
  32. lines := strings.Split(string(b), "\n")
  33. if len(lines) < 6 {
  34. t.Fatal("too few lines")
  35. }
  36. n := 0
  37. frame := func(line, code string) {
  38. check(t, lines[n], line)
  39. n++
  40. // The source might not be available while running the test.
  41. if strings.HasPrefix(lines[n], "\t") {
  42. check(t, lines[n], code)
  43. n++
  44. }
  45. }
  46. frame("stack_test.go", "\tmethod.N15_runtime_debug.T: return Stack()")
  47. frame("stack_test.go", "\tmethod.N15_runtime_debug.T: return t.ptrmethod()")
  48. frame("stack_test.go", "\tTestStack: b := T(0).method()")
  49. frame("testing/testing.go", "")
  50. }
  51. func check(t *testing.T, line, has string) {
  52. if strings.Index(line, has) < 0 {
  53. t.Errorf("expected %q in %q", has, line)
  54. }
  55. }