Skip to content

Commit c650843

Browse files
feat: add Must and MustV function
1 parent 5042716 commit c650843

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Package testr provides a minimal extension to the standard library's testing.
77
- Minimal assertion API with `Equal`, `ErrorIs`, `ErrorAs`, and `Panic`.
88
- Optional message using `WithMessage`.
99
- Stop the execution using `WithFailNow`.
10-
- Ease of initialization with `Must`.
10+
- Ease of initialization with `Must` and `MustV`.

example_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package testr_test
22

33
import (
4+
"encoding/json"
45
"fmt"
5-
"io"
6-
"strings"
76

87
"github.com/minizilla/testr"
98
)
@@ -88,10 +87,18 @@ func ExampleWithFailNow() {
8887
}
8988

9089
func ExampleMust() {
91-
r := strings.NewReader("testr")
92-
b := testr.Must(io.ReadAll(r))
93-
fmt.Println(string(b))
90+
var v string
91+
testr.Must(json.Unmarshal([]byte(`"testr"`), &v))
92+
fmt.Println(v)
9493

9594
// Output:
9695
// testr
9796
}
97+
98+
func ExampleMustV() {
99+
b := testr.MustV(json.Marshal("testr"))
100+
fmt.Println(string(b))
101+
102+
// Output:
103+
// "testr"
104+
}

testr.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,23 @@ func (assert *Assertion) Panic(f func(), options ...Option) {
9797
f()
9898
}
9999

100-
// Must is a helper that wraps a call to a function returning (T, error)
100+
// Must is a helper that wraps a call to a function returning error
101101
// and panics if the error is non-nil. It is intended for use in variable
102102
// initializations such as
103103
//
104-
// t := testr.Must(template.New("name").Parse("text"))
105-
func Must[T any](v T, err error) T {
104+
// t := testr.MustV(json.Unmarshal(b, &v))
105+
func Must(err error) {
106+
if err != nil {
107+
panic(err)
108+
}
109+
}
110+
111+
// MustV is a helper that wraps a call to a function returning (V, error)
112+
// and panics if the error is non-nil. It is intended for use in variable
113+
// initializations such as
114+
//
115+
// t := testr.MustV(json.Marshal(v))
116+
func MustV[V any](v V, err error) V {
106117
if err != nil {
107118
panic(err)
108119
}

testr_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,12 @@ func TestNilT(t *testing.T) {
353353

354354
func TestMust(t *testing.T) {
355355
assert := testr.New(t)
356-
assert.Equal(testr.Must("ok", nil), "ok")
357-
assert.Panic(func() { testr.Must("panic", errors.New("intentional error")) })
356+
testr.Must(nil)
357+
assert.Panic(func() { testr.Must(errors.New("intentional error")) })
358+
}
359+
360+
func TestMustV(t *testing.T) {
361+
assert := testr.New(t)
362+
assert.Equal(testr.MustV("ok", nil), "ok")
363+
assert.Panic(func() { testr.MustV("panic", errors.New("intentional error")) })
358364
}

0 commit comments

Comments
 (0)