-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
59 lines (51 loc) · 1.76 KB
/
printer.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
// -----------------------------------------------------------------------------
// asserts for more convinient testing - printing fail information
//
// Copyright (C) 2024 Frank Mueller / Oldenburg / Germany / World
// -----------------------------------------------------------------------------
package asserts // import "tideland.dev/go/asserts"
import (
"fmt"
"path"
"runtime"
"strings"
)
// logf prints a log message with the given information on stdout.
func logf(t Tester, format string, args ...any) {
location, fun := here(3)
format = fmt.Sprintf("%s assertion log at %s(): %s\n", location, fun, format)
t.Logf(format, args...)
}
// failf prints a fail message with the given information on stderr.
func failf(t Tester, assertion string, format string, args ...any) {
location, fun := here(4)
format = fmt.Sprintf("%s assertion '%s' fail at %s(): %s\n", location, assertion, fun, format)
t.Errorf(format, args...)
}
// here returns the filename and position based on a given offset.
// It's used by the fail function to print the location of the failure.
func here(offset int) (string, string) {
// Retrieve program counters.
pcs := make([]uintptr, 1)
n := runtime.Callers(offset, pcs)
if n == 0 {
return "", ""
}
pcs = pcs[:n]
// Build ID based on program counters.
frames := runtime.CallersFrames(pcs)
for {
frame, more := frames.Next()
_, fun := path.Split(frame.Function)
parts := strings.Split(fun, ".")
fun = strings.Join(parts[1:], ".")
_, file := path.Split(frame.File)
location := fmt.Sprintf("%s:%d:0:", file, frame.Line)
if !more {
return location, fun
}
}
}
// -----------------------------------------------------------------------------
// end of file
// -----------------------------------------------------------------------------