-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_test.go
53 lines (47 loc) · 1.06 KB
/
result_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
package fluent
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ResultMap_Ok(t *testing.T) {
value := "a"
r := Ok(value)
expected := len(value)
called := new(bool)
*called = false
actual := MapResult(r, func(val string) int {
*called = true
return len(val)
})
assert.True(t, *called, "mapper called")
assert.Equal(t, expected, actual.Get())
}
func Test_ResultMap_Err(t *testing.T) {
r := Err[string](testErr)
called := new(bool)
*called = false
actual := MapResult(r, func(val string) int {
*called = true
return len(val)
})
assert.False(t, *called, "mapper called")
assert.True(t, actual.IsErr())
}
func Test_ResultFromCall_Ok(t *testing.T) {
expected := 951357897
fn := func() (int, error) {
return expected, nil
}
r := CallResult(fn)
assert.True(t, r.IsOk(), "IsOk")
assert.Equal(t, expected, r.Get())
}
func Test_ResultFromCall_Err(t *testing.T) {
expected := testErr
fn := func() (int, error) {
return 0, expected
}
r := CallResult(fn)
assert.True(t, r.IsErr(), "IsErr")
assert.Equal(t, expected, r.GetErr())
}