Skip to content

Commit

Permalink
mtest: stub the test interface and add failure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Jan 24, 2024
1 parent 627b084 commit b21e0b7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
11 changes: 8 additions & 3 deletions mtest/mtest.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Package mtest is a support library for writing tests.
package mtest

import "testing"
// TB is the subset of the testing.TB interface used by this package.
type TB interface {
Helper()
Fatal(...any)
Fatalf(string, ...any)
}

// MustPanic executes a function f that is expected to panic.
// If it does so, MustPanic returns the value recovered from the
// panic. Otherwise, it logs a fatal error in t.
func MustPanic(t *testing.T, f func()) (val any) {
func MustPanic(t TB, f func()) (val any) {
t.Helper()
defer func() { val = recover() }()
f()
Expand All @@ -17,7 +22,7 @@ func MustPanic(t *testing.T, f func()) (val any) {
// MustPanicf executes a function f that is expected to panic. If it does so,
// MustPanicf returns the value recovered from the panic. Otherwise it logs a
// fatal error in t.
func MustPanicf(t *testing.T, f func(), msg string, args ...any) (val any) {
func MustPanicf(t TB, f func(), msg string, args ...any) (val any) {
t.Helper()
defer func() { val = recover() }()
f()
Expand Down
60 changes: 56 additions & 4 deletions mtest/mtest_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,69 @@
package mtest_test

import (
"fmt"
"testing"

"github.com/creachadair/mds/mtest"
)

// testStub implements the mtest.TB interface as a capturing shim to verify
// that test failures are reported properly.
type testStub struct {
failed bool
text string
}

func (t *testStub) Fatal(args ...any) {
t.failed = true
t.text = fmt.Sprint(args...)
}

func (t *testStub) Fatalf(msg string, args ...any) {
t.failed = true
t.text = fmt.Sprintf(msg, args...)
}

func (*testStub) Helper() {}

func TestMustPanic(t *testing.T) {
v := mtest.MustPanic(t, func() { panic("pass") })
t.Logf("Panic reported: %v", v)
t.Run("OK", func(t *testing.T) {
v := mtest.MustPanic(t, func() { panic("pass") })
t.Logf("Panic reported: %v", v)
})

t.Run("Fail", func(t *testing.T) {
var s testStub
v := mtest.MustPanic(&s, func() {})
if !s.failed {
t.Error("Test did not fail as expected")
}
if s.text == "" {
t.Error("Failure did not log a message")
}
if v != nil {
t.Errorf("Unexpected panic value: %v", v)
}
})
}

func TestMustPanicf(t *testing.T) {
v := mtest.MustPanicf(t, func() { panic("pass") }, "bad things: %d", 5)
t.Logf("Panic reported: %v", v)
t.Run("OK", func(t *testing.T) {
v := mtest.MustPanicf(t, func() { panic("pass") }, "bad things")
t.Logf("Panic reported: %v", v)
})

t.Run("Fail", func(t *testing.T) {
var s testStub
v := mtest.MustPanicf(&s, func() {}, "bad: %d", 11)
if !s.failed {
t.Error("Test did not fail as expected")
}
if s.text != "bad: 11" {
t.Errorf("Wrong message: got %q, want bad: 11", s.text)
}
if v != nil {
t.Errorf("Unexpected panic value: %v", v)
}
})
}

0 comments on commit b21e0b7

Please sign in to comment.