-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_test.go
68 lines (52 loc) · 1.26 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package result
import (
"errors"
"testing"
)
func TestResult(t *testing.T) {
r := Ok(1)
if !r.IsOk() {
t.Error("expected result to be ok")
}
if r.IsErr() {
t.Error("expected result to not be an error")
}
if !r.IsOkAnd(func(v int) bool { return v == 1 }) {
t.Error("expected result to be ok and have value 1")
}
if r.IsErrAnd(func(e error) bool { return e != nil }) {
t.Error("expected result to not be an error and have error nil")
}
v := r.Ok()
if *v != 1 {
t.Error("expected result to have value 1")
}
err := r.Map(func(v *int) { *v = 2 })
if err != nil {
t.Error("expected result to not have error")
}
if *v != 2 {
t.Error("expected result to have value 2")
}
}
func TestErrResult(t *testing.T) {
r := Err[int](errors.New("error"))
if r.IsOk() {
t.Error("expected result to not be ok")
}
if !r.IsErr() {
t.Error("expected result to be an error")
}
if r.IsOkAnd(func(v int) bool { return v == 1 }) {
t.Error("expected result to not be ok and have value 1")
}
if !r.IsErrAnd(func(e error) bool { return e != nil }) {
t.Error("expected result to be an error and have error nil")
}
if *r.Err() == nil {
t.Error("expected result to have error")
}
if r.UnwrapErr() == nil {
t.Error("expected result to have error")
}
}