forked from rjNemo/underscore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_test.go
41 lines (34 loc) · 823 Bytes
/
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
package underscore_test
import (
"errors"
"testing"
u "github.com/rjNemo/underscore"
"github.com/stretchr/testify/assert"
)
func TestSuccess(t *testing.T) {
res := isAnswerToLife(42)
assert.True(t, res.IsSuccess())
}
func TestFailure(t *testing.T) {
res := isAnswerToLife(13)
assert.False(t, res.IsSuccess())
}
func TestIsOK(t *testing.T) {
res, err := isAnswerToLife(42).ToValue()
assert.NoError(t, err)
assert.Equal(t, "You get it", *res)
}
func TestIsError(t *testing.T) {
life := isAnswerToLife(13)
res, err := life.ToValue()
assert.Error(t, err)
assert.Equal(t, "nope", life.(u.Err[string]).Error())
assert.Nil(t, res)
}
func isAnswerToLife(num int) u.Result[string] {
if num == 42 {
res := "You get it"
return u.ToResult(&res, nil)
}
return u.ToResult[string](nil, errors.New("nope"))
}