-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps_test.go
More file actions
200 lines (148 loc) · 3.39 KB
/
maps_test.go
File metadata and controls
200 lines (148 loc) · 3.39 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
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
191
192
193
194
195
196
197
198
199
200
package mewl
import (
"fmt"
"slices"
"sort"
"testing"
"github.com/code-gorilla-au/odize"
)
func TestMapKeys(t *testing.T) {
want := []string{"1", "2"}
got := MapKeys(map[string]int{"1": 1, "2": 2})
ForEach(want, func(item string, _ int, _ []string) {
odize.AssertTrue(t, slices.Contains(got, item))
})
}
func ExampleMapKeys() {
obj := map[int]string{
1: "1",
2: "flash",
}
got := MapKeys(obj)
sort.Ints(got)
fmt.Println(got)
// Output: [1 2]
}
func TestMapValues(t *testing.T) {
want := []int{1, 2, 3}
got := MapValues(map[string]int{"1": 1, "2": 2, "3": 3})
ForEach(want, func(item int, _ int, _ []int) {
odize.AssertTrue(t, slices.Contains(got, item))
})
}
func ExampleMapValues() {
obj := map[int]string{
1: "hello",
2: "world",
}
got := MapValues(obj)
sort.Strings(got)
fmt.Println(got)
// Output: [hello world]
}
func TestMapOmitKey(t *testing.T) {
got := MapOmitKeys(map[string]int{"1": 1, "2": 2}, "2")
odize.AssertEqual(t, map[string]int{"1": 1}, got)
}
func ExampleMapOmitKeys() {
obj := map[string]int{
"hello": 1,
"world": 2,
"bin": 3,
}
got := MapOmitKeys(obj, "hello", "world")
fmt.Println(got)
// Output: map[bin:3]
}
func TestMapOmitKey_missing_key_returns_whole_object(t *testing.T) {
got := MapOmitKeys(map[string]int{"1": 1, "2": 2}, "9")
odize.AssertEqual(t, map[string]int{"1": 1, "2": 2}, got)
}
func TestMapPickKey(t *testing.T) {
want := map[int]int{1: 1, 2: 2}
got := MapPickKeys(map[int]int{1: 1, 2: 2, 3: 3}, 1, 2)
odize.AssertEqual(t, want, got)
}
func ExampleMapPickKeys() {
obj := map[int]string{
1: "hello",
2: "world",
3: "bin",
}
got := MapPickKeys(obj, 1)
fmt.Println(got)
// Output: map[1:hello]
}
func TestMapPickKey_missing_keys(t *testing.T) {
want := map[int]int{2: 2}
got := MapPickKeys(map[int]int{1: 1, 2: 2, 3: 3}, 99, 2)
odize.AssertEqual(t, want, got)
}
func TestMapPickKey_no_keys(t *testing.T) {
want := map[int]int{}
got := MapPickKeys(map[int]int{1: 1, 2: 2, 3: 3})
odize.AssertEqual(t, want, got)
}
func TestMapOmitBy(t *testing.T) {
want := map[int]int{1: 1}
got := MapOmitBy(map[int]int{1: 1, 2: 2}, func(item int) bool {
return item == 2
})
odize.AssertEqual(t, want, got)
}
func ExampleMapOmitBy() {
obj := map[string]int{
"hello": 1,
"world": 2,
}
got := MapOmitBy(obj, func(item int) bool {
return item == 1
})
fmt.Println(got)
// Output: map[world:2]
}
func TestMapPickBy(t *testing.T) {
want := map[int]int{1: 1}
got := MapPickBy(map[int]int{1: 1, 2: 2}, func(item int) bool {
return item == 1
})
odize.AssertEqual(t, want, got)
}
func ExampleMapPickBy() {
obj := map[string]int{
"hello": 1,
"world": 2,
}
got := MapPickBy(obj, func(item int) bool {
return item == 1
})
fmt.Println(got)
// Output: map[hello:1]
}
func TestMapClone(t *testing.T) {
group := odize.NewGroup(t, nil)
err := group.
Test("should clone map of strings", func(t *testing.T) {
testVal := map[string]string{
"hello": "world",
}
result := MapClone(testVal)
odize.AssertEqual(t, testVal, result)
}).
Test("should clone empty map", func(t *testing.T) {
testVal := map[string]string{}
result := MapClone(testVal)
odize.AssertEqual(t, testVal, result)
}).
Run()
odize.AssertNoError(t, err)
}
func ExampleMapClone() {
obj := map[string]int{
"hello": 1,
"world": 2,
}
got := MapClone(obj)
fmt.Println(got)
// Output: map[hello:1 world:2]
}