-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult_test.go
107 lines (85 loc) · 1.68 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
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
package shine
import (
"errors"
"os"
"strconv"
"testing"
)
type closeableValue struct {
closed bool
}
func (v *closeableValue) Close() error {
v.closed = true
return nil
}
type closeableError struct {
closed bool
}
func (v *closeableError) Close() error {
v.closed = true
return nil
}
func (v *closeableError) Error() string {
return ""
}
func TestNewResultForErr(t *testing.T) {
r := NewResult(strconv.Atoi("not a number"))
if _, ok := r.(Ok[int]); ok {
t.Log("NewResult(value, not nil) should return Err")
t.FailNow()
}
}
func TestNewResultForNonErr(t *testing.T) {
r := NewResult(strconv.Atoi("1"))
if _, ok := r.(Err[int]); ok {
t.Log("NewResult(value, nil) should return Ok")
t.FailNow()
}
}
func TestUnwrapOrDefaultForOk(t *testing.T) {
r := NewResult(strconv.Atoi("1"))
if r.UnwrapOrDefault() != 1 {
t.Log("UnwrapOrDefault with Ok(1) should return 1")
t.FailNow()
}
}
func TestUnwrapOrDefaultForErr(t *testing.T) {
r := NewResult(strconv.Atoi("not a number"))
if r.UnwrapOrDefault() != 0 {
t.Log("UnwrapOrDefault with Err[int] should return 0")
t.FailNow()
}
}
func TestErrorIs(t *testing.T) {
switch r := NewResult(strconv.Atoi("not a number")).(type) {
case Ok[int]:
t.Fail()
case Err[int]:
if !errors.Is(r, strconv.ErrSyntax) {
t.Fail()
}
}
}
func TestErrorAs(t *testing.T) {
err := NewErr[struct{}](new(os.PathError))
var pe *os.PathError
if !errors.As(err, &pe) {
t.Fail()
}
}
func TestOk_Close(t *testing.T) {
v := new(closeableValue)
r := NewOk(v)
_ = r.Close()
if !v.closed {
t.Fail()
}
}
func TestErr_Close(t *testing.T) {
v := new(closeableError)
r := NewErr[struct{}](v)
_ = r.Close()
if !v.closed {
t.Fail()
}
}