-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbogo_test.go
More file actions
878 lines (767 loc) · 24 KB
/
bogo_test.go
File metadata and controls
878 lines (767 loc) · 24 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
package bogo
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStringEncodingDecoding(t *testing.T) {
// encode
data, err := Encode("abcd")
require.NoError(t, err)
assert.Equal(t, Version, data[0]) // bogo version
assert.Equal(t, TypeString, int(data[1])) // data type
assert.Equal(t, 1, int(data[2])) // space to hold single byte num
assert.Equal(t, 4, int(data[3])) // length of 4 chars
assert.Equal(t, []byte{'a', 'b', 'c', 'd'}, data[4:]) // length of 4 chars
// decode
d, err := Decode(data)
require.NoError(t, err)
assert.Equal(t, "abcd", d.(string))
}
func TestBoolEncodingDecoding(t *testing.T) {
tests := []struct {
name string
input bool
expected []byte
}{
{
name: "encode and decode true",
input: true,
expected: []byte{0, 1}, // expected encoded data for true
},
{
name: "encode and decode false",
input: false,
expected: []byte{0, 2}, // expected encoded data for false
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode the input value
data, err := Encode(tt.input)
require.NoError(t, err)
assert.Equal(t, Version, data[0]) // bogo version
dataType := TypeBoolFalse
if tt.input {
dataType = TypeBoolTrue
} else {
dataType = TypeBoolFalse
}
assert.Equal(t, int(dataType), int(data[1])) // data type
assert.Equal(t, 2, len(data))
assert.Equal(t, tt.expected, data)
// Decode the data back
d, err := Decode(data)
require.NoError(t, err)
assert.Equal(t, tt.input, d.(bool))
})
}
}
func TestNullEncodingDecoding(t *testing.T) {
tests := []struct {
name string
input any
}{
{"nil value", nil},
{"nil interface", (*interface{})(nil)},
{"nil slice", ([]int)(nil)},
{"nil map", (map[string]int)(nil)},
{"nil pointer", (*string)(nil)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode
data, err := Encode(tt.input)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, Version, data[0]) // version
assert.Equal(t, int(TypeNull), int(data[1])) // null type
assert.Equal(t, 2, len(data)) // should be exactly 2 bytes
// Decode
result, err := Decode(data)
require.NoError(t, err)
assert.Nil(t, result)
})
}
}
// TestBlobEncodingDecoding tests blob encoding via structs (requires object support)
func TestBlobEncodingDecoding(t *testing.T) {
t.Skip("Requires object/struct support - will implement later")
}
func TestBlobFullRoundTrip(t *testing.T) {
tests := []struct {
name string
data []byte
}{
{"empty", []byte{}},
{"small", []byte{0x01, 0x02, 0x03}},
{"uuid", []byte{0x55, 0x0e, 0x84, 0x00, 0xe2, 0x9b, 0x41, 0xd4, 0xa7, 0x16, 0x44, 0x66, 0x55, 0x44, 0x00, 0x00}},
{"large", make([]byte, 1000)},
}
// Initialize large blob
for i := range tests[3].data {
tests[3].data[i] = byte(i % 256)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode using main Encode function
encoded, err := Encode(tt.data)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, Version, encoded[0]) // version
assert.Equal(t, byte(TypeBlob), encoded[1]) // blob type
// Decode using main Decode function
decoded, err := Decode(encoded)
require.NoError(t, err)
// Should get back the same data
assert.Equal(t, tt.data, decoded.([]byte))
})
}
}
func TestBlobTypedEncodingDecoding(t *testing.T) {
// Test direct blob encoding (not wrapped in struct)
tests := []struct {
name string
data []byte
}{
{"empty", []byte{}},
{"small", []byte{0x01, 0x02, 0x03}},
{"medium", make([]byte, 100)},
}
for i := range tests[2].data {
tests[2].data[i] = byte(i)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// This will test the direct blob encoding once implemented
encoded, err := encodeBlob(tt.data)
require.NoError(t, err)
// Check format: TypeBlob + LenSize + DataSize + Data
assert.Equal(t, byte(TypeBlob), encoded[0])
// Decode
decoded, err := decodeBlob(encoded[1:])
require.NoError(t, err)
assert.Equal(t, tt.data, decoded)
})
}
}
func TestTimestampEncodingDecoding(t *testing.T) {
tests := []struct {
name string
timestamp int64
}{
{"unix epoch", 0},
{"positive timestamp", 1705317045123}, // 2024-01-15T10:30:45.123Z
{"negative timestamp", -86400000}, // 1969-12-31T00:00:00.000Z
{"max positive", 9223372036854775807}, // max int64
{"large negative", -1000000000000}, // 1938-04-24T22:13:20.000Z
{"recent timestamp", 1640995200000}, // 2022-01-01T00:00:00.000Z
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test direct timestamp encoding
encoded, err := encodeTimestamp(tt.timestamp)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, byte(TypeTimestamp), encoded[0])
assert.Equal(t, 9, len(encoded)) // 1 type + 8 data bytes
// Decode
decoded, err := decodeTimestamp(encoded[1:])
require.NoError(t, err)
assert.Equal(t, tt.timestamp, decoded)
})
}
}
// TestTimestampFullRoundTrip tests timestamp encoding via structs (requires object support)
func TestTimestampFullRoundTrip(t *testing.T) {
t.Skip("Requires object/struct support - will implement later")
}
func TestTimeEncodingDecoding(t *testing.T) {
tests := []struct {
name string
time time.Time
}{
{"unix epoch", time.Unix(0, 0).UTC()},
{"current time", time.Date(2024, 1, 15, 10, 30, 45, 123000000, time.UTC)},
{"past", time.Date(1969, 12, 31, 0, 0, 0, 0, time.UTC)},
{"future", time.Date(2100, 1, 1, 0, 0, 0, 0, time.UTC)},
{"with nanoseconds", time.Date(2024, 6, 15, 14, 30, 45, 123456789, time.UTC)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode using main Encode function
encoded, err := Encode(tt.time)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, Version, encoded[0]) // version
assert.Equal(t, byte(TypeTimestamp), encoded[1]) // timestamp type
assert.Equal(t, 10, len(encoded)) // 1 version + 1 type + 8 data bytes
// Decode using main Decode function
decoded, err := Decode(encoded)
require.NoError(t, err)
// Should get back the timestamp in milliseconds
expectedMillis := tt.time.UnixMilli()
assert.Equal(t, time.UnixMilli(expectedMillis).UTC(), decoded.(time.Time).UTC())
// Convert back to time and compare (with millisecond precision)
decodedTime := decoded.(time.Time).UTC()
assert.Equal(t, tt.time.Truncate(time.Millisecond), decodedTime.Truncate(time.Millisecond))
})
}
}
func TestByteEncodingDecoding(t *testing.T) {
tests := []struct {
name string
data byte
}{
{"zero", 0},
{"small", 42},
{"max", 255},
{"mid", 128},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test direct byte encoding
encoded, err := encodeByte(tt.data)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, byte(TypeByte), encoded[0])
assert.Equal(t, tt.data, encoded[1])
assert.Equal(t, 2, len(encoded)) // 1 type + 1 data byte
// Decode
decoded, err := decodeByte(encoded[1:])
require.NoError(t, err)
assert.Equal(t, tt.data, decoded)
})
}
}
func TestByteFullRoundTrip(t *testing.T) {
tests := []struct {
name string
data byte
}{
{"zero", 0},
{"small", 42},
{"max", 255},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode using main Encode function
encoded, err := Encode(tt.data)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, Version, encoded[0]) // version
assert.Equal(t, byte(TypeByte), encoded[1]) // byte type
assert.Equal(t, 3, len(encoded)) // 1 version + 1 type + 1 data byte
// Decode using main Decode function
decoded, err := Decode(encoded)
require.NoError(t, err)
// Should get back the same byte
assert.Equal(t, tt.data, decoded.(byte))
})
}
}
func TestObjectEncodingDecoding(t *testing.T) {
tests := []struct {
name string
object map[string]any
}{
{"empty object", map[string]any{}},
{"simple object", map[string]any{
"name": "John",
"age": int64(25),
}},
{"complex object", map[string]any{
"name": "Alice",
"age": int64(30),
"active": true,
"score": 95.5,
"metadata": nil,
"timestamp": int64(1705317045123),
}},
{"nested object", map[string]any{
"user": map[string]any{
"name": "Bob",
"details": map[string]any{
"email": "bob@example.com",
"level": int64(3),
},
},
"settings": map[string]any{
"theme": "dark",
"notifications": true,
},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test direct object encoding
encoded, err := encodeObject(tt.object)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, byte(TypeObject), encoded[0])
// Decode
decoded, err := decodeObject(encoded[1:])
require.NoError(t, err)
// Should get back the same object
assert.Equal(t, tt.object, decoded)
})
}
}
func TestObjectFullRoundTrip(t *testing.T) {
tests := []struct {
name string
object map[string]any
}{
{"simple", map[string]any{"key": "value", "num": int64(42)}},
{"mixed types", map[string]any{
"str": "hello",
"int": int64(123),
"bool": true,
"float": 3.14,
"null": nil,
"bytes": []byte{1, 2, 3},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode using main Encode function
encoded, err := Encode(tt.object)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, Version, encoded[0]) // version
assert.Equal(t, byte(TypeObject), encoded[1]) // object type
// Decode using main Decode function
decoded, err := Decode(encoded)
require.NoError(t, err)
// Should get back the same object
decodedMap := decoded.(map[string]any)
assert.Equal(t, len(tt.object), len(decodedMap))
for key, expectedValue := range tt.object {
actualValue, exists := decodedMap[key]
assert.True(t, exists, "Key %s should exist", key)
assert.Equal(t, expectedValue, actualValue, "Value for key %s should match", key)
}
})
}
}
func TestStructEncodingDecoding(t *testing.T) {
type Person struct {
Name string `bogo:"name"`
Age int `bogo:"age"`
City string
}
tests := []struct {
name string
person Person
}{
{"simple person", Person{Name: "John", Age: 30, City: "New York"}},
{"empty person", Person{}},
{"partial person", Person{Name: "Alice", Age: 25}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode using main Encode function
encoded, err := Encode(tt.person)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, Version, encoded[0]) // version
assert.Equal(t, byte(TypeObject), encoded[1]) // object type (structs encode as objects)
// Decode using main Decode function
decoded, err := Decode(encoded)
require.NoError(t, err)
// Should get back a map representing the struct
decodedMap := decoded.(map[string]any)
assert.NotNil(t, decodedMap)
// Check that struct fields are present with correct tag names or field names
if tt.person.Name != "" {
assert.Equal(t, tt.person.Name, decodedMap["Name"]) // uses bogo tag
}
if tt.person.Age != 0 {
assert.Equal(t, int64(tt.person.Age), decodedMap["Age"]) // uses bogo tag, int becomes int64
}
if tt.person.City != "" {
assert.Equal(t, tt.person.City, decodedMap["City"]) // uses field name
}
})
}
}
func TestTypedListEncodingDecoding(t *testing.T) {
tests := []struct {
name string
input any
expected any
}{
{"string list", []string{"hello", "world", "test"}, []string{"hello", "world", "test"}},
{"int list", []int{1, 2, 3, 4, 5}, []int64{1, 2, 3, 4, 5}},
{"byte list", []byte{10, 20, 30, 40, 50}, []byte{10, 20, 30, 40, 50}},
{"bool list", []bool{true, false, true, false}, []bool{true, false, true, false}},
{"float list", []float64{1.1, 2.2, 3.3}, []float64{1.1, 2.2, 3.3}},
{"uint list", []uint64{100, 200, 300}, []uint64{100, 200, 300}},
{"single element", []int{42}, []int64{42}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test direct typed list encoding
encoded, err := encodeTypedList(tt.input)
require.NoError(t, err)
// Check encoding format
assert.Equal(t, byte(TypeTypedList), encoded[0])
// Decode
decoded, err := decodeTypedList(encoded[1:])
require.NoError(t, err)
assert.Equal(t, tt.expected, decoded)
})
}
}
func TestTypedListFullRoundTrip(t *testing.T) {
tests := []struct {
name string
input any
expected any
}{
{"string list", []string{"hello", "world"}, []string{"hello", "world"}},
{"int list", []int{1, 2, 3}, []int64{1, 2, 3}},
{"byte list", []byte{10, 20, 30}, []byte{10, 20, 30}},
{"bool list", []bool{true, false, true}, []bool{true, false, true}},
{"float list", []float64{1.1, 2.2}, []float64{1.1, 2.2}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Note: This test won't automatically use typed lists
// The main encoder decides based on list homogeneity
// For now, we'll test the typed list functions directly
// Encode using typed list function directly
encoded, err := encodeTypedList(tt.input)
require.NoError(t, err)
// Decode using typed list function directly
decoded, err := decodeTypedList(encoded[1:])
require.NoError(t, err)
assert.Equal(t, tt.expected, decoded)
})
}
}
func TestMarshalUnmarshal(t *testing.T) {
tests := []struct {
name string
input any
expected any
}{
{"string", "hello", "hello"},
{"int", 42, int64(42)},
{"bool", true, true},
{"null", nil, nil},
{"float", 3.14, 3.14},
{"byte", byte(255), byte(255)},
{"blob", []byte{1, 2, 3}, []byte{1, 2, 3}},
{"object", map[string]any{"key": "value"}, map[string]any{"key": "value"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Marshal
data, err := Marshal(tt.input)
require.NoError(t, err)
// Unmarshal
var result any
err = Unmarshal(data, &result)
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
func TestNumbersEncodingDecoding(t *testing.T) {
tests := []struct {
name string
input any
bt Type
}{
{"integer", 47, TypeInt},
{"int16", int16(12), TypeInt},
{"min_unsigned_id", ^uint64(0), TypeUint},
{"max_unsigned_id_less_1", ^uint64(0) - 1, TypeUint},
{"min_signed_int", -(1 << 63), TypeInt},
{"uint_1", uint8(1), TypeByte},
{"zero", 0, TypeInt},
{"float", 3.1415926535897932384626433832795028841971693993751058209749445923819343, TypeFloat},
{"float", 0.1, TypeFloat},
{"negative-float", -0.0005893, TypeFloat},
{"negative-float", 0.5, TypeFloat},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := Encode(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.bt, Type(data[1]))
i, err := Decode(data)
assert.NoError(t, err)
switch in := tt.input.(type) {
case uint8:
assert.Equal(t, byte(in), i)
case int:
assert.Equal(t, int64(in), i)
case int16:
assert.Equal(t, int64(in), i)
case uint64:
assert.Equal(t, uint64(in), i)
case float64:
assert.Equal(t, float64(in), i)
case float32:
assert.Equal(t, float64(in), i)
default:
t.Errorf("result type not asserted. type=%T", data)
}
})
}
}
// Test unused helper functions
func TestUnusedHelperFunctions(t *testing.T) {
t.Run("decodeNull", func(t *testing.T) {
result, err := decodeNull()
assert.NoError(t, err)
assert.Nil(t, result)
})
t.Run("decodeBoolTrue", func(t *testing.T) {
result, err := decodeBoolTrue()
assert.NoError(t, err)
assert.True(t, result)
})
t.Run("decodeBoolFalse", func(t *testing.T) {
result, err := decodeBoolFalse()
assert.NoError(t, err)
assert.False(t, result)
})
}
// Test Type.String() method
func TestTypeStringMethod(t *testing.T) {
tests := []struct {
typeVal Type
expected string
}{
{TypeNull, "<null>"},
{TypeBoolTrue, "<bool:true>"},
{TypeBoolFalse, "<bool:false>"},
{TypeString, "<string>"},
{TypeUntypedList, "<list>"},
{TypeTypedList, "<typed_list>"},
{TypeObject, "<object>"},
{TypeByte, "<byte>"},
{TypeInt, "<int>"},
{TypeUint, "<uint>"},
{TypeFloat, "<float>"},
{TypeBlob, "<blob>"},
{TypeTimestamp, "<timestamp>"},
{Type(99), "<unknown>"}, // Test unknown type
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.typeVal.String())
})
}
}
// Test wrapError utility function
func TestWrapError(t *testing.T) {
baseErr := assert.AnError
t.Run("wrapError with no additional messages", func(t *testing.T) {
result := wrapError(baseErr)
assert.Equal(t, baseErr, result)
})
t.Run("wrapError with additional message", func(t *testing.T) {
result := wrapError(baseErr, "additional context")
assert.Contains(t, result.Error(), baseErr.Error())
assert.Contains(t, result.Error(), "additional context")
})
}
// Test encodeTimeValue helper function
func TestEncodeTimeValue(t *testing.T) {
testTime := time.Date(2024, 1, 15, 10, 30, 45, 123000000, time.UTC)
encoded, err := encodeTimeValue(testTime)
require.NoError(t, err)
// Should be 9 bytes: 1 type + 8 timestamp
assert.Equal(t, 9, len(encoded))
assert.Equal(t, byte(TypeTimestamp), encoded[0])
// Decode and verify
timestamp, err := decodeTimestamp(encoded[1:])
require.NoError(t, err)
assert.Equal(t, testTime.UnixMilli(), timestamp)
}
// Test all branches in isZeroValue function
func TestIsZeroValueAllBranches(t *testing.T) {
tests := []struct {
name string
value any
expected bool
}{
{"nil", nil, true},
{"empty string", "", true},
{"non-empty string", "hello", false},
{"int zero", int(0), true},
{"int non-zero", int(42), false},
{"int8 zero", int8(0), true},
{"int8 non-zero", int8(42), false},
{"int16 zero", int16(0), true},
{"int16 non-zero", int16(42), false},
{"int32 zero", int32(0), true},
{"int32 non-zero", int32(42), false},
{"int64 zero", int64(0), true},
{"int64 non-zero", int64(42), false},
{"uint zero", uint(0), true},
{"uint non-zero", uint(42), false},
{"uint8 zero", uint8(0), true},
{"uint8 non-zero", uint8(42), false},
{"uint16 zero", uint16(0), true},
{"uint16 non-zero", uint16(42), false},
{"uint32 zero", uint32(0), true},
{"uint32 non-zero", uint32(42), false},
{"uint64 zero", uint64(0), true},
{"uint64 non-zero", uint64(42), false},
{"float32 zero", float32(0.0), true},
{"float32 non-zero", float32(3.14), false},
{"float64 zero", float64(0.0), true},
{"float64 non-zero", float64(3.14), false},
{"bool false", false, true},
{"bool true", true, false},
{"slice", []int{1, 2, 3}, false}, // default case
{"struct", struct{}{}, false}, // default case
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isZeroValue(tt.value)
assert.Equal(t, tt.expected, result)
})
}
}
// Test error paths and edge cases
func TestErrorPathsAndEdgeCases(t *testing.T) {
t.Run("decodeByte with insufficient data", func(t *testing.T) {
_, err := decodeByte([]byte{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "insufficient data")
})
t.Run("decodeTimestamp with insufficient data", func(t *testing.T) {
_, err := decodeTimestamp([]byte{1, 2, 3}) // Less than 8 bytes
assert.Error(t, err)
assert.Contains(t, err.Error(), "insufficient data")
})
t.Run("decodeBlob with insufficient size data", func(t *testing.T) {
// Size len says 5 bytes needed, but only 2 provided
_, err := decodeBlob([]byte{5, 10})
assert.Error(t, err)
assert.Contains(t, err.Error(), "insufficient data for size")
})
t.Run("decodeBlob with insufficient content data", func(t *testing.T) {
// Says blob size is 10, but only provides 3 bytes
_, err := decodeBlob([]byte{1, 10, 1, 2, 3})
assert.Error(t, err)
assert.Contains(t, err.Error(), "insufficient data for blob content")
})
t.Run("encodeString with empty string", func(t *testing.T) {
encoded, err := encodeString("")
require.NoError(t, err)
assert.Equal(t, []byte{byte(TypeString), 1, 0}, encoded)
})
t.Run("encode unsupported type", func(t *testing.T) {
// Use a channel which is an unsupported type
ch := make(chan int)
_, err := encode(ch)
assert.Error(t, err)
assert.Contains(t, err.Error(), "unsupported type")
})
t.Run("encodeTypedList with empty list falls back to regular list", func(t *testing.T) {
emptyList := []string{}
encoded, err := encodeTypedList(emptyList)
require.NoError(t, err)
// Should be regular list type, not typed list
assert.Equal(t, byte(TypeUntypedList), encoded[0])
})
t.Run("encodeTypedList with mixed types falls back to regular list", func(t *testing.T) {
// This won't actually work since Go is strongly typed, but test the logic
// by using interface{} list
mixedList := []interface{}{"string", 42}
_, err := encodeList(mixedList)
require.NoError(t, err)
// Just verify it doesn't panic
})
t.Run("encodeObject with unsupported type", func(t *testing.T) {
_, err := encodeObject("not a map")
assert.Error(t, err)
assert.Contains(t, err.Error(), "object type not supported")
})
t.Run("encodeFieldEntry with key too long", func(t *testing.T) {
longKey := string(make([]byte, 256)) // 256 bytes, exceeds 255 limit
_, err := encodeFieldEntry(longKey, "value")
assert.Error(t, err)
assert.Contains(t, err.Error(), "key too long")
})
}
// Test decodeList edge cases to improve coverage
func TestDecodeListEdgeCases(t *testing.T) {
t.Run("decodeList with nil destination", func(t *testing.T) {
err := decodeList([]byte{}, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid decoder destination")
})
t.Run("decodeList with non-pointer destination", func(t *testing.T) {
var slice []int
err := decodeList([]byte{}, slice) // Not a pointer
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid decoder destination")
})
t.Run("decodeList with byte elements", func(t *testing.T) {
// Create list data with byte elements
data := []byte{
TypeByte, 42, // First byte element
TypeByte, 43, // Second byte element
}
var result []any
err := decodeList(data, &result)
assert.Error(t, err) // This will error due to type mismatch logic
})
}
// Test decodeValue edge cases
func TestDecodeValueEdgeCases(t *testing.T) {
t.Run("decodeValue with empty data", func(t *testing.T) {
result, err := decodeValue([]byte{})
assert.NoError(t, err)
assert.Nil(t, result)
})
t.Run("decodeValue with unsupported type", func(t *testing.T) {
// Use a type value that doesn't exist
_, err := decodeValue([]byte{byte(99), 1, 2, 3})
assert.Error(t, err)
assert.Contains(t, err.Error(), "unsupported value type")
})
t.Run("decodeValue with list type now supported", func(t *testing.T) {
// Test the list case that now works
result, err := decodeValue([]byte{TypeUntypedList, 1, 0})
assert.NoError(t, err)
assert.Equal(t, []any{}, result) // Empty list
})
}
// Test API-level error handling
func TestAPIErrorHandling(t *testing.T) {
t.Run("Decode with insufficient data", func(t *testing.T) {
// Empty data
_, err := Decode([]byte{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "insufficient data")
// Only version byte, no type
_, err = Decode([]byte{0x00})
assert.Error(t, err)
assert.Contains(t, err.Error(), "insufficient data")
})
t.Run("Decode with unsupported version", func(t *testing.T) {
// Wrong version
_, err := Decode([]byte{0x99, TypeString, 1, 5, 'h', 'e', 'l', 'l', 'o'})
assert.Error(t, err)
assert.Contains(t, err.Error(), "unsupported version")
assert.Contains(t, err.Error(), "153") // 0x99 = 153 in decimal
assert.Contains(t, err.Error(), "expected version 0")
})
t.Run("API should never call os.Exit", func(t *testing.T) {
// This test ensures the fix worked - if os.Exit was called, this test would never complete
_, err := Decode([]byte{0x99, TypeNull})
assert.Error(t, err)
// If we reach this point, os.Exit was not called
assert.True(t, true, "API correctly returned error instead of calling os.Exit")
})
}