-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode_test.go
More file actions
653 lines (591 loc) · 15.5 KB
/
encode_test.go
File metadata and controls
653 lines (591 loc) · 15.5 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
package maml
import (
"bytes"
"fmt"
"math"
"testing"
)
func TestMarshalPrimitives(t *testing.T) {
tests := []struct {
val any
expected string
}{
{nil, "null"},
{true, "true"},
{false, "false"},
{42, "42"},
{int8(1), "1"},
{int16(2), "2"},
{int32(3), "3"},
{int64(4), "4"},
{uint(5), "5"},
{uint8(6), "6"},
{uint16(7), "7"},
{uint32(8), "8"},
{uint64(9), "9"},
{3.14, "3.14"},
{float32(1.5), "1.5"},
{"hello", `"hello"`},
}
for _, tc := range tests {
data, err := Marshal(tc.val)
if err != nil {
t.Errorf("Marshal(%v): %v", tc.val, err)
continue
}
if string(data) != tc.expected {
t.Errorf("Marshal(%v) = %q, want %q", tc.val, string(data), tc.expected)
}
}
}
func TestMarshalNegativeZero(t *testing.T) {
data, err := Marshal(math.Copysign(0, -1))
if err != nil {
t.Fatal(err)
}
if string(data) != "-0" {
t.Errorf("expected '-0', got %q", string(data))
}
}
func TestMarshalFloatEnsureDecimal(t *testing.T) {
data, err := Marshal(1.0)
if err != nil {
t.Fatal(err)
}
s := string(data)
// Should be "1" with possible ".0" suffix to remain a number
t.Logf("Marshal(1.0) = %q", s)
}
func TestMarshalFloatSpecial(t *testing.T) {
_, err := Marshal(math.Inf(1))
if err == nil {
t.Error("expected error for Inf")
}
_, err = Marshal(math.NaN())
if err == nil {
t.Error("expected error for NaN")
}
}
func TestMarshalSlice(t *testing.T) {
data, err := Marshal([]int{1, 2, 3})
if err != nil {
t.Fatal(err)
}
expected := "[\n 1\n 2\n 3\n]"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalEmptySlice(t *testing.T) {
data, err := Marshal([]int{})
if err != nil {
t.Fatal(err)
}
if string(data) != "[]" {
t.Errorf("expected '[]', got %q", string(data))
}
}
func TestMarshalNilSlice(t *testing.T) {
var s []int
data, err := Marshal(s)
if err != nil {
t.Fatal(err)
}
if string(data) != "null" {
t.Errorf("expected 'null', got %q", string(data))
}
}
func TestMarshalMap(t *testing.T) {
m := map[string]int{"b": 2, "a": 1}
data, err := Marshal(m)
if err != nil {
t.Fatal(err)
}
// Keys should be sorted
expected := "{\n a: 1\n b: 2\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalEmptyMap(t *testing.T) {
m := map[string]int{}
data, err := Marshal(m)
if err != nil {
t.Fatal(err)
}
if string(data) != "{}" {
t.Errorf("expected '{}', got %q", string(data))
}
}
func TestMarshalNilMap(t *testing.T) {
var m map[string]int
data, err := Marshal(m)
if err != nil {
t.Fatal(err)
}
if string(data) != "null" {
t.Errorf("expected 'null', got %q", string(data))
}
}
func TestMarshalMapNonStringKey(t *testing.T) {
m := map[int]int{1: 2}
_, err := Marshal(m)
if err == nil {
t.Error("expected error for non-string map key")
}
}
func TestMarshalStruct(t *testing.T) {
type Config struct {
Name string `maml:"name"`
Port int `maml:"port"`
}
c := Config{Name: "test", Port: 8080}
data, err := Marshal(c)
if err != nil {
t.Fatal(err)
}
expected := "{\n name: \"test\"\n port: 8080\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalStructOmitEmpty(t *testing.T) {
type Config struct {
Name string `maml:"name"`
Port int `maml:"port,omitempty"`
}
c := Config{Name: "test"}
data, err := Marshal(c)
if err != nil {
t.Fatal(err)
}
expected := "{\n name: \"test\"\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalStructIgnore(t *testing.T) {
type Config struct {
Name string `maml:"name"`
Secret string `maml:"-"`
}
c := Config{Name: "test", Secret: "hidden"}
data, err := Marshal(c)
if err != nil {
t.Fatal(err)
}
expected := "{\n name: \"test\"\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalNestedStruct(t *testing.T) {
type Inner struct {
Value int `maml:"value"`
}
type Outer struct {
Inner Inner `maml:"inner"`
}
o := Outer{Inner: Inner{Value: 42}}
data, err := Marshal(o)
if err != nil {
t.Fatal(err)
}
expected := "{\n inner: {\n value: 42\n }\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalPointer(t *testing.T) {
n := 42
data, err := Marshal(&n)
if err != nil {
t.Fatal(err)
}
if string(data) != "42" {
t.Errorf("expected '42', got %q", string(data))
}
var p *int
data, err = Marshal(p)
if err != nil {
t.Fatal(err)
}
if string(data) != "null" {
t.Errorf("expected 'null', got %q", string(data))
}
}
type customMarshaler struct {
val string
}
func (c customMarshaler) MarshalMAML() ([]byte, error) {
return []byte(`"custom:` + c.val + `"`), nil
}
func TestMarshalCustom(t *testing.T) {
c := customMarshaler{val: "hello"}
data, err := Marshal(c)
if err != nil {
t.Fatal(err)
}
if string(data) != `"custom:hello"` {
t.Errorf("expected '\"custom:hello\"', got %q", string(data))
}
}
type textMarshaler struct {
val string
}
func (t textMarshaler) MarshalText() ([]byte, error) {
return []byte(t.val), nil
}
func TestMarshalTextMarshaler(t *testing.T) {
tm := textMarshaler{val: "hello"}
data, err := Marshal(tm)
if err != nil {
t.Fatal(err)
}
if string(data) != `"hello"` {
t.Errorf("expected '\"hello\"', got %q", string(data))
}
}
func TestMarshalValue(t *testing.T) {
v := NewInt(42)
data, err := Marshal(v)
if err != nil {
t.Fatal(err)
}
if string(data) != "42" {
t.Errorf("expected '42', got %q", string(data))
}
}
func TestMarshalUnsupportedType(t *testing.T) {
_, err := Marshal(make(chan int))
if err == nil {
t.Error("expected error for channel type")
}
}
func TestMarshalNilInterface(t *testing.T) {
var v any
data, err := Marshal(v)
if err != nil {
t.Fatal(err)
}
if string(data) != "null" {
t.Errorf("expected 'null', got %q", string(data))
}
}
func TestEncoderEncode(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(&buf)
if err := enc.Encode(42); err != nil {
t.Fatal(err)
}
if buf.String() != "42" {
t.Errorf("expected '42', got %q", buf.String())
}
}
func TestMarshalEmbeddedStruct(t *testing.T) {
type Base struct {
ID int `maml:"id"`
}
type Extended struct {
Base
Name string `maml:"name"`
}
e := Extended{Base: Base{ID: 1}, Name: "test"}
data, err := Marshal(e)
if err != nil {
t.Fatal(err)
}
expected := "{\n id: 1\n name: \"test\"\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalArray(t *testing.T) {
data, err := Marshal([3]int{1, 2, 3})
if err != nil {
t.Fatal(err)
}
expected := "[\n 1\n 2\n 3\n]"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
func TestMarshalOmitEmptyVariants(t *testing.T) {
type Config struct {
B bool `maml:"b,omitempty"`
I int `maml:"i,omitempty"`
U uint `maml:"u,omitempty"`
F float64 `maml:"f,omitempty"`
S string `maml:"s,omitempty"`
Sl []int `maml:"sl,omitempty"`
M map[string]int `maml:"m,omitempty"`
P *int `maml:"p,omitempty"`
Ifc any `maml:"ifc,omitempty"`
St struct{} `maml:"st,omitempty"`
}
c := Config{}
data, err := Marshal(c)
if err != nil {
t.Fatal(err)
}
// All fields should be omitted
if string(data) != "{}" {
t.Errorf("expected empty struct, got %q", string(data))
}
}
func TestMarshalQuotedKey(t *testing.T) {
type Config struct {
Value int `maml:"a b"`
}
c := Config{Value: 1}
data, err := Marshal(c)
if err != nil {
t.Fatal(err)
}
expected := "{\n \"a b\": 1\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
// Test marshalValue with TextMarshaler on addressable value (via pointer receiver)
type addrTextMarshaler struct {
val string
}
func (t *addrTextMarshaler) MarshalText() ([]byte, error) {
return []byte(t.val), nil
}
func TestMarshalAddrTextMarshaler(t *testing.T) {
type Wrapper struct {
TM addrTextMarshaler `maml:"tm"`
}
w := &Wrapper{TM: addrTextMarshaler{val: "addr-text"}}
data, err := Marshal(w)
if err != nil {
t.Fatal(err)
}
expected := "{\n tm: \"addr-text\"\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
// Test marshalValue with Marshaler on addressable value (via pointer receiver)
type addrMarshaler struct {
val string
}
func (m *addrMarshaler) MarshalMAML() ([]byte, error) {
return []byte(`"addr:` + m.val + `"`), nil
}
func TestMarshalAddrMarshaler(t *testing.T) {
type Wrapper struct {
M addrMarshaler `maml:"m"`
}
w := &Wrapper{M: addrMarshaler{val: "hello"}}
data, err := Marshal(w)
if err != nil {
t.Fatal(err)
}
expected := "{\n m: \"addr:hello\"\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}
// Test marshalValue with interface wrapping a value
func TestMarshalInterfaceWrappedValue(t *testing.T) {
var iface any = 42
data, err := Marshal(iface)
if err != nil {
t.Fatal(err)
}
if string(data) != "42" {
t.Errorf("expected '42', got %q", string(data))
}
// Interface wrapping a nil
var nilIface any
data, err = Marshal(nilIface)
if err != nil {
t.Fatal(err)
}
if string(data) != "null" {
t.Errorf("expected 'null', got %q", string(data))
}
}
// Test isZeroValue for struct type
func TestMarshalOmitEmptyStruct(t *testing.T) {
type Inner struct {
Value int `maml:"value"`
}
type Config struct {
Name string `maml:"name"`
Inner Inner `maml:"inner,omitempty"`
}
// Zero struct should be omitted
c := Config{Name: "test"}
data, err := Marshal(c)
if err != nil {
t.Fatal(err)
}
expected := "{\n name: \"test\"\n}"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
// Non-zero struct should not be omitted
c2 := Config{Name: "test", Inner: Inner{Value: 42}}
data, err = Marshal(c2)
if err != nil {
t.Fatal(err)
}
expected2 := "{\n name: \"test\"\n inner: {\n value: 42\n }\n}"
if string(data) != expected2 {
t.Errorf("expected %q, got %q", expected2, string(data))
}
}
// Test marshalValue with empty struct (all fields omitted)
func TestMarshalEmptyStruct(t *testing.T) {
type Empty struct{}
data, err := Marshal(Empty{})
if err != nil {
t.Fatal(err)
}
if string(data) != "{}" {
t.Errorf("expected '{}', got %q", string(data))
}
}
// Test Marshaler error path
type errMarshaler struct{}
func (e errMarshaler) MarshalMAML() ([]byte, error) {
return nil, fmt.Errorf("marshaler error")
}
func TestMarshalMarshalerError(t *testing.T) {
_, err := Marshal(errMarshaler{})
if err == nil {
t.Error("expected error from Marshaler")
}
}
// Test Marshaler error via pointer receiver (CanAddr path)
type errAddrMarshaler struct{}
func (e *errAddrMarshaler) MarshalMAML() ([]byte, error) {
return nil, fmt.Errorf("addr marshaler error")
}
func TestMarshalAddrMarshalerError(t *testing.T) {
type Wrapper struct {
M errAddrMarshaler `maml:"m"`
}
_, err := Marshal(&Wrapper{})
if err == nil {
t.Error("expected error from addr Marshaler")
}
}
// Test TextMarshaler error path
type errTextMarshaler struct{}
func (e errTextMarshaler) MarshalText() ([]byte, error) {
return nil, fmt.Errorf("text marshaler error")
}
func TestMarshalTextMarshalerError(t *testing.T) {
_, err := Marshal(errTextMarshaler{})
if err == nil {
t.Error("expected error from TextMarshaler")
}
}
// Test TextMarshaler error via pointer receiver (CanAddr path)
type errAddrTextMarshaler struct{}
func (e *errAddrTextMarshaler) MarshalText() ([]byte, error) {
return nil, fmt.Errorf("addr text marshaler error")
}
func TestMarshalAddrTextMarshalerError(t *testing.T) {
type Wrapper struct {
TM errAddrTextMarshaler `maml:"tm"`
}
_, err := Marshal(&Wrapper{})
if err == nil {
t.Error("expected error from addr TextMarshaler")
}
}
// Test error propagation in array element marshaling
func TestMarshalSliceElementError(t *testing.T) {
// Slice of channels - channel is unmarshalable
s := []any{make(chan int)}
_, err := Marshal(s)
if err == nil {
t.Error("expected error for unmarshalable element in slice")
}
}
// Test error propagation in map value marshaling
func TestMarshalMapValueError(t *testing.T) {
m := map[string]any{"a": make(chan int)}
_, err := Marshal(m)
if err == nil {
t.Error("expected error for unmarshalable value in map")
}
}
// Test error propagation in struct field marshaling
func TestMarshalStructFieldError(t *testing.T) {
type Config struct {
Ch chan int `maml:"ch"`
}
c := Config{Ch: make(chan int)}
_, err := Marshal(c)
if err == nil {
t.Error("expected error for unmarshalable field in struct")
}
}
// Test isZeroValue default case (type not in switch)
func TestMarshalOmitEmptyUnsupportedType(t *testing.T) {
// The isZeroValue function has a default case that returns false
// This is for types not in the switch. We can test this by having
// an omitempty field with a type that falls into default (e.g., complex128)
// But complex128 would fail in marshalValue anyway...
// Actually, the default case of isZeroValue would be hit by, e.g., a func type
// But func can't be marshaled either.
// Let's use a channel field. Even though it will fail to marshal when non-zero,
// when it IS the zero value, isZeroValue's default returns false, so it won't be omitted
// and will attempt to marshal, failing. But that's OK, we just need the default branch
// to be hit. However the test should check the omit behavior.
// Actually, a nil channel with omitempty would call isZeroValue which goes to default -> false
// So the field is not omitted, and then marshalValue is called on it, which fails.
// That means the test won't pass cleanly. Let me think of another approach.
//
// Actually, we need a type that isZeroValue default covers AND marshalValue can handle.
// Looking at the switch in isZeroValue: it handles bool, int*, uint*, float*, string,
// slice, map, pointer, interface, struct. The "default" covers everything else.
// The only types that marshalValue can also handle that aren't in isZeroValue would be...
// Actually all types marshalValue handles are covered by isZeroValue.
// The default case in isZeroValue is really for channel, func, complex, etc.
// These would all fail in marshalValue. So the test would involve calling isZeroValue
// on a channel-like type... but we can't easily trigger this through the public API
// without a marshal failure.
//
// However, we CAN test it: a zero-value channel with omitempty will NOT be omitted
// (because default returns false), and then the marshal will fail. That's fine -
// the branch IS covered even though the overall test produces an error.
type HasChan struct {
Name string `maml:"name"`
Ch chan int `maml:"ch,omitempty"`
}
_, err := Marshal(HasChan{Name: "test"})
if err == nil {
t.Error("expected error for channel field")
}
}
// Test interface wrapping non-nil value in marshalValue
func TestMarshalNonNilInterface(t *testing.T) {
var iface any = "hello"
data, err := Marshal(iface)
if err != nil {
t.Fatal(err)
}
if string(data) != `"hello"` {
t.Errorf("expected '\"hello\"', got %q", string(data))
}
}
// Test nil interface inside a container (hits the rv.Kind()==Interface && rv.IsNil() path)
func TestMarshalSliceWithNilInterface(t *testing.T) {
s := []any{nil, 42}
data, err := Marshal(s)
if err != nil {
t.Fatal(err)
}
expected := "[\n null\n 42\n]"
if string(data) != expected {
t.Errorf("expected %q, got %q", expected, string(data))
}
}