github.com/gopherd/gonum@v0.0.4/mat/errors_test.go (about) 1 // Copyright ©2013 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mat 6 7 import "testing" 8 9 func leaksPanic(fn func()) (panicked bool) { 10 defer func() { 11 r := recover() 12 panicked = r != nil 13 }() 14 _ = Maybe(fn) 15 return 16 } 17 18 func TestMaybe(t *testing.T) { 19 t.Parallel() 20 for i, test := range []struct { 21 fn func() 22 panics bool 23 errors bool 24 }{ 25 { 26 fn: func() {}, 27 panics: false, 28 errors: false, 29 }, 30 { 31 fn: func() { panic("panic") }, 32 panics: true, 33 errors: false, 34 }, 35 { 36 fn: func() { panic(Error{"panic"}) }, 37 panics: false, 38 errors: true, 39 }, 40 } { 41 panicked := leaksPanic(test.fn) 42 if panicked != test.panics { 43 t.Errorf("unexpected panic state for test %d: got: panicked=%t want: panicked=%t", 44 i, panicked, test.panics) 45 } 46 if test.errors { 47 err := Maybe(test.fn) 48 stack, ok := err.(ErrorStack) 49 if !ok { 50 t.Errorf("unexpected error type: got:%T want:%T", stack, ErrorStack{}) 51 } 52 if stack.StackTrace == "" { 53 t.Error("expected non-empty stack trace") 54 } 55 } 56 } 57 }