From 4b296f11d2776de7fb0a83b751718003c5e033b1 Mon Sep 17 00:00:00 2001 From: Jeremy Bowers Date: Thu, 25 Jul 2024 18:57:49 -0400 Subject: [PATCH] Declare a minimal interface for just what portions of the test interface we use. --- README.md | 3 +++ slogassert.go | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7b90b1c..282285a 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,9 @@ one is signed.) ## Version History +* v0.3.4: + * Create a custom interface for just the components of testing.TB + * slogassert actually uses. * v0.3.3: * Export LogMessageMatch.Matches for external use. * Add utility function for testing. diff --git a/slogassert.go b/slogassert.go index 1558526..f9d107f 100644 --- a/slogassert.go +++ b/slogassert.go @@ -37,7 +37,6 @@ import ( "sort" "strings" "sync" - "testing" "time" ) @@ -60,7 +59,19 @@ type Handler struct { m sync.Mutex logMessages []LogMessage - t testing.TB + t Tester +} + +// The Tester interface defines the incoming testing interface. +// +// The standard library *testing.T and *testing.B values conform to this +// already. +// +// If your testing library doesn't have an equivalent of Helper, it is fine +// to implement it as a no-op. +type Tester interface { + Helper() + Fatalf(string, ...any) } // New creates a new testing logger, logging with the given level. @@ -70,7 +81,7 @@ type Handler struct { // // It is recommended to generally call defer handler.AssertEmpty() on // the result of this call. -func New(t testing.TB, leveler slog.Leveler, wrapped slog.Handler) *Handler { +func New(t Tester, leveler slog.Leveler, wrapped slog.Handler) *Handler { if t == nil { panic("t must not be nil for a slogtest.Handler") }