Skip to content

Commit 773c2b5

Browse files
committed
Improve test coverage.
1 parent a7b7c69 commit 773c2b5

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

stack.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ func (c Call) file() string {
143143
return file
144144
}
145145

146+
func (c Call) line() int {
147+
if c.fn == nil {
148+
return 0
149+
}
150+
_, line := c.fn.FileLine(c.pc)
151+
return line
152+
}
153+
146154
// CallStack records a sequence of function invocations from a goroutine
147155
// stack.
148156
type CallStack []Call

stack_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,42 @@ func TestCallFormat(t *testing.T) {
8787
}
8888
}
8989

90+
func TestTrimAbove(t *testing.T) {
91+
trace := trimAbove()
92+
if got, want := len(trace), 2; got != want {
93+
t.Errorf("got len(trace) == %v, want %v, trace: %n", got, want, trace)
94+
}
95+
if got, want := fmt.Sprintf("%n", trace[1]), "TestTrimAbove"; got != want {
96+
t.Errorf("got %q, want %q", got, want)
97+
}
98+
}
99+
100+
func trimAbove() stack.CallStack {
101+
call := stack.Caller(1)
102+
trace := stack.Trace()
103+
return trace.TrimAbove(call)
104+
}
105+
106+
func TestTrimBelow(t *testing.T) {
107+
trace := trimBelow()
108+
if got, want := fmt.Sprintf("%n", trace[0]), "TestTrimBelow"; got != want {
109+
t.Errorf("got %q, want %q", got, want)
110+
}
111+
}
112+
113+
func trimBelow() stack.CallStack {
114+
call := stack.Caller(1)
115+
trace := stack.Trace()
116+
return trace.TrimBelow(call)
117+
}
118+
119+
func TestTrimRuntime(t *testing.T) {
120+
trace := stack.Trace().TrimRuntime()
121+
if got, want := len(trace), 1; got != want {
122+
t.Errorf("got len(trace) == %v, want %v, trace: %#v", got, want, trace)
123+
}
124+
}
125+
90126
func BenchmarkCallVFmt(b *testing.B) {
91127
c := stack.Caller(0)
92128
b.ResetTimer()
@@ -95,6 +131,12 @@ func BenchmarkCallVFmt(b *testing.B) {
95131
}
96132
}
97133

134+
func BenchmarkCallerAndVFmt(b *testing.B) {
135+
for i := 0; i < b.N; i++ {
136+
fmt.Fprint(ioutil.Discard, stack.Caller(0))
137+
}
138+
}
139+
98140
func BenchmarkCallPlusVFmt(b *testing.B) {
99141
c := stack.Caller(0)
100142
b.ResetTimer()
@@ -159,12 +201,30 @@ func BenchmarkCallPlusNFmt(b *testing.B) {
159201
}
160202
}
161203

204+
func BenchmarkCaller(b *testing.B) {
205+
for i := 0; i < b.N; i++ {
206+
stack.Caller(0)
207+
}
208+
}
209+
162210
func BenchmarkTrace(b *testing.B) {
163211
for i := 0; i < b.N; i++ {
164212
stack.Trace()
165213
}
166214
}
167215

216+
func BenchmarkFuncForPC(b *testing.B) {
217+
pc, _, _, _ := runtime.Caller(0)
218+
pc--
219+
var fn *runtime.Func
220+
b.ResetTimer()
221+
for i := 0; i < b.N; i++ {
222+
fn = runtime.FuncForPC(pc)
223+
}
224+
b.StopTimer()
225+
b.Log(fn.FileLine(pc))
226+
}
227+
168228
func deepStack(depth int, b *testing.B) stack.CallStack {
169229
if depth > 0 {
170230
return deepStack(depth-1, b)

stackinternal_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package stack
22

3-
import "testing"
3+
import (
4+
"runtime"
5+
"testing"
6+
)
47

58
func TestFindSigpanic(t *testing.T) {
69
t.Parallel()
@@ -9,3 +12,22 @@ func TestFindSigpanic(t *testing.T) {
912
t.Errorf("got == %v, want == %v", got, want)
1013
}
1114
}
15+
16+
func TestCaller(t *testing.T) {
17+
t.Parallel()
18+
19+
c := Caller(0)
20+
_, file, line, ok := runtime.Caller(0)
21+
line--
22+
if !ok {
23+
t.Fatal("runtime.Caller(0) failed")
24+
}
25+
26+
if got, want := c.file(), file; got != want {
27+
t.Errorf("got file == %v, want file == %v", got, want)
28+
}
29+
30+
if got, want := c.line(), line; got != want {
31+
t.Errorf("got line == %v, want line == %v", got, want)
32+
}
33+
}

0 commit comments

Comments
 (0)