github.com/lingyao2333/mo-zero@v1.4.1/core/logx/util_test.go (about) 1 package logx 2 3 import ( 4 "path/filepath" 5 "runtime" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestGetCaller(t *testing.T) { 13 _, file, _, _ := runtime.Caller(0) 14 assert.Contains(t, getCaller(1), filepath.Base(file)) 15 assert.True(t, len(getCaller(1<<10)) == 0) 16 } 17 18 func TestGetTimestamp(t *testing.T) { 19 ts := getTimestamp() 20 tm, err := time.Parse(timeFormat, ts) 21 assert.Nil(t, err) 22 assert.True(t, time.Since(tm) < time.Minute) 23 } 24 25 func TestPrettyCaller(t *testing.T) { 26 tests := []struct { 27 name string 28 file string 29 line int 30 want string 31 }{ 32 { 33 name: "regular", 34 file: "logx_test.go", 35 line: 123, 36 want: "logx_test.go:123", 37 }, 38 { 39 name: "relative", 40 file: "adhoc/logx_test.go", 41 line: 123, 42 want: "adhoc/logx_test.go:123", 43 }, 44 { 45 name: "long path", 46 file: "github.com/lingyao2333/mo-zero/core/logx/util_test.go", 47 line: 12, 48 want: "logx/util_test.go:12", 49 }, 50 { 51 name: "local path", 52 file: "/Users/kevin/go-zero/core/logx/util_test.go", 53 line: 1234, 54 want: "logx/util_test.go:1234", 55 }, 56 } 57 58 for _, test := range tests { 59 test := test 60 t.Run(test.name, func(t *testing.T) { 61 assert.Equal(t, test.want, prettyCaller(test.file, test.line)) 62 }) 63 } 64 } 65 66 func BenchmarkGetCaller(b *testing.B) { 67 b.ReportAllocs() 68 69 for i := 0; i < b.N; i++ { 70 getCaller(1) 71 } 72 }