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