-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathunionby_test.go
168 lines (163 loc) · 4.02 KB
/
unionby_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
package go2linq
import (
"cmp"
"errors"
"iter"
"strconv"
"testing"
"github.com/solsw/iterhelper"
)
func TestUnionBy_string_int(t *testing.T) {
type args struct {
first iter.Seq[string]
second iter.Seq[string]
keySelector func(string) int
}
tests := []struct {
name string
args args
want iter.Seq[string]
}{
{name: "1",
args: args{
first: iterhelper.VarSeq("one", "three", "five"),
second: iterhelper.VarSeq("two", "four"),
keySelector: func(s string) int { return len(s) },
},
want: iterhelper.VarSeq("one", "three", "five"),
},
{name: "2",
args: args{
first: iterhelper.VarSeq("two", "four"),
second: iterhelper.VarSeq("one", "three", "five"),
keySelector: func(s string) int { return len(s) },
},
want: iterhelper.VarSeq("two", "four", "three"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := UnionBy(tt.args.first, tt.args.second, tt.args.keySelector)
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("UnionBy() = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(tt.want))
}
})
}
}
func TestUnionBy_Planet(t *testing.T) {
type args struct {
first iter.Seq[Planet]
second iter.Seq[Planet]
keySelector func(Planet) Planet
}
tests := []struct {
name string
args args
want iter.Seq[Planet]
}{
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/set-operations#union-and-unionby
{name: "UnionBy",
args: args{
first: iterhelper.VarSeq(Mercury, Venus, Earth, Mars, Jupiter),
second: iterhelper.VarSeq(Mars, Jupiter, Saturn, Uranus, Neptune),
keySelector: Identity[Planet],
},
want: iterhelper.VarSeq(Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := UnionBy(tt.args.first, tt.args.second, tt.args.keySelector)
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("UnionBy() = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(tt.want))
}
})
}
}
func TestUnionByCmp_int_string(t *testing.T) {
ii, _ := Range(1, 10)
type args struct {
first iter.Seq[int]
second iter.Seq[int]
keySelector func(int) string
compare func(string, string) int
}
tests := []struct {
name string
args args
want iter.Seq[int]
wantErr bool
expectedErr error
}{
{name: "NilFirst",
args: args{
first: nil,
second: ii,
keySelector: nil,
compare: nil,
},
wantErr: true,
expectedErr: ErrNilSource,
},
{name: "NilSecond",
args: args{
first: ii,
second: nil,
keySelector: nil,
compare: nil,
},
wantErr: true,
expectedErr: ErrNilSource,
},
{name: "NilSelector",
args: args{
first: ii,
second: ii,
keySelector: nil,
compare: nil,
},
wantErr: true,
expectedErr: ErrNilSelector,
},
{name: "NilComparer",
args: args{
first: ii,
second: ii,
keySelector: func(i int) string { return strconv.Itoa(i) },
compare: nil,
},
wantErr: true,
expectedErr: ErrNilCompare,
},
{name: "SameEnumerable1",
args: args{
first: ii,
second: ii,
keySelector: func(i int) string { return strconv.FormatBool(i%2 == 0) },
compare: cmp.Compare[string],
},
want: iterhelper.VarSeq(1, 2),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UnionByCmp(tt.args.first, tt.args.second, tt.args.keySelector, tt.args.compare)
if (err != nil) != tt.wantErr {
t.Errorf("UnionByCmp() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if !errors.Is(err, tt.expectedErr) {
t.Errorf("UnionByCmp() error = %v, expectedErr %v", err, tt.expectedErr)
}
return
}
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("UnionByCmp() = %v, want %v", got, tt.want)
}
})
}
}