-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjoin_test.go
198 lines (187 loc) · 6.27 KB
/
join_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package go2linq
import (
"fmt"
"iter"
"slices"
"strings"
"testing"
"github.com/solsw/iterhelper"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/JoinTest.cs
func TestJoin_string_rune(t *testing.T) {
seq := iterhelper.VarSeq("fs", "sf", "ff", "ss")
type args struct {
outer iter.Seq[string]
inner iter.Seq[string]
outerKeySelector func(string) rune
innerKeySelector func(string) rune
resultSelector func(string, string) string
}
tests := []struct {
name string
args args
want iter.Seq[string]
}{
{name: "SimpleJoin",
args: args{
outer: iterhelper.VarSeq("first", "second", "third"),
inner: iterhelper.VarSeq("essence", "offer", "eating", "psalm"),
outerKeySelector: func(oel string) rune { return ([]rune(oel))[0] },
innerKeySelector: func(iel string) rune { return ([]rune(iel))[1] },
resultSelector: func(oel, iel string) string { return oel + ":" + iel },
},
want: iterhelper.VarSeq("first:offer", "second:essence", "second:psalm"),
},
{name: "SameEnumerable",
args: args{
outer: seq,
inner: seq,
outerKeySelector: func(oel string) rune { return ([]rune(oel))[0] },
innerKeySelector: func(iel string) rune { return ([]rune(iel))[1] },
resultSelector: func(oel, iel string) string { return oel + ":" + iel },
},
want: iterhelper.VarSeq("fs:sf", "fs:ff", "sf:fs", "sf:ss", "ff:sf", "ff:ff", "ss:fs", "ss:ss"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Join(tt.args.outer, tt.args.inner, tt.args.outerKeySelector, tt.args.innerKeySelector, tt.args.resultSelector)
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("Join() = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(tt.want))
}
})
}
}
func TestJoin_string(t *testing.T) {
type args struct {
outer iter.Seq[string]
inner iter.Seq[string]
outerKeySelector func(string) string
innerKeySelector func(string) string
resultSelector func(string, string) string
}
tests := []struct {
name string
args args
want iter.Seq[string]
}{
{name: "CustomComparer",
args: args{
outer: iterhelper.VarSeq("ABCxxx", "abcyyy", "defzzz", "ghizzz"),
inner: iterhelper.VarSeq("000abc", "111gHi", "222333"),
outerKeySelector: func(oel string) string {
return strings.ToLower(oel[:3])
},
innerKeySelector: func(iel string) string {
return strings.ToLower(iel[3:])
},
resultSelector: func(oel, iel string) string { return oel + ":" + iel },
},
want: iterhelper.VarSeq("ABCxxx:000abc", "abcyyy:000abc", "ghizzz:111gHi"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Join(tt.args.outer, tt.args.inner, tt.args.outerKeySelector, tt.args.innerKeySelector, tt.args.resultSelector)
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("Join() = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(tt.want))
}
})
}
}
func TestJoinEq_CustomComparer(t *testing.T) {
got, _ := JoinEq(
iterhelper.VarSeq("ABCxxx", "abcyyy", "defzzz", "ghizzz"),
iterhelper.VarSeq("000abc", "111gHi", "222333"),
func(oel string) string { return oel[:3] },
func(iel string) string { return iel[3:] },
func(oel, iel string) string { return oel + ":" + iel },
caseInsensitiveEqual,
)
want := iterhelper.VarSeq("ABCxxx:000abc", "abcyyy:000abc", "ghizzz:111gHi")
equal, _ := SequenceEqual(got, want)
if !equal {
t.Errorf("JoinEq = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(want))
}
}
func TestJoin_DifferentSourceTypes(t *testing.T) {
got, _ := Join(
iterhelper.VarSeq(5, 3, 7),
iterhelper.VarSeq("bee", "giraffe", "tiger", "badger", "ox", "cat", "dog"),
Identity[int],
func(iel string) int { return len(iel) },
func(oel int, iel string) string { return fmt.Sprintf("%d:%s", oel, iel) },
)
want := iterhelper.VarSeq("5:tiger", "3:bee", "3:cat", "3:dog", "7:giraffe")
equal, _ := SequenceEqual(got, want)
if !equal {
t.Errorf("Join = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(want))
}
}
// JoinEx1 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.join
func ExampleJoin_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 of Person-Pet pairs where each element is an OwnerNameAndPetName type that contains a
// Pet's name and the name of the Person that owns the Pet.
join, _ := Join(
iterhelper.VarSeq(magnus, terry, charlotte),
iterhelper.VarSeq(barley, boots, whiskers, daisy),
Identity[Person],
func(pet Pet) Person { return pet.Owner },
func(person Person, pet Pet) OwnerNameAndPetName {
return OwnerNameAndPetName{Owner: person.Name, Pet: pet.Name}
},
)
for obj := range join {
fmt.Printf("%s - %s\n", obj.Owner, obj.Pet)
}
// Output:
// Hedlund, Magnus - Daisy
// Adams, Terry - Barley
// Adams, Terry - 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#join
func ExampleJoin_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 products and categories based on CategoryId
join, _ := Join(
slices.Values(products),
slices.Values(categories),
func(product Product) int { return product.CategoryId },
func(category Category) int { return category.Id },
func(product Product, category Category) string {
return fmt.Sprintf("%s - %s", product.Name, category.CategoryName)
},
)
for item := range join {
fmt.Println(item)
}
// Output:
// Cola - Beverage
// Tea - Beverage
// Apple - Fruit
// Kiwi - Fruit
// Carrot - Vegetable
}