-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathas_test.go
More file actions
507 lines (431 loc) · 12.1 KB
/
Copy pathas_test.go
File metadata and controls
507 lines (431 loc) · 12.1 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package core
import (
"errors"
"fmt"
"testing"
)
var _ TestCase = asTestCase{}
var _ TestCase = asFnTestCase{}
var _ TestCase = sliceAsTestCase{}
var _ TestCase = sliceAsFnTestCase{}
var _ TestCase = asErrorTestCase{}
var _ TestCase = asErrorsTestCase{}
const testHello = "hello"
// asTestCase tests As function
type asTestCase struct {
// Interface fields - input/output test data
input any
want any
// String fields - test identification
name string
// Boolean fields (1 byte) - expected result flags
wantOK bool
}
// newAsTestCase creates a new asTestCase
func newAsTestCase(name string, input, want any, wantOK bool) asTestCase {
return asTestCase{
name: name,
input: input,
want: want,
wantOK: wantOK,
}
}
func (tc asTestCase) Name() string {
return tc.name
}
func (tc asTestCase) Test(t *testing.T) {
t.Helper()
switch want := tc.want.(type) {
case string:
tc.testStringConversion(t, want)
case int:
tc.testIntConversion(t, want)
case error:
tc.testErrorConversion(t, want)
default:
tc.testDefaultConversion(t)
}
}
func (tc asTestCase) testStringConversion(t *testing.T, want string) {
t.Helper()
got, ok := As[any, string](tc.input)
if ok != tc.wantOK {
t.Errorf("As() ok = %v, want %v", ok, tc.wantOK)
}
if got != want {
t.Errorf("As() got = %v, want %v", got, want)
}
}
func (tc asTestCase) testIntConversion(t *testing.T, want int) {
t.Helper()
got, ok := As[any, int](tc.input)
if ok != tc.wantOK {
t.Errorf("As() ok = %v, want %v", ok, tc.wantOK)
}
if got != want {
t.Errorf("As() got = %v, want %v", got, want)
}
}
func (tc asTestCase) testErrorConversion(t *testing.T, want error) {
t.Helper()
got, ok := As[any, error](tc.input)
if ok != tc.wantOK {
t.Errorf("As() ok = %v, want %v", ok, tc.wantOK)
}
if ok && got.Error() != want.Error() {
t.Errorf("As() got = %v, want %v", got, want)
}
}
func (tc asTestCase) testDefaultConversion(t *testing.T) {
t.Helper()
// Test cases where conversion should fail
got, ok := As[any, string](tc.input)
if ok != tc.wantOK {
t.Errorf("As() ok = %v, want %v", ok, tc.wantOK)
}
if tc.wantOK && got != "" {
t.Errorf("As() got = %v, want zero value", got)
}
}
// asFnTestCase tests AsFn function
type asFnTestCase struct {
name string
fn func(any) (string, bool)
input any
want string
wantOK bool
}
// newAsFnTestCase creates a new asFnTestCase
func newAsFnTestCase(name string, fn func(any) (string, bool), input any, want string, wantOK bool) asFnTestCase {
return asFnTestCase{
name: name,
fn: fn,
input: input,
want: want,
wantOK: wantOK,
}
}
func (tc asFnTestCase) Name() string {
return tc.name
}
func (tc asFnTestCase) Test(t *testing.T) {
t.Helper()
got, ok := AsFn(tc.fn, tc.input)
if ok != tc.wantOK {
t.Errorf("AsFn() ok = %v, want %v", ok, tc.wantOK)
}
if got != tc.want {
t.Errorf("AsFn() got = %v, want %v", got, tc.want)
}
}
// sliceAsTestCase tests SliceAs function
type sliceAsTestCase struct {
name string
input []any
want []string
}
// newSliceAsTestCase creates a new sliceAsTestCase
func newSliceAsTestCase(name string, input []any, want []string) sliceAsTestCase {
return sliceAsTestCase{
name: name,
input: input,
want: want,
}
}
func (tc sliceAsTestCase) Name() string {
return tc.name
}
func (tc sliceAsTestCase) Test(t *testing.T) {
t.Helper()
got := SliceAs[any, string](tc.input)
if !AssertEqual(t, len(tc.want), len(got), "slice length") {
return
}
for i, v := range got {
if v != tc.want[i] {
t.Errorf("SliceAs()[%d] = %v, want %v", i, v, tc.want[i])
}
}
}
// sliceAsFnTestCase tests SliceAsFn function
type sliceAsFnTestCase struct {
name string
fn func(any) (string, bool)
input []any
want []string
}
// newSliceAsFnTestCase creates a new sliceAsFnTestCase
func newSliceAsFnTestCase(name string, fn func(any) (string, bool), input []any, want []string) sliceAsFnTestCase {
return sliceAsFnTestCase{
name: name,
fn: fn,
input: input,
want: want,
}
}
func (tc sliceAsFnTestCase) Name() string {
return tc.name
}
func (tc sliceAsFnTestCase) Test(t *testing.T) {
t.Helper()
got := SliceAsFn(tc.fn, tc.input)
if !AssertEqual(t, len(tc.want), len(got), "slice length") {
return
}
for i, v := range got {
if v != tc.want[i] {
t.Errorf("SliceAsFn()[%d] = %v, want %v", i, v, tc.want[i])
}
}
}
// Custom types for testing AsError
type errorWithAsError struct {
msg string
}
func (e errorWithAsError) AsError() error {
if e.msg == "" {
return nil
}
return errors.New(e.msg)
}
type errorWithOK struct {
msg string
ok bool
}
func (e errorWithOK) Error() string {
return e.msg
}
func (e errorWithOK) OK() bool {
return e.ok
}
// asErrorTestCase tests AsError function
type asErrorTestCase struct {
// Interface fields - input test data
input any
// String fields - test identification and expected message
name string
wantMsg string
// Boolean fields (1 byte) - expected result flags
wantErr bool
}
// newAsErrorTestCase creates a new asErrorTestCase
func newAsErrorTestCase(name string, input any, wantMsg string, wantErr bool) asErrorTestCase {
return asErrorTestCase{
name: name,
input: input,
wantMsg: wantMsg,
wantErr: wantErr,
}
}
func (tc asErrorTestCase) Name() string {
return tc.name
}
func (tc asErrorTestCase) Test(t *testing.T) {
t.Helper()
got := AsError(tc.input)
if (got != nil) != tc.wantErr {
t.Errorf("AsError() error = %v, wantErr %v", got, tc.wantErr)
return
}
if got != nil && got.Error() != tc.wantMsg {
t.Errorf("AsError() error message = %v, want %v", got.Error(), tc.wantMsg)
}
}
// asErrorsTestCase tests AsErrors function
type asErrorsTestCase struct {
name string
input []any
wantMsgs []string
wantLen int
}
// newAsErrorsTestCase creates a new asErrorsTestCase
func newAsErrorsTestCase(name string, input []any, wantMsgs []string, wantLen int) asErrorsTestCase {
return asErrorsTestCase{
name: name,
input: input,
wantMsgs: wantMsgs,
wantLen: wantLen,
}
}
func (tc asErrorsTestCase) Name() string {
return tc.name
}
func (tc asErrorsTestCase) Test(t *testing.T) {
t.Helper()
got := AsErrors(tc.input)
if !AssertEqual(t, tc.wantLen, len(got), "slice length") {
return
}
for i, err := range got {
if err.Error() != tc.wantMsgs[i] {
t.Errorf("AsErrors()[%d] = %v, want %v", i, err.Error(), tc.wantMsgs[i])
}
}
}
func TestAs(t *testing.T) {
testCases := []asTestCase{
newAsTestCase("string to string", testHello, testHello, true),
newAsTestCase("int to int", 42, 42, true),
newAsTestCase("error to error", errors.New("test error"), errors.New("test error"), true),
newAsTestCase("int to string fails", 42, "", false),
newAsTestCase("nil to string", nil, "", false),
newAsTestCase("nil to error", nil, error(nil), false),
}
RunTestCases(t, testCases)
}
func TestAsFn(t *testing.T) {
// Custom conversion function
intToString := func(v any) (string, bool) {
if i, ok := v.(int); ok {
return fmt.Sprintf("%d", i), true
}
return "", false
}
testCases := []asFnTestCase{
newAsFnTestCase("with valid conversion function", intToString, 42, "42", true),
newAsFnTestCase("with valid conversion function but wrong type", intToString, "not an int", "", false),
newAsFnTestCase("with nil function", nil, 42, "", false),
newAsFnTestCase("with function returning false", func(any) (string, bool) { return "ignored", false },
42, "ignored", false),
}
RunTestCases(t, testCases)
}
func TestSliceAs(t *testing.T) {
testCases := []sliceAsTestCase{
newSliceAsTestCase("mixed types to string", S[any](testHello, 42, "world", 3.14, "!"),
S(testHello, "world", "!")),
newSliceAsTestCase("all strings", S[any]("a", "b", "c"), S("a", "b", "c")),
newSliceAsTestCase("no strings", S[any](1, 2, 3, 4.5, true), nil),
newSliceAsTestCase("empty slice", S[any](), nil),
newSliceAsTestCase("nil slice", nil, nil),
newSliceAsTestCase("with nil values", S[any](testHello, nil, "world"), S(testHello, "world")),
}
RunTestCases(t, testCases)
}
func TestSliceAsFn(t *testing.T) {
// Custom conversion function that adds prefix
prefixString := func(v any) (string, bool) {
if s, ok := v.(string); ok {
return "prefix:" + s, true
}
return "", false
}
testCases := []sliceAsFnTestCase{
newSliceAsFnTestCase("with custom conversion", prefixString, S[any]("a", 1, "b", 2, "c"),
S("prefix:a", "prefix:b", "prefix:c")),
newSliceAsFnTestCase("with nil function", nil, S[any]("a", "b", "c"), nil),
newSliceAsFnTestCase("empty slice", prefixString, S[any](), nil),
newSliceAsFnTestCase("nil slice", prefixString, nil, nil),
newSliceAsFnTestCase("all filtered out", prefixString, S[any](1, 2, 3), nil),
}
RunTestCases(t, testCases)
}
func TestAsError(t *testing.T) {
var typedNil *typedNilError
testCases := []asErrorTestCase{
newAsErrorTestCase("standard error", errors.New("standard error"), "standard error", true),
newAsErrorTestCase("nil error", error(nil), "", false),
newAsErrorTestCase("type with AsError returning error",
errorWithAsError{msg: "custom error"}, "custom error", true),
newAsErrorTestCase("type with AsError returning nil",
errorWithAsError{msg: ""}, "", false),
newAsErrorTestCase("type with OK returning false",
errorWithOK{msg: "not ok error", ok: false}, "not ok error", true),
newAsErrorTestCase("type with OK returning true", errorWithOK{msg: "ok error", ok: true}, "", false),
newAsErrorTestCase("non-error type", "not an error", "", false),
newAsErrorTestCase("nil value", nil, "", false),
newAsErrorTestCase("integer", 42, "", false),
newAsErrorTestCase("typed-nil error", error(typedNil), "", false),
}
RunTestCases(t, testCases)
}
func TestAsErrors(t *testing.T) {
testCases := []asErrorsTestCase{
newAsErrorsTestCase("mixed values with errors", S[any](
errors.New("error1"),
"not an error",
errors.New("error2"),
42,
errorWithAsError{msg: "error3"},
nil,
), S("error1", "error2", "error3"), 3),
newAsErrorsTestCase("all errors", S[any](
errors.New("a"),
errors.New("b"),
errors.New("c"),
), S("a", "b", "c"), 3),
newAsErrorsTestCase("no errors", S[any]("a", 1, true, nil), S[string](), 0),
newAsErrorsTestCase("empty slice", S[any](), S[string](), 0),
newAsErrorsTestCase("nil slice", nil, S[string](), 0),
newAsErrorsTestCase("with OK interface", S[any](
errorWithOK{msg: "fail", ok: false},
errorWithOK{msg: "pass", ok: true},
errorWithOK{msg: "fail2", ok: false},
), S("fail", "fail2"), 2),
}
RunTestCases(t, testCases)
}
// Benchmark tests
func BenchmarkAs(b *testing.B) {
input := "test string"
for b.Loop() {
_, _ = As[any, string](input)
}
}
func BenchmarkAsFn(b *testing.B) {
fn := func(v any) (string, bool) {
s, ok := v.(string)
return s, ok
}
var input any = "test string"
for b.Loop() {
_, _ = AsFn(fn, input)
}
}
func BenchmarkSliceAs(b *testing.B) {
input := S[any]("a", 1, "b", 2, "c", 3, "d", 4, "e", 5)
for b.Loop() {
_ = SliceAs[any, string](input)
}
}
func BenchmarkAsError(b *testing.B) {
err := errors.New("test error")
for b.Loop() {
_ = AsError(err)
}
}
// Test with concrete types to ensure generics work correctly
func TestAsWithConcreteTypes(t *testing.T) {
// Test int to int64
i := 42
if v, ok := As[int, int64](i); ok || v != 0 {
t.Errorf("As[int, int64](%d) = %v, %v; want 0, false", i, v, ok)
}
// Test *int to *int
pi := &i
if v, ok := As[*int, *int](pi); !ok || v != pi {
t.Errorf("As[*int, *int](%p) = %p, %v; want %p, true", pi, v, ok, pi)
}
// Test interface{} to concrete type
var value any = testHello
if v, ok := As[any, string](value); !ok || v != testHello {
t.Errorf("As[any, string](%v) = %v, %v; want hello, true", value, v, ok)
}
}
// Test SliceAsFn with various function types
func TestSliceAsFnEdgeCases(t *testing.T) {
// Function that panics
panicFn := func(_ any) (string, bool) {
panic("test panic")
}
// Verify panic propagates
defer func() {
if r := recover(); r == nil {
t.Error("SliceAsFn with panic function should panic")
}
}()
_ = SliceAsFn(panicFn, S[any]("will panic"))
}
// Custom error type used by the typed-nil AsError case in TestAsError.
type typedNilError struct{}
func (*typedNilError) Error() string { return "typed-nil" }