-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutils_test.go
61 lines (50 loc) · 1014 Bytes
/
testutils_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
56
57
58
59
60
61
package sparse_test
import (
"context"
"errors"
"fmt"
"os"
"runtime"
"strings"
)
func must2[Ret any](r Ret, err error) Ret {
if err != nil {
if errors.Is(err, context.Canceled) {
fmt.Println("Terminated")
os.Exit(1)
}
panic(fmt.Sprintf("%+v", err))
}
return r
}
func currentCallerFuncShortName(skip int) string {
name := currentCallerFuncName(skip + 1)
if name == "" {
return ""
}
return name[strings.LastIndex(name, ".")+1:]
}
// skip = -1 will return currentCallerFuncName, skip = 0 will return the caller of currentCallerFuncName, etc.
func currentCallerFuncName(skip int) string {
pc, _, _, ok := runtime.Caller(skip + 1)
if !ok {
return ""
}
details := runtime.FuncForPC(pc)
if details == nil {
return ""
}
return details.Name()
}
func currentCallerCodeLineNum(skip int) int {
pc, _, _, ok := runtime.Caller(skip + 1)
if !ok {
return 0
}
details := runtime.FuncForPC(pc)
if details == nil {
return 0
}
_, line := details.FileLine(pc)
return line
}