-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
78 lines (68 loc) · 1.51 KB
/
error_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ego
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestTryCatchErr(t *testing.T) {
thrownErr := Errorf("thrown error")
value, err := Try(func() bool {
panic(thrownErr)
})
t.Log(fmt.Sprintf("%+v", thrownErr))
require.Equal(t, err, thrownErr)
require.False(t, value)
}
func TestTryCatchString(t *testing.T) {
value, err := Try(func() int {
panic("this is an error")
})
require.Containsf(t, err.Error(), "this is an error", "unexpected error message")
require.Equal(t, value, 0)
}
func TestTryReturnNil(t *testing.T) {
throwOnly := func() {
panic("some error")
}
value, err := Try(func() any {
throwOnly()
return nil
})
require.Error(t, err)
require.Nil(t, value)
}
func TestAssertNilOnNil(t *testing.T) {
value, err := Try(func() int {
AssertNil(nil)
return 42
})
require.Nil(t, err)
require.Equal(t, value, 42)
}
func TestAssertNilOnError(t *testing.T) {
thrownErr := Errorf("thrown error")
value, err := Try(func() int {
AssertNil(thrownErr)
return 42
})
require.Equal(t, err, thrownErr)
require.Equal(t, value, 0)
}
func TestUnwrapOk(t *testing.T) {
classicDoer := func(a int) (int, error) {
return a, nil
}
require.Equal(t, Unwrap(classicDoer(1337)), 1337)
}
func TestUnwrapErr(t *testing.T) {
returnedErr := errors.New("thrown error")
classicDoer := func(a int) (int, error) {
return a, returnedErr
}
value, err := Try(func() int {
return Unwrap(classicDoer(1337))
})
require.ErrorIs(t, err, returnedErr)
require.Equal(t, value, 0)
}