From 2532759ce1977927b5b364e09a24b79c57e0a20f Mon Sep 17 00:00:00 2001 From: Daniel Bennett Date: Thu, 11 Sep 2025 12:57:18 -0400 Subject: [PATCH] test: add Panic and NotPanic --- examples_test.go | 10 ++++++++++ internal/assertions/assertions.go | 20 ++++++++++++++++++++ must/examples_test.go | 10 ++++++++++ must/must.go | 12 ++++++++++++ test.go | 12 ++++++++++++ 5 files changed, 64 insertions(+) diff --git a/examples_test.go b/examples_test.go index c04cf63..a4b859a 100644 --- a/examples_test.go +++ b/examples_test.go @@ -990,6 +990,16 @@ func ExampleUnreachable() { // Output: } +func ExamplePanic() { + Panic(t, func() { panic("hi mom") }) + // Output: +} + +func ExampleNotPanic() { + NotPanic(t, func() {}) + // Output: +} + func ExampleValidJSON() { js := `{"key": ["v1", "v2"]}` ValidJSON(t, js) diff --git a/internal/assertions/assertions.go b/internal/assertions/assertions.go index d6254e1..f1b5bd5 100644 --- a/internal/assertions/assertions.go +++ b/internal/assertions/assertions.go @@ -142,6 +142,26 @@ func Unreachable() (s string) { return } +func Panic(f func()) (s string) { + defer func() { + if r := recover(); r == nil { + s = "expected panic; did not panic" + } + }() + f() + return +} + +func NotPanic(f func()) (s string) { + defer func() { + if r := recover(); r != nil { + s = fmt.Sprintf("expected not to panic; panicked: %v", r) + } + }() + f() + return +} + func Error(err error) (s string) { if err == nil { s = "expected non-nil error; got nil\n" diff --git a/must/examples_test.go b/must/examples_test.go index 98c28f1..14ff954 100644 --- a/must/examples_test.go +++ b/must/examples_test.go @@ -992,6 +992,16 @@ func ExampleUnreachable() { // Output: } +func ExamplePanic() { + Panic(t, func() { panic("hi mom") }) + // Output: +} + +func ExampleNotPanic() { + NotPanic(t, func() {}) + // Output: +} + func ExampleValidJSON() { js := `{"key": ["v1", "v2"]}` ValidJSON(t, js) diff --git a/must/must.go b/must/must.go index 491977d..1163246 100644 --- a/must/must.go +++ b/must/must.go @@ -51,6 +51,18 @@ func Unreachable(t T, settings ...Setting) { invoke(t, assertions.Unreachable(), settings...) } +// Panic asserts func f panics. +func Panic(t T, f func(), settings ...Setting) { + t.Helper() + invoke(t, assertions.Panic(f), settings...) +} + +// NotPanic asserts func f does not panic. +func NotPanic(t T, f func(), settings ...Setting) { + t.Helper() + invoke(t, assertions.NotPanic(f), settings...) +} + // Error asserts err is a non-nil error. func Error(t T, err error, settings ...Setting) { t.Helper() diff --git a/test.go b/test.go index 050d617..e0a0b81 100644 --- a/test.go +++ b/test.go @@ -49,6 +49,18 @@ func Unreachable(t T, settings ...Setting) { invoke(t, assertions.Unreachable(), settings...) } +// Panic asserts func f panics. +func Panic(t T, f func(), settings ...Setting) { + t.Helper() + invoke(t, assertions.Panic(f), settings...) +} + +// NotPanic asserts func f does not panic. +func NotPanic(t T, f func(), settings ...Setting) { + t.Helper() + invoke(t, assertions.NotPanic(f), settings...) +} + // Error asserts err is a non-nil error. func Error(t T, err error, settings ...Setting) { t.Helper()