Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1343,9 +1343,15 @@ func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs .
if !funcDidPanic {
return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...)
}
panicErr, ok := panicValue.(error)
if !ok || panicErr.Error() != errString {
return Fail(t, fmt.Sprintf("func %#v should panic with error message:\t%#v\n\tPanic value:\t%#v\n\tPanic stack:\t%s", f, errString, panicValue, panickedStack), msgAndArgs...)
panicErr, isError := panicValue.(error)
if !isError || panicErr.Error() != errString {
msg := fmt.Sprintf("func %#v should panic with error message:\t%#v\n", f, errString)
if isError {
msg += fmt.Sprintf("\tError message:\t%#v\n", panicErr.Error())
}
msg += fmt.Sprintf("\tPanic value:\t%#v\n", panicValue)
msg += fmt.Sprintf("\tPanic stack:\t%s\n", panickedStack)
return Fail(t, msg, msgAndArgs...)
}

return true
Expand Down
53 changes: 33 additions & 20 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1587,30 +1587,43 @@ func TestPanicsWithValue(t *testing.T) {
func TestPanicsWithError(t *testing.T) {
t.Parallel()

mockT := new(testing.T)

if !PanicsWithError(mockT, "panic", func() {
mockT := new(captureTestingT)
succeeded := PanicsWithError(mockT, "panic", func() {
panic(errors.New("panic"))
}) {
t.Error("PanicsWithError should return true")
}
})
mockT.checkResultAndErrMsg(t, true, succeeded, "")

if PanicsWithError(mockT, "Panic!", func() {
}) {
t.Error("PanicsWithError should return false")
}
succeeded = PanicsWithError(mockT, "Panic!", func() {})
Equal(t, false, succeeded, "PanicsWithError should return false")
Contains(t, mockT.msg, "Panic value:\t<nil>")

if PanicsWithError(mockT, "at the disco", func() {
panic(errors.New("panic"))
}) {
t.Error("PanicsWithError should return false")
}
succeeded = PanicsWithError(mockT, "expected panic err msg", func() {
panic(errors.New("actual panic err msg"))
})
Equal(t, false, succeeded, "PanicsWithError should return false")
Contains(t, mockT.msg, `Error message: "actual panic err msg"`)

if PanicsWithError(mockT, "Panic!", func() {
panic("panic")
}) {
t.Error("PanicsWithError should return false")
}
succeeded = PanicsWithError(mockT, "expected panic err msg", func() {
panic(&PanicsWithErrorWrapper{"wrapped", errors.New("actual panic err msg")})
})
Equal(t, false, succeeded, "PanicsWithError should return false")
Contains(t, mockT.msg, `Error message: "wrapped: actual panic err msg"`)

succeeded = PanicsWithError(mockT, "expected panic msg", func() {
panic("actual panic msg")
})
Equal(t, false, succeeded, "PanicsWithError should return false")
Contains(t, mockT.msg, `Panic value: "actual panic msg"`)
NotContains(t, mockT.msg, "Error message:", "PanicsWithError should not report error message if not due an error")
}

type PanicsWithErrorWrapper struct {
Prefix string
Err error
}

func (e PanicsWithErrorWrapper) Error() string {
return e.Prefix + ": " + e.Err.Error()
}

func TestNotPanics(t *testing.T) {
Expand Down