Skip to content

Commit b5a0821

Browse files
Merge pull request #1400 from olivergondza/issue-1399
assert.PanicsWithError: report error message
2 parents cbd8483 + a87733a commit b5a0821

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

assert/assertions.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,9 +1343,15 @@ func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs .
13431343
if !funcDidPanic {
13441344
return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...)
13451345
}
1346-
panicErr, ok := panicValue.(error)
1347-
if !ok || panicErr.Error() != errString {
1348-
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...)
1346+
panicErr, isError := panicValue.(error)
1347+
if !isError || panicErr.Error() != errString {
1348+
msg := fmt.Sprintf("func %#v should panic with error message:\t%#v\n", f, errString)
1349+
if isError {
1350+
msg += fmt.Sprintf("\tError message:\t%#v\n", panicErr.Error())
1351+
}
1352+
msg += fmt.Sprintf("\tPanic value:\t%#v\n", panicValue)
1353+
msg += fmt.Sprintf("\tPanic stack:\t%s\n", panickedStack)
1354+
return Fail(t, msg, msgAndArgs...)
13491355
}
13501356

13511357
return true

assert/assertions_test.go

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,30 +1587,43 @@ func TestPanicsWithValue(t *testing.T) {
15871587
func TestPanicsWithError(t *testing.T) {
15881588
t.Parallel()
15891589

1590-
mockT := new(testing.T)
1591-
1592-
if !PanicsWithError(mockT, "panic", func() {
1590+
mockT := new(captureTestingT)
1591+
succeeded := PanicsWithError(mockT, "panic", func() {
15931592
panic(errors.New("panic"))
1594-
}) {
1595-
t.Error("PanicsWithError should return true")
1596-
}
1593+
})
1594+
mockT.checkResultAndErrMsg(t, true, succeeded, "")
15971595

1598-
if PanicsWithError(mockT, "Panic!", func() {
1599-
}) {
1600-
t.Error("PanicsWithError should return false")
1601-
}
1596+
succeeded = PanicsWithError(mockT, "Panic!", func() {})
1597+
Equal(t, false, succeeded, "PanicsWithError should return false")
1598+
Contains(t, mockT.msg, "Panic value:\t<nil>")
16021599

1603-
if PanicsWithError(mockT, "at the disco", func() {
1604-
panic(errors.New("panic"))
1605-
}) {
1606-
t.Error("PanicsWithError should return false")
1607-
}
1600+
succeeded = PanicsWithError(mockT, "expected panic err msg", func() {
1601+
panic(errors.New("actual panic err msg"))
1602+
})
1603+
Equal(t, false, succeeded, "PanicsWithError should return false")
1604+
Contains(t, mockT.msg, `Error message: "actual panic err msg"`)
16081605

1609-
if PanicsWithError(mockT, "Panic!", func() {
1610-
panic("panic")
1611-
}) {
1612-
t.Error("PanicsWithError should return false")
1613-
}
1606+
succeeded = PanicsWithError(mockT, "expected panic err msg", func() {
1607+
panic(&PanicsWithErrorWrapper{"wrapped", errors.New("actual panic err msg")})
1608+
})
1609+
Equal(t, false, succeeded, "PanicsWithError should return false")
1610+
Contains(t, mockT.msg, `Error message: "wrapped: actual panic err msg"`)
1611+
1612+
succeeded = PanicsWithError(mockT, "expected panic msg", func() {
1613+
panic("actual panic msg")
1614+
})
1615+
Equal(t, false, succeeded, "PanicsWithError should return false")
1616+
Contains(t, mockT.msg, `Panic value: "actual panic msg"`)
1617+
NotContains(t, mockT.msg, "Error message:", "PanicsWithError should not report error message if not due an error")
1618+
}
1619+
1620+
type PanicsWithErrorWrapper struct {
1621+
Prefix string
1622+
Err error
1623+
}
1624+
1625+
func (e PanicsWithErrorWrapper) Error() string {
1626+
return e.Prefix + ": " + e.Err.Error()
16141627
}
16151628

16161629
func TestNotPanics(t *testing.T) {

0 commit comments

Comments
 (0)