/
comb_test.go
104 lines (85 loc) · 1.68 KB
/
comb_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
package comb_test
import (
"errors"
"testing"
"github.com/kode4food/comb"
"github.com/stretchr/testify/assert"
)
func TestCompose(t *testing.T) {
as := assert.New(t)
c := comb.Compose(
func(in bool) (bool, error) {
if in {
return false, nil
}
return false, errors.New("test error")
},
func(in bool) (bool, error) {
return !in, nil
},
)
r, err := c(true)
as.True(r)
as.Nil(err)
r, err = c(false)
as.False(r)
as.EqualError(err, "test error")
}
func TestThen(t *testing.T) {
as := assert.New(t)
c := comb.Comb[bool, bool](func(in bool) (bool, error) {
if in {
return false, nil
}
return false, errors.New("test error")
}).Then(func(in bool) (bool, error) {
return !in, nil
})
r, err := c(true)
as.True(r)
as.Nil(err)
r, err = c(false)
as.False(r)
as.EqualError(err, "test error")
}
func TestMust(t *testing.T) {
as := assert.New(t)
c := comb.Comb[bool, bool](func(b bool) (bool, error) {
if b {
return true, nil
}
return false, errors.New("test error")
}).Must()
r := c(true)
as.True(r)
defer func() {
rec := recover()
as.EqualError(rec.(error), "test error")
}()
r = c(false)
as.False(r)
}
func TestBind(t *testing.T) {
as := assert.New(t)
c := comb.Comb[bool, string](func(in bool) (string, error) {
if in {
return "true", nil
}
return "false", nil
}).Bind(func(s string) comb.Comb[string, string] {
if s == "true" {
return func(s string) (string, error) {
return "Hello", nil
}
}
return func(_ string) (string, error) {
return "", errors.New("was false")
}
})
s, err := c(true)
as.Nil(err)
as.Equal("Hello", s)
s, err = c(false)
as.Equal("", s)
as.EqualError(err, "was false")
}