-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_collection.go
More file actions
270 lines (222 loc) · 6.62 KB
/
Copy pathassert_collection.go
File metadata and controls
270 lines (222 loc) · 6.62 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package testastic
import (
"fmt"
"reflect"
"slices"
"strconv"
"strings"
"testing"
)
// Display truncation thresholds.
const (
maxSliceDisplayLen = 10
maxMapDisplayLen = 10
)
// Len asserts that the collection has the expected length.
// Works with slices, maps, strings, arrays, and channels.
func Len(tb testing.TB, collection any, expected int) {
tb.Helper()
actual := getLen(collection)
if actual == -1 {
tb.Fatalf(
"testastic: assertion failed\n\n Len\n error: cannot get length of %T",
collection,
)
return
}
if actual != expected {
tb.Errorf(
"testastic: assertion failed\n\n Len\n expected: %s\n actual: %s",
red(strconv.Itoa(expected)), green(strconv.Itoa(actual)),
)
}
}
// Empty asserts that the collection is empty.
// Works with slices, maps, strings, arrays, and channels.
func Empty(tb testing.TB, collection any) {
tb.Helper()
length := getLen(collection)
if length == -1 {
tb.Fatalf(
"testastic: assertion failed\n\n Empty\n error: cannot get length of %T",
collection,
)
return
}
if length != 0 {
tb.Errorf(
"testastic: assertion failed\n\n Empty\n expected: %s\n actual: %s",
red("empty (length 0)"), green(fmt.Sprintf("length %d", length)),
)
}
}
// NotEmpty asserts that the collection is not empty.
// Works with slices, maps, strings, arrays, and channels.
func NotEmpty(tb testing.TB, collection any) {
tb.Helper()
length := getLen(collection)
if length == -1 {
tb.Fatalf(
"testastic: assertion failed\n\n NotEmpty\n error: cannot get length of %T",
collection,
)
return
}
if length == 0 {
tb.Errorf(
"testastic: assertion failed\n\n NotEmpty\n expected: %s\n actual: %s",
red("non-empty"), green("empty (length 0)"),
)
}
}
// SliceContains asserts that a slice contains element using == comparison.
// Reports a non-fatal error on failure, allowing the test to continue.
func SliceContains[T comparable](tb testing.TB, slice []T, element T) {
tb.Helper()
if slices.Contains(slice, element) {
return
}
tb.Errorf(
"testastic: assertion failed\n\n SliceContains\n slice: %s\n element: %s (not found)",
green(formatSlice(slice)), red(formatVal(element)),
)
}
// SliceNotContains asserts that a slice does not contain element using == comparison.
func SliceNotContains[T comparable](tb testing.TB, slice []T, element T) {
tb.Helper()
if slices.Contains(slice, element) {
tb.Errorf(
"testastic: assertion failed\n\n SliceNotContains\n slice: %s\n element: %s (found)",
green(formatSlice(slice)), red(formatVal(element)),
)
}
}
// SliceEqual asserts that two slices are equal (same length and elements in same order).
func SliceEqual[T comparable](tb testing.TB, expected, actual []T) {
tb.Helper()
if len(expected) != len(actual) {
tb.Errorf(
"testastic: assertion failed\n\n SliceEqual\n expected: %s (len %d)\n actual: %s (len %d)",
red(formatSlice(expected)), len(expected), green(formatSlice(actual)), len(actual),
)
return
}
for i := range expected {
if expected[i] != actual[i] {
tb.Errorf(
"testastic: assertion failed\n\n SliceEqual\n diff at [%d]: %s != %s",
i, red(formatVal(expected[i])), green(formatVal(actual[i])),
)
return
}
}
}
// MapHasKey asserts that a map contains the given key using map index lookup.
func MapHasKey[K comparable, V any](tb testing.TB, m map[K]V, key K) {
tb.Helper()
if _, ok := m[key]; !ok {
tb.Errorf(
"testastic: assertion failed\n\n MapHasKey\n map: %s\n key: %s (not found)",
green(formatMap(m)), red(formatVal(key)),
)
}
}
// MapNotHasKey asserts that a map does not contain the given key using map index lookup.
func MapNotHasKey[K comparable, V any](tb testing.TB, m map[K]V, key K) {
tb.Helper()
if _, ok := m[key]; ok {
tb.Errorf(
"testastic: assertion failed\n\n MapNotHasKey\n map: %s\n key: %s (found)",
green(formatMap(m)), red(formatVal(key)),
)
}
}
// MapHasValue asserts that a map contains the given value using == comparison.
// Performs a linear scan of all map values.
func MapHasValue[K comparable, V comparable](tb testing.TB, m map[K]V, value V) {
tb.Helper()
for _, v := range m {
if v == value {
return
}
}
tb.Errorf(
"testastic: assertion failed\n\n MapHasValue\n map: %s\n value: %s (not found)",
green(formatMap(m)), red(formatVal(value)),
)
}
// MapNotHasValue asserts that a map does not contain the given value using == comparison.
// Performs a linear scan of all map values.
func MapNotHasValue[K comparable, V comparable](tb testing.TB, m map[K]V, value V) {
tb.Helper()
for _, v := range m {
if v == value {
tb.Errorf(
"testastic: assertion failed\n\n MapNotHasValue\n map: %s\n value: %s (found)",
green(formatMap(m)), red(formatVal(value)),
)
return
}
}
}
// MapEqual asserts that two maps have the same keys and values using == comparison.
// Reports the first differing key found.
func MapEqual[K comparable, V comparable](tb testing.TB, expected, actual map[K]V) {
tb.Helper()
if len(expected) != len(actual) {
tb.Errorf(
"testastic: assertion failed\n\n MapEqual\n expected: %s (len %d)\n actual: %s (len %d)",
red(formatMap(expected)), len(expected), green(formatMap(actual)), len(actual),
)
return
}
for k, ev := range expected {
av, ok := actual[k]
if !ok {
tb.Errorf(
"testastic: assertion failed\n\n MapEqual\n missing key: %s",
red(formatVal(k)),
)
return
}
if ev != av {
tb.Errorf(
"testastic: assertion failed\n\n MapEqual\n diff at key %s: %s != %s",
formatVal(k), red(formatVal(ev)), green(formatVal(av)),
)
return
}
}
}
// getLen returns the length of a collection, or -1 if not a collection type.
func getLen(collection any) int {
if collection == nil {
return 0
}
v := reflect.ValueOf(collection)
//nolint:exhaustive // Only collection types have length.
switch v.Kind() {
case reflect.Slice, reflect.Map, reflect.String, reflect.Array, reflect.Chan:
return v.Len()
default:
return -1
}
}
// formatSlice formats a slice for display, truncating if too long.
func formatSlice[T any](s []T) string {
if len(s) <= maxSliceDisplayLen {
return fmt.Sprintf("%v", s)
}
head := make([]string, maxSliceDisplayLen)
for i := range head {
head[i] = fmt.Sprintf("%v", s[i])
}
return fmt.Sprintf("[%s ... (%d total)]", strings.Join(head, " "), len(s))
}
// formatMap formats a map for display, truncating if too many entries.
func formatMap[K comparable, V any](m map[K]V) string {
if len(m) <= maxMapDisplayLen {
return fmt.Sprintf("%v", m)
}
return fmt.Sprintf("map[...] (%d entries)", len(m))
}