github.com/d4l3k/go@v0.0.0-20151015000803-65fc379daeda/src/runtime/debug/stack_test.go (about)

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