-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallers_test.go
55 lines (46 loc) · 1.08 KB
/
callers_test.go
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
package faket
import (
"path/filepath"
"testing"
"github.com/prashantv/faket/internal/want"
)
func TestGetCallers(t *testing.T) {
t.Run("skip all", func(t *testing.T) {
pc := getCallers(1000)
if len(pc) > 0 {
t.Fatalf("expected empty pc, got:\n%v", pc)
}
})
t.Run("large depth", func(t *testing.T) {
const depth = 128
pc := recurse(depth, func() []uintptr {
return getCallers(0)
})
if len(pc) < depth {
t.Fatalf("len(pc) = %v < %v", len(pc), depth)
}
})
}
func TestGetCaller(t *testing.T) {
t.Run("skip all", func(t *testing.T) {
pc := getCaller(1000)
want.Equal(t, "pc", pc, 0)
})
t.Run("skip 0", func(t *testing.T) {
got := pcToFunction(getCallerCaller(0))
want.Equal(t, "caller function", filepath.Base(got), "faket.getCallerCaller")
})
t.Run("skip 1", func(t *testing.T) {
got := pcToFunction(getCallerCaller(1))
want.Contains(t, "caller function", got, "TestGetCaller")
})
}
func getCallerCaller(skip int) uintptr {
return getCaller(skip)
}
func recurse[T any](n int, fn func() T) T {
if n == 0 {
return fn()
}
return recurse(n-1, fn)
}