aboutsummaryrefslogtreecommitdiff
path: root/assert/panic_test.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2026-08-18 11:30:54 +0200
committerStefan Majewsky <majewsky@gmx.net>2026-08-18 11:30:54 +0200
commit50ba9cfb04e257fafa3fdd566f9f70a1986339e8 (patch)
treeb4601a46bd2c9b90297e8055bec7d60afc07cdb1 /assert/panic_test.go
parent22e8f3548a72c78deccedc5a65c1cba029f52e6d (diff)
downloadgo-gg-50ba9cfb04e257fafa3fdd566f9f70a1986339e8.tar.gz
assert: add Panics, PanicsWith
I want to use this for test coverage in microprom. This turned out to be a bigger change than expected because testcapture has a dependency on assert, which I had to invert to be able to use testcapture.Capture() in assert.
Diffstat (limited to 'assert/panic_test.go')
-rw-r--r--assert/panic_test.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/assert/panic_test.go b/assert/panic_test.go
new file mode 100644
index 0000000..135ce18
--- /dev/null
+++ b/assert/panic_test.go
@@ -0,0 +1,35 @@
+// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
+// SPDX-License-Identifier: Apache-2.0
+
+package assert_test
+
+import (
+ "testing"
+
+ "go.xyrillian.de/gg/assert"
+ "go.xyrillian.de/gg/testcapture"
+)
+
+func TestPanics(t *testing.T) {
+ //only testing error cases here, coverage of the happy path is provided by
+ //usage of assert.Panics() in actual tests of other packages
+ tc := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
+ assert.Panics(t, func() {
+ // no panic
+ })
+ })
+ assert.Equal(t, tc.Outcome, testcapture.OutcomeFailed)
+ assert.Equal(t, tc.Messages, []testcapture.Message{
+ testcapture.Log("did not panic"),
+ })
+
+ tc = testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
+ assert.PanicsWith[string](t, func() {
+ panic(42)
+ })
+ })
+ assert.Equal(t, tc.Outcome, testcapture.OutcomeFailed)
+ assert.Equal(t, tc.Messages, []testcapture.Message{
+ testcapture.Log("panicked with incorrect type: expected string, but got 42"),
+ })
+}