-
Notifications
You must be signed in to change notification settings - Fork 0
/
option_test.go
137 lines (113 loc) · 3.2 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
134
135
136
137
package result
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
)
func TestOptionUnwrap(t *testing.T) {
type test struct {
input *Option[string]
expectError bool
expectedValue string
}
notSetOption := NewOption[string]()
someOption := NewOption[string]().SetSome("some")
noneOption := NewOption[string]().SetNone()
errOption := NewOption[string]().SetError(errors.New("error"))
tests := []test{
{input: notSetOption, expectError: true, expectedValue: ""},
{input: someOption, expectError: false, expectedValue: "some"},
{input: noneOption, expectError: true, expectedValue: ""},
{input: errOption, expectError: true, expectedValue: ""},
}
for _, tc := range tests {
gotValue, gotError := tc.input.Unwrap()
if gotError != nil && !tc.expectError {
t.Fatalf("expected no error, got error %v", gotError.Error())
} else if gotError == nil && tc.expectError {
t.Fatalf("expected error, got no error")
}
if gotValue != tc.expectedValue {
t.Fatalf("expected %v, got %v", tc.expectedValue, gotValue)
}
}
}
func TestOptionWait(t *testing.T) {
type test struct {
input *Option[string]
kind OptionKind
expectedValue string
expectError bool
}
someOption := NewOption[string]()
noneOption := NewOption[string]()
errOption := NewOption[string]()
neverOption := NewOption[string]()
tests := []test{
{input: someOption, kind: Some, expectError: false, expectedValue: "some"},
{input: noneOption, kind: None, expectError: true, expectedValue: ""},
{input: errOption, kind: Error, expectError: true, expectedValue: ""},
{input: neverOption, kind: neverSet, expectError: true, expectedValue: ""},
}
for _, tc := range tests {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
var waitSuccess bool
wg := sync.WaitGroup{}
wg.Add(1)
var gotValue string
var gotError error
go func(opt *Option[string]) {
defer wg.Done()
gotValue, gotError = opt.Wait(ctx)
waitSuccess = true
}(tc.input)
switch tc.kind {
case Some:
tc.input.SetSome(tc.expectedValue)
case None:
tc.input.SetNone()
case Error:
tc.input.SetError(fmt.Errorf("error"))
case neverSet:
// nothing ever set, let the wait context fail
}
wg.Wait()
if !waitSuccess {
t.Fatal("wait failed")
}
if gotError != nil && !tc.expectError {
t.Fatalf("expected no error, got error %v", gotError.Error())
} else if gotError == nil && tc.expectError {
t.Fatalf("expected error, got no error")
}
if gotValue != tc.expectedValue {
t.Fatalf("expected %v, got %v", tc.expectedValue, gotValue)
}
}
}
func TestOptionKind(t *testing.T) {
type test struct {
input *Option[string]
want OptionKind
}
notSetOption := NewOption[string]()
someOption := NewOption[string]().SetSome("some")
noneOption := NewOption[string]().SetNone()
errOption := NewOption[string]().SetError(errors.New("error"))
tests := []test{
{input: notSetOption, want: NotSet},
{input: someOption, want: Some},
{input: noneOption, want: None},
{input: errOption, want: Error},
}
for _, tc := range tests {
got := tc.input.Kind()
if got != tc.want {
t.Fatalf("expected %v, got %v", tc.want, got)
}
}
}