summaryrefslogtreecommitdiff
path: root/assert/panic.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-08-18 13:40:30 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-08-18 13:40:30 +0200
commit5cc0f9db288ffaf44d104974707338d52530cece (patch)
treef0ad530dd072c55c6f46dfea23af993fb31333d4 /assert/panic.go
parentab5012e0083ff6623ffab78f52f9ea1120d80ffb (diff)
parenta3ccdd9f491c20771cfda343ebebd4b5d0ea6602 (diff)
downloadgo-gg-5cc0f9db288ffaf44d104974707338d52530cece.tar.gz
Merge branch 'microprom'
Diffstat (limited to 'assert/panic.go')
-rw-r--r--assert/panic.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/assert/panic.go b/assert/panic.go
new file mode 100644
index 0000000..e19b815
--- /dev/null
+++ b/assert/panic.go
@@ -0,0 +1,32 @@
+// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
+// SPDX-License-Identifier: Apache-2.0
+
+package assert
+
+import (
+ "go.xyrillian.de/gg/testcapture"
+)
+
+// Panics runs the provided action and fails the test if it does not panic.
+// On success, the error value passed to the call of panic is returned.
+func Panics(t TestingTB, action func()) any {
+ return PanicsWith[any](t, action)
+}
+
+// PanicsWith is like Panics(), but also checks if the recovered error value
+// is of type T, failing the test if the type assertion fails.
+func PanicsWith[T any](t TestingTB, action func()) T {
+ t.Helper()
+ result := testcapture.Capture(t.Context(), t.Name(), func(_ TestingTB) {
+ action()
+ })
+ if result.Outcome != testcapture.OutcomePanicked {
+ t.Fatal("did not panic")
+ }
+ value, ok := result.Panic.(T)
+ if !ok {
+ var zero T
+ t.Fatalf("panicked with incorrect type: expected %T, but got %T: %#v", zero, result.Panic, result.Panic)
+ }
+ return value
+}