summaryrefslogtreecommitdiff
path: root/assert
diff options
context:
space:
mode:
Diffstat (limited to 'assert')
-rw-r--r--assert/assert.go37
-rw-r--r--assert/errequal_test.go8
-rw-r--r--assert/panic.go32
-rw-r--r--assert/panic_test.go35
4 files changed, 73 insertions, 39 deletions
diff --git a/assert/assert.go b/assert/assert.go
index a13c15e..fe725f6 100644
--- a/assert/assert.go
+++ b/assert/assert.go
@@ -5,38 +5,9 @@
// Each assertion in this package returns a bool to indicate whether the check succeeded, and logs a t.Error() when the check does not succeed.
package assert
-import (
- "context"
- "io"
- "testing"
-)
+import "go.xyrillian.de/gg/testcapture"
// TestingTB contains all the public functions of [testing.TB] (as of Go 1.26).
-// Functions in this package use this type instead of [testing.TB] because the capture device used by package testcapture cannot implement [testing.TB]: It contains methods that are private to the standard library.
-type TestingTB interface {
- ArtifactDir() string
- Attr(key, value string)
- Chdir(dir string)
- Cleanup(func())
- Context() context.Context
- Error(args ...any)
- Errorf(format string, args ...any)
- Fail()
- Failed() bool
- FailNow()
- Fatal(args ...any)
- Fatalf(format string, args ...any)
- Helper()
- Log(args ...any)
- Logf(format string, args ...any)
- Name() string
- Output() io.Writer
- Setenv(key, value string)
- Skip(args ...any)
- Skipf(format string, args ...any)
- SkipNow()
- Skipped() bool
- TempDir() string
-}
-
-var _ TestingTB = testing.TB(nil)
+// Functions in this package use this type instead of [testing.TB] to allow
+// mocks of [testing.TB] to be substituted in tests for this package.
+type TestingTB = testcapture.TestingTB
diff --git a/assert/errequal_test.go b/assert/errequal_test.go
index b97342a..2cefeab 100644
--- a/assert/errequal_test.go
+++ b/assert/errequal_test.go
@@ -11,7 +11,6 @@ import (
"testing"
"go.xyrillian.de/gg/assert"
- "go.xyrillian.de/gg/testcapture"
)
func TestErrEqual(t *testing.T) {
@@ -67,13 +66,10 @@ func TestErrEqual(t *testing.T) {
}, `expected an error matching /foo/, but got no error`)
// test matching against unexpected type
- result := testcapture.Capture(t.Context(), t.Name(), func(t assert.TestingTB) {
+ result := assert.PanicsWith[string](t, func() {
assert.ErrEqual(t, errors.New("42"), 42)
})
- assert.Equal(t, result, testcapture.Result{
- Outcome: testcapture.OutcomePanicked,
- Panic: "cannot handle `expected` of type int",
- })
+ assert.Equal(t, result, "cannot handle `expected` of type int")
// an earlier version had a bug because this call caused reflect.Value.IsNil() to be called on a value of kind Struct
expectErrors(t, func(t assert.TestingTB) {
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
+}
diff --git a/assert/panic_test.go b/assert/panic_test.go
new file mode 100644
index 0000000..a3d6877
--- /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 int: 42"),
+ })
+}