-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_test.go
133 lines (120 loc) · 2.8 KB
/
option_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package option
import (
"encoding/json"
"errors"
"testing"
)
func TestSome(t *testing.T) {
opt := Some(42)
if IsNone(opt) {
t.Errorf("Some(42) should be Some")
}
if val := opt.Ok(); val != 42 {
t.Errorf("Expected 42, got %v", val)
}
}
func TestNone(t *testing.T) {
customError := errors.New("custom error")
opt := None[int](customError)
if IsSome(opt) {
t.Errorf("None should not be Some")
}
if cause := opt.Cause(); cause == nil || !errors.Is(cause, customError) {
t.Errorf("Expected custom error, got %v", cause)
}
}
func TestProcess(t *testing.T) {
opt := Some(5)
result := Process(opt, func(v int) (int, error) {
if v == 0 {
return 0, errors.New("zero value")
}
return 2 * v, nil
})
if result.Ok() != 10 {
t.Errorf("Expected processed value 10, got %v", result.Ok())
}
optZero := Some(0)
resultZero := Process(optZero, func(v int) (int, error) {
if v == 0 {
return 0, errors.New("zero value")
}
return 2 * v, nil
})
if !IsNone(resultZero) || resultZero.Cause() == nil || resultZero.Cause().Error() != "zero value" {
t.Errorf("Expected zero value error, got %v", resultZero.Cause())
}
}
func TestMap(t *testing.T) {
opt := Some(5)
newOpt := Map(opt, func(v int) int {
return v + 3
})
if !IsSome(newOpt) {
t.Errorf("Map should return Some")
}
if val := newOpt.Ok(); val != 8 {
t.Errorf("Expected 8, got %v", val)
}
}
func TestFlatten(t *testing.T) {
opt := Some(Some(100))
flatOpt := Flatten(opt)
if !IsSome(flatOpt) {
t.Errorf("Flatten should return Some")
}
if val := flatOpt.Ok(); val != 100 {
t.Errorf("Expected 100, got %v", val)
}
}
func TestNoneFlatten(t *testing.T) {
opt := None[Option[int]]()
flatOpt := Flatten(opt)
if IsSome(flatOpt) {
t.Errorf("Flatten of None should return None")
}
if !errors.Is(flatOpt.Cause(), Nil) {
t.Errorf("")
}
}
func TestOptionMarshalJSON(t *testing.T) {
opt := Some(42)
data, err := json.Marshal(opt)
if err != nil {
t.Errorf("MarshalJSON failed: %v", err)
}
if string(data) != "42" {
t.Errorf("Expected 42, got %v", string(data))
}
opt = None[int](errors.New("custom error"))
data, err = json.Marshal(opt)
if err != nil {
t.Errorf("MarshalJSON failed: %v", err)
}
if string(data) != "null" {
t.Errorf("Expected null, got %v", string(data))
}
}
func TestOptionUnmarshalJSON(t *testing.T) {
opt := Some(0)
err := json.Unmarshal([]byte("42"), opt)
if err != nil {
t.Errorf("UnmarshalJSON failed: %v", err)
}
if !IsSome(opt) {
t.Errorf("Expected Some, got None")
}
if val := opt.Ok(); val != 42 {
t.Errorf("Expected 42, got %v", val)
}
err = json.Unmarshal([]byte("null"), opt)
if err != nil {
t.Errorf("UnmarshalJSON failed: %v", err)
}
if IsSome(opt) {
t.Errorf("Expected None, got Some")
}
if !errors.Is(opt.Cause(), Nil) {
t.Errorf("Expected Nil, got %v", opt.Cause())
}
}