-
Notifications
You must be signed in to change notification settings - Fork 6
/
tomap_test.go
169 lines (162 loc) · 4.54 KB
/
tomap_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package go2linq
import (
"errors"
"fmt"
"iter"
"reflect"
"slices"
"strings"
"testing"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/ToDictionaryTest.cs
func TestToMap_string_rune(t *testing.T) {
type args struct {
source iter.Seq[string]
keySelector func(string) rune
}
tests := []struct {
name string
args args
want map[rune]string
wantErr bool
expectedErr error
}{
{name: "NilKeySelectorNoComparerNoElementSelector",
args: args{
source: Empty[string](),
},
wantErr: true,
expectedErr: ErrNilSelector,
},
{name: "JustKeySelector",
args: args{
source: VarToSeq("zero", "one", "two"),
keySelector: func(s string) rune { return []rune(s)[0] },
},
want: map[rune]string{'z': "zero", 'o': "one", 't': "two"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToMap(tt.args.source, tt.args.keySelector)
if (err != nil) != tt.wantErr {
t.Errorf("ToMap() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if !errors.Is(err, tt.expectedErr) {
t.Errorf("ToMap() error = %v, expectedErr %v", err, tt.expectedErr)
}
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToMap() = %v, want %v", got, tt.want)
}
})
}
}
func TestToMap_string_string(t *testing.T) {
type args struct {
source iter.Seq[string]
keySelector func(string) string
}
tests := []struct {
name string
args args
want map[string]string
wantErr bool
expectedErr error
}{
{name: "DuplicateKeys",
args: args{
source: VarToSeq("zero", "One", "Two", "three"),
keySelector: func(s string) string { return strings.ToLower(string([]rune(s)[:1])) },
},
wantErr: true,
expectedErr: ErrDuplicateKeys,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ToMap(tt.args.source, tt.args.keySelector)
if (err != nil) != tt.wantErr {
t.Errorf("ToMap() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if !errors.Is(err, tt.expectedErr) {
t.Errorf("ToMap() error = %v, expectedErr %v", err, tt.expectedErr)
}
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToMap() = %v, want %v", got, tt.want)
}
})
}
}
func TestToMapSel_string_rune_int(t *testing.T) {
type args struct {
source iter.Seq[string]
keySelector func(string) rune
elementSelector func(string) int
}
tests := []struct {
name string
args args
want map[rune]int
}{
{name: "KeyAndElementSelector",
args: args{
source: VarToSeq("zero", "one", "two"),
keySelector: func(s string) rune { return []rune(s)[0] },
elementSelector: func(s string) int { return len(s) },
},
want: map[rune]int{'z': 4, 'o': 3, 't': 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := ToMapSel(tt.args.source, tt.args.keySelector, tt.args.elementSelector)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToMapSel() = %v, want %v", got, tt.want)
}
})
}
}
func TestCustomSelector_string_string_int(t *testing.T) {
source := []string{"zero", "one", "THREE"}
keySelector := func(s string) string { return strings.ToLower(string([]rune(s)[0])) }
elementSelector := func(s string) int { return len(s) }
got, _ := ToMapSel(slices.Values(source), keySelector, elementSelector)
if len(got) != 3 {
t.Errorf("len(ToMapSel()) = %v, want 3", len(got))
}
want := map[string]int{"z": 4, "o": 3, "t": 5}
if !reflect.DeepEqual(got, want) {
t.Errorf("ToMapSel() = %v, want %v", got, want)
}
}
// ToDictionaryEx1 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.todictionary
func ExampleToMap() {
packages := []Package{
{Company: "Coho Vineyard", Weight: 25.2, TrackingNumber: 89453312},
{Company: "Lucerne Publishing", Weight: 18.7, TrackingNumber: 89112755},
{Company: "Wingtip Toys", Weight: 6.0, TrackingNumber: 299456122},
{Company: "Adventure Works", Weight: 33.8, TrackingNumber: 4665518773},
}
// Create a map of Package objects, using TrackingNumber as the key.
dictionary, _ := ToMap(
slices.Values(packages),
func(p Package) int64 { return p.TrackingNumber },
)
for k, p := range dictionary {
fmt.Printf("Key %d: %s, %g pounds\n", k, p.Company, p.Weight)
}
// Unordered output:
// Key 89453312: Coho Vineyard, 25.2 pounds
// Key 89112755: Lucerne Publishing, 18.7 pounds
// Key 299456122: Wingtip Toys, 6 pounds
// Key 4665518773: Adventure Works, 33.8 pounds
}