-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_test.go
More file actions
150 lines (128 loc) · 2.7 KB
/
Copy pathinterface_test.go
File metadata and controls
150 lines (128 loc) · 2.7 KB
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
package optional_test
import (
"errors"
"fmt"
"testing"
"github.com/nzmprlr/optional"
)
type testInterface interface {
Test()
}
type testStruct struct{}
func (testStruct) Test() {}
func Test_Error(t *testing.T) {
cases := []error{
nil,
errors.New(""),
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o := optional.Error(c)
if e, p := o.Empty(), o.Present(); e == p {
t.Errorf("interface Empty and Present are same")
}
})
}
}
func Test_Interface_Get(t *testing.T) {
cases := []any{
nil,
testInterface(nil),
testStruct{},
&testStruct{},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if optional.Interface(c).Get() != c {
t.Errorf("interface Get failed")
}
})
}
}
func Test_Interface_Empty_and_Present(t *testing.T) {
cases := []any{
nil,
error(nil),
testInterface(nil),
testStruct{},
&testStruct{},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o := optional.Interface(c)
if e, p := o.Empty(), o.Present(); e == p {
t.Errorf("interface Empty and Present are same")
}
})
}
}
func Test_Interface_IfEmpty(t *testing.T) {
cases := []struct {
v any
willCall bool
called bool
}{
{nil, true, false},
{error(nil), true, false},
{testInterface(nil), true, false},
{testStruct{}, false, false},
{&testStruct{}, false, false},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
optional.Interface(c.v).IfEmpty(func(a any) {
c.called = true
})
if !c.willCall && c.called {
t.Errorf("interface IfEmpty failed")
}
})
}
}
func Test_Interface_IfPresent(t *testing.T) {
cases := []struct {
v any
willCall bool
called bool
}{
{nil, false, false},
{error(nil), false, false},
{testInterface(nil), false, false},
{testStruct{}, true, false},
{&testStruct{}, true, false},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
optional.Interface(c.v).IfPresent(func(a any) {
c.called = true
})
if !c.willCall && c.called {
t.Errorf("interface IfPresent failed")
}
})
}
}
func Test_Interface_If_Else(t *testing.T) {
cases := []struct {
v any
condition bool
e any
result any
}{
{nil, false, nil, nil},
{nil, true, nil, nil},
{nil, false, testStruct{}, testStruct{}},
{testStruct{}, false, nil, nil},
{testStruct{}, true, nil, testStruct{}},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o := optional.Interface(c.v).If(func(a any) bool {
return c.condition
}).Else(c.e)
if o != c.result {
t.Errorf("interface If-Else failed")
}
})
}
}