-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmust_test.go
54 lines (47 loc) · 1.03 KB
/
must_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package gokit
import (
"errors"
"testing"
)
func TestMustReturn(t *testing.T) {
// No Error
foo := func() (int, error) {
return 42, nil
}
result1, _ := foo()
actual1 := MustReturn(foo())
if result1 != actual1 {
t.Errorf("expected %d, got %d", result1, actual1)
}
// return error
fooError := func() (int, error) {
return 0, errors.New("some error")
}
defer func() {
if v := recover(); v == nil {
t.Error("MustReturn did not panic")
}
}()
_ = MustReturn(fooError())
}
func TestMustReturn2(t *testing.T) {
// No Error
foo := func() (int, string, error) {
return 42, "hello", nil
}
result1, result2, _ := foo()
actual1, actual2 := MustReturn2(foo())
if result1 != actual1 || result2 != actual2 {
t.Errorf("expected %d, %s, got %d, %s", result1, result2, actual1, actual2)
}
// return error
fooError := func() (int, string, error) {
return 0, "", errors.New("some error")
}
defer func() {
if v := recover(); v == nil {
t.Error("MustReturn2 did not panic")
}
}()
_, _ = MustReturn2(fooError())
}