github.com/haraldrudell/parl@v0.4.176/perrors/is-panic_test.go (about) 1 /* 2 © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package perrors 7 8 import ( 9 "testing" 10 11 "github.com/haraldrudell/parl/pruntime" 12 ) 13 14 func TestIsPanic(t *testing.T) { 15 message := "message" 16 err := New(message) 17 18 var isPanic bool 19 var stack pruntime.Stack 20 var recoveryIndex int 21 var panicIndex int 22 errPanic := func() (err error) { 23 defer func() { 24 recover() 25 err = New("panic") 26 }() 27 panic(1) 28 }() 29 30 isPanic, _, _, _ = IsPanic(err) 31 if isPanic { 32 t.Error("non-panic error has isPanic true") 33 } 34 35 isPanic, stack, recoveryIndex, panicIndex = IsPanic(errPanic) 36 var frames = stack.Frames() 37 t.Logf("isPanic: %t stack: %d recoveryIndex: %d panicIndex: %d", 38 isPanic, len(frames), recoveryIndex, panicIndex) 39 t.Logf("\nstack:%s", stack) 40 if !isPanic { 41 t.Error("panic error has isPanic false") 42 } 43 if len(frames) == 0 { 44 t.Error("stack length zero exp non-zero") 45 } 46 // recoveryIndex cn be 0 47 if panicIndex == 0 { 48 t.Error("panicIndex zero exp non-zero") 49 } 50 }