1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
package stack_test
import (
"regexp"
"strings"
"testing"
"github.com/facebookgo/stack"
)
func indirect1() stack.Stack {
return stack.Callers(0)
}
func indirect2() stack.Stack {
return indirect1()
}
func indirect3() stack.Stack {
return indirect2()
}
func TestCallers(t *testing.T) {
s := indirect3()
matches := []string{
"stack_test.go:12 +indirect1$",
"stack_test.go:16 +indirect2$",
"stack_test.go:20 +indirect3$",
"stack_test.go:24 +TestCallers$",
}
match(t, s.String(), matches)
}
func TestCallersMulti(t *testing.T) {
m := stack.CallersMulti(0)
const expected = "stack_test.go:35 TestCallersMulti"
first := m.Stacks()[0][0].String()
if !strings.HasSuffix(first, expected) {
t.Fatalf(`expected suffix "%s" got "%s"`, expected, first)
}
}
func TestCallersMultiWithTwo(t *testing.T) {
m := stack.CallersMulti(0)
m.AddCallers(0)
matches := []string{
"stack_test.go:44 +TestCallersMultiWithTwo$",
"",
"",
`\(Stack 2\)$`,
"stack_test.go:45 +TestCallersMultiWithTwo$",
}
match(t, m.String(), matches)
}
type typ struct{}
func (m typ) indirect1() stack.Stack {
return stack.Callers(0)
}
func (m typ) indirect2() stack.Stack {
return m.indirect1()
}
func (m typ) indirect3() stack.Stack {
return m.indirect2()
}
func TestCallersWithStruct(t *testing.T) {
var m typ
s := m.indirect3()
matches := []string{
"stack_test.go:59 +typ.indirect1$",
"stack_test.go:63 +typ.indirect2$",
"stack_test.go:67 +typ.indirect3$",
"stack_test.go:72 +TestCallersWithStruct$",
}
match(t, s.String(), matches)
}
func TestCaller(t *testing.T) {
f := stack.Caller(0)
const expected = "stack_test.go:83 TestCaller"
if !strings.HasSuffix(f.String(), expected) {
t.Fatalf(`expected suffix "%s" got "%s"`, expected, f)
}
}
func match(t testing.TB, s string, matches []string) {
lines := strings.Split(s, "\n")
for i, m := range matches {
if !regexp.MustCompile(m).MatchString(lines[i]) {
t.Fatalf(
"did not find expected match \"%s\" on line %d in:\n%s",
m,
i,
s,
)
}
}
}
|