-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mtest: stub the test interface and add failure tests
- Loading branch information
1 parent
627b084
commit b21e0b7
Showing
2 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |