-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
54 lines (43 loc) · 1.36 KB
/
utils.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
package texit
import (
"errors"
"runtime"
"strings"
)
// ErrTestFuncNameNotFound error in the case of no testing function was found by `func_name` function
var ErrTestFuncNameNotFound = errors.New("Test function name has not found")
// Returns the name of testing function
// An error is returned if no testing function is found
// (this function call is not in the call graph of a testing function)
func testFuncName() (string, int, error) {
// Program counter
var pc uintptr = 1
// Populate the stack trace starting to 1 cause 0 is this function (`func_name`)
for i := 1; pc != 0; i++ {
// Retrieve runtime informations
ptr, _, _, ok := runtime.Caller(i)
pc = ptr
// If there isn't significant information, go to next level
if (pc == 0) || (!ok) {
continue
}
// Retrieve called function
f := runtime.FuncForPC(pc)
// Get the current functiont name
name := f.Name()
// Extract the short name by subtracting the package name
idx := strings.LastIndexByte(f.Name(), '.')
if idx > 0 {
name = name[(idx + 1):]
}
// Get line in file to have unique position in caller
_, line := f.FileLine(pc)
// If the prefix of the function is "Test"
if strings.HasPrefix(name, "Test") {
// So, it's a testing function
return name, line, nil
}
}
// Returns an error if no testing function was found
return "", 0, ErrTestFuncNameNotFound
}