github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekasys/stacktrace_test.go (about) 1 // Copyright © 2019. All rights reserved. 2 // Author: Ilya Stroy. 3 // Contacts: iyuryevich@pm.me, https://github.com/qioalice 4 // License: https://opensource.org/licenses/MIT 5 6 package ekasys_test 7 8 import ( 9 "fmt" 10 "testing" 11 12 "github.com/qioalice/ekago/v3/ekasys" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 // Test GetStackTrace with 'skip' == 0, 'depth' == 1, 18 // tests: 19 // - GetStackTrace returns slice with len == 1 (as depth) 20 // - frame.Function contains current test name 21 func TestGetStackTraceCommonDepth1(t *testing.T) { 22 23 frames := ekasys.GetStackTrace(0, 1) 24 25 assert.Len(t, frames, 1, "invalid len of frames") 26 assert.Contains(t, frames[0].Function, "TestGetStackTraceCommonDepth1", 27 "wrong function name") 28 } 29 30 // Test GetStackTrace with 'skip' == -3 (include hidden frames), 31 // 'depth' == -1 (full depth) tests: 32 // - GetStackTrace returns slice with len >= 3 33 // (at least hidden frames were included to the output) 34 // - first three returned frames have valid function names 35 func TestGetStackTraceCommonDepthAbsolutelyFull(t *testing.T) { 36 37 frames := ekasys.GetStackTrace(-3, -1) 38 39 assert.True(t, len(frames) >= 3, "invalid len of frames") 40 41 funcNames := []string{ 42 "runtime.Callers", "getStackFramePoints", "GetStackTrace", 43 } 44 45 for i := 0; i < len(funcNames) && i < len(frames); i++ { 46 assert.Contains(t, frames[i].Function, funcNames[i], 47 "wrong function name") 48 } 49 50 frames.Print(nil) 51 } 52 53 type T struct{} 54 55 func (T) foo() ekasys.StackFrame { 56 return ekasys.GetStackTrace(0, 1)[0] 57 } 58 59 // TestStackFrame_DoFormat just see what StackFrame.DoFormat generates. 60 func TestStackFrame_DoFormat(t *testing.T) { 61 62 frame := ekasys.GetStackTrace(0, 1)[0] 63 fmt.Println(frame.DoFormat()) 64 65 frame = new(T).foo() 66 fmt.Println(frame.DoFormat()) 67 } 68 69 // Bench StackFrame.doFormat func (generating readable output of stack frame). 70 func BenchmarkStackFrame_DoFormat(b *testing.B) { 71 72 b.ReportAllocs() 73 b.StopTimer() 74 75 frame := ekasys.GetStackTrace(0, 1)[0] 76 77 b.StartTimer() 78 for i := 0; i < b.N; i++ { 79 frame.Format = "" 80 frame.DoFormat() 81 } 82 }