-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps_transform_test.go
147 lines (124 loc) · 2.69 KB
/
maps_transform_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
138
139
140
141
142
143
144
145
146
147
package generics_test
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/zeroflucs-given/generics"
)
func TestKeyValuesToMap(t *testing.T) {
// Arrange
input := []generics.KeyValuePair[string, string]{
{
Key: "hello",
Value: "world",
},
{
Key: "fizz",
Value: "buzz",
},
}
// Act
result := generics.KeyValuesToMap(input)
// Assert
require.NotNil(t, result, "Should have a result")
require.Len(t, result, 2, "Should have right number of keys")
require.Equal(t, map[string]string{
"hello": "world",
"fizz": "buzz",
}, result, "Should have expected output")
}
func TestKeys(t *testing.T) {
// Arrange
input := map[string]string{
"hello": "world",
"fizz": "buzz",
}
// Act
result := generics.Keys(input)
// Assert
require.NotNil(t, result, "Should have a result")
require.ElementsMatch(t, result, []string{"hello", "fizz"})
}
func TestMapValues(t *testing.T) {
// Arrange
input := map[int]string{
1: "one",
2: "two",
}
// Act
remapped := generics.MapValues(input, func(k int, v string) bool {
return v == "one"
})
// Assert
require.Equal(t, true, remapped[1])
require.Equal(t, false, remapped[2])
}
func TestMapValuesWithContext(t *testing.T) {
// Arrange
ctx := context.TODO()
input := map[int]string{
1: "one",
2: "two",
}
// Act
remapped, err := generics.MapValuesWithContext(ctx, input, func(ctx context.Context, k int, v string) (bool, error) {
return v == "one", nil
})
// Assert
require.NoError(t, err)
require.Equal(t, true, remapped[1])
require.Equal(t, false, remapped[2])
}
func TestMapValuesWithContextError(t *testing.T) {
// Arrange
ctx := context.TODO()
input := map[int]string{
1: "one",
2: "two",
}
fail := errors.New("fail")
// Act
remapped, err := generics.MapValuesWithContext(ctx, input, func(ctx context.Context, k int, v string) (bool, error) {
if v == "two" {
return false, fail
}
return v == "one", nil
})
// Assert
require.ErrorIs(t, err, fail)
require.Nil(t, remapped)
}
func TestValues(t *testing.T) {
// Arrange
input := map[string]string{
"hello": "world",
"fizz": "buzz",
}
// Act
result := generics.Values(input)
// Assert
require.NotNil(t, result, "Should have a result")
require.ElementsMatch(t, result, []string{"world", "buzz"})
}
func TestToKeyValues(t *testing.T) {
// Arrange
input := map[string]string{
"hello": "world",
"fizz": "buzz",
}
// Act
result := generics.ToKeyValues(input)
// Assert
require.NotNil(t, result, "Should have a result")
require.ElementsMatch(t, result, []generics.KeyValuePair[string, string]{
{
Key: "hello",
Value: "world",
},
{
Key: "fizz",
Value: "buzz",
},
})
}