OSDN Git Service

test (#52)
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / mat / errors_test.go
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         for i, test := range []struct {
20                 fn     func()
21                 panics bool
22                 errors bool
23         }{
24                 {
25                         fn:     func() {},
26                         panics: false,
27                         errors: false,
28                 },
29                 {
30                         fn:     func() { panic("panic") },
31                         panics: true,
32                         errors: false,
33                 },
34                 {
35                         fn:     func() { panic(Error{"panic"}) },
36                         panics: false,
37                         errors: true,
38                 },
39         } {
40                 panicked := leaksPanic(test.fn)
41                 if panicked != test.panics {
42                         t.Errorf("unexpected panic state for test %d: got: panicked=%t want: panicked=%t",
43                                 i, panicked, test.panics)
44                 }
45                 if test.errors {
46                         err := Maybe(test.fn)
47                         stack, ok := err.(ErrorStack)
48                         if !ok {
49                                 t.Errorf("unexpected error type: got:%T want:%T", stack, ErrorStack{})
50                         }
51                         if stack.StackTrace == "" {
52                                 t.Error("expected non-empty stack trace")
53                         }
54                 }
55         }
56 }