Merge pull request #176 from petergtz/master
Print panic object when using ShouldNot(Panic())
diff --git a/matchers/panic_matcher.go b/matchers/panic_matcher.go
index 75ab251..640f4db 100644
--- a/matchers/panic_matcher.go
+++ b/matchers/panic_matcher.go
@@ -2,11 +2,14 @@
import (
"fmt"
- "github.com/onsi/gomega/format"
"reflect"
+
+ "github.com/onsi/gomega/format"
)
-type PanicMatcher struct{}
+type PanicMatcher struct {
+ object interface{}
+}
func (matcher *PanicMatcher) Match(actual interface{}) (success bool, err error) {
if actual == nil {
@@ -24,6 +27,7 @@
success = false
defer func() {
if e := recover(); e != nil {
+ matcher.object = e
success = true
}
}()
@@ -38,5 +42,5 @@
}
func (matcher *PanicMatcher) NegatedFailureMessage(actual interface{}) (message string) {
- return format.Message(actual, "not to panic")
+ return format.Message(actual, fmt.Sprintf("not to panic, but panicked with\n%s", format.Object(matcher.object, 1)))
}
diff --git a/matchers/panic_matcher_test.go b/matchers/panic_matcher_test.go
index 17f3935..6b859a7 100644
--- a/matchers/panic_matcher_test.go
+++ b/matchers/panic_matcher_test.go
@@ -33,4 +33,13 @@
Ω(func() {}).ShouldNot(Panic())
})
})
+
+ Context("when assertion fails", func() {
+ It("should print the object passed to Panic", func() {
+ failuresMessages := InterceptGomegaFailures(func() {
+ Ω(func() { panic("ack!") }).ShouldNot(Panic())
+ })
+ Ω(failuresMessages).Should(ConsistOf(MatchRegexp("not to panic, but panicked with\\s*<string>: ack!")))
+ })
+ })
})