mirror of
https://github.com/stretchr/testify.git
synced 2025-07-15 00:29:53 +00:00
Fixed didPanic to now detect panic(nil).
Previously, the function would not detect panic(nil) calls. In didPanic, removed the anonymous function call, instead, added named return values. Added extra test cases for the panic(nil) call.
This commit is contained in:
parent
1e36bfe104
commit
083ff1c044
@ -1004,27 +1004,21 @@ func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool {
|
||||
type PanicTestFunc func()
|
||||
|
||||
// didPanic returns true if the function passed to it panics. Otherwise, it returns false.
|
||||
func didPanic(f PanicTestFunc) (bool, interface{}, string) {
|
||||
|
||||
didPanic := false
|
||||
var message interface{}
|
||||
var stack string
|
||||
func() {
|
||||
|
||||
defer func() {
|
||||
if message = recover(); message != nil {
|
||||
didPanic = true
|
||||
stack = string(debug.Stack())
|
||||
}
|
||||
}()
|
||||
|
||||
// call the target function
|
||||
f()
|
||||
func didPanic(f PanicTestFunc) (didPanic bool, message interface{}, stack string) {
|
||||
didPanic = true
|
||||
|
||||
defer func() {
|
||||
message = recover()
|
||||
if didPanic {
|
||||
stack = string(debug.Stack())
|
||||
}
|
||||
}()
|
||||
|
||||
return didPanic, message, stack
|
||||
// call the target function
|
||||
f()
|
||||
didPanic = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Panics asserts that the code inside the specified PanicTestFunc panics.
|
||||
|
@ -923,10 +923,18 @@ func TestCondition(t *testing.T) {
|
||||
|
||||
func TestDidPanic(t *testing.T) {
|
||||
|
||||
if funcDidPanic, _, _ := didPanic(func() {
|
||||
panic("Panic!")
|
||||
}); !funcDidPanic {
|
||||
t.Error("didPanic should return true")
|
||||
const panicMsg = "Panic!"
|
||||
|
||||
if funcDidPanic, msg, _ := didPanic(func() {
|
||||
panic(panicMsg)
|
||||
}); !funcDidPanic || msg != panicMsg {
|
||||
t.Error("didPanic should return true, panicMsg")
|
||||
}
|
||||
|
||||
if funcDidPanic, msg, _ := didPanic(func() {
|
||||
panic(nil)
|
||||
}); !funcDidPanic || msg != nil {
|
||||
t.Error("didPanic should return true, nil")
|
||||
}
|
||||
|
||||
if funcDidPanic, _, _ := didPanic(func() {
|
||||
@ -963,6 +971,12 @@ func TestPanicsWithValue(t *testing.T) {
|
||||
t.Error("PanicsWithValue should return true")
|
||||
}
|
||||
|
||||
if !PanicsWithValue(mockT, nil, func() {
|
||||
panic(nil)
|
||||
}) {
|
||||
t.Error("PanicsWithValue should return true")
|
||||
}
|
||||
|
||||
if PanicsWithValue(mockT, "Panic!", func() {
|
||||
}) {
|
||||
t.Error("PanicsWithValue should return false")
|
||||
|
Loading…
x
Reference in New Issue
Block a user