-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocation_test.go
More file actions
165 lines (119 loc) · 3.91 KB
/
Copy pathlocation_test.go
File metadata and controls
165 lines (119 loc) · 3.91 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package loc
import (
"path"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// padding
// padding
// padding
func TestLocation(t *testing.T) {
testLocationInside(t)
}
func testLocationInside(t *testing.T) {
t.Helper()
pc := Caller(0)
name, file, line := pc.NameFileLine()
assert.Equal(t, "loc.testLocationInside", path.Base(name))
assert.Equal(t, "location_test.go", filepath.Base(file))
assert.Equal(t, 24, line)
}
func TestLocationShort(t *testing.T) {
pc := Caller(0)
assert.Equal(t, "location_test.go:32", pc.String())
}
func TestLocation2(t *testing.T) {
func() {
func() {
l := FuncEntry(0)
ver := runtime.Version()
exp := "location_test.go:38"
if strings.HasPrefix(ver, "go1.24") {
exp = "location_test.go:36"
}
assert.Equal(t, exp, l.String(), "ver: %v", ver)
}()
}()
}
func TestLocationOnce(t *testing.T) {
var pc PC
CallerOnce(-1, &pc)
assert.Equal(t, "location.go:44", pc.String())
pc++
save := pc
CallerOnce(-1, &pc)
assert.Equal(t, save, pc) // not changed
//
pc = 0
FuncEntryOnce(-1, &pc)
assert.Equal(t, "location.go:51", pc.String())
pc++
save = pc
FuncEntryOnce(-1, &pc)
assert.Equal(t, save, pc) // not changed
}
func TestLocationCropFileName(t *testing.T) {
assert.Equal(t, "github.com/nikandfor/tlog/sub/module/file.go",
cropFilename("/path/to/src/github.com/nikandfor/tlog/sub/module/file.go", "github.com/nikandfor/tlog/sub/module.(*type).method"))
assert.Equal(t, "github.com/nikandfor/tlog/sub/module/file.go",
cropFilename("/path/to/src/github.com/nikandfor/tlog/sub/module/file.go", "github.com/nikandfor/tlog/sub/module.method"))
assert.Equal(t, "github.com/nikandfor/tlog/root.go", cropFilename("/path/to/src/github.com/nikandfor/tlog/root.go", "github.com/nikandfor/tlog.type.method"))
assert.Equal(t, "github.com/nikandfor/tlog/root.go", cropFilename("/path/to/src/github.com/nikandfor/tlog/root.go", "github.com/nikandfor/tlog.method"))
assert.Equal(t, "root.go", cropFilename("/path/to/src/root.go", "github.com/nikandfor/tlog.method"))
assert.Equal(t, "sub/file.go", cropFilename("/path/to/src/sub/file.go", "github.com/nikandfor/tlog/sub.method"))
assert.Equal(t, "root.go", cropFilename("/path/to/src/root.go", "tlog.method"))
assert.Equal(t, "subpkg/file.go", cropFilename("/path/to/src/subpkg/file.go", "subpkg.method"))
assert.Equal(t, "subpkg/file.go", cropFilename("/path/to/src/subpkg/file.go", "github.com/nikandfor/tlog/subpkg.(*type).method"))
assert.Equal(t, "errors/fmt_test.go",
cropFilename("/home/runner/work/errors/errors/fmt_test.go", "tlog.app/go/error.TestErrorFormatCaller"))
assert.Equal(t, "jq/object_test.go", cropFilename("/Users/nik/nikandfor/jq/object_test.go", "nikand.dev/go/jq.TestObject"))
}
func TestCaller(t *testing.T) {
a, b := Caller(0),
Caller(0)
// assert.False(t, a == b, "%x == %x", uintptr(a), uintptr(b))
assert.NotEqual(t, a, b)
}
func TestSetCache(t *testing.T) {
l := PC(0x1234567890)
assert.False(t, Cached(l))
SetCache(l, "", "", 0)
assert.False(t, Cached(l))
assert.NotEqual(t, "file.go:10", l.String())
SetCache(l, "Name", "file.go", 10)
assert.True(t, Cached(l))
assert.Equal(t, "file.go:10", l.String())
SetCacheBytes(l, []byte("name"), []byte("file"), 11)
name, file, line := l.NameFileLine()
assert.Equal(t, "name", name)
assert.Equal(t, "file", file)
assert.Equal(t, 11, line)
SetCacheBytes(l, nil, nil, 12)
name, file, line = l.NameFileLine()
assert.Equal(t, "", name)
assert.Equal(t, "", file)
assert.Equal(t, 12, line)
SetCacheBytes(l, nil, nil, 0)
assert.False(t, Cached(l))
}
func BenchmarkLocationCaller(b *testing.B) {
b.ReportAllocs()
var l PC
for i := 0; i < b.N; i++ {
l = Caller(0)
}
_ = l
}
func BenchmarkLocationNameFileLine(b *testing.B) {
b.ReportAllocs()
var n, f string
var line int
l := Caller(0)
for i := 0; i < b.N; i++ {
n, f, line = l.nameFileLine()
}
_, _, _ = n, f, line //nolint:dogsled
}