-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_test.go
More file actions
104 lines (89 loc) · 2.31 KB
/
Copy pathstack_test.go
File metadata and controls
104 lines (89 loc) · 2.31 KB
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
103
104
package faultline
import (
"fmt"
"strings"
"testing"
)
func TestStackTrace(t *testing.T) {
err := New("test")
type stackTracer interface {
StackTrace() StackTrace
}
st, ok := err.(stackTracer)
if !ok {
t.Fatal("error does not implement stackTracer")
}
trace := st.StackTrace()
if len(trace) == 0 {
t.Fatal("empty stack trace")
}
// First frame should be in this test file
f := trace[0]
file := f.file()
if !strings.HasSuffix(file, "stack_test.go") {
t.Errorf("first frame file: got %q, want suffix stack_test.go", file)
}
line := f.line()
if line == 0 {
t.Error("first frame line is 0")
}
name := f.name()
if !strings.Contains(name, "TestStackTrace") {
t.Errorf("first frame name: got %q, want to contain TestStackTrace", name)
}
}
func TestFrameFormat(t *testing.T) {
err := New("test")
type stackTracer interface {
StackTrace() StackTrace
}
st := err.(stackTracer)
trace := st.StackTrace()
f := trace[0]
// %s should be just the filename
s := fmt.Sprintf("%s", f)
if !strings.HasSuffix(s, "stack_test.go") {
t.Errorf("%%s: got %q", s)
}
// %d should be a line number
d := fmt.Sprintf("%d", f)
if d == "0" || d == "" {
t.Errorf("%%d: got %q", d)
}
// %n should be function name without package path
n := fmt.Sprintf("%n", f)
if n != "TestFrameFormat" {
t.Errorf("%%n: got %q, want TestFrameFormat", n)
}
// %v should be file:line
v := fmt.Sprintf("%v", f)
if !strings.Contains(v, "stack_test.go:") {
t.Errorf("%%v: got %q", v)
}
// %+v should include function name and full path
pv := fmt.Sprintf("%+v", f)
if !strings.Contains(pv, "TestFrameFormat") {
t.Errorf("%%+v: got %q, want to contain TestFrameFormat", pv)
}
if !strings.Contains(pv, "\n\t") {
t.Errorf("%%+v: got %q, want to contain newline+tab", pv)
}
}
func TestStackTraceFormat(t *testing.T) {
err := New("test")
type stackTracer interface {
StackTrace() StackTrace
}
st := err.(stackTracer)
trace := st.StackTrace()
// %+v should produce multi-line output
pv := fmt.Sprintf("%+v", trace)
if !strings.Contains(pv, "TestStackTraceFormat") {
t.Errorf("%%+v: got %q, want to contain TestStackTraceFormat", pv)
}
// %v should produce a bracketed list
v := fmt.Sprintf("%v", trace)
if !strings.HasPrefix(v, "[") || !strings.HasSuffix(v, "]") {
t.Errorf("%%v: got %q, want bracketed list", v)
}
}