-
Notifications
You must be signed in to change notification settings - Fork 0
/
optional_test.go
173 lines (140 loc) · 3.85 KB
/
optional_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package optional
import (
"encoding/json"
"log"
"testing"
)
func assert(t *testing.T, condition bool, message string, args ...any) {
if !condition {
t.Helper()
t.Errorf(message, args...)
}
}
func TestPrimitive(t *testing.T) {
v := New[int](1)
assert(t, !v.IsNil(), "IsNil failed")
assert(t, v.ForceValue() == 1, "ForceValue failed")
w, ok := v.Value()
assert(t, ok && w == 1, "Value failed")
v2 := Nil[int]()
assert(t, v2.IsNil(), "IsNil failed")
}
func TestStruct(t *testing.T) {
type S struct {
A int
}
var v Type[S] = New[S](S{A: 123})
assert(t, !v.IsNil(), "IsNil failed")
assert(t, v.ForceValue().A == 123, "ForceValue failed")
v2 := Nil[S]()
assert(t, v2.IsNil(), "IsNil failed")
}
type AAA interface {
print() int
}
type aaa struct{ I int }
func (s aaa) print() int { return s.I }
type bbb struct{ I int }
func (s *bbb) print() int { return s.I }
func TestInterface(t *testing.T) {
v := New[AAA](aaa{I: 123})
assert(t, !v.IsNil(), "IsNil failed")
assert(t, v.ForceValue().print() == 123, "ForceValue failed")
v2 := Nil[AAA]()
assert(t, v2.IsNil(), "IsNil failed")
// ----
v = New[AAA](&bbb{I: 123})
assert(t, !v.IsNil(), "IsNil failed")
assert(t, v.ForceValue().print() == 123, "ForceValue failed")
// ----
var inter AAA = nil
v2 = New(inter)
assert(t, v2.IsNil(), "should be nil")
// ----
func() {
var inter AAA = nil
v := New[AAA](inter)
assert(t, v.IsNil(), "should be nil")
}()
func() {
var ptr *bbb
var inter AAA = ptr
log.Println(inter, inter == nil)
v := New[AAA](inter)
// NOTE: this case is un-natual, but it works like assigning to an interface
assert(t, !v.IsNil(), "should NOT be nil")
}()
}
func TestPointer(t *testing.T) {
var o = 123
var ptr = &o
var v2 = New(ptr)
assert(t, !v2.IsNil(), "IsNil failed")
assert(t, v2.ForceValue() == ptr, "ForceValue failed")
// ----
var v = FromPtr(ptr)
assert(t, !v.IsNil(), "IsNil failed")
assert(t, v.ForceValue() == 123, "ForceValue failed")
}
func TestNestedOptional(t *testing.T) {
var v Type[Type[int]] = New(New(123))
assert(t, !v.IsNil(), "IsNil failed")
if w, ok := v.Value(); ok {
assert(t, !w.IsNil(), "IsNil failed")
assert(t, w.ForceValue() == 123, "ForceValue failed")
}
var v2 Type[Type[int]] = Nil[Type[int]]()
assert(t, v2.IsNil(), "IsNil failed")
var v3 Type[Type[int]] = New(Nil[int]())
assert(t, !v3.IsNil(), "IsNil failed")
}
func TestCompact(t *testing.T) {
var v Type[Type[int]] = New(New(123))
v2 := Compact(v)
assert(t, !v2.IsNil(), "IsNil failed")
assert(t, v2.ForceValue() == 123, "ForceValue failed")
v = Nil[Type[int]]()
v2 = Compact(v)
assert(t, v2.IsNil(), "IsNil failed")
v = New(Nil[int]())
v2 = Compact(v)
assert(t, v2.IsNil(), "IsNil failed")
}
func TestMap(t *testing.T) {
v := New[int](1)
v2 := Map(v, func(t int) string {
return "123"
})
assert(t, !v2.IsNil(), "IsNil failed")
assert(t, v2.ForceValue() == "123", "ForceValue failed")
}
func TestValueOrDefault(t *testing.T) {
v := New[int](1)
assert(t, v.ValueOrDefault(10) == 1, "ValueOrDefault failed")
assert(t, v.ValueOrLazyDefault(func() int { panic("don't call me") }) == 1, "ValueOrLazyDefault failed")
v2 := Nil[int]()
assert(t, v2.ValueOrDefault(10) == 10, "ValueOrDefault failed")
assert(t, v2.ValueOrLazyDefault(func() int { return 100 }) == 100, "ValueOrLazyDefault failed")
}
func TestEncodeAndDecode(t *testing.T) {
var v = New(New(123))
data, err := json.Marshal(v)
assert(t, err == nil, "")
assert(t, string(data) == "123", "")
var v2 Type[Type[int]]
err = json.Unmarshal(data, &v2)
assert(t, err == nil, "")
assert(t, !v2.IsNil(), "")
if w, ok := Compact(v2).Value(); ok {
assert(t, w == 123, "")
}
// empty
v3 := Nil[int]()
data, err = json.Marshal(v3)
assert(t, err == nil, "")
assert(t, string(data) == "null", "")
var v4 Type[int]
err = json.Unmarshal(data, &v4)
assert(t, err == nil, "")
assert(t, v4.IsNil(), "")
}