initial commit
[govpp.git] / vendor / github.com / onsi / gomega / matchers / panic_matcher.go
1 package matchers
2
3 import (
4         "fmt"
5         "reflect"
6
7         "github.com/onsi/gomega/format"
8 )
9
10 type PanicMatcher struct {
11         object interface{}
12 }
13
14 func (matcher *PanicMatcher) Match(actual interface{}) (success bool, err error) {
15         if actual == nil {
16                 return false, fmt.Errorf("PanicMatcher expects a non-nil actual.")
17         }
18
19         actualType := reflect.TypeOf(actual)
20         if actualType.Kind() != reflect.Func {
21                 return false, fmt.Errorf("PanicMatcher expects a function.  Got:\n%s", format.Object(actual, 1))
22         }
23         if !(actualType.NumIn() == 0 && actualType.NumOut() == 0) {
24                 return false, fmt.Errorf("PanicMatcher expects a function with no arguments and no return value.  Got:\n%s", format.Object(actual, 1))
25         }
26
27         success = false
28         defer func() {
29                 if e := recover(); e != nil {
30                         matcher.object = e
31                         success = true
32                 }
33         }()
34
35         reflect.ValueOf(actual).Call([]reflect.Value{})
36
37         return
38 }
39
40 func (matcher *PanicMatcher) FailureMessage(actual interface{}) (message string) {
41         return format.Message(actual, "to panic")
42 }
43
44 func (matcher *PanicMatcher) NegatedFailureMessage(actual interface{}) (message string) {
45         return format.Message(actual, fmt.Sprintf("not to panic, but panicked with\n%s", format.Object(matcher.object, 1)))
46 }