-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
203 lines (190 loc) · 4.41 KB
/
slice.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
199
200
201
202
203
package assert
import (
"fmt"
"slices"
"testing"
)
// SliceNil asserts that s is nil.
//
//nolint:thelper // It's called below.
func SliceNil[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool {
ok := s == nil
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_nil[%s]", typeName[E]()),
"not nil:\ns = "+ValueStringer(s),
opts...,
)
}
return ok
}
// SliceNotNil asserts that s is not nil.
//
//nolint:thelper // It's called below.
func SliceNotNil[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool {
ok := s != nil
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_not_nil[%s]", typeName[E]()),
"nil",
opts...,
)
}
return ok
}
// SliceEmpty asserts that s is empty.
//
//nolint:thelper // It's called below.
func SliceEmpty[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool {
ok := len(s) == 0
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_empty[%s]", typeName[E]()),
fmt.Sprintf("not empty:\nlength = %d\ns = %s", len(s), ValueStringer(s)),
opts...,
)
}
return ok
}
// SliceNotEmpty asserts that s is not empty.
//
//nolint:thelper // It's called below.
func SliceNotEmpty[S ~[]E, E any](tb testing.TB, s S, opts ...Option) bool {
ok := len(s) != 0
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_not_empty[%s]", typeName[E]()),
"empty",
opts...,
)
}
return ok
}
// SliceLen asserts that s has length l.
//
//nolint:thelper // It's called below.
func SliceLen[S ~[]E, E any](tb testing.TB, s S, l int, opts ...Option) bool {
ok := len(s) == l
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_len[%s]", typeName[E]()),
fmt.Sprintf("unexpected length:\nexpected = %d\nactual = %d", l, len(s)),
opts...,
)
}
return ok
}
// SliceEqual asserts that s1 and s2 are equal.
//
//nolint:thelper // It's called below.
func SliceEqual[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool {
ok := slices.Equal(s1, s2)
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_equal[%s]", typeName[E]()),
fmt.Sprintf("not equal:\ns1 = %s\ns2 = %s", ValueStringer(s1), ValueStringer(s2)),
opts...,
)
}
return ok
}
// SliceNotEqual asserts that s1 and s2 are not equal.
//
//nolint:thelper // It's called below.
func SliceNotEqual[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool {
ok := !slices.Equal(s1, s2)
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_not_equal[%s]", typeName[E]()),
fmt.Sprintf("equal:\ns1 = %s\ns2 = %s", ValueStringer(s1), ValueStringer(s2)),
opts...,
)
}
return ok
}
// SliceContains asserts that s contains v.
//
//nolint:thelper // It's called below.
func SliceContains[S ~[]E, E comparable](tb testing.TB, s S, v E, opts ...Option) bool {
ok := slices.Contains(s, v)
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_contains[%s]", typeName[E]()),
fmt.Sprintf("not contains:\ns = %s\nv = %s", ValueStringer(s), ValueStringer(v)),
opts...,
)
}
return ok
}
// SliceNotContains asserts that s does not contain v.
//
//nolint:thelper // It's called below.
func SliceNotContains[S ~[]E, E comparable](tb testing.TB, s S, v E, opts ...Option) bool {
ok := !slices.Contains(s, v)
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_not_contains[%s]", typeName[E]()),
fmt.Sprintf("contains:\ns = %s\nv = %s", ValueStringer(s), ValueStringer(v)),
opts...,
)
}
return ok
}
// SliceContainsAll asserts that s1 contains all elements in s2.
//
//nolint:thelper // It's called below.
func SliceContainsAll[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool {
ok := sliceContainsAll(s1, s2)
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_contains_all[%s]", typeName[E]()),
fmt.Sprintf("not contains all:\ns1 = %s\ns2 = %s", ValueStringer(s1), ValueStringer(s2)),
opts...,
)
}
return ok
}
// SliceNotContainsAll asserts that s1 does not contain all elements in s2.
//
//nolint:thelper // It's called below.
func SliceNotContainsAll[S ~[]E, E comparable](tb testing.TB, s1, s2 S, opts ...Option) bool {
ok := !sliceContainsAll(s1, s2)
if !ok {
tb.Helper()
Fail(
tb,
fmt.Sprintf("slice_not_contains_all[%s]", typeName[E]()),
fmt.Sprintf("contains all:\ns1 = %s\ns2 = %s", ValueStringer(s1), ValueStringer(s2)),
opts...,
)
}
return ok
}
func sliceContainsAll[S ~[]E, E comparable](s1, s2 S) bool {
for _, v := range s2 {
if !slices.Contains(s1, v) {
return false
}
}
return true
}