-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_err_test.go
111 lines (87 loc) · 1.98 KB
/
result_err_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package fluent
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
var testErr = errors.New("err")
func Test_ResultErr_IsOk(t *testing.T) {
r := Err[int](testErr)
assert.False(t, r.IsOk())
}
func Test_ResultErr_IsErr(t *testing.T) {
r := Err[int](testErr)
assert.True(t, r.IsErr())
}
func Test_ResultErr_Ok(t *testing.T) {
r := Err[int](testErr)
ok := r.Ok()
assert.False(t, ok.IsPresent(), "present")
}
func Test_ResultErr_Err(t *testing.T) {
r := Err[int](testErr)
e := r.Err()
assert.True(t, e.IsPresent(), "present")
}
func Test_ResultErr_Map(t *testing.T) {
r := Err[int](testErr)
called := new(bool)
*called = false
actual := r.Map(func(val int) int {
*called = true
return val * 2
})
assert.True(t, actual.IsErr(), "IsErr")
assert.False(t, *called, "mapper called")
}
func Test_ResultErr_MapErr(t *testing.T) {
r := Err[int](testErr)
called := new(bool)
*called = false
expected := 987654231
actual := r.MapErr(func(e error) int {
*called = true
return expected
})
assert.False(t, actual.IsErr(), "IsErr")
assert.True(t, *called, "mapper called")
assert.Equal(t, expected, actual.Get())
}
func Test_ResultErr_Get(t *testing.T) {
r := Err[int](testErr)
defer func() {
recover()
}()
r.Get()
t.Error("should panic")
}
func Test_ResultErr_GetErr(t *testing.T) {
r := Err[int](testErr)
actual := r.GetErr()
assert.Equal(t, testErr, actual)
}
func Test_ResultErr_OrElse(t *testing.T) {
r := Err[int](testErr)
expected := 987654321
actual := r.OrElse(expected)
assert.Equal(t, expected, actual)
}
func Test_ResultErr_OrElseGet(t *testing.T) {
r := Err[int](testErr)
actual := r.OrElseGet(func() int {
return 1
})
assert.Equal(t, 1, actual)
}
func Test_ResultErr_Or(t *testing.T) {
r := Err[int](testErr)
expected := 987654321
actual := r.Or(func() Result[int] {
return Ok(expected)
})
assert.Equal(t, expected, actual.Get())
}
func Test_ResultErr_String(t *testing.T) {
r := Err[int](testErr)
assert.NotEmpty(t, r.String())
}