Add PanicAssertionFunc (#1337, #730)

Add a `PanicAssertionFunc` to ease writing table-driven tests for panic
assertion.

Closes #730
pull/1567/head
Fahim Bagar 2024-03-05 16:13:30 +07:00 committed by GitHub
parent bb548d0473
commit 8585d8de96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 0 deletions

View File

@ -45,6 +45,10 @@ type BoolAssertionFunc func(TestingT, bool, ...interface{}) bool
// for table driven tests.
type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool
// PanicAssertionFunc is a common function prototype when validating a panic value. Can be useful
// for table driven tests.
type PanicAssertionFunc = func(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool
// Comparison is a custom function that returns true on success and false on failure
type Comparison func() (success bool)

View File

@ -2789,6 +2789,42 @@ func TestErrorAssertionFunc(t *testing.T) {
}
}
func ExamplePanicAssertionFunc() {
t := &testing.T{} // provided by test
tests := []struct {
name string
panicFn PanicTestFunc
assertion PanicAssertionFunc
}{
{"with panic", func() { panic(nil) }, Panics},
{"without panic", func() {}, NotPanics},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.assertion(t, tt.panicFn)
})
}
}
func TestPanicAssertionFunc(t *testing.T) {
tests := []struct {
name string
panicFn PanicTestFunc
assertion PanicAssertionFunc
}{
{"not panic", func() {}, NotPanics},
{"panic", func() { panic(nil) }, Panics},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.assertion(t, tt.panicFn)
})
}
}
func TestEventuallyFalse(t *testing.T) {
mockT := new(testing.T)