-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_ok_test.go
109 lines (84 loc) · 1.87 KB
/
result_ok_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
package fluent
import (
"testing"
"github.com/stretchr/testify/assert"
)
var testValue = 951753852
func Test_ResultOk_IsOk(t *testing.T) {
r := Ok(testValue)
assert.True(t, r.IsOk())
}
func Test_ResultOk_IsErr(t *testing.T) {
r := Ok(testValue)
assert.False(t, r.IsErr())
}
func Test_ResultOk_Ok(t *testing.T) {
r := Ok(testValue)
ok := r.Ok()
assert.True(t, ok.IsPresent(), "present")
}
func Test_ResultOk_Err(t *testing.T) {
r := Ok(testValue)
e := r.Err()
assert.False(t, e.IsPresent(), "present")
}
func Test_ResultOk_Map(t *testing.T) {
r := Ok(testValue)
called := new(bool)
*called = false
actual := r.Map(func(val int) int {
*called = true
return val * 2
})
assert.True(t, actual.IsOk(), "IsErr")
assert.True(t, *called, "mapper called")
assert.Equal(t, testValue*2, actual.Get())
}
func Test_ResultOk_MapErr(t *testing.T) {
r := Ok(testValue)
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.False(t, *called, "mapper called")
}
func Test_ResultOk_Get(t *testing.T) {
r := Ok(testValue)
actual := r.Get()
assert.Equal(t, testValue, actual)
}
func Test_ResultOk_GetErr(t *testing.T) {
r := Ok(testValue)
defer func() {
recover()
}()
r.GetErr()
t.Error("should panic")
}
func Test_ResultOk_OrElse(t *testing.T) {
r := Ok(testValue)
actual := r.OrElse(1)
assert.Equal(t, testValue, actual)
}
func Test_ResultOk_OrElseGet(t *testing.T) {
r := Ok(testValue)
actual := r.OrElseGet(func() int {
return 1
})
assert.Equal(t, testValue, actual)
}
func Test_ResultOk_Or(t *testing.T) {
r := Ok(testValue)
actual := r.Or(func() Result[int] {
return Ok(1)
})
assert.Equal(t, testValue, actual.Get())
}
func Test_ResultOk_String(t *testing.T) {
r := Ok(testValue)
assert.NotEmpty(t, r.String())
}