Skip to content

Commit

Permalink
Declare a minimal interface for just what portions of the test interf…
Browse files Browse the repository at this point in the history
…ace we use.
  • Loading branch information
thejerf committed Jul 25, 2024
1 parent e018d77 commit 4b296f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 14 additions & 3 deletions slogassert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"sort"
"strings"
"sync"
"testing"
"time"
)

Expand All @@ -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.
Expand All @@ -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")
}
Expand Down

0 comments on commit 4b296f1

Please sign in to comment.