-
Notifications
You must be signed in to change notification settings - Fork 6
/
groupjoin_test.go
181 lines (169 loc) · 5.32 KB
/
groupjoin_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
170
171
172
173
174
175
176
177
178
179
180
181
package go2linq
import (
"fmt"
"iter"
"reflect"
"slices"
"strings"
"testing"
"github.com/solsw/errorhelper"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/GroupJoinTest.cs
func TestGroupJoin_SimpleGroupJoin(t *testing.T) {
got, _ := GroupJoin(
VarToSeq("first", "second", "third"),
VarToSeq("essence", "offer", "eating", "psalm"),
func(oel string) rune { return []rune(oel)[0] },
func(iel string) rune { return []rune(iel)[1] },
func(oel string, iels iter.Seq[string]) string {
ss, _ := Strings(iels)
return fmt.Sprintf("%v:%v", oel, strings.Join(ss, ";"))
},
)
want := VarToSeq("first:offer", "second:essence;psalm", "third:")
equal, _ := SequenceEqual(got, want)
if !equal {
t.Errorf("GroupJoin_SimpleGroupJoin = %v, want %v", StringDef(got), StringDef(want))
}
}
func TestGroupJoin_SameEnumerable(t *testing.T) {
outer := []string{"fs", "sf", "ff", "ss"}
inner := outer
groupJoin, _ := GroupJoin(
slices.Values(outer),
slices.Values(inner),
func(oel string) rune { return []rune(oel)[0] },
func(iel string) rune { return []rune(iel)[1] },
func(oel string, iels iter.Seq[string]) string {
ss, _ := Strings(iels)
return fmt.Sprintf("%v:%v", oel, strings.Join(ss, ";"))
},
)
got := slices.Collect(groupJoin)
want := []string{"fs:sf;ff", "sf:fs;ss", "ff:sf;ff", "ss:fs;ss"}
if !reflect.DeepEqual(got, want) {
t.Errorf("GroupJoin_SameEnumerable = %v, want %v", got, want)
}
}
func TestGroupJoinEq_CustomComparer(t *testing.T) {
outer := []string{"ABCxxx", "abcyyy", "defzzz", "ghizzz"}
inner := []string{"000abc", "111gHi", "222333", "333AbC"}
got, _ := GroupJoinEq(
slices.Values(outer),
slices.Values(inner),
func(oel string) string { return oel[:3] },
func(iel string) string { return iel[3:] },
func(oel string, iels iter.Seq[string]) string {
ss, _ := Strings(iels)
return fmt.Sprintf("%v:%v", oel, strings.Join(ss, ";"))
},
caseInsensitiveEqual,
)
want := VarToSeq("ABCxxx:000abc;333AbC", "abcyyy:000abc;333AbC", "defzzz:", "ghizzz:111gHi")
equal, _ := SequenceEqual(got, want)
if !equal {
t.Errorf("GroupJoinEq_CustomComparer = %v, want %v", StringDef(got), StringDef(want))
}
}
func TestGroupJoin_DifferentSourceTypes(t *testing.T) {
outer := []int{5, 3, 7, 4}
inner := []string{"bee", "giraffe", "tiger", "badger", "ox", "cat", "dog"}
got, _ := GroupJoin(
slices.Values(outer),
slices.Values(inner),
Identity[int],
func(iel string) int { return len(iel) },
func(oel int, iels iter.Seq[string]) string {
ss, _ := Strings(iels)
return fmt.Sprintf("%v:%v", oel, strings.Join(ss, ";"))
},
)
want := VarToSeq("5:tiger", "3:bee;cat;dog", "7:giraffe", "4:")
equal, _ := SequenceEqual(got, want)
if !equal {
t.Errorf("GroupJoin_DifferentSourceTypes = %v, want %v", StringDef(got), StringDef(want))
}
}
// GroupJoinEx1 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.groupjoin
func ExampleGroupJoin_ex1() {
magnus := Person{Name: "Hedlund, Magnus"}
terry := Person{Name: "Adams, Terry"}
charlotte := Person{Name: "Weiss, Charlotte"}
barley := Pet{Name: "Barley", Owner: terry}
boots := Pet{Name: "Boots", Owner: terry}
whiskers := Pet{Name: "Whiskers", Owner: charlotte}
daisy := Pet{Name: "Daisy", Owner: magnus}
// Create a list where each element is an OwnerAndPets type that contains a person's name and
// a collection of names of the pets they own.
people := []Person{magnus, terry, charlotte}
pets := []Pet{barley, boots, whiskers, daisy}
groupJoin, _ := GroupJoin(
slices.Values(people),
slices.Values(pets),
Identity[Person],
func(pet Pet) Person { return pet.Owner },
func(person Person, pets iter.Seq[Pet]) OwnerAndPets {
return OwnerAndPets{
OwnerName: person.Name,
Pets: errorhelper.Must(Select(pets, func(pet Pet) string { return pet.Name }))}
},
)
for obj := range groupJoin {
// Output the owner's name.
fmt.Printf("%s:\n", obj.OwnerName)
// Output each of the owner's pet's names.
for pet := range obj.Pets {
fmt.Printf(" %s\n", pet)
}
}
// Output:
// Hedlund, Magnus:
// Daisy
// Adams, Terry:
// Barley
// Boots
// Weiss, Charlotte:
// Whiskers
}
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/join-operations#query-expression-syntax-examples
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/join-operations#groupjoin
func ExampleGroupJoin_ex2() {
products := []Product{
{Name: "Cola", CategoryId: 0},
{Name: "Tea", CategoryId: 0},
{Name: "Apple", CategoryId: 1},
{Name: "Kiwi", CategoryId: 1},
{Name: "Carrot", CategoryId: 2},
}
categories := []Category{
{Id: 0, CategoryName: "Beverage"},
{Id: 1, CategoryName: "Fruit"},
{Id: 2, CategoryName: "Vegetable"},
}
// Join categories and product based on CategoryId and grouping result
productGroups, _ := GroupJoin(
slices.Values(categories),
slices.Values(products),
func(category Category) int { return category.Id },
func(product Product) int { return product.CategoryId },
func(category Category, products iter.Seq[Product]) iter.Seq[Product] {
return products
},
)
for productGroup := range productGroups {
fmt.Println("Group")
for product := range productGroup {
fmt.Printf("%8s\n", product.Name)
}
}
// Output:
// Group
// Cola
// Tea
// Group
// Apple
// Kiwi
// Group
// Carrot
}