From 50ba9cfb04e257fafa3fdd566f9f70a1986339e8 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 18 Aug 2026 11:30:54 +0200 Subject: 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. --- assert/panic_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 assert/panic_test.go (limited to 'assert/panic_test.go') 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 +// 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"), + }) +} -- cgit v1.3.1