-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlocation_provider_impl.go
47 lines (40 loc) · 1.03 KB
/
location_provider_impl.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
package assert
import (
"runtime"
"strings"
)
func provideLocation(skip int) *location {
testName, path, line := testCallerInfo(skip + 1)
fileName := fileNameFromPath(path)
return &location{
Test: testName,
FileName: fileName,
Line: line,
}
}
func testCallerInfo(skip int) (testName string, path string, line int) {
testName, path, line = callerInfo(skip + 1)
for i := skip + 2; !strings.HasPrefix(testName, "Test"); i++ {
testName, _, _ = callerInfo(i)
}
return
}
func callerInfo(skip int) (methodName string, path string, line int) {
pc, path, line, ok := runtime.Caller(skip + 1)
if !ok {
panic("you shall not pass!")
}
methodName = methodNameFromPC(pc)
return
}
func methodNameFromPC(pc uintptr) string {
fullName := runtime.FuncForPC(pc).Name()
return substringAfterLast(fullName, ".")
}
func fileNameFromPath(path string) string {
return substringAfterLast(path, "/")
}
func substringAfterLast(str string, separator string) string {
index := strings.LastIndex(str, separator)
return str[index+1:]
}