github.com/nikandfor/loc@v0.5.1/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 } 80 81 func TestCaller(t *testing.T) { 82 a, b := Caller(0), 83 Caller(0) 84 85 // assert.False(t, a == b, "%x == %x", uintptr(a), uintptr(b)) 86 assert.NotEqual(t, a, b) 87 } 88 89 func TestSetCache(t *testing.T) { 90 l := PC(0x1234567890) 91 92 assert.False(t, Cached(l)) 93 94 SetCache(l, "", "", 0) 95 96 assert.False(t, Cached(l)) 97 98 assert.NotEqual(t, "file.go:10", l.String()) 99 100 SetCache(l, "Name", "file.go", 10) 101 102 assert.True(t, Cached(l)) 103 104 assert.Equal(t, "file.go:10", l.String()) 105 106 SetCacheBytes(l, []byte("name"), []byte("file"), 11) 107 108 name, file, line := l.NameFileLine() 109 assert.Equal(t, "name", name) 110 assert.Equal(t, "file", file) 111 assert.Equal(t, 11, line) 112 113 SetCacheBytes(l, nil, nil, 12) 114 115 name, file, line = l.NameFileLine() 116 assert.Equal(t, "", name) 117 assert.Equal(t, "", file) 118 assert.Equal(t, 12, line) 119 120 SetCacheBytes(l, nil, nil, 0) 121 122 assert.False(t, Cached(l)) 123 } 124 125 func BenchmarkLocationCaller(b *testing.B) { 126 b.ReportAllocs() 127 128 var l PC 129 130 for i := 0; i < b.N; i++ { 131 l = Caller(0) 132 } 133 134 _ = l 135 } 136 137 func BenchmarkLocationNameFileLine(b *testing.B) { 138 b.ReportAllocs() 139 140 var n, f string 141 var line int 142 143 l := Caller(0) 144 145 for i := 0; i < b.N; i++ { 146 n, f, line = l.nameFileLine() 147 } 148 149 _, _, _ = n, f, line //nolint:dogsled 150 }