-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoption_test.go
190 lines (158 loc) · 5.04 KB
/
goption_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package goption_test
import (
"errors"
"log"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/manicar2093/goption"
)
func toPointer[T any](value T) *T {
var pointer T = value
return &pointer
}
var _ = Describe("Goption", func() {
Describe("Empty", func() {
It("creates an empty Optional", func() {
Expect(func() {
got := goption.Empty[string]()
log.Println(got)
}).ToNot(Panic())
})
})
Describe("Of", func() {
DescribeTable("creates an Optional with given data", func(expectedValue interface{}) {
Expect(func() {
goption.Of(expectedValue)
}).ToNot(Panic())
},
Entry("no pointer string value", "hello"),
Entry("no pointer int value", int(6)),
Entry("no pointer int8 value", int8(6)),
Entry("no pointer uint16 value", uint16(6)),
Entry("no pointer uint32 value", uint32(6)),
Entry("no pointer uint64 value", uint64(6)),
Entry("no pointer uint8 value", uint8(6)),
Entry("no pointer uint16 value", uint16(6)),
Entry("no pointer uint32 value", uint32(6)),
Entry("no pointer uint64 value", uint64(6)),
Entry("no pointer complex64 value", complex64(6)),
Entry("no pointer complex128 value", complex128(6)),
Entry("no pointer struct value", struct{ Name string }{Name: "name"}),
Entry("pointer string value", toPointer("hello")),
Entry("pointer int value", toPointer(int(6))),
Entry("pointer int8 value", toPointer(int8(6))),
Entry("pointer uint16 value", toPointer(uint16(6))),
Entry("pointer uint32 value", toPointer(uint32(6))),
Entry("pointer uint64 value", toPointer(uint64(6))),
Entry("pointer uint8 value", toPointer(uint8(6))),
Entry("pointer uint16 value", toPointer(uint16(6))),
Entry("pointer uint32 value", toPointer(uint32(6))),
Entry("pointer uint64 value", toPointer(uint64(6))),
Entry("pointer complex64 value", toPointer(complex64(6))),
Entry("pointer complex128 value", toPointer(complex128(6))),
Entry("pointer struct value", toPointer(struct{ Name string }{Name: "name"})),
)
})
Describe("IsPresent", func() {
DescribeTable("identify if optional has a valid data", func(expectedValue interface{}, isPresent bool) {
Expect(goption.Of(expectedValue).IsPresent()).To(Equal(isPresent))
},
Entry("data is nil", nil, false),
Entry("filled pointer", toPointer(struct{ Name string }{Name: "name"}), true),
Entry("empty struct", struct{ Name string }{}, false),
Entry("empty string", "", false),
Entry("filled string", "full string", true),
Entry("empty int", int(0), false),
Entry("filled int", int(1), true),
Entry("empty optional", goption.Optional[string]{}, false),
Entry("empty slice", []string{}, false),
Entry("empty slice of pointers", []*string{}, false),
)
})
Describe("OrElseError", func() {
When("optional is empty", func() {
It("returns given error", func() {
var (
opt = goption.Empty[string]()
expectedError = errors.New("expected error")
)
got, err := opt.OrElseError(expectedError)
Expect(got).To(BeEmpty())
Expect(err).To(Equal(expectedError))
})
})
When("optional has a valid value", func() {
It("returns it", func() {
var (
expectedValue = "expectedValue"
opt = goption.Of(expectedValue)
expectedError = errors.New("expected error")
)
got, err := opt.OrElseError(expectedError)
Expect(err).ToNot(HaveOccurred())
Expect(got).To(Equal(expectedValue))
})
})
})
Describe("OrElse", func() {
When("optional is empty", func() {
It("returns given data", func() {
var (
opt = goption.Empty[string]()
expectedOtherData = "other expected data"
)
got := opt.OrElse(expectedOtherData)
Expect(got).To(Equal(expectedOtherData))
})
})
When("optional has valid data", func() {
It("returns it", func() {
var (
expectedValue = "expectedValue"
opt = goption.Of(expectedValue)
expectedOtherData = "other expected data"
)
got := opt.OrElse(expectedOtherData)
Expect(got).To(Equal(expectedValue))
})
})
})
Describe("Get", func() {
When("optional has a valida data", func() {
It("returns it", func() {
var (
expectedValue string = "a valid value"
opt = goption.Of(expectedValue)
)
got, _ := opt.Get()
Expect(got).To(Equal(expectedValue))
})
})
When("optional is empty", func() {
It("returns a goption.ErrNoSuchElement", func() {
got, err := goption.Empty[string]().Get()
Expect(got).To(BeEmpty())
Expect(err).To(MatchError(goption.ErrNoSuchElement))
})
})
})
Describe("MustGet", func() {
When("optional has no data", func() {
It("panics with ErrNoSuchElement", func() {
var opt = goption.Empty[string]()
Expect(func() {
opt.MustGet()
}).To(PanicWith(goption.ErrNoSuchElement))
})
})
When("optional has valid data", func() {
It("returns it", func() {
var (
expectedValue = "expectedValue"
opt = goption.Of(expectedValue)
)
Expect(opt.MustGet()).To(Equal(expectedValue))
})
})
})
})