-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
groupjoin_test.go
57 lines (48 loc) · 1.19 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
package linq_test
import (
"reflect"
"testing"
"github.com/makiuchi-d/linq/v2"
)
func TestGroupJoin(t *testing.T) {
type Person struct {
Name string
}
type Pet struct {
Name string
Owner *Person
}
alice := &Person{"Alice"}
bob := &Person{"Bob"}
charlie := &Person{"Charlie"}
abby := &Pet{"Abby", alice}
bailey := &Pet{"Bailey", bob}
bella := &Pet{"Bella", bob}
cody := &Pet{"Cody", charlie}
people := linq.FromSlice([]*Person{alice, bob, charlie})
pets := linq.FromSlice([]*Pet{cody, bella, abby, bailey})
r, err := linq.ToSlice(
linq.GroupJoin(
people, pets,
func(p *Person) (*Person, error) { return p, nil },
func(p *Pet) (*Person, error) { return p.Owner, nil },
func(person *Person, pets linq.Enumerable[*Pet]) ([]string, error) {
names, err := linq.ToSlice(
linq.Select(pets, func(p *Pet) (string, error) { return p.Name, nil }))
if err != nil {
return nil, err
}
return append([]string{person.Name}, names...), nil
}))
if err != nil {
t.Fatalf("%v", err)
}
exp := [][]string{
{"Alice", "Abby"},
{"Bob", "Bella", "Bailey"},
{"Charlie", "Cody"},
}
if !reflect.DeepEqual(r, exp) {
t.Fatalf("%v, wants %v", r, exp)
}
}