-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice_test.go
More file actions
118 lines (102 loc) · 2.07 KB
/
Copy pathslice_test.go
File metadata and controls
118 lines (102 loc) · 2.07 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
package optional_test
import (
"fmt"
"reflect"
"testing"
"github.com/nzmprlr/optional"
)
func Test_Slice_Get(t *testing.T) {
cases := [][]int{
nil,
[]int{},
[]int{0, 1, -1},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if !reflect.DeepEqual(optional.Slice(c).Get(), c) {
t.Errorf("slice Get failed")
}
})
}
}
func Test_Slice_Empty_and_Present(t *testing.T) {
cases := [][]int{
nil,
[]int{},
[]int{0, 1, -1},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o := optional.Slice(c)
if e, p := o.Empty(), o.Present(); e == p {
t.Errorf("slice Empty and Present are same")
}
})
}
}
func Test_Slice_IfEmpty(t *testing.T) {
cases := []struct {
v []int
willCall bool
called bool
}{
{nil, true, false},
{[]int{}, true, false},
{[]int{1}, false, false},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
optional.Slice(c.v).IfEmpty(func(m []int) {
c.called = true
})
if !c.willCall && c.called {
t.Errorf("slice IfEmpty failed")
}
})
}
}
func Test_Slice_IfPresent(t *testing.T) {
cases := []struct {
v []int
willCall bool
called bool
}{
{nil, false, false},
{[]int{}, true, false},
{[]int{1}, true, false},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
optional.Slice(c.v).IfPresent(func(m []int) {
c.called = true
})
if !c.willCall && c.called {
t.Errorf("slice IfPresent failed")
}
})
}
}
func Test_Slice_If_Else(t *testing.T) {
cases := []struct {
v []int
condition bool
e []int
result []int
}{
{nil, false, nil, nil},
{nil, true, nil, nil},
{nil, false, []int{}, []int{}},
{[]int{1}, false, nil, nil},
{[]int{1}, true, nil, []int{1}},
}
for i, c := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o := optional.Slice(c.v).If(func(m []int) bool {
return c.condition
}).Else(c.e)
if !reflect.DeepEqual(o, c.result) {
t.Errorf("slice If-Else failed")
}
})
}
}