tlog.app/go/loc@v0.6.2-0.20231112073106-b6382a0ac518/location_test.go (about) 1 package loc 2 3 import ( 4 "path" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestLocation(t *testing.T) { 12 testLocationInside(t) 13 } 14 15 func testLocationInside(t *testing.T) { 16 t.Helper() 17 18 pc := Caller(0) 19 name, file, line := pc.NameFileLine() 20 assert.Equal(t, "loc.testLocationInside", path.Base(name)) 21 assert.Equal(t, "location_test.go", filepath.Base(file)) 22 assert.Equal(t, 18, line) 23 } 24 25 func TestLocationShort(t *testing.T) { 26 pc := Caller(0) 27 assert.Equal(t, "location_test.go:26", pc.String()) 28 } 29 30 func TestLocation2(t *testing.T) { 31 func() { 32 func() { 33 l := FuncEntry(0) 34 35 assert.Equal(t, "location_test.go:32", l.String()) 36 }() 37 }() 38 } 39 40 func TestLocationOnce(t *testing.T) { 41 var pc PC 42 43 CallerOnce(-1, &pc) 44 assert.Equal(t, "location.go:44", pc.String()) 45 46 pc++ 47 save := pc 48 49 CallerOnce(-1, &pc) 50 51 assert.Equal(t, save, pc) // not changed 52 53 // 54 pc = 0 55 56 FuncEntryOnce(-1, &pc) 57 assert.Equal(t, "location.go:51", pc.String()) 58 59 pc++ 60 save = pc 61 62 FuncEntryOnce(-1, &pc) 63 64 assert.Equal(t, save, pc) // not changed 65 } 66 67 func TestLocationCropFileName(t *testing.T) { 68 assert.Equal(t, "github.com/nikandfor/tlog/sub/module/file.go", 69 cropFilename("/path/to/src/github.com/nikandfor/tlog/sub/module/file.go", "github.com/nikandfor/tlog/sub/module.(*type).method")) 70 assert.Equal(t, "github.com/nikandfor/tlog/sub/module/file.go", 71 cropFilename("/path/to/src/github.com/nikandfor/tlog/sub/module/file.go", "github.com/nikandfor/tlog/sub/module.method")) 72 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")) 73 assert.Equal(t, "github.com/nikandfor/tlog/root.go", cropFilename("/path/to/src/github.com/nikandfor/tlog/root.go", "github.com/nikandfor/tlog.method")) 74 assert.Equal(t, "root.go", cropFilename("/path/to/src/root.go", "github.com/nikandfor/tlog.method")) 75 assert.Equal(t, "sub/file.go", cropFilename("/path/to/src/sub/file.go", "github.com/nikandfor/tlog/sub.method")) 76 assert.Equal(t, "root.go", cropFilename("/path/to/src/root.go", "tlog.method")) 77 assert.Equal(t, "subpkg/file.go", cropFilename("/path/to/src/subpkg/file.go", "subpkg.method")) 78 assert.Equal(t, "subpkg/file.go", cropFilename("/path/to/src/subpkg/file.go", "github.com/nikandfor/tlog/subpkg.(*type).method")) 79 assert.Equal(t, "errors/fmt_test.go", 80 cropFilename("/home/runner/work/errors/errors/fmt_test.go", "tlog.app/go/error.TestErrorFormatCaller")) 81 } 82 83 func TestCaller(t *testing.T) { 84 a, b := Caller(0), 85 Caller(0) 86 87 // assert.False(t, a == b, "%x == %x", uintptr(a), uintptr(b)) 88 assert.NotEqual(t, a, b) 89 } 90 91 func TestSetCache(t *testing.T) { 92 l := PC(0x1234567890) 93 94 assert.False(t, Cached(l)) 95 96 SetCache(l, "", "", 0) 97 98 assert.False(t, Cached(l)) 99 100 assert.NotEqual(t, "file.go:10", l.String()) 101 102 SetCache(l, "Name", "file.go", 10) 103 104 assert.True(t, Cached(l)) 105 106 assert.Equal(t, "file.go:10", l.String()) 107 108 SetCacheBytes(l, []byte("name"), []byte("file"), 11) 109 110 name, file, line := l.NameFileLine() 111 assert.Equal(t, "name", name) 112 assert.Equal(t, "file", file) 113 assert.Equal(t, 11, line) 114 115 SetCacheBytes(l, nil, nil, 12) 116 117 name, file, line = l.NameFileLine() 118 assert.Equal(t, "", name) 119 assert.Equal(t, "", file) 120 assert.Equal(t, 12, line) 121 122 SetCacheBytes(l, nil, nil, 0) 123 124 assert.False(t, Cached(l)) 125 } 126 127 func BenchmarkLocationCaller(b *testing.B) { 128 b.ReportAllocs() 129 130 var l PC 131 132 for i := 0; i < b.N; i++ { 133 l = Caller(0) 134 } 135 136 _ = l 137 } 138 139 func BenchmarkLocationNameFileLine(b *testing.B) { 140 b.ReportAllocs() 141 142 var n, f string 143 var line int 144 145 l := Caller(0) 146 147 for i := 0; i < b.N; i++ { 148 n, f, line = l.nameFileLine() 149 } 150 151 _, _, _ = n, f, line //nolint:dogsled 152 }