Skip to content

Commit e292cbb

Browse files
feat: add Must4, add tests
1 parent d6eca30 commit e292cbb

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

must/must.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ func Must1(err error) {
88
}
99
}
1010

11-
// Must panics if err is not nil and otherwise returns value.
11+
// Must panics if err is not nil and otherwise returns v.
1212
//
1313
// It is named as Must without a number suffix cause functions with
1414
// two parameters are most common.
15-
func Must[T any](value T, err error) T {
15+
func Must[T any](v T, err error) T {
1616
if err != nil {
1717
panic(err)
1818
}
1919

20-
return value
20+
return v
2121
}
2222

23-
// Must3 panics if err is not nil, otherwise returns value1 and value2.
24-
func Must3[T1 any, T2 any](value1 T1, value2 T2, err error) (T1, T2) {
23+
// Must3 panics if err is not nil, otherwise returns v1 and v2.
24+
func Must3[T1 any, T2 any](v1 T1, v2 T2, err error) (T1, T2) {
2525
if err != nil {
2626
panic(err)
2727
}
2828

29-
return value1, value2
29+
return v1, v2
30+
}
31+
32+
// Must4 panics if err is not nil, otherwise returns v1, v2 and v3.
33+
func Must4[T1 any, T2 any, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3) {
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
return v1, v2, v3
3039
}

must/must_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package must
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestMust1(t *testing.T) {
11+
t.Run("should return normally", func(t *testing.T) {
12+
assert.NotPanics(t, func() { Must1(nil) })
13+
})
14+
t.Run("should panic on error", func(t *testing.T) {
15+
assert.Panics(t, func() { Must1(errors.New("panic")) })
16+
})
17+
}
18+
19+
func TestMust(t *testing.T) {
20+
t.Run("should return normally", func(t *testing.T) {
21+
var v1 int
22+
assert.NotPanics(t, func() { v1 = Must(123, nil) })
23+
assert.Equal(t, v1, 123)
24+
})
25+
t.Run("should panic on error", func(t *testing.T) {
26+
assert.Panics(t, func() { Must(123, errors.New("panic")) })
27+
})
28+
}
29+
30+
func TestMust3(t *testing.T) {
31+
t.Run("should return normally", func(t *testing.T) {
32+
var v1 int
33+
var v2 string
34+
assert.NotPanics(t, func() { v1, v2 = Must3(123, "my string", nil) })
35+
assert.Equal(t, v1, 123)
36+
assert.Equal(t, v2, "my string")
37+
})
38+
t.Run("should panic on error", func(t *testing.T) {
39+
assert.Panics(t, func() { Must3(123, "my string", errors.New("panic")) })
40+
})
41+
}
42+
43+
func TestMust4(t *testing.T) {
44+
t.Run("should return normally", func(t *testing.T) {
45+
var v1 int
46+
var v2 string
47+
var v3 bool
48+
assert.NotPanics(t, func() { v1, v2, v3 = Must4(123, "my string", false, nil) })
49+
assert.Equal(t, v1, 123)
50+
assert.Equal(t, v2, "my string")
51+
assert.Equal(t, v3, false)
52+
})
53+
t.Run("should panic on error", func(t *testing.T) {
54+
assert.Panics(t, func() { Must4(123, "my string", false, errors.New("panic")) })
55+
})
56+
}

0 commit comments

Comments
 (0)