aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/gomega/matchers/panic_matcher.go
blob: adc8cee630659cd45fb61410ece158afbf3f22ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package matchers

import (
	"fmt"
	"reflect"

	"github.com/onsi/gomega/format"
)

type PanicMatcher struct {
	Expected interface{}
	object   interface{}
}

func (matcher *PanicMatcher) Match(actual interface{}) (success bool, err error) {
	if actual == nil {
		return false, fmt.Errorf("PanicMatcher expects a non-nil actual.")
	}

	actualType := reflect.TypeOf(actual)
	if actualType.Kind() != reflect.Func {
		return false, fmt.Errorf("PanicMatcher expects a function.  Got:\n%s", format.Object(actual, 1))
	}
	if !(actualType.NumIn() == 0 && actualType.NumOut() == 0) {
		return false, fmt.Errorf("PanicMatcher expects a function with no arguments and no return value.  Got:\n%s", format.Object(actual, 1))
	}

	success = false
	defer func() {
		if e := recover(); e != nil {
			matcher.object = e

			if matcher.Expected == nil {
				success = true
				return
			}

			valueMatcher, valueIsMatcher := matcher.Expected.(omegaMatcher)
			if !valueIsMatcher {
				valueMatcher = &EqualMatcher{Expected: matcher.Expected}
			}

			success, err = valueMatcher.Match(e)
			if err != nil {
				err = fmt.Errorf("PanicMatcher's value matcher failed with:\n%s%s", format.Indent, err.Error())
			}
		}
	}()

	reflect.ValueOf(actual).Call([]reflect.Value{})

	return
}

func (matcher *PanicMatcher) FailureMessage(actual interface{}) (message string) {
	if matcher.Expected == nil {
		// We wanted any panic to occur, but none did.
		return format.Message(actual, "to panic")
	}

	if matcher.object == nil {
		// We wanted a panic with a specific value to occur, but none did.
		switch matcher.Expected.(type) {
		case omegaMatcher:
			return format.Message(actual, "to panic with a value matching", matcher.Expected)
		default:
			return format.Message(actual, "to panic with", matcher.Expected)
		}
	}

	// We got a panic, but the value isn't what we expected.
	switch matcher.Expected.(type) {
	case omegaMatcher:
		return format.Message(
			actual,
			fmt.Sprintf(
				"to panic with a value matching\n%s\nbut panicked with\n%s",
				format.Object(matcher.Expected, 1),
				format.Object(matcher.object, 1),
			),
		)
	default:
		return format.Message(
			actual,
			fmt.Sprintf(
				"to panic with\n%s\nbut panicked with\n%s",
				format.Object(matcher.Expected, 1),
				format.Object(matcher.object, 1),
			),
		)
	}
}

func (matcher *PanicMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	if matcher.Expected == nil {
		// We didn't want any panic to occur, but one did.
		return format.Message(actual, fmt.Sprintf("not to panic, but panicked with\n%s", format.Object(matcher.object, 1)))
	}

	// We wanted a to ensure a panic with a specific value did not occur, but it did.
	switch matcher.Expected.(type) {
	case omegaMatcher:
		return format.Message(
			actual,
			fmt.Sprintf(
				"not to panic with a value matching\n%s\nbut panicked with\n%s",
				format.Object(matcher.Expected, 1),
				format.Object(matcher.object, 1),
			),
		)
	default:
		return format.Message(actual, "not to panic with", matcher.Expected)
	}
}