-
Notifications
You must be signed in to change notification settings - Fork 6
/
contains_test.go
178 lines (169 loc) · 4.11 KB
/
contains_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
package go2linq
import (
"fmt"
"iter"
"slices"
"strings"
"testing"
"github.com/solsw/errorhelper"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/ContainsTest.cs
func TestContains_string(t *testing.T) {
type args struct {
source iter.Seq[string]
value string
}
tests := []struct {
name string
args args
want bool
}{
{name: "NoMatchNoComparer",
args: args{
source: VarToSeq("foo", "bar", "baz"),
value: "BAR",
},
want: false,
},
{name: "MatchNoComparer",
args: args{
source: VarToSeq("foo", "bar", "baz"),
value: strings.ToLower("BAR"),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Contains(tt.args.source, tt.args.value)
if got != tt.want {
t.Errorf("Contains() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainsEq_string(t *testing.T) {
type args struct {
source iter.Seq[string]
value string
equal func(string, string) bool
}
tests := []struct {
name string
args args
want bool
}{
{name: "NoMatchWithCustomComparer",
args: args{
source: VarToSeq("foo", "bar", "baz"),
value: "gronk",
equal: caseInsensitiveEqual,
},
want: false,
},
{name: "MatchWithCustomComparer",
args: args{
source: VarToSeq("foo", "bar", "baz"),
value: "BAR",
equal: caseInsensitiveEqual,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := ContainsEq(tt.args.source, tt.args.value, tt.args.equal)
if got != tt.want {
t.Errorf("ContainsEq() = %v, want %v", got, tt.want)
}
})
}
}
func TestContainsEq_int(t *testing.T) {
type args struct {
source iter.Seq[int]
value int
equal func(int, int) bool
}
tests := []struct {
name string
args args
want bool
}{
{name: "ImmediateReturnWhenMatchIsFound",
args: args{
source: VarToSeq(10, 1, 5, 0),
value: 2,
equal: func(i1, i2 int) bool { return 10/i1 == i2 },
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := ContainsEq(tt.args.source, tt.args.value, tt.args.equal)
if got != tt.want {
t.Errorf("ContainsEq() = %v, want %v", got, tt.want)
}
})
}
}
// first example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.contains
func ExampleContains_ex1() {
fruits := []string{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
fruit := "mango"
hasMango, _ := Contains(slices.Values(fruits), fruit)
var what string
if hasMango {
what = "does"
} else {
what = "does not"
}
fmt.Printf("The array %s contain '%s'.\n", what, fruit)
// Output:
// The array does contain 'mango'.
}
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/quantifier-operations#query-expression-syntax-examples
// https://learn.microsoft.com/dotnet/csharp/programming-guide/concepts/linq/quantifier-operations#contains
func ExampleContains_ex2() {
markets := []Market{
{Name: "Emily's", Items: []string{"kiwi", "cheery", "banana"}},
{Name: "Kim's", Items: []string{"melon", "mango", "olive"}},
{Name: "Adam's", Items: []string{"kiwi", "apple", "orange"}},
}
where, _ := Where(
slices.Values(markets),
func(m Market) bool {
return errorhelper.Must(Contains(slices.Values(m.Items), "kiwi"))
},
)
names, _ := Select(where, func(m Market) string { return m.Name })
for name := range names {
fmt.Printf("%s market\n", name)
}
// Output:
// Emily's market
// Adam's market
}
// second example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.contains
func ExampleContainsEq() {
fruits := []Product{
{Name: "apple", Code: 9},
{Name: "orange", Code: 4},
{Name: "lemon", Code: 12},
}
apple := Product{Name: "apple", Code: 9}
kiwi := Product{Name: "kiwi", Code: 8}
var equal = func(p1, p2 Product) bool {
return p1.Code == p2.Code && p1.Name == p2.Name
}
hasApple, _ := ContainsEq(VarToSeq(fruits...), apple, equal)
hasKiwi, _ := ContainsEq(VarToSeq(fruits...), kiwi, equal)
fmt.Printf("Apple? %t\n", hasApple)
fmt.Printf("Kiwi? %t\n", hasKiwi)
// Output:
// Apple? true
// Kiwi? false
}