-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper_test.go
1462 lines (1338 loc) · 41.7 KB
/
wrapper_test.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
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package wrappers
import (
"encoding/json"
"strconv"
"strings"
"testing"
"time"
"github.com/biter777/countries"
)
func TestNew(t *testing.T) {
tests := []struct {
name string
wrapper WrapperProvider
}{
{
name: "New WrapperBool",
wrapper: func() WrapperProvider { return New[*WrapperBool]() }(),
},
{
name: "New WrapperCountry",
wrapper: func() WrapperProvider { return New[*WrapperCountry]() }(),
},
{
name: "New WrapperFloat",
wrapper: func() WrapperProvider { return New[*WrapperFloat]() }(),
},
{
name: "New WrapperInt",
wrapper: func() WrapperProvider { return New[*WrapperInt]() }(),
},
{
name: "New WrapperString",
wrapper: func() WrapperProvider { return New[*WrapperString]() }(),
},
{
name: "New WrapperTime",
wrapper: func() WrapperProvider { return New[*WrapperTime]() }(),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Check if the wrapper is initialized
if !tt.wrapper.IsInitialized() {
t.Errorf("Wrapper not initialized")
}
})
}
}
func TestNewWithValue(t *testing.T) {
tests := []struct {
name string
wrapper WrapperProvider
want any
}{
{
name: "NewWithValue WrapperBool with true",
wrapper: func() WrapperProvider { wrapper, _ := NewWithValue[*WrapperBool](true); return wrapper }(),
want: true,
},
{
name: "NewWithValue WrapperCountry with CountryCodeUS",
wrapper: func() WrapperProvider { wrapper, _ := NewWithValue[*WrapperCountry](countries.US); return wrapper }(),
want: "United States",
},
{
name: "NewWithValue WrapperFloat with 123.456",
wrapper: func() WrapperProvider { wrapper, _ := NewWithValue[*WrapperFloat](123.456); return wrapper }(),
want: 123.456,
},
{
name: "NewWithValue WrapperInt with 100",
wrapper: func() WrapperProvider { wrapper, _ := NewWithValue[*WrapperInt](100); return wrapper }(),
want: int64(100),
},
{
name: "NewWithValue WrapperString with 'Test String'",
wrapper: func() WrapperProvider { wrapper, _ := NewWithValue[*WrapperString]("Test String"); return wrapper }(),
want: "Test String",
},
{
name: "NewWithValue WrapperTime with time.Time",
wrapper: func() WrapperProvider {
wrapper, _ := NewWithValue[*WrapperTime](time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC))
return wrapper
}(),
want: time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC).Format(time.RFC3339),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Check if the wrapper is initialized
if !tt.wrapper.IsInitialized() {
t.Errorf("Wrapper not initialized")
}
// Check if the wrapped value is correct
if tt.wrapper.UnwrapAny() != tt.want {
t.Errorf("Wrapped value = %v, want %v", tt.wrapper.UnwrapAny(), tt.want)
}
})
}
}
// We're going to create a new wrapper type that only accepts positive integers.
const (
wrapperCustomIntPositiveName Name = "WrapperCustomIntPositive"
)
type wrapperCustomIntPositive struct {
WrapperInt
}
func (wrapper *wrapperCustomIntPositive) handleValue(value int64) error {
if value < 0 {
wrapper.Discard()
return ErrorValue(wrapperCustomIntPositiveName, value, "positive int")
} else {
wrapper.Value = value
}
return nil
}
func (wrapper *wrapperCustomIntPositive) Wrap(value any, discard bool) error {
switch v := value.(type) {
case nil:
wrapper.Discard()
if !discard {
return ErrorNil(WrapperStringName)
}
case WrapperProvider:
if v.IsDiscarded() {
wrapper.Discard()
return nil
}
return wrapper.Wrap(v.UnwrapAny(), discard)
case string:
converted, err := strconv.Atoi(v)
if err != nil {
wrapper.Discard()
if !discard {
return ErrorValue(wrapperCustomIntPositiveName, value, "int")
}
}
wrapper.handleValue(int64(converted))
case int:
wrapper.handleValue(int64(v))
case int16:
wrapper.handleValue(int64(v))
case int32:
wrapper.handleValue(int64(v))
case int64:
wrapper.handleValue(int64(v))
}
return nil
}
var _ WrapperProvider = (*WrapperString)(nil) // Ensure that WrapperString implements WrapperProvider.
func TestNewWithValueDiscard(t *testing.T) {
tests := []struct {
name string
wrapper WrapperProvider
want any
wantDiscard bool
}{
{
name: "NewWithValueDiscard WrapperBool with true",
wrapper: NewWithValueDiscard[*WrapperBool](true),
want: true,
wantDiscard: false,
},
{
name: "NewWithValueDiscard WrapperCountry with CountryCodeUS",
wrapper: NewWithValueDiscard[*WrapperCountry](countries.US),
want: "United States",
wantDiscard: false,
},
{
name: "NewWithValueDiscard WrapperFloat with 123.456",
wrapper: NewWithValueDiscard[*WrapperFloat](123.456),
want: 123.456,
wantDiscard: false,
},
{
name: "NewWithValueDiscard WrapperInt with 100",
wrapper: NewWithValueDiscard[*WrapperInt](100),
want: int64(100),
wantDiscard: false,
},
{
name: "NewWithValueDiscard WrapperString with 'Test String'",
wrapper: NewWithValueDiscard[*WrapperString]("Test String"),
want: "Test String",
wantDiscard: false,
},
{
name: "NewWithValueDiscard WrapperTime with time.Time",
wrapper: NewWithValueDiscard[*WrapperTime](time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC)),
want: time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC).Format(time.RFC3339),
wantDiscard: false,
},
{
name: "NewWithValueDiscard WrapperIntPositive with 100",
wrapper: NewWithValueDiscard[*wrapperCustomIntPositive](100),
want: int64(100),
wantDiscard: false,
},
{
name: "NewWithValueDiscard WrapperIntPositive with -100",
wrapper: NewWithValueDiscard[*wrapperCustomIntPositive](-100),
want: int64(0),
wantDiscard: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Check if the wrapper is initialized
if !tt.wrapper.IsInitialized() {
t.Errorf("Wrapper not initialized")
}
// Check if the wrapped value is discarded
if !tt.wrapper.IsDiscarded() && tt.wantDiscard {
t.Errorf("Wrapped value is not discarded")
}
if tt.wrapper.UnwrapAny() != tt.want {
t.Errorf("Wrapped value = %v, want %v", tt.wrapper.UnwrapAny(), tt.want)
}
})
}
}
type ExampleNested struct {
NestedNumberValue *WrapperInt `json:"nested_number_value"`
NestedStringValue *WrapperString `json:"nested_string_value"`
}
type ExampleOmit struct {
OmitNumberValue *WrapperInt `json:"omit_number_value,omitempty"`
OmitStringValue *WrapperString `json:"omit_string_value,omitempty"`
}
// ExampleDiscarder holds Discarder types
type ExampleDiscarder struct {
DiscarderNumberValue *Discarder[*WrapperInt] `json:"discarder_number_value"`
}
// Updated Example struct to include Discarder
type Example struct {
NumberValue *WrapperInt `json:"number_value"`
StringValue *WrapperString `json:"string_value"`
BoolValue *WrapperBool `json:"bool_value"`
FloatValue *WrapperFloat `json:"float_value"`
TimeValue *WrapperTime `json:"time_value"`
Nested ExampleNested `json:"nested"`
Omit *ExampleOmit `json:"omit,omitempty"`
Discarder ExampleDiscarder `json:"discarder"`
}
// trimJson trims JSON strings of whitespace and newlines for better comparison while keeping them readable.
func trimJson(jsonStr string) string {
return strings.ReplaceAll(strings.ReplaceAll(jsonStr, "\n", ""), "\t", "")
}
// compareJSON compares two JSON objects represented as interface{}
func compareJSON(a, b interface{}) bool {
switch aTyped := a.(type) {
case map[string]interface{}:
bTyped, ok := b.(map[string]interface{})
if !ok {
return false
}
if len(aTyped) != len(bTyped) {
return false
}
for key, aValue := range aTyped {
bValue, exists := bTyped[key]
if !exists {
return false
}
if !compareJSON(aValue, bValue) {
return false
}
}
return true
case []interface{}:
bTyped, ok := b.([]interface{})
if !ok {
return false
}
if len(aTyped) != len(bTyped) {
return false
}
for i := range aTyped {
if !compareJSON(aTyped[i], bTyped[i]) {
return false
}
}
return true
default:
return a == b
}
}
// Helper function to compare two Example structs
func compareExamples(t *testing.T, original, unmarshalled *Example) {
// Compare NumberValue
if original.NumberValue != nil && unmarshalled.NumberValue != nil {
if original.NumberValue.IsDiscarded() != unmarshalled.NumberValue.IsDiscarded() {
t.Errorf("NumberValue discard state mismatch: got %v, want %v",
unmarshalled.NumberValue.IsDiscarded(), original.NumberValue.IsDiscarded())
}
if !original.NumberValue.IsDiscarded() && original.NumberValue.Unwrap() != unmarshalled.NumberValue.Unwrap() {
t.Errorf("NumberValue mismatch: got %v, want %v",
unmarshalled.NumberValue.Unwrap(), original.NumberValue.Unwrap())
}
}
// Compare StringValue
if original.StringValue != nil && unmarshalled.StringValue != nil {
if original.StringValue.IsDiscarded() != unmarshalled.StringValue.IsDiscarded() {
t.Errorf("StringValue discard state mismatch: got %v, want %v",
unmarshalled.StringValue.IsDiscarded(), original.StringValue.IsDiscarded())
}
if !original.StringValue.IsDiscarded() && original.StringValue.Unwrap() != unmarshalled.StringValue.Unwrap() {
t.Errorf("StringValue mismatch: got %v, want %v",
unmarshalled.StringValue.Unwrap(), original.StringValue.Unwrap())
}
}
// Compare BoolValue
if original.BoolValue != nil && unmarshalled.BoolValue != nil {
if original.BoolValue.IsDiscarded() != unmarshalled.BoolValue.IsDiscarded() {
t.Errorf("BoolValue discard state mismatch: got %v, want %v",
unmarshalled.BoolValue.IsDiscarded(), original.BoolValue.IsDiscarded())
}
if !original.BoolValue.IsDiscarded() && original.BoolValue.Unwrap() != unmarshalled.BoolValue.Unwrap() {
t.Errorf("BoolValue mismatch: got %v, want %v",
unmarshalled.BoolValue.Unwrap(), original.BoolValue.Unwrap())
}
}
// Compare FloatValue
if original.FloatValue != nil && unmarshalled.FloatValue != nil {
if original.FloatValue.IsDiscarded() != unmarshalled.FloatValue.IsDiscarded() {
t.Errorf("FloatValue discard state mismatch: got %v, want %v",
unmarshalled.FloatValue.IsDiscarded(), original.FloatValue.IsDiscarded())
}
if !original.FloatValue.IsDiscarded() && original.FloatValue.Unwrap() != unmarshalled.FloatValue.Unwrap() {
t.Errorf("FloatValue mismatch: got %v, want %v",
unmarshalled.FloatValue.Unwrap(), original.FloatValue.Unwrap())
}
}
// Compare TimeValue
if original.TimeValue != nil && unmarshalled.TimeValue != nil {
if original.TimeValue.IsDiscarded() != unmarshalled.TimeValue.IsDiscarded() {
t.Errorf("TimeValue discard state mismatch: got %v, want %v",
unmarshalled.TimeValue.IsDiscarded(), original.TimeValue.IsDiscarded())
}
if !original.TimeValue.IsDiscarded() && original.TimeValue.Unwrap() != (unmarshalled.TimeValue.Unwrap()) {
t.Errorf("TimeValue mismatch: got %v, want %v",
unmarshalled.TimeValue.Unwrap(), original.TimeValue.Unwrap())
}
}
// Compare Nested.NestedNumberValue
if original.Nested.NestedNumberValue != nil && unmarshalled.Nested.NestedNumberValue != nil {
if original.Nested.NestedNumberValue.IsDiscarded() != unmarshalled.Nested.NestedNumberValue.IsDiscarded() {
t.Errorf("Nested.NestedNumberValue discard state mismatch: got %v, want %v",
unmarshalled.Nested.NestedNumberValue.IsDiscarded(), original.Nested.NestedNumberValue.IsDiscarded())
}
if !original.Nested.NestedNumberValue.IsDiscarded() && original.Nested.NestedNumberValue.Unwrap() != unmarshalled.Nested.NestedNumberValue.Unwrap() {
t.Errorf("Nested.NestedNumberValue mismatch: got %v, want %v",
unmarshalled.Nested.NestedNumberValue.Unwrap(), original.Nested.NestedNumberValue.Unwrap())
}
}
// Compare Nested.NestedStringValue
if original.Nested.NestedStringValue != nil && unmarshalled.Nested.NestedStringValue != nil {
if original.Nested.NestedStringValue.IsDiscarded() != unmarshalled.Nested.NestedStringValue.IsDiscarded() {
t.Errorf("Nested.NestedStringValue discard state mismatch: got %v, want %v",
unmarshalled.Nested.NestedStringValue.IsDiscarded(), original.Nested.NestedStringValue.IsDiscarded())
}
if !original.Nested.NestedStringValue.IsDiscarded() && original.Nested.NestedStringValue.Unwrap() != unmarshalled.Nested.NestedStringValue.Unwrap() {
t.Errorf("Nested.NestedStringValue mismatch: got %v, want %v",
unmarshalled.Nested.NestedStringValue.Unwrap(), original.Nested.NestedStringValue.Unwrap())
}
}
// Compare Omit Fields
if original.Omit != nil && unmarshalled.Omit != nil {
// Compare Omit.OmitNumberValue
if original.Omit.OmitNumberValue != nil && unmarshalled.Omit.OmitNumberValue != nil {
if original.Omit.OmitNumberValue.IsDiscarded() != unmarshalled.Omit.OmitNumberValue.IsDiscarded() {
t.Errorf("Omit.OmitNumberValue discard state mismatch: got %v, want %v",
unmarshalled.Omit.OmitNumberValue.IsDiscarded(), original.Omit.OmitNumberValue.IsDiscarded())
}
if !original.Omit.OmitNumberValue.IsDiscarded() && original.Omit.OmitNumberValue.Unwrap() != unmarshalled.Omit.OmitNumberValue.Unwrap() {
t.Errorf("Omit.OmitNumberValue mismatch: got %v, want %v",
unmarshalled.Omit.OmitNumberValue.Unwrap(), original.Omit.OmitNumberValue.Unwrap())
}
}
// Compare Omit.OmitStringValue
if original.Omit.OmitStringValue != nil && unmarshalled.Omit.OmitStringValue != nil {
if original.Omit.OmitStringValue.IsDiscarded() != unmarshalled.Omit.OmitStringValue.IsDiscarded() {
t.Errorf("Omit.OmitStringValue discard state mismatch: got %v, want %v",
unmarshalled.Omit.OmitStringValue.IsDiscarded(), original.Omit.OmitStringValue.IsDiscarded())
}
if !original.Omit.OmitStringValue.IsDiscarded() && original.Omit.OmitStringValue.Unwrap() != unmarshalled.Omit.OmitStringValue.Unwrap() {
t.Errorf("Omit.OmitStringValue mismatch: got %v, want %v",
unmarshalled.Omit.OmitStringValue.Unwrap(), original.Omit.OmitStringValue.Unwrap())
}
}
}
// Compare Discarder Field
if original.Discarder.DiscarderNumberValue != nil && unmarshalled.Discarder.DiscarderNumberValue != nil {
discarderOriginal := original.Discarder.DiscarderNumberValue
discarderUnmarshalled := unmarshalled.Discarder.DiscarderNumberValue
// Check if the Proxy is nil for both original and unmarshalled Discarder
if discarderOriginal.Proxy == nil && discarderUnmarshalled.Proxy == nil {
// Check discard state
if discarderOriginal.Proxy.IsDiscarded() != discarderUnmarshalled.Proxy.IsDiscarded() {
t.Errorf("Discarder.DiscarderNumberValue discard state mismatch: got %v, want %v",
discarderUnmarshalled.Proxy.IsDiscarded(), discarderOriginal.Proxy.IsDiscarded())
}
// Check unwrapped value
if !discarderOriginal.Proxy.IsDiscarded() {
originalVal := discarderOriginal.Proxy.UnwrapAny()
unmarshalledVal := discarderUnmarshalled.Proxy.UnwrapAny()
if originalVal != unmarshalledVal {
t.Errorf("Discarder.DiscarderNumberValue mismatch: got %v, want %v",
unmarshalledVal, originalVal)
}
}
}
}
}
// Helper function to initialize an Example struct with valid data
func initializeValidExample() *Example {
example := &Example{
NumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(100, false)
return w
}(),
StringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Test String", false)
return w
}(),
BoolValue: func() *WrapperBool {
w := New[*WrapperBool]()
w.Wrap(true, false)
return w
}(),
FloatValue: func() *WrapperFloat {
w := New[*WrapperFloat]()
w.Wrap(123.456, false)
return w
}(),
TimeValue: func() *WrapperTime {
w := New[*WrapperTime]()
w.Wrap(time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC), false)
return w
}(),
}
// Initialize Nested struct with valid data
example.Nested.NestedNumberValue = func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(200, false)
return w
}()
example.Nested.NestedStringValue = func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Nested String", false)
return w
}()
// Initialize Omit struct with valid data
example.Omit = &ExampleOmit{
OmitNumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(300, false)
return w
}(),
OmitStringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Omit String", false)
return w
}(),
}
// Initialize Discarder with valid data
discarderWrapper := New[*WrapperInt]()
discarderWrapper.Wrap(400, false) // Valid value
example.Discarder.DiscarderNumberValue = NewDiscarder(discarderWrapper)
return example
}
// Helper function to initialize an Example struct with some invalid and discarded fields
func initializeExampleWithDiscards() *Example {
example := &Example{
NumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap("invalid_int", true) // Invalid int with discard=true
return w
}(),
StringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Valid String", false)
return w
}(),
BoolValue: func() *WrapperBool {
w := New[*WrapperBool]()
w.Wrap("yes", false) // Valid boolean representation
return w
}(),
FloatValue: func() *WrapperFloat {
w := New[*WrapperFloat]()
w.Wrap("invalid_float", true) // Invalid float with discard=true
return w
}(),
TimeValue: func() *WrapperTime {
w := New[*WrapperTime]()
w.Wrap("invalid_time_format", true) // Invalid time with discard=true
return w
}(),
}
// Initialize Nested struct with some invalid data
example.Nested.NestedNumberValue = func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(500, false)
return w
}()
example.Nested.NestedStringValue = func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Nested Valid String", false)
return w
}()
// Initialize Omit struct with some invalid data
example.Omit = &ExampleOmit{
OmitNumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap("invalid_omit_int", true) // Invalid int with discard=true
return w
}(),
// OmitStringValue left as nil to test omitempty
}
// Initialize Discarder with invalid data
discarderWrapper := New[*WrapperInt]()
discarderWrapper.Wrap("invalid_discarder_int", true) // Invalid int, should be discarded
example.Discarder.DiscarderNumberValue = NewDiscarder(discarderWrapper)
return example
}
// TestMarshallingAndUnmarshalling tests marshalling and unmarshalling of the Example struct, including Discarder.
func TestMarshallingAndUnmarshalling(t *testing.T) {
tests := []struct {
name string
initialExample *Example
expectedJSON string
}{
{
name: "All Fields Valid",
initialExample: initializeValidExample(),
expectedJSON: trimJson(`{
"number_value":100,
"string_value":"Test String",
"bool_value":true,
"float_value":123.456,
"time_value":"2025-01-16T06:34:08Z",
"nested":{
"nested_number_value":200,
"nested_string_value":"Nested String"
},
"omit":{
"omit_number_value":300,
"omit_string_value":"Omit String"
},
"discarder":{
"discarder_number_value":400
}
}`),
},
{
name: "Some Fields Invalid and Discarded",
initialExample: initializeExampleWithDiscards(),
expectedJSON: trimJson(`{
"number_value":null,
"string_value":"Valid String",
"bool_value":true,
"float_value":null,
"time_value":null,
"nested":{
"nested_number_value":500,
"nested_string_value":"Nested Valid String"
},
"omit":{
"omit_number_value":null
},
"discarder":{
"discarder_number_value":null
}
}`),
},
{
name: "Omit Struct Completely (All Omit Fields Nil)",
initialExample: func() *Example {
example := &Example{
NumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(50, false)
return w
}(),
StringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Another Test String", false)
return w
}(),
BoolValue: func() *WrapperBool {
w := New[*WrapperBool]()
w.Wrap(false, false)
return w
}(),
FloatValue: func() *WrapperFloat {
w := New[*WrapperFloat]()
w.Wrap(789.012, false)
return w
}(),
TimeValue: func() *WrapperTime {
w := New[*WrapperTime]()
w.Wrap(time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC), false)
return w
}(),
}
// Initialize Nested struct with valid data
example.Nested.NestedNumberValue = func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(600, false)
return w
}()
example.Nested.NestedStringValue = func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Nested Complete String", false)
return w
}()
// Omit struct fields left as nil to test omitempty
// Initialize Discarder with valid data
discarderWrapper := New[*WrapperInt]()
discarderWrapper.Wrap(700, false) // Valid value
example.Discarder.DiscarderNumberValue = NewDiscarder(discarderWrapper)
return example
}(),
expectedJSON: trimJson(`{
"number_value":50,
"string_value":"Another Test String",
"bool_value":false,
"float_value":789.012,
"time_value":"2025-01-16T06:34:08Z",
"nested":{
"nested_number_value":600,
"nested_string_value":"Nested Complete String"
},
"discarder":{
"discarder_number_value":700
}
}`),
},
{
name: "Discarder Discards Invalid Value",
initialExample: func() *Example {
example := initializeValidExample()
// Overwrite Discarder with invalid data
example.Discarder.DiscarderNumberValue.Proxy.Wrap("invalid_discarder_int", true)
return example
}(),
expectedJSON: trimJson(`{
"number_value":100,
"string_value":"Test String",
"bool_value":true,
"float_value":123.456,
"time_value":"2025-01-16T06:34:08Z",
"nested":{
"nested_number_value":200,
"nested_string_value":"Nested String"
},
"omit":{
"omit_number_value":300,
"omit_string_value":"Omit String"
},
"discarder":{
"discarder_number_value":null
}
}`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Marshal the initialExample to JSON
jsonData, err := json.Marshal(tt.initialExample)
if err != nil {
t.Fatalf("Failed to marshal Example struct: %v", err)
}
// For better comparison, unmarshal both expectedJSON and actual jsonData into interface{}
var expected interface{}
err = json.Unmarshal([]byte(tt.expectedJSON), &expected)
if err != nil {
t.Fatalf("Failed to unmarshal expected JSON: %v", err)
}
var actual interface{}
err = json.Unmarshal(jsonData, &actual)
if err != nil {
t.Fatalf("Failed to unmarshal actual JSON: %v", err)
}
// Compare the two unmarshalled interfaces
if !compareJSON(expected, actual) {
t.Errorf("Marshalled JSON mismatch.\nExpected: %v\nActual: %v", tt.expectedJSON, string(jsonData))
}
// Now unmarshal jsonData back into a new Example struct
var unmarshalled Example
err = json.Unmarshal(jsonData, &unmarshalled)
if err != nil {
t.Fatalf("Failed to unmarshal JSON back into Example struct: %v", err)
}
// Compare the initialExample and unmarshalled Example structs
compareExamples(t, tt.initialExample, &unmarshalled)
})
}
}
// TestMarshallingWithOmitFields tests marshalling of Example structs where omit fields are nil or discarded, including Discarder.
func TestMarshallingWithOmitFields(t *testing.T) {
tests := []struct {
name string
initialExample *Example
expectedJSON string
}{
{
name: "Omit Struct with All Omit Fields Set",
initialExample: func() *Example {
example := &Example{
NumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(10, false)
return w
}(),
StringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Sample String", false)
return w
}(),
BoolValue: func() *WrapperBool {
w := New[*WrapperBool]()
w.Wrap(true, false)
return w
}(),
FloatValue: func() *WrapperFloat {
w := New[*WrapperFloat]()
w.Wrap(99.99, false)
return w
}(),
TimeValue: func() *WrapperTime {
w := New[*WrapperTime]()
w.Wrap(time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC), false)
return w
}(),
}
// Initialize Nested struct with valid data
example.Nested.NestedNumberValue = func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(20, false)
return w
}()
example.Nested.NestedStringValue = func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Nested Valid String", false)
return w
}()
// Initialize Omit struct with all fields set
example.Omit = &ExampleOmit{
OmitNumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(30, false)
return w
}(),
OmitStringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Omit Valid String", false)
return w
}(),
}
// Initialize Discarder with valid data
discarderWrapper := New[*WrapperInt]()
discarderWrapper.Wrap(40, false) // Valid value
example.Discarder.DiscarderNumberValue = NewDiscarder(discarderWrapper)
return example
}(),
expectedJSON: trimJson(`{
"number_value":10,
"string_value":"Sample String",
"bool_value":true,
"float_value":99.99,
"time_value":"2025-01-16T06:34:08Z",
"nested":{
"nested_number_value":20,
"nested_string_value":"Nested Valid String"
},
"omit":{
"omit_number_value":30,
"omit_string_value":"Omit Valid String"
},
"discarder":{
"discarder_number_value":40
}
}`),
},
{
name: "Omit Struct with Some Omit Fields Discarded",
initialExample: func() *Example {
example := &Example{
NumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(15, false)
return w
}(),
StringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Another String", false)
return w
}(),
BoolValue: func() *WrapperBool {
w := New[*WrapperBool]()
w.Wrap(false, false)
return w
}(),
FloatValue: func() *WrapperFloat {
w := New[*WrapperFloat]()
w.Wrap(50.5, false)
return w
}(),
TimeValue: func() *WrapperTime {
w := New[*WrapperTime]()
w.Wrap(time.Date(2025, 1, 16, 6, 34, 8, 685, time.UTC), false)
return w
}(),
}
// Initialize Nested struct with some invalid data
example.Nested.NestedNumberValue = func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap("invalid_nested_int", true) // Invalid int with discard=true
return w
}()
example.Nested.NestedStringValue = func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Nested Another String", false)
return w
}()
// Initialize Omit struct with some fields discarded
example.Omit = &ExampleOmit{
OmitNumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap("invalid_omit_int", true) // Invalid int with discard=true
return w
}(),
// OmitStringValue left as nil to test omitempty
}
// Initialize Discarder with invalid data
discarderWrapper := New[*WrapperInt]()
discarderWrapper.Wrap("invalid_discarder_int", true) // Invalid int, should be discarded
example.Discarder.DiscarderNumberValue = NewDiscarder(discarderWrapper)
return example
}(),
expectedJSON: `{
"number_value":15,
"string_value":"Another String",
"bool_value":false,
"float_value":50.5,
"time_value":"2025-01-16T06:34:08Z",
"nested":{
"nested_number_value":null,
"nested_string_value":"Nested Another String"
},
"omit":{
"omit_number_value":null
},
"discarder":{
"discarder_number_value":null
}
}`,
},
{
name: "Omit Struct Fully Omitted (All Omit Fields Discarded)",
initialExample: func() *Example {
example := &Example{
NumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(12, false)
return w
}(),
StringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Full Omit Test", false)
return w
}(),
BoolValue: func() *WrapperBool {
w := New[*WrapperBool]()
w.Wrap(false, false)
return w
}(),
FloatValue: func() *WrapperFloat {
w := New[*WrapperFloat]()
w.Wrap(88.88, false)
return w
}(),
TimeValue: func() *WrapperTime {
w := New[*WrapperTime]()
w.Wrap(time.Date(2025, 1, 16, 6, 34, 8, 0, time.UTC), false)
return w
}(),
}
// Initialize Nested struct with valid data
example.Nested.NestedNumberValue = func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap(24, false)
return w
}()
example.Nested.NestedStringValue = func() *WrapperString {
w := New[*WrapperString]()
w.Wrap("Nested Full Omit Test", false)
return w
}()
// Initialize Omit struct with all fields discarded
example.Omit = &ExampleOmit{
OmitNumberValue: func() *WrapperInt {
w := New[*WrapperInt]()
w.Wrap("invalid_omit_int", true) // Invalid int with discard=true
return w
}(),
OmitStringValue: func() *WrapperString {
w := New[*WrapperString]()
w.Wrap(nil, true) // Invalid string with discard=true
return w
}(),
}