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
5package debug_test
6
7import (
8	. "runtime/debug"
9	"strings"
10	"testing"
11)
12
13type T int
14
15func (t *T) ptrmethod() []byte {
16	return Stack()
17}
18func (t T) method() []byte {
19	return t.ptrmethod()
20}
21
22/*
23	The traceback should look something like this, modulo line numbers and hex constants.
24	Don't worry much about the base levels, but check the ones in our own package.
25
26		goroutine 10 [running]:
27		runtime/debug.Stack(0x0, 0x0, 0x0)
28			/Users/r/go/src/runtime/debug/stack.go:28 +0x80
29		runtime/debug.(*T).ptrmethod(0xc82005ee70, 0x0, 0x0, 0x0)
30			/Users/r/go/src/runtime/debug/stack_test.go:15 +0x29
31		runtime/debug.T.method(0x0, 0x0, 0x0, 0x0)
32			/Users/r/go/src/runtime/debug/stack_test.go:18 +0x32
33		runtime/debug.TestStack(0xc8201ce000)
34			/Users/r/go/src/runtime/debug/stack_test.go:37 +0x38
35		testing.tRunner(0xc8201ce000, 0x664b58)
36			/Users/r/go/src/testing/testing.go:456 +0x98
37		created by testing.RunTests
38			/Users/r/go/src/testing/testing.go:561 +0x86d
39*/
40func TestStack(t *testing.T) {
41	b := T(0).method()
42	lines := strings.Split(string(b), "\n")
43	if len(lines) < 6 {
44		t.Fatal("too few lines")
45	}
46	n := 0
47	frame := func(line, code string) {
48		check(t, lines[n], code)
49		n++
50		check(t, lines[n], line)
51		n++
52	}
53	n++
54	frame("stack.go", "debug.Stack")
55	frame("stack_test.go", "ptrmethod")
56	frame("stack_test.go", "method")
57	frame("stack_test.go", "test.TestStack")
58	frame("testing.go", "")
59}
60
61func check(t *testing.T, line, has string) {
62	if !strings.Contains(line, has) {
63		t.Errorf("expected %q in %q", has, line)
64	}
65}
66