Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PanicAssertionFunc #1337

Merged
merged 3 commits into from
Mar 5, 2024
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
4 changes: 4 additions & 0 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
36 changes: 36 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down